-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynalist_to_markdown.py
219 lines (180 loc) · 7.09 KB
/
dynalist_to_markdown.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import dataclasses, fnmatch, os.path, pathlib, re, requests, yaml
class DynalistToMarkdown:
api_base = 'https://dynalist.io/api/v1/'
def __init__(self, config,
directory = 'dynalist',
overwrite = False,
http_session = requests
):
self.directory = directory
self.overwrite = overwrite
self.http_session = http_session
config = yaml.safe_load(config)
self.api_key = config['api_key']
self.page_configs = [
self.PageConfig(**page)
for page in config['pages']
]
def export(self):
if not self.overwrite and os.path.exists(self.directory):
raise Exception('Directory exists: ' + self.directory)
self.fetch_files()
self.process_files()
def document_to_markdown(self, node_by_id, config, out):
for context in self.traverse_nodes(node_by_id, 'root'):
node = context[-1]
content = node['content']
if config.include_notes:
note = node.get('note', '')
if note != '':
content += f' ({note})'
if config.obsidian_internal_links:
content = self.replace_markdown_links(content)
depth = len(context)
if depth <= config.heading_depth:
if depth > 1 or config.page_header:
if depth > 1:
print(file=out)
heading = '#' * depth
print(f'{heading} {content}', file=out)
else:
indent = depth - config.heading_depth - 1
indent = '\t' * indent
print(f'{indent}- {content}', file=out)
def fetch_files(self):
response = self.request('file/list')
files = response['files']
self.file_by_id = self.index_nodes(files)
self.root_file_id = response['root_file_id']
def get_document(self, document_id):
return self.request('doc/read', file_id=document_id)
def page_config(self, page):
config = None
for page_config in self.page_configs:
if page_config.match(page):
if config:
config = config.merge(page_config)
else:
config = page_config
return config
def process_files(self):
for context in self.traverse_nodes(self.file_by_id, self.root_file_id):
path = '/'.join(c['title'] for c in context[1:])
node = context[-1]
type = node['type']
config = self.page_config(path)
if type == 'document' and not config.ignore:
self.process_document(path, node, config)
def process_document(self, path, node, config):
document_id = node['id']
document = self.get_document(document_id)
nodes = document['nodes']
node_by_id = self.index_nodes(nodes)
print(f'{self.directory}/{path}')
dir = os.path.dirname(path)
pathlib.Path(self.directory, dir).mkdir(parents=True, exist_ok=True)
with open(f'{self.directory}/{path}.md', 'w') as out:
self.document_to_markdown(node_by_id, config, out)
def request(self, api_path, **parameters):
url = self.api_base + api_path
response = self.http_session.post(url, json={'token': self.api_key, **parameters})
if response.status_code != 200:
raise Exception(f'Dynalist API error: {response.status_code}')
result = response.json()
if result.get('_code') == 'InvalidToken':
raise Exception('Invalid API key: see https://dynalist.io/developer')
return result
def index_nodes(self, nodes):
return {
node['id']: node
for node in nodes
}
def traverse_nodes(self, node_by_id, root_id):
def traverse(context, id):
node = node_by_id[id]
context = context + [node]
yield context
children = node.get('children')
if children:
for child_id in children:
yield from traverse(context, child_id)
return traverse([], root_id)
markdown_link_re = re.compile(r'\[([^\[]+)\]\(([^\)]+)\)')
def replace_markdown_links(self, s):
return re.sub(self.markdown_link_re, self.replace_obsidian_internal_links, s)
# TODO: match fragment part and link to heading?
dynalist_document_link_re = re.compile(r'^https://dynalist.io/d/([^#]+)')
def replace_obsidian_internal_links(self, group):
url = group[2]
url_group = self.dynalist_document_link_re.match(url)
if url_group:
document_id = url_group[1]
document = self.file_by_id.get(document_id)
if document:
text = group[1]
title = document['title']
if text == title:
return f'[[{title}]]'
else:
return f'[[{title}|{text}]]'
return group[0]
@dataclasses.dataclass
class PageConfig:
name : str
heading_depth : int = None
ignore : bool = None
include_notes : bool = None
obsidian_internal_links : bool = None
page_header : bool = None
def match(self, page):
return fnmatch.fnmatch(page, self.name)
def merge(self, page_config):
changes = {
key: value
for key, value in dataclasses.asdict(page_config).items()
if value is not None
}
return dataclasses.replace(self, **changes)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
description = 'Export Dynalist to markdown',
formatter_class = lambda prog: argparse.HelpFormatter(prog, max_help_position=40, width=79),
)
parser.add_argument(
'--config',
help = 'Path to config file (default: ./dynalist_to_markdown.yaml)',
default = 'dynalist_to_markdown.yaml',
)
parser.add_argument(
'--directory',
help = 'Directory for output (default: dynalist)',
default = 'dynalist',
)
parser.add_argument(
'--overwrite',
help = 'Overwrite files in output directory',
action = 'store_true',
)
parser.add_argument(
'--cache',
help = 'Enable requests cache (useful for development)',
action = 'store_true',
)
args = parser.parse_args()
if args.cache:
import requests_cache
http_session = requests_cache.CachedSession(
cache_name = 'dynalist_exporter',
allowable_methods = ('GET', 'POST')
)
else:
http_session = requests
with open(args.config) as config:
exporter = DynalistToMarkdown(
config = config,
directory = args.directory,
overwrite = args.overwrite,
http_session = http_session,
)
exporter.export()