[Checkins] SVN: stxutils/trunk/s Snapshot a grotty partial STX -> ReST converter.
Tres Seaver
tseaver at palladion.com
Mon Feb 9 18:57:06 EST 2009
Log message for revision 96354:
Snapshot a grotty partial STX -> ReST converter.
Changed:
U stxutils/trunk/setup.py
A stxutils/trunk/stxutils/restify.py
A stxutils/trunk/stxutils/torst.py
-=-
Modified: stxutils/trunk/setup.py
===================================================================
--- stxutils/trunk/setup.py 2009-02-09 23:10:40 UTC (rev 96353)
+++ stxutils/trunk/setup.py 2009-02-09 23:57:06 UTC (rev 96354)
@@ -11,10 +11,12 @@
include_package_data=True,
zip_safe=False,
install_requires=[
- 'zope.structuredtext',
+ 'zope.structuredtext>=3.4.0',
+ 'docutils>=0.5',
],
entry_points = """\
[console_scripts]
stx2html = stxutils.tohtml:main
+ stx2rst = stxutils.torst:main
"""
)
Added: stxutils/trunk/stxutils/restify.py
===================================================================
--- stxutils/trunk/stxutils/restify.py (rev 0)
+++ stxutils/trunk/stxutils/restify.py 2009-02-09 23:57:06 UTC (rev 96354)
@@ -0,0 +1,229 @@
+from docutils import nodes
+from docutils import utils
+
+class ReST:
+
+ element_types = {
+ '#text': '_text',
+ 'StructuredTextDocument': 'document',
+ 'StructuredTextParagraph': 'paragraph',
+ 'StructuredTextExample': 'example',
+ 'StructuredTextBullet': 'bullet',
+ 'StructuredTextNumbered': 'numbered',
+ 'StructuredTextDescription': 'description',
+ 'StructuredTextDescriptionTitle': 'descriptionTitle',
+ 'StructuredTextDescriptionBody': 'descriptionBody',
+ 'StructuredTextSection': 'section',
+ 'StructuredTextSectionTitle': 'sectionTitle',
+ 'StructuredTextLiteral': 'literal',
+ 'StructuredTextEmphasis': 'emphasis',
+ 'StructuredTextStrong': 'strong',
+ 'StructuredTextLink': 'link',
+ 'StructuredTextXref': 'xref',
+ 'StructuredTextInnerLink':'innerLink',
+ 'StructuredTextNamedLink':'namedLink',
+ 'StructuredTextUnderline':'underline',
+ 'StructuredTextTable':'table',
+ 'StructuredTextSGML':'sgml',
+ 'StructuredTextImage': 'image',
+ }
+
+ LEVEL_MARKERS = ('#', '=', '+', '~', '-')
+
+ def dispatch(self, doc, level, output):
+ getattr(self, self.element_types[doc.getNodeName()]
+ )(doc, level, output)
+
+ def __call__(self, doc, level=1):
+ r=[]
+ self.dispatch(doc, level-1, r.append)
+ return ''.join(r)
+
+ def _text(self, doc, level, output):
+ output(doc.getNodeValue())
+
+ def document(self, doc, level, output):
+ children = doc.getChildNodes()
+
+ for c in children:
+ self.dispatch(c, level, output)
+
+ def section(self, doc, level, output):
+ children=doc.getChildNodes()
+ for c in children:
+ self.dispatch(c, level + 1, output)
+
+ def sectionTitle(self, doc, level, output):
+ tmp = []
+ for c in doc.getChildNodes():
+ self.dispatch(c, level, tmp.append)
+ header = ''.join(tmp).strip()
+ divider = self.LEVEL_MARKERS[level - 1]
+ output('%s\n' % header)
+ output(divider * len(header))
+ output('\n\n')
+
+ def description(self, doc, level, output):
+ p = doc.getPreviousSibling()
+ if p is None or p.getNodeName() is not doc.getNodeName():
+ output('.. comment:: description list\n')
+ output('\n')
+ for c in doc.getChildNodes():
+ getattr(self, self.element_types[c.getNodeName()])(c, level, output)
+ n=doc.getNextSibling()
+ if n is None or n.getNodeName() is not doc.getNodeName():
+ output('\n\n')
+
+ def descriptionTitle(self, doc, level, output):
+ for c in doc.getChildNodes():
+ self.dispatch(c, level, output)
+
+ def descriptionBody(self, doc, level, output):
+ tmp = []
+ for c in doc.getChildNodes():
+ self.dispatch(c, level, tmp.append)
+ output('\n '.join(tmp))
+ output('\n')
+
+ def bullet(self, doc, level, output):
+ p = doc.getPreviousSibling()
+ if p is None or p.getNodeName() is not doc.getNodeName():
+ output('.. comment:: bullet list\n')
+ output('\n')
+ for c in doc.getChildNodes():
+ output('- ')
+ self.dispatch(c, level, output)
+ n = doc.getNextSibling()
+ if n is None or n.getNodeName() is not doc.getNodeName():
+ output('\n\n')
+
+ def numbered(self, doc, level, output):
+ p = doc.getPreviousSibling()
+ if p is None or p.getNodeName() is not doc.getNodeName():
+ output('.. comment:: numbered list\n')
+ output('\n')
+ for c in doc.getChildNodes():
+ output('#. ')
+ self.dispatch(c, level, output)
+ n=doc.getNextSibling()
+ if n is None or n.getNodeName() is not doc.getNodeName():
+ output('\n\n')
+
+ def example(self, doc, level, output):
+ output('::\n\n')
+ for c in doc.getChildNodes():
+ output(c.getNodeValue())
+ output('\n\n')
+
+ def paragraph(self, doc, level, output):
+ tmp = []
+ for c in doc.getChildNodes():
+ self.dispatch(c, level, tmp.append)
+ para = ''.join(tmp)
+ for line in para.splitlines():
+ output('%s\n' % line.lstrip())
+ output('\n\n')
+
+ def link(self, doc, level, output):
+ output('`')
+ for c in doc.getChildNodes():
+ self.dispatch(c, level, output)
+ output(' <%s>`_' % doc.href)
+
+ def emphasis(self, doc, level, output):
+ output('*')
+ for c in doc.getChildNodes():
+ getattr(self, self.element_types[c.getNodeName()])(c, level, output)
+ output('*>')
+
+ def literal(self, doc, level, output):
+ output(':: \n\n')
+ for c in doc.getChildNodes():
+ output(c.getNodeValue())
+ output('\n\n')
+
+ def strong(self, doc, level, output):
+ output('**')
+ for c in doc.getChildNodes():
+ self.dispatch(c, level, output)
+ output('**')
+
+ def underline(self, doc, level, output):
+ output("_")
+ for c in doc.getChildNodes():
+ getattr(self, self.element_types[c.getNodeName()])(c, level, output)
+ output("_")
+
+ def innerLink(self, doc, level, output):
+ assert 0 # TBD
+ output('<a href="#ref');
+ for c in doc.getChildNodes():
+ getattr(self, self.element_types[c.getNodeName()])(c, level, output)
+ output('">[')
+ for c in doc.getChildNodes():
+ getattr(self, self.element_types[c.getNodeName()])(c, level, output)
+ output(']</a>')
+
+ def namedLink(self, doc, level, output):
+ assert 0 # TBD
+ output('<a name="ref')
+ for c in doc.getChildNodes():
+ self.dispatch(c, level, output)
+ output('">[')
+ for c in doc.getChildNodes():
+ self.dispatch(c, level, output)
+ output(']</a>')
+
+ def sgml(self,doc,level,output):
+ assert 0 # TBD
+ for c in doc.getChildNodes():
+ self.dispatch(c, level, output)
+
+ def xref(self, doc, level, output):
+ assert 0 # TBD
+ val = doc.getNodeValue()
+ output('<a href="#ref%s">[%s]</a>' % (val, val) )
+
+ def table(self,doc,level,output):
+ """
+ A StructuredTextTable holds StructuredTextRow(s) which
+ holds StructuredTextColumn(s). A StructuredTextColumn
+ is a type of StructuredTextParagraph and thus holds
+ the actual data.
+ """
+ assert 0 # TBD
+ output('<table border="1" cellpadding="2">\n')
+ for row in doc.getRows()[0]:
+ output("<tr>\n")
+ for column in row.getColumns()[0]:
+ if hasattr(column,"getAlign"):
+ str = ('<%s colspan="%s" align="%s" valign="%s">'
+ % (column.getType(),
+ column.getSpan(),
+ column.getAlign(),
+ column.getValign()))
+ else:
+ str = '<td colspan="%s">' % column.getSpan()
+ output(str)
+ for c in column.getChildNodes():
+ getattr(self, self.element_types[c.getNodeName()]
+ )(c, level, output)
+ if hasattr(column,"getType"):
+ output("</"+column.getType()+">\n")
+ else:
+ output("</td>\n")
+ output("</tr>\n")
+ output("</table>\n")
+
+ def image(self, doc, level, output):
+ assert 0 # TBD
+ if hasattr(doc, 'key'):
+ output('<a name="%s"></a>\n' % doc.key)
+ output('<img src="%s" alt="%s" />\n' % (doc.href, doc.getNodeValue()))
+ if doc.getNodeValue() and hasattr(doc, 'key'):
+ output('<p><b>Figure %s</b> %s</p>\n' % (doc.key,
+ doc.getNodeValue()))
+
+def restify(stxdoc):
+ rest = ReST()
+ return rest(stxdoc)
Added: stxutils/trunk/stxutils/torst.py
===================================================================
--- stxutils/trunk/stxutils/torst.py (rev 0)
+++ stxutils/trunk/stxutils/torst.py 2009-02-09 23:57:06 UTC (rev 96354)
@@ -0,0 +1,68 @@
+""" stx2rst -- convert structured text to restructured text.
+
+Usage: stx2rst [OPTIONS]
+
+Options include:
+
+o '--input' / '-i' Open the named file for input
+ if not passed, use stdin).
+
+o '--output' / '-o' Open the named file for output
+ if not passed, use stdout).
+"""
+import getopt
+import sys
+
+from docutils.writers import UnfilteredWriter
+from zope.structuredtext.document import Document
+from zope.structuredtext.stng import structurize
+from stxutils.restify import restify
+
+def usage(msg='', rc=1):
+ print __doc__
+ if msg:
+ print
+ print msg
+ print
+ sys.exit(rc)
+
+def main(argv=None):
+
+ input = sys.stdin
+ output = sys.stdout
+
+ if argv is None:
+ argv = sys.argv[1:]
+
+ try:
+ opts, args = getopt.gnu_getopt(argv, 'i:o:h?',
+ ['--input=',
+ '--output=',
+ '--help',
+ ])
+ except getopt.GetoptError:
+ usage()
+
+ if args:
+ usage('No arguments allowed!')
+
+ for k, v in opts:
+ if k in ('-h', '-?', '--help'):
+ usage(rc=2)
+ elif k in ('-i', '--input'):
+ input = open(v, 'r')
+ elif k in ('-o', '--output'):
+ output = open(v, 'w')
+ else:
+ usage()
+
+ raw = input.read()
+ st = structurize(raw)
+ doc = Document()(st)
+ rst = restify(doc)
+ output.write(rst)
+
+
+if __name__ == '__main__':
+ import sys
+ main(sys.argv)
More information about the Checkins
mailing list