summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2015-01-03 23:13:59 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2015-01-03 23:13:59 -0800
commitddb86aeedf4815d87016233e77d211c97ac73197 (patch)
tree9606e05f557d6ccb8521d33f3fcd17c5e6ea948f
parent6d39d67bc90e2a6e4cd22539be270f246069e64a (diff)
spec2md.py: Add reference link definitions for internal anchors.
We can use this to simplify spec.txt, using `[whitespace]` instead of `[whitespace](#whitespace)`, for example.
-rw-r--r--spec2md.py25
1 files changed, 17 insertions, 8 deletions
diff --git a/spec2md.py b/spec2md.py
index bbed997..fde79bb 100644
--- a/spec2md.py
+++ b/spec2md.py
@@ -5,6 +5,8 @@ import sys
stage = 0
example = 0
section = ""
+mdlines = []
+refs = []
if len(sys.argv) > 1:
specfile = sys.argv[1]
@@ -16,17 +18,17 @@ with open(specfile, 'r', encoding='utf-8') as spec:
if re.match(r'^\.$', ln):
if stage == 0:
example += 1
- sys.stdout.write("\n<div class=\"example\" id=\"example-{0}\" data-section=\"{1}\">\n".format(example, section))
- sys.stdout.write("<div class=\"examplenum\"><a href=\"#example-{0}\">Example {0}</a>&nbsp;&nbsp;<a class=\"dingus\" title=\"open in interactive dingus\">(interact)</a></div>\n\n".format(example))
- sys.stdout.write("````````````````````````````````````````````````````````` markdown\n")
+ mdlines.append("\n<div class=\"example\" id=\"example-{0}\" data-section=\"{1}\">\n".format(example, section))
+ mdlines.append("<div class=\"examplenum\"><a href=\"#example-{0}\">Example {0}</a>&nbsp;&nbsp;<a class=\"dingus\" title=\"open in interactive dingus\">(interact)</a></div>\n\n".format(example))
+ mdlines.append("````````````````````````````````````````````````````````` markdown\n")
stage = 1
elif stage == 1:
- sys.stdout.write("`````````````````````````````````````````````````````````\n\n")
- sys.stdout.write("````````````````````````````````````````````````````````` html\n")
+ mdlines.append("`````````````````````````````````````````````````````````\n\n")
+ mdlines.append("````````````````````````````````````````````````````````` html\n")
stage = 2
elif stage == 2:
- sys.stdout.write("`````````````````````````````````````````````````````````\n\n")
- sys.stdout.write("</div>\n")
+ mdlines.append("`````````````````````````````````````````````````````````\n\n")
+ mdlines.append("</div>\n")
stage = 0
else:
sys.stderr.out("Encountered unknown stage {0}\n".format(stage))
@@ -36,6 +38,13 @@ with open(specfile, 'r', encoding='utf-8') as spec:
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)
- sys.stdout.write(ln)
+ mdlines.append(ln)
+
+mdtext = ''.join(mdlines) + '\n\n' + '\n'.join(refs) + '\n'
+
+sys.stdout.write(mdtext)