From d948cb2b921ef0384015bbd432d8b7a7015fee11 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sun, 4 Jan 2015 13:07:18 -0800 Subject: spec2md.py -> makespec.py. Moved HTML generation out of Makefile. --- Makefile | 9 +++----- makespec.py | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ spec2md.py | 50 --------------------------------------- 3 files changed, 80 insertions(+), 56 deletions(-) create mode 100644 makespec.py delete mode 100644 spec2md.py diff --git a/Makefile b/Makefile index 0791f1a..fc47464 100644 --- a/Makefile +++ b/Makefile @@ -178,7 +178,7 @@ dingus: js/commonmark.js ### Spec ### spec.md: $(SPEC) - python3 spec2md.py $< > $@ + python3 makespec.py $< markdown > $@ spec: spec.html @anchors=`perl -ne '@matches = / id="([^"]*)"/g; foreach $$match (@matches) { print "$$match\n"; }' $<`; \ @@ -188,11 +188,8 @@ spec: spec.html echo "Link to missing anchor #$$link"; \ done -spec.html: spec.md template.html - pandoc --no-highlight --number-sections --template template.html -s --toc -S $< | \ - perl -pe 's/a href="@([^"]*)"/a id="\1" href="#\1" class="definition"/g' | \ - perl -pe 's/␣/ <\/span>/g' \ - > $@ +spec.html: spec.txt template.html + python3 makespec.py $< html > $@ spec.pdf: spec.md template.tex specfilter.hs pandoc -s $< --template template.tex \ diff --git a/makespec.py b/makespec.py new file mode 100644 index 0000000..4d157a5 --- /dev/null +++ b/makespec.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +import re +import sys +from subprocess import * + +if len(sys.argv) == 3: + specfile = sys.argv[1] + specformat = sys.argv[2] + if not (specformat in ["html", "markdown"]): + sys.stderr.write("Format must be html or markdown\n") + exit(1) +else: + sys.stderr.write("Usage: makespec.py SPECFILE [html|markdown]\n") + exit(1) + +def pipe_through_prog(prog, text): + p1 = Popen(prog.split(), stdout=PIPE, stdin=PIPE, stderr=PIPE) + [result, err] = p1.communicate(input=text.encode('utf-8')) + return [p1.returncode, result.decode('utf-8'), err] + +def replaceAnchor(match): + refs.append("[{0}]: #{1}".format(match.group(1), match.group(2))) + return '{0}'.format(match.group(1), match.group(2)) + +stage = 0 +example = 0 +section = "" +mdlines = [] +refs = [] + +with open(specfile, 'r', encoding='utf-8') as spec: + for ln in spec: + if re.match(r'^\.$', ln): + if stage == 0: + example += 1 + mdlines.append("\n
\n".format(example, section)) + mdlines.append("\n\n".format(example)) + mdlines.append("````````````````````````````````````````````````````````` markdown\n") + stage = 1 + elif stage == 1: + mdlines.append("`````````````````````````````````````````````````````````\n\n") + mdlines.append("````````````````````````````````````````````````````````` html\n") + stage = 2 + elif stage == 2: + mdlines.append("`````````````````````````````````````````````````````````\n\n") + mdlines.append("
\n") + stage = 0 + else: + sys.stderr.out("Encountered unknown stage {0}\n".format(stage)) + sys.exit(1) + else: + if stage == 0: + match = re.match(r'^#{1,6} *(.*)', ln) + if match: + section = match.group(1) + else: + ln = re.sub(r'\[([^]]*)\]\(@([^)]*)\)', replaceAnchor, ln) + else: + ln = re.sub(r' ', '␣', ln) + mdlines.append(ln) + +mdtext = ''.join(mdlines) + '\n\n' + '\n'.join(refs) + '\n' + +if specformat == "markdown": + sys.stdout.write(mdtext) +elif specformat == "html": + prog = "pandoc -s --toc -S --no-highlight --number-sections --template template.html" + [retcode, result, err] = pipe_through_prog(prog, mdtext) + if retcode == 0: + result = re.sub(r'␣', ' ', result) + sys.stdout.write(result) + else: + sys.stderr.write("Error converting markdown version of spec:\n") + sys.stderr.write(err) + exit(1) + +exit(0) diff --git a/spec2md.py b/spec2md.py deleted file mode 100644 index fde79bb..0000000 --- a/spec2md.py +++ /dev/null @@ -1,50 +0,0 @@ -#!/usr/bin/env python3 -import re -import sys - -stage = 0 -example = 0 -section = "" -mdlines = [] -refs = [] - -if len(sys.argv) > 1: - specfile = sys.argv[1] -else: - specfile = 'spec.txt' - -with open(specfile, 'r', encoding='utf-8') as spec: - for ln in spec: - if re.match(r'^\.$', ln): - if stage == 0: - example += 1 - mdlines.append("\n
\n".format(example, section)) - mdlines.append("\n\n".format(example)) - mdlines.append("````````````````````````````````````````````````````````` markdown\n") - stage = 1 - elif stage == 1: - mdlines.append("`````````````````````````````````````````````````````````\n\n") - mdlines.append("````````````````````````````````````````````````````````` html\n") - stage = 2 - elif stage == 2: - mdlines.append("`````````````````````````````````````````````````````````\n\n") - mdlines.append("
\n") - stage = 0 - else: - sys.stderr.out("Encountered unknown stage {0}\n".format(stage)) - sys.exit(1) - else: - if stage == 0: - match = re.match(r'^#{1,6} *(.*)', ln) - if match: - section = match.group(1) - else: - for match in re.finditer(r'\[([^]]*)\]\(@([^)]*)\)', ln): - refs.append("[{0}]: #{1}".format(match.group(1), match.group(2))) - else: - ln = re.sub(r' ', '␣', ln) - mdlines.append(ln) - -mdtext = ''.join(mdlines) + '\n\n' + '\n'.join(refs) + '\n' - -sys.stdout.write(mdtext) -- cgit v1.2.3