import argparse, re, sys def fd_fn_forward(post_id, note_id): return f"{note_id}" def fd_fn_reverse(post_id, note_id, note_txt): return f"
  • {note_txt}
  • "; 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 = ["

    Notes

    ", f"
    ", "
    ", "
      "] ## 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("
    ") 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)