From 6d39d67bc90e2a6e4cd22539be270f246069e64a Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sat, 3 Jan 2015 22:33:08 -0800 Subject: Rewrote spec2md in python. Better to only require python, not python and perl. --- spec2md.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 spec2md.py (limited to 'spec2md.py') diff --git a/spec2md.py b/spec2md.py new file mode 100644 index 0000000..bbed997 --- /dev/null +++ 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
\n".format(example, section)) + sys.stdout.write("\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("
\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) -- cgit v1.2.3