cmark

My personal build of CMark ✏️

Commit
6d39d67bc90e2a6e4cd22539be270f246069e64a
Parent
918867accab909c6efb2f762773b9f72c8f1014b
Author
John MacFarlane <jgm@berkeley.edu>
Date

Rewrote spec2md in python.

Better to only require python, not python and perl.

Diffstat

3 files changed, 42 insertions, 37 deletions

Status File Name N° Changes Insertions Deletions
Modified Makefile 2 1 1
Deleted spec2md.pl 36 0 36
Added spec2md.py 41 41 0
diff --git a/Makefile b/Makefile
@@ -178,7 +178,7 @@ dingus: js/commonmark.js
 ### Spec ###
 
 spec.md: $(SPEC)
-	perl spec2md.pl < $< > $@
+	python3 spec2md.py $< > $@
 
 spec: spec.html
 	@anchors=`perl -ne '@matches = / id="([^"]*)"/g; foreach $$match (@matches) { print "$$match\n"; }' $<`; \
diff --git a/spec2md.pl b/spec2md.pl
@@ -1,36 +0,0 @@
-#!/usr/bin/env perl
-use strict;
-use warnings;
-
-my $stage = 0;
-my $example = 0;
-my @match;
-my $section = "";
-
-while (<STDIN>) {
-  if (/^\.$/) {
-    if ($stage == 0) {
-      $example++;
-      print "\n<div class=\"example\" id=\"example-$example\" data-section=\"$section\">\n";
-      print "<div class=\"examplenum\"><a href=\"#example-$example\">Example $example</a>&nbsp;&nbsp;<a class=\"dingus\" title=\"open in interactive dingus\">(interact)</a></div>\n\n";
-      print "````````````````````````````````````````````````````````` markdown\n";
-    } elsif ($stage == 1) {
-      print "`````````````````````````````````````````````````````````\n\n";
-      print "````````````````````````````````````````````````````````` html\n";
-    } elsif ($stage == 2) {
-      print "`````````````````````````````````````````````````````````\n\n";
-      print "</div>\n\n";
-    } else {
-      die "Encountered unknown stage $stage";
-    }
-    $stage = ($stage + 1) % 3;
-  } else {
-    if ($stage == 0 && (@match = ($_ =~ /^#{1,6} *(.*)/))) {
-        $section = $match[0];
-    }
-    if ($stage != 0) {
-      $_ =~ s/ /␣/g;
-    }
-    print $_;
-  }
-}
diff --git a/spec2md.py b/spec2md.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+import re
+import sys
+
+stage = 0
+example = 0
+section = ""
+
+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
+                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")
+                stage = 1
+            elif stage == 1:
+                sys.stdout.write("`````````````````````````````````````````````````````````\n\n")
+                sys.stdout.write("````````````````````````````````````````````````````````` html\n")
+                stage = 2
+            elif stage == 2:
+                sys.stdout.write("`````````````````````````````````````````````````````````\n\n")
+                sys.stdout.write("</div>\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' ', '␣', ln)
+            sys.stdout.write(ln)