aboutsummaryrefslogtreecommitdiff
path: root/tools/makespec.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/makespec.py')
-rwxr-xr-xtools/makespec.py50
1 files changed, 28 insertions, 22 deletions
diff --git a/tools/makespec.py b/tools/makespec.py
index b2b8ab2..925f23c 100755
--- a/tools/makespec.py
+++ b/tools/makespec.py
@@ -1,16 +1,24 @@
#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
import re
import sys
from subprocess import *
from string import Template
+def out(str):
+ sys.stdout.buffer.write(str.encode('utf-8'))
+
+def err(str):
+ sys.stderr.buffer.write(str.encode('utf-8'))
+
if len(sys.argv) == 2:
specformat = sys.argv[1]
if not (specformat in ["html", "markdown"]):
- sys.stderr.write("Format must be html or markdown\n")
+ err("Format must be html or markdown\n")
exit(1)
else:
- sys.stderr.write("Usage: makespec.py [html|markdown]\n")
+ err("Usage: makespec.py [html|markdown]\n")
exit(1)
def toIdentifier(s):
@@ -29,9 +37,8 @@ def parseYaml(yaml):
return metadata
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]
+ result = check_output(prog.split(), input=text.encode('utf-8'))
+ return result.decode('utf-8')
def replaceAnchor(match):
refs.append("[{0}]: #{1}".format(match.group(1), match.group(2)))
@@ -60,7 +67,10 @@ with open('spec.txt', 'r', encoding='utf-8') as spec:
if stage == 0:
example += 1
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("<div class=\"examplenum\"><a href=\"#example-{0}\">Example {0}</a>".format(example))
+ if specformat == "html":
+ mdlines.append("&nbsp;&nbsp;<a class=\"dingus\" title=\"open in interactive dingus\">(interact)</a>")
+ mdlines.append("</div>\n\n")
mdlines.append("````````````````````````````````````````````````````````` markdown\n")
stage = 1
elif stage == 1:
@@ -97,13 +107,13 @@ with open('spec.txt', 'r', encoding='utf-8') as spec:
lastnum[level - 1] = lastnum[level - 1] + 1
number = '.'.join([str(x) for x in lastnum])
ident = toIdentifier(section)
- ln = re.sub(r' ', ' ' + number + ' ', ln, count=1)
+ ln = re.sub(r' ', ' <span class="number">' + number + '</span> ', ln, count=1)
sections.append(dict(level=level,
contents=section,
ident=ident,
number=number))
refs.append("[{0}]: #{1}".format(section, ident))
- ln = re.sub(r'# +', '# <a id="{0}"></a> '.format(ident),
+ ln = re.sub(r'# +', '# <a id="{0}"></a>'.format(ident),
ln, count=1)
else:
ln = re.sub(r'\[([^]]*)\]\(@([^)]*)\)', replaceAnchor, ln)
@@ -116,7 +126,7 @@ yaml = ''.join(yamllines)
metadata = parseYaml(yaml)
if specformat == "markdown":
- sys.stdout.write(yaml + '\n\n' + mdtext)
+ out(yaml + '\n\n' + mdtext)
elif specformat == "html":
with open("tools/template.html", "r", encoding="utf-8") as templatefile:
template = Template(templatefile.read())
@@ -127,39 +137,35 @@ elif specformat == "html":
section['contents'] + '](#' + section['ident'] + ')')
toc = '<div id="TOC">\n\n' + '\n'.join(toclines) + '\n\n</div>\n\n'
prog = "cmark --smart"
- [retcode, result, err] = pipe_through_prog(prog, toc + mdtext)
- if retcode == 0:
+ result = pipe_through_prog(prog, toc + mdtext)
+ if result == '':
+ err("Error converting markdown version of spec to HTML.\n")
+ exit(1)
+ else:
result = re.sub(r'␣', '<span class="space"> </span>', result)
result = re.sub(r'<h([1-6])><a id="([^\"]*)"><\/a> ',
"<h\\1 id=\"\\2\">", result)
# put plural s inside links for better visuals:
result = re.sub(r'<\/a>s', "s</a>", result)
- sys.stdout.write(template.substitute(metadata, body=result))
+ out(template.substitute(metadata, body=result))
# check for errors:
idents = []
for ident in re.findall(r'id="([^"]*)"', result):
if ident in idents:
- sys.stderr.write("WARNING: duplicate identifier '" + ident +
- "'\n")
+ err("WARNING: duplicate identifier '" + ident + "'\n")
else:
idents.append(ident)
for href in re.findall(r'href="#([^"]*)"', result):
if not (href in idents):
- sys.stderr.write("WARNING: internal link with no anchor '" +
- href + "'\n")
+ err("WARNING: internal link with no anchor '" + href + "'\n")
reftexts = []
for ref in refs:
ref = re.sub('].*',']',ref).upper()
if ref in reftexts:
- sys.stderr.write("WARNING: duplicate reference link '" +
- ref + "'\n")
+ err("WARNING: duplicate reference link '" + ref + "'\n")
else:
reftexts.append(ref)
- else:
- sys.stderr.write("Error converting markdown version of spec:\n")
- sys.stderr.write(err)
- exit(1)
exit(0)