-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzettelcon.py
executable file
·288 lines (220 loc) · 8.53 KB
/
zettelcon.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python3
import datetime
import glob
import os
import pickle
import re
import textwrap
import time
from argparse import ArgumentParser
from collections import defaultdict
from multiprocessing import Pool
from pprint import pformat
BACKLINK_START = "## Backlinks"
CACHEFILENAME = ".zettelcon_cache.pickle"
# ASSUMES the standard zettlr wikilink syntax for links
REX_LINK = re.compile(r"\[\[(?P<linktarget>.+?)\]\]")
# ASSUMES the standard single-line hashtag syntax for titles
REX_TITLE = re.compile(r"^#\s+(.+)")
REX_LINECLEANER = re.compile(r"^\s*(\*|-|\+|\d+\.|>) (\[ \]|\[x\])? *")
REX_TRAILINGNEWLINES = re.compile(r"(\n*)\Z", re.MULTILINE)
NOWSTR = datetime.datetime.now().isoformat(sep=" ", timespec="seconds")
def main():
parser = ArgumentParser(
description="Tool to insert automatic backlinks into Zettlr note collections or other interlinked (markdown) files."
)
parser.add_argument(
"-f",
"--folder",
help="Path to folder with all the zettels in it.",
required=True
)
parser.add_argument(
"-s",
"--suffix",
help="Suffix for the files to consider. Defaults to .md",
default=".md",
)
parser.add_argument(
"-c",
"--clear-backlinks",
help="Instead of generating backlinks, revert all files to a no-backlinks state",
action="store_true",
)
parser.add_argument(
"-n",
"--nprocs",
help="Number of worker processes to run for file reading and writing.",
default=2,
type=int,
)
parser.add_argument(
"-ic",
"--ignore-cache",
help="Don't use zettelcon's cache, force writing to _all_ Zettel files (even the ones where backlinks haven't changed).",
action="store_true",
)
args = parser.parse_args()
params = vars(args)
process_directory(**params)
def process_directory(
folder, suffix, nprocs, clear_backlinks=False, ignore_cache=False
):
t_start = time.time()
files = glob.glob(os.path.join(folder, f"**/*{suffix}"), recursive=True)
pool = Pool(processes=nprocs)
if clear_backlinks:
pool.map(clear_backlinks_from_file, files)
print("Cleared backlinks from all files")
return
links = []
res = pool.map(get_file_outlinks, files)
for outlinks in res:
links.extend(outlinks)
links = change_ids_to_filepaths(links, files)
bundled_links_current = bundle_backlinks_per_targetfile(links)
bundled_links_to_write = {**bundled_links_current}
cachefile = os.path.join(folder, CACHEFILENAME)
if not ignore_cache and os.path.isfile(cachefile):
with open(cachefile, "rb") as fh:
bundled_links_cached = pickle.load(fh)
for targetfile, links_current in bundled_links_current.items():
links_cached = None
if targetfile in bundled_links_cached:
links_cached = bundled_links_cached[targetfile]
if links_cached == links_current:
del bundled_links_to_write[targetfile]
with open(cachefile, "wb") as fh:
pickle.dump(bundled_links_current, fh)
unreferenced_files = set(files) - set(bundled_links_current.keys())
pool.map(clear_backlinks_from_file, unreferenced_files)
print(f"\nFound {len(unreferenced_files)} files with no links to them")
for file in sorted(unreferenced_files):
print(f" - {os.path.basename(file)}")
print(f"\nUpdating {len(bundled_links_to_write)} files in place...")
if len(bundled_links_to_write) == 0:
print(" - No new links to write.")
for target in bundled_links_to_write.keys():
print(" - Updating {}".format(os.path.basename(target)))
pool.map(write_backlinks_to_file, bundled_links_to_write.values())
t_end = time.time()
duration = t_end - t_start
print(
f"\nWrote backlinks to {len(bundled_links_to_write)} files in {duration:.3f}s"
)
print(NOWSTR)
def clear_backlinks_from_file(filepath):
write_backlink_section_to_file("", filepath)
def bundle_backlinks_per_targetfile(links):
"""
Takes a list of backlinks that contain metadata about source and target file.
Returns a dict that maps target file names to backlinks which point to it.
"""
backlinks_for_file = defaultdict(list)
for link_i in links:
filename = link_i["link_target"]
backlinks_for_file[filename].append(link_i)
return backlinks_for_file
def write_backlinks_to_file(backlinks):
"""
ASSUMES all the backlinks point to the same file
"""
target_file = backlinks[0]["link_target"]
backlinks_by_src = defaultdict(list)
for backlink in backlinks:
backlinks_by_src[backlink["link_source"]].append(backlink)
entries = []
for source_file, src_backlinks in backlinks_by_src.items():
source_file_title = src_backlinks[0]["link_source_title"]
source_file_relative = os.path.relpath(
source_file, start=os.path.dirname(target_file)
)
entry = "> - [{}]({})\n".format(source_file_title, source_file_relative)
for backlink in src_backlinks:
# ASSUMES two spaces are used for list indentation
entry += "> - {}\n".format(backlink["link_context"])
entries.append(entry)
backlink_section = f"{BACKLINK_START}\n\n"
backlink_section += "> \n".join(entries)
backlink_section += f"\n_Backlinks last generated {NOWSTR}_\n"
write_backlink_section_to_file(backlink_section, target_file)
def write_backlink_section_to_file(section_text, filepath):
with open(filepath, "r", encoding="utf-8") as fh:
contents = fh.read()
with open(filepath, "w", encoding="utf-8") as fh:
try:
backlink_sec_idx = contents.index(BACKLINK_START)
except ValueError:
# no backlink section in file
backlink_sec_idx = None
main_content = contents[:backlink_sec_idx]
res = REX_TRAILINGNEWLINES.search(main_content)
num_existing_newlines = len(res.group(1))
num_needed_newlines = max(2 - num_existing_newlines, 0)
backlink_section = "\n" * num_needed_newlines
backlink_section += section_text
# ASSUMES backlink section is last part of page
contents_backlinked = main_content + backlink_section
fh.write(contents_backlinked)
def change_ids_to_filepaths(links, all_filenames):
out = []
for entry in links:
target_candidates = []
for filename in all_filenames:
if entry["link_target"] in filename:
target_candidates.append(filename)
# ASSUMES note IDs are unique, also among rest of file names
if len(target_candidates) == 1:
entry["link_target_orig"] = entry["link_target"]
entry["link_target"] = target_candidates[0]
out.append(entry)
elif len(target_candidates) == 0:
print(
"\nTARGET '{}' NOT FOUND (linked from {})".format(
entry["link_target"], os.path.basename(entry["link_source"])
)
)
print(" - {}".format(textwrap.fill(entry["link_context"])))
elif len(target_candidates) > 1:
print(
"\nMULTIPLE TARGETS FOUND FOR {}: {}".format(
entry, pformat(target_candidates)
)
)
pass # multiple targets found
return out
def get_file_outlinks(path):
with open(path, "r", encoding="utf-8") as fh:
contents = fh.read()
paragraphs = [para.strip() for para in contents.split("\n")]
outlinks = []
first_header = ""
for para in paragraphs:
reached_backlink_section = BACKLINK_START in para
if reached_backlink_section:
break
if first_header == "":
res = REX_TITLE.match(para)
if res:
first_header = res.group(1)
links = find_links_in_text(para)
links = [
{"link_source_title": first_header, "link_source": path, **entry}
for entry in links
]
outlinks.extend(links)
return outlinks
def find_links_in_text(paragraph):
clean_para = REX_LINECLEANER.sub("", paragraph)
out = []
for res in REX_LINK.finditer(paragraph):
link = {
"link_target": res.group("linktarget"),
"link_context": clean_para,
"context_pos_start": res.start(),
"context_pos_end": res.end(),
}
out.append(link)
return out
if __name__ == "__main__":
main()