79 lines
2.4 KiB
Python
79 lines
2.4 KiB
Python
|
|
import argparse, re, sys
|
||
|
|
|
||
|
|
def fd_fn_forward(post_id, note_id):
|
||
|
|
return f"<sup class='footnote'><a href='#fn-{post_id}-{note_id}' id='fnref-{post_id}-{note_id}' onclick='return fdfootnote_show({post_id})'>{note_id}</a></sup>"
|
||
|
|
|
||
|
|
def fd_fn_reverse(post_id, note_id, note_txt):
|
||
|
|
return f"<li id='fn-{post_id}-{note_id}'>{note_txt} <span class='footnotereverse'><a href='#fnref-{post_id}-{note_id}'>↩</a></span></li>";
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
|
||
|
|
p = argparse.ArgumentParser(description="CC notes formatter")
|
||
|
|
p.add_argument("--text", "-t", help="Text file", required=True)
|
||
|
|
p.add_argument("--divider", "-d", help="In-text markup divider for notes", default="## Notes")
|
||
|
|
p.add_argument("--output", "-o", help="Output file", required=True)
|
||
|
|
p.add_argument("--format", "-f", help="Output format (cc, html)", default="cc")
|
||
|
|
|
||
|
|
args = p.parse_args()
|
||
|
|
notes = {}
|
||
|
|
|
||
|
|
with open(args.text) as fp:
|
||
|
|
text = fp.read()
|
||
|
|
s = text.split(args.divider)
|
||
|
|
if len(s) != 2:
|
||
|
|
print(f"Problem spliting text with divider {args.divider}.")
|
||
|
|
exit(0)
|
||
|
|
NOTES = s[1].splitlines()
|
||
|
|
|
||
|
|
## notes
|
||
|
|
for l in NOTES:
|
||
|
|
n = re.search(r'\[\d+\\]', l)
|
||
|
|
if n:
|
||
|
|
notes[l[n.start()+1:n.end()-2]] = l[n.end():].strip()
|
||
|
|
|
||
|
|
txt_lines = []
|
||
|
|
|
||
|
|
if args.format == "html":
|
||
|
|
POST_ID = 512
|
||
|
|
notes_lines = ["<h2>Notes</h2>", f"<div class='footnotes' id='footnotes-{POST_ID}'>", "<div class='footnotedivider'></div>", "<ol>"]
|
||
|
|
|
||
|
|
## text (in-place)
|
||
|
|
with open(args.text) as fp:
|
||
|
|
for l in fp:
|
||
|
|
line = l
|
||
|
|
offset = 0
|
||
|
|
|
||
|
|
if line.startswith(args.divider):
|
||
|
|
txt_lines.append(line)
|
||
|
|
break
|
||
|
|
|
||
|
|
for n in re.finditer(r'\[\d+\\]', l):
|
||
|
|
n_nbr = l[n.start()+1:n.end()-2]
|
||
|
|
if n_nbr in notes:
|
||
|
|
|
||
|
|
if args.format == "cc":
|
||
|
|
line = line[:n.start()+1+offset] + f"{n_nbr}. {notes[n_nbr]}" + line[n.end()-2+offset:]
|
||
|
|
offset += len(notes[n_nbr]) + 2
|
||
|
|
|
||
|
|
elif args.format == "html":
|
||
|
|
forward = fd_fn_forward(POST_ID, n_nbr)
|
||
|
|
line = line[:n.start()+offset-1] + forward + line[n.end()+offset:]
|
||
|
|
offset += len(forward) - (len(n[0])) - 1
|
||
|
|
reverse = fd_fn_reverse(POST_ID, n_nbr, notes[n_nbr])
|
||
|
|
notes_lines.append(reverse)
|
||
|
|
|
||
|
|
txt_lines.append(line)
|
||
|
|
|
||
|
|
if args.format == "html":
|
||
|
|
notes_lines.append("</ol></div>")
|
||
|
|
|
||
|
|
if args.output:
|
||
|
|
with open(args.output, 'w') as fp:
|
||
|
|
fp.writelines(txt_lines)
|
||
|
|
if args.format == "html":
|
||
|
|
fp.writelines(s + '\n' for s in notes_lines)
|
||
|
|
else:
|
||
|
|
sys.stdout.writelines(txt_lines)
|
||
|
|
if args.format == "html":
|
||
|
|
sys.stdout.writelines(s + '\n' for s in notes_lines)
|