-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace-tags.py
executable file
·58 lines (43 loc) · 1.42 KB
/
replace-tags.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
#! /usr/bin/env python3
"""
Replace the tags line in output html with actual links
(they aren't actual links because i don't feel like writing them as links the original markdown)
"""
import re
import shutil
import sys
def tag_to_html(tag: str) -> str:
return "".join([
"<a href=\"{}\">".format("./tag-{}.html".format(tag)),
"#{}".format(tag),
"</a>",
])
def main(content):
for line in content.split("\n"):
g = re.match(r"<p>~ tags : (.*)<\/p>", line.strip())
if g:
tags = [tag.strip().lstrip("#") for tag in g.group(1).split("*")]
# print(tags)
# print(line)
if any(" " in tag for tag in tags):
raise Exception("improper tags?: {}".format(line))
tag_line = " * ".join(
# "[{}](./tag-{}.html)".format("#{}".format(tag), tag) # this is markdown oops
tag_to_html(tag)
for tag in tags
)
# print(tag_line)
yield "<p>~ tags : {}</p>".format(tag_line)
else:
yield line
if __name__ == "__main__":
try:
fname = sys.argv[1]
with open(fname) as f:
content = f.read()
shutil.copy(fname, "{}.bak".format(fname))
with open(fname, "w") as f:
f.write("\n".join(main(content)))
except IndexError:
content = sys.stdin.read()
main(content)