convert [ref]s into footnotes
This commit is contained in:
parent
ce0c1476c8
commit
ce94c70363
37
main.py
37
main.py
@ -45,6 +45,16 @@ def code_parser(el):
|
||||
return lang
|
||||
|
||||
|
||||
def find_all_matches(pattern, string, group=0):
|
||||
pat = re.compile(pattern)
|
||||
pos = 0
|
||||
out = []
|
||||
while m := pat.search(string, pos):
|
||||
pos = m.start() + 1
|
||||
out.append(m[group])
|
||||
return out
|
||||
|
||||
|
||||
mkdirp('output')
|
||||
|
||||
with open(in_file, 'r', encoding='utf-8') as f:
|
||||
@ -98,6 +108,33 @@ for item in items:
|
||||
|
||||
content = '\n'.join(lines)
|
||||
content = md(content, code_language_callback=code_parser).strip() + '\n'
|
||||
ref_regex = re.compile(r'\[ref](.+?)\[/ref]')
|
||||
refs = []
|
||||
stuff = find_all_matches(ref_regex, content, 1)
|
||||
if stuff:
|
||||
for thing in stuff:
|
||||
if not thing in refs:
|
||||
refs = refs + [thing]
|
||||
|
||||
current = 0
|
||||
while m := ref_regex.search(content, current):
|
||||
thing = m.group(1)
|
||||
current = m.start() + 1
|
||||
try:
|
||||
idx = refs.index(thing) + 1
|
||||
content = content.replace(m.group(0), f"[^{idx}]")
|
||||
except ValueError:
|
||||
print(f"{thing} not found")
|
||||
continue
|
||||
|
||||
if len(refs) > 0:
|
||||
ref_output = []
|
||||
for i in range(0, len(refs)):
|
||||
ref = refs[i]
|
||||
ref_output.append(f"[^{i + 1}]: {ref}")
|
||||
|
||||
content = content + '\n' + '\n\n'.join(ref_output) + '\n'
|
||||
|
||||
frontmatter = f"---\nlayout: {post_type}\nid: {post_id}\ntitle: \"{title}\"\ncreator: \"{creator}\"\ndate: {date}\ncategories:\n{categories}\ntags:\n{tags}\ndraft: {'true' if status == 'draft' else 'false'}\npublished: {'true' if status == 'publish' else 'false'}\n---\n\n"
|
||||
|
||||
title_slug = slugify(title)
|
||||
|
Loading…
Reference in New Issue
Block a user