From 47ac8ab9d6a00a34d5914cc21ca61a0984818bf8 Mon Sep 17 00:00:00 2001 From: gauthiier Date: Thu, 17 Apr 2025 18:52:33 +0200 Subject: [PATCH] HAHA! commit --- README.md | 29 ++++++++++++++++- cc-format.sh | 40 ++++++++++++++++++++++++ format-cc-notes.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++ template.html | 34 ++++++++++++++++++++ 4 files changed, 180 insertions(+), 1 deletion(-) create mode 100755 cc-format.sh create mode 100644 format-cc-notes.py create mode 100644 template.html diff --git a/README.md b/README.md index 133a65f..a9a299e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,30 @@ # CC-FORMAT -Scripts that format a word processor text into a (semi-)ready format to be published on http://computationalculture.net \ No newline at end of file +Scripts that format a word processor text into a (semi-)ready format to be published on http://computationalculture.net + +Dependency: [pandoc](https://pandoc.org) + +``` +$> ./cc-format.sh + +usage: ./cc-format.sh [action] [files...] +where action can be: [src-md|md-html|src-cc-html|src-html] +``` + +``` +$> python format-cc-notes.py --help + +usage: format-cc-notes.py [-h] --text TEXT [--divider DIVIDER] --output OUTPUT [--format FORMAT] + +CC notes formatter + +options: + -h, --help show this help message and exit + --text TEXT, -t TEXT Text file + --divider DIVIDER, -d DIVIDER + In-text markup divider for notes + --output OUTPUT, -o OUTPUT + Output file + --format FORMAT, -f FORMAT + Output format (cc, html) +``` \ No newline at end of file diff --git a/cc-format.sh b/cc-format.sh new file mode 100755 index 0000000..72e8140 --- /dev/null +++ b/cc-format.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +if ! [[ "$1" =~ ^(src-md|md-html|src-cc-html|src-html|html-txt) ]]; then + echo "usage: $0 [action] [files...]" + echo "where action can be: [src-md|md-html|src-cc-html|src-html]" + exit 1 +fi + +if [[ $# -lt 2 ]]; then + echo "wrong number of parameters (need at least a command and an input file)" + exit 2 +fi + +case $1 in + + src-md) + echo "converting input file to md (via pandoc)" + pandoc --wrap=none -t markdown "${@:2}" -o "${@:2}".md + ;; + md-html) + echo "converting md input file to html" + pandoc --wrap=none --standalone --mathjax=https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js "${@:2}" -o "${@:2}".html + ;; + src-cc-html) + echo "converting src input file to CC html" + pandoc --wrap=none -t markdown "${@:2}" -o "${@:2}".md + python format-cc-notes.py -t "${@:2}".md -o "${@:2}".html.md -f cc + pandoc --wrap=none --template ./template.html --mathjax=https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js "${@:2}".html.md -o "${@:2}".CC.html + rm "${@:2}".html.md + rm "${@:2}".md + ;; + src-html) + echo "converting src input file to bare html" + pandoc --wrap=none -t markdown "${@:2}" -o "${@:2}".md + python format-cc-notes.py -t "${@:2}".md -o "${@:2}".html.md -f html + pandoc --wrap=none --template ./template.html --mathjax=https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js "${@:2}".html.md -o "${@:2}".html + rm "${@:2}".html.md + rm "${@:2}".md + ;; +esac \ No newline at end of file diff --git a/format-cc-notes.py b/format-cc-notes.py new file mode 100644 index 0000000..d238ff6 --- /dev/null +++ b/format-cc-notes.py @@ -0,0 +1,78 @@ +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) diff --git a/template.html b/template.html new file mode 100644 index 0000000..63710a3 --- /dev/null +++ b/template.html @@ -0,0 +1,34 @@ + + + + + + $if(title-prefix)$$title-prefix$ – $endif$$pagetitle$ + + $if(math)$ + $math$ + $endif$ + + +$body$ + +