From 2b899d50d2484772830272da393b5286e37dc090 Mon Sep 17 00:00:00 2001 From: "michele.simionato" Date: Tue, 11 Nov 2008 18:05:05 +0000 Subject: Added the tool scheme2rst.py --- scheme2rst.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 scheme2rst.py (limited to 'scheme2rst.py') diff --git a/scheme2rst.py b/scheme2rst.py new file mode 100644 index 0000000..4aa0a37 --- /dev/null +++ b/scheme2rst.py @@ -0,0 +1,50 @@ +""" +usage: %prog [options] +-r, --rst: make an .rst file only +""" + +import os, sys, re, webbrowser +from docutils.core import publish_cmdline +from ms.optionparser import OptionParser + +BIGCOMMENT = re.compile(r'#\|(.*)\|#(.*)', re.DOTALL) +SNIPPET = re.compile(r'\n;\s*([-A-Z\d_/!\?]+)\s*\n(.*?)\n\s*;END', re.DOTALL) +SNIPPETNAME = re.compile(r'\n\$\$([-A-Z\d_/!\?]+)\n') +INCLUDE = re.compile(r'@@([-\w\d_\.]+)') + +def indent(text): + return '\n'.join(' ' + ln for ln in text.splitlines()) + +def scheme2rst(fname, codeblock=False): + mo = BIGCOMMENT.search(file(fname).read()) + if mo is None: + sys.exit('No #| ..|# found!') + text, code = mo.groups() + snippet = dict((mo.group(1), mo.group(2)) for mo in SNIPPET.finditer(code)) + if codeblock: + templ = '\n.. code-block:: scheme\n\n%s\n' + else: + templ = '\n::\n\n%s\n' + def repl(mo): + "replace SNIPPETNAME with the content of the snippet dictionary" + name = mo.group(1) + return templ % indent(snippet[name]) + def include_file(mo): + return templ % indent(file(mo.group(1)).read()) + rst = INCLUDE.sub(include_file, SNIPPETNAME.sub(repl, text)) + rstfile = os.path.splitext(fname)[0] + '.rst' + file(rstfile, 'w').write(rst) + return rstfile + +if __name__ == "__main__": + option, schemefiles = OptionParser(__doc__).parse_args() + if not option and not schemefiles: + OptionParser.exit("usage: python rst2st.py ") + for schemefile in schemefiles: + fname, ext = os.path.splitext(schemefile) + assert ext in ('.ss', '.scm') + rstfile = scheme2rst(schemefile) # generate .rst + if not option.rst: # generate .html unless option -r was set + htmlfile = fname + '.html' + publish_cmdline(writer_name='html', argv=[rstfile, htmlfile]) + webbrowser.open(htmlfile) -- cgit v1.2.1