HAHA! commit

This commit is contained in:
gauthiier 2025-04-17 18:52:33 +02:00
parent 770b8b7df2
commit 47ac8ab9d6
4 changed files with 180 additions and 1 deletions

View File

@ -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
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)
```

40
cc-format.sh Executable file
View File

@ -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

78
format-cc-notes.py Normal file
View File

@ -0,0 +1,78 @@
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}'>&#8617;</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)

34
template.html Normal file
View File

@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>$if(title-prefix)$$title-prefix$ $endif$$pagetitle$</title>
<style type="text/css">
body {
margin: 0 auto;
max-width: 63em;
padding-left: 50px;
padding-right: 50px;
padding-top: 50px;
padding-bottom: 50px;
hyphens: auto;
overflow-wrap: break-word;
text-rendering: optimizeLegibility;
font-kerning: normal;
}
blockquote {
margin: 1em 1.4em 1em 1.4em;
padding-left: 1em;
color: #202020;
font-family: 'Courier New';
}
</style>
$if(math)$
$math$
$endif$
</head>
<body>
$body$
</body>
</html>