-
Notifications
You must be signed in to change notification settings - Fork 4
/
update-citations.py
executable file
·33 lines (26 loc) · 1.05 KB
/
update-citations.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
#!/usr/bin/env python
# Update citations will replace citations in the form [@name] with {% cite name %},
# and add the citations to the end of the page. We do this for jekyll scholar
import os
import re
import sys
def main(input_file):
if not os.path.exists(input_file):
sys.exit("%s does not exist." % input_file)
with open(input_file, "r") as fd:
content = fd.read()
# Replace references
for ref in re.findall('<span class="citation" data-cites="(.*?)"></span>', content):
original = '<span class="citation" data-cites="' + ref + '"></span>'
replacewith = "{% cite " + ref.lstrip("@") + " %}"
content = content.replace(original, replacewith)
# Replace escaped quotes
content = content.replace('\\"', '"')
content = content + "<br>\n\n{% bibliography --cited %}"
with open(input_file, "w") as fd:
fd.write(content)
if __name__ == "__main__":
if len(sys.argv) < 2:
sys.exit("Please provide an input markdown file to parse!")
input_file = sys.argv[1]
main(input_file)