cmark

My personal build of CMark ✏️

Commit
04fcb7a24ea4ac179f5033c00f7cb23b8ebb9e35
Parent
a97d9f95383302f06450fff914c0f253fe06fb79
Author
John MacFarlane <jgm@berkeley.edu>
Date

Use cmark itself to build spec.html.

Removes build dependency on pandoc.

Closes #256.

Note: we have lost "smart punctuation," but we can either (a) add an option to do this in the cmark renderer, or (b) insert unicode punctuation in the spec as needed. Not an urgent issue in any case.

Diffstat

3 files changed, 34 insertions, 25 deletions

Status File Name N° Changes Insertions Deletions
Modified makespec.py 29 24 5
Modified spec.txt 3 1 2
Modified template.html 27 9 18
diff --git a/makespec.py b/makespec.py
@@ -2,6 +2,7 @@
 import re
 import sys
 from subprocess import *
+from string import Template
 
 if len(sys.argv) == 3:
     specfile = sys.argv[1]
@@ -16,6 +17,18 @@ else:
 def toIdentifier(s):
    return re.sub(r'\s+', '-', re.sub(r'\W+', ' ', s.strip().lower()))
 
+def parseYaml(yaml):
+    metadata = {}
+    def parseField(match):
+        key = match.group(1)
+        val = match.group(2).strip()
+        if re.match(r'^\'', val):
+            val = val[1:len(val) - 1]
+        metadata[key] = val
+    fieldre = re.compile('^(\w+):(.*)$', re.MULTILINE)
+    re.sub(fieldre, parseField, 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'))
@@ -83,11 +96,14 @@ with open(specfile, '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)
                     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, count=1)
                 else:
                     ln = re.sub(r'\[([^]]*)\]\(@([^)]*)\)', replaceAnchor, ln)
             else:
@@ -95,22 +111,25 @@ with open(specfile, 'r', encoding='utf-8') as spec:
             mdlines.append(ln)
 
 mdtext = ''.join(mdlines) + '\n\n' + '\n'.join(refs) + '\n'
+yaml = ''.join(yamllines)
+metadata = parseYaml(yaml)
 
 if specformat == "markdown":
-    sys.stdout.write(mdtext)
+    sys.stdout.write(yaml + '\n\n' + mdtext)
 elif specformat == "html":
+    with open("template.html", "r", encoding="utf-8") as templatefile:
+        template = Template(templatefile.read())
     toclines = []
     for section in sections:
         indent = '    ' * (section['level'] - 1)
         toclines.append(indent + '* [' + section['number'] + ' ' +
                         section['contents'] + '](#' + section['ident'] + ')')
     toc = '<div id="TOC">\n\n' + '\n'.join(toclines) + '\n\n</div>\n\n'
-    yaml = ''.join(yamllines) + '\n'
-    prog = "pandoc -s -S --no-highlight --number-sections --template template.html"
-    [retcode, result, err] = pipe_through_prog(prog, yaml + toc + mdtext)
+    prog = "build/src/cmark"
+    [retcode, result, err] = pipe_through_prog(prog, toc + mdtext)
     if retcode == 0:
         result = re.sub(r'␣', '<span class="space"> </span>', result)
-        sys.stdout.write(result)
+        sys.stdout.write(template.substitute(metadata, body=result))
     else:
         sys.stderr.write("Error converting markdown version of spec:\n")
         sys.stderr.write(err)
diff --git a/spec.txt b/spec.txt
@@ -1,7 +1,6 @@
 ---
 title: CommonMark Spec
-author:
-- John MacFarlane
+author: John MacFarlane
 version: 0.15
 date: 2014-12-31
 license: '[CC-BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/)'
diff --git a/template.html b/template.html
@@ -2,7 +2,7 @@
 <html>
 <head>
 <meta charset="UTF-8">
-<title>$title$</title>
+<title>${title}</title>
 <style type="text/css">
   body { font-family: Helvetica, arial, freesans, clean, sans-serif;
     line-height: 1.4;
@@ -40,10 +40,8 @@ pre {
 code { font-family: monospace; background-color: #D3E1E4; }
 pre > code { background-color: transparent; }
 div.example > pre { float:left; width: 48%; }
-div.example > pre.markdown { clear:left; }
-pre.tree { font-weight: bold; color: #777; }
-pre.markdown { background-color: #D3E1E4; }
-pre.html { background-color: #C9CaCE; }
+div.example > pre:nth-child(2) { clear:left; background-color: #D3E1E4; }
+div.example > pre:nth-child(3) { clear:right; background-color: #C9CaCE; }
 #watermark {
  position:fixed;
  bottom:0px;
@@ -73,27 +71,23 @@ a.footnoteRef > sup {
 <script type="text/javascript">
 $$(document).ready(function() {
   $$("div.example").each(function(e) {
-    var t = $$(this).find('pre.markdown > code').text();
+    var t = $$(this).find('code.markdown').text();
     $$(this).find('a.dingus').click(function(f) {
       window.open('/dingus.html?text=' +
         encodeURIComponent(t.replace(/→/g,"\t")));
     });
   });
-  $$("pre.markdown").dblclick(function(e) { window.open('/dingus.html?text=' +
+  $$("code.markdown").dblclick(function(e) { window.open('/dingus.html?text=' +
       encodeURIComponent($$(this).find('code').text()));
   });
 });
 </script>
 </head>
 <body>
-$if(title)$
-<h1 class="title">$title$</h1>
-$endif$
-$if(version)$
-<div class="version">Version $version$ ($date$)</div>
-$endif$
+<h1 class="title">${title}</h1>
+<div class="version">Version ${version} (${date})</div>
 <div class="authors">
-$for(author)$<span class="author">$author$</span>$sep$; $endfor$
+    <span class="author">${author}</span>
 </div>
 <div class="license">
 <a rel="license"
@@ -110,10 +104,7 @@ $for(author)$<span class="author">$author$</span>$sep$; $endfor$
    href="http://creativecommons.org/licenses/by-sa/4.0/">Creative
    Commons Attribution-ShareAlike 4.0 International License</a>.</span>
 </div>
-<div id="TOC">
-$toc$
-</div>
 <div id="watermark"></div>
-$body$
+${body}
 </body>
 </html>