diff options
author | richard <richard@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2003-06-05 07:01:20 +0000 |
---|---|---|
committer | richard <richard@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2003-06-05 07:01:20 +0000 |
commit | 81699f3831ba8d2f45b43717dbd9353684a0d8c8 (patch) | |
tree | 601a3555e2694c1b06f950cc26f972e11373d419 /sandbox/richard/pythonpoint/pythonpoint.py | |
parent | 0d49b81ebc29e3266cfa8c1ebdcddbc35370097c (diff) | |
download | docutils-81699f3831ba8d2f45b43717dbd9353684a0d8c8.tar.gz |
pythonpoint working again
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@1380 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'sandbox/richard/pythonpoint/pythonpoint.py')
-rw-r--r-- | sandbox/richard/pythonpoint/pythonpoint.py | 396 |
1 files changed, 177 insertions, 219 deletions
diff --git a/sandbox/richard/pythonpoint/pythonpoint.py b/sandbox/richard/pythonpoint/pythonpoint.py index 3313f0513..bcea529d6 100644 --- a/sandbox/richard/pythonpoint/pythonpoint.py +++ b/sandbox/richard/pythonpoint/pythonpoint.py @@ -1,153 +1,133 @@ import cStringIO, cgi, sys, urllib -import docutils.utils -from docutils.parsers import get_parser_class +import docutils.core, docutils.io +from docutils import writers, nodes, languages -class DumbPythonPointFormatter: - def __init__(self): - self.out = cStringIO.StringIO() - self.w = self.out.write +class Writer(writers.Writer): + + settings_spec = () + settings_default_overrides = {} + output = None + + def translate(self): + visitor = DumbPythonPointFormatter(self.document) + self.document.walkabout(visitor) + self.output = visitor.astext() + #self.head_prefix = visitor.head_prefix + #self.head = visitor.head + #self.body_prefix = visitor.body_prefix + #self.body = visitor.body + #self.body_suffix = visitor.body_suffix + +class DumbPythonPointFormatter(nodes.NodeVisitor): + + def __init__(self, document): + nodes.NodeVisitor.__init__(self, document) self.section = 0 self.closers = [] - self.slidenum = 1 + self.slidenum = 0 + self.body = [] + self.w = self.body.append + self.suppress_para = 0 - def format(self, node): - '''Format a node - ''' - for entry in node: - self.formatOneTag(entry) + def astext(self): + return ''.join(self.body) - def formatOneTag(self, tag): - if tag.tagname == '#text': - meth = self.format__text - else: - if not hasattr(self, 'format_'+tag.tagname): - print >>sys.stderr, '**skipping %s'%tag.tagname - return - else: - meth = getattr(self, 'format_'+tag.tagname) - meth(tag) + def visit_reference(self, node): + pass + def depart_reference(self, node): + pass + + def visit_document(self, node): + self.w('<presentation>\n' + '<stylesheet module="modern" function="getParagraphStyles"/>\n' + '<section name="Main">\n') + def depart_document(self, node): + self.w('</section>\n' + '</presentation>\n') - def open_slide(self, node): + def visit_section(self, node): if node.attributes.has_key('dupname'): name = node.attributes['dupname'] else: name = node.attributes['name'] - self.w('<slide id="Slide%03d" title="%s">\n'%(self.slidenum, name)) self.slidenum += 1 + self.w('<slide id="Slide%03d" title="%s">\n' + '<frame x="160" y="72" width="600" height="432" leftmargin="36"' + 'rightmargin="0">\n'%(self.slidenum, name)) + def depart_section(self, node): + self.w('</frame>\n</slide>\n') - def open_frame(self): - self.w('<frame x="160" y="72" width="600" height="432" leftmargin="36" rightmargin="0">\n') - - # - # Root Element - # - # ((title, subtitle?)?, docinfo?, %structure.model;) - # - def format_document(self, document): - ''' ((title, subtitle?)?, docinfo?, %structure.model;) - - - ''' - self.document = document - - # make sure the structure is what we're expecting - self.w('<presentation>\n') - - # TODO: get this into the stx - self.w('<stylesheet module="modern" function="getParagraphStyles"/>\n') - - self.w('<section name="Main">\n') - - # TODO: get this into the stx - self.w('<fixedimage filename="logo.gif" x="0" y="0" width="134" height="70"/>\n') - - # now for the body - for entry in document: - assert entry.tagname == 'section' - self.formatOneTag(entry) - - self.w('</section>\n') - self.w('</presentation>\n') - - return self.out.getvalue() - - def format_title(self, node): + def visit_title(self, node): self.w('<para style="Heading2">') - if node.children: self.format(node) + self.suppress_para = 1 + def depart_title(self, node): + self.suppress_para = 0 self.w('</para>\n') - def format_section(self, node): - self.open_slide(node) - self.open_frame() - if node.children: self.format(node) - self.w('</frame>\n') - self.w('</slide>\n') - - def format_paragraph(self, node): - ''' %text.model; - ''' - # TODO: there are situations where the <p> </p> are unnecessary - self.w('<para>') - if node.children: self.format(node) - self.w('</para>\n') + def visit_paragraph(self, node): + if not self.suppress_para: self.w('<para>') + def depart_paragraph(self, node): + if not self.suppress_para: self.w('</para>\n') # Simple lists - def format_bullet_list(self, node): - if node.children: self.format(node) - - def format_enumerated_list(self, node): - if node.children: self.format(node) + def visit_bullet_list(self, node): + pass + def depart_bullet_list(self, node): + pass + def visit_enumerated_list(self, node): + pass + def depart_enumerated_list(self, node): + pass - def format_list_item(self, node): + def visit_list_item(self, node): self.w('<para style="Bullet">') - if node.children: self.format(node[0]) + self.suppress_para = 1 + def depart_list_item(self, node): + self.suppress_para = 0 self.w('</para>\n') # Definition List - def format_definition_list(self, node): - if node.children: self.format(node) - - def format_definition_list_item(self, node): - ''' (term, classifier?, definition) - ''' - if node.children: self.format(node) + def visit_definition_list(self, node): + pass + def depart_definition_list(self, node): + pass + def visit_definition_list_item(self, node): + pass + def depart_definition_list_item(self, node): + pass - def format_term(self, node): - ''' %text.model; - ''' + def visit_term(self, node): self.w('<para><b>') - if node.children:self.format(node[0]) + def depart_term(self, node): self.w('</b>') - def format_classifier(self, node): - ''' %text.model; - ''' - # TODO: handle the classifier better + def visit_classifier(self, node): self.w('<i>') - if node.children: self.format(node[0]) + def depart_classifier(self, node): self.w('</i>') - def format_definition(self, node): - ''' (%body.elements;)+ - ''' + def visit_definition(self, node): self.w('</para>\n<para style="Definition">') - for child in node.children: - self.w(child[0].data) + self.suppress_para = 1 + def depart_definition(self, node): + self.suppress_para = 0 self.w('</para>\n') # Literal Block - def format_literal_block(self, node): + def visit_literal_block(self, node): self.w('<pre>') - if node.children: self.format(node) + self.suppress_para = 1 + def depart_literal_block(self, node): + self.suppress_para = 0 self.w('</pre>\n') # Block Quote - def format_block_quote(self, node): + def visit_block_quote(self, node): self.w('<para style="Indent">') - if node.children: self.format(node) + def depart_block_quote(self, node): self.w('</para>\n') - def format_image(self, node): + def visit_image(self, node): ''' EMPTY uri CDATA #REQUIRED alt CDATA #IMPLIED @@ -158,13 +138,15 @@ class DumbPythonPointFormatter: attrs = node.attributes l = ['src="%(uri)s"'%attrs] # TODO: scale - self.w('<image filename="%s">'%node.attributes['uri']) + self.w('<image filename="%s"/>'%node.attributes['uri']) + def depart_image(self, node): + pass # # Tables: # NOT IN DOM YET # - def format_table(self, node): + def visit_table(self, node): ''' +------------------------+------------+----------+----------+ | Header row, column 1 | Header 2 | Header 3 | Header 4 | @@ -180,100 +162,73 @@ class DumbPythonPointFormatter: +------------------------+------------+---------------------+ ''' self.w('<table border=1>\n') - if node.children: self.format(node) + def depart_table(self, node): self.w('</table>\n') - def format_tgroup(self, node): - # we get the number of columns, if that's important - if node.children: self.format(node) + def visit_tgroup(self, node): + pass + def depart_tgroup(self, node): + pass - def format_colspec(self, node): - # we get colwidth, but don't need it + def visit_colspec(self, node): + pass + def depart_colspec(self, node): pass - def format_thead(self, node): - for row in node.children: - self.w('<tr>') - for cell in row.children: - s = '' - attrs = cell.attributes - if attrs.has_key('morecols'): - s = s + ' colspan=%d'%(attrs['morecols']+1) - if attrs.has_key('morerows'): - s = s + ' rowspan=%d'%(attrs['morerows']+1) - self.w('<th valign="top" align="left"%s>'%s) - if cell.children: self.format(cell) - self.w('</th>\n') - self.w('</tr>\n') - - def format_tbody(self, node): - for row in node.children: - self.w('<tr>') - for cell in row.children: - s = '' - attrs = cell.attributes - if attrs.has_key('morecols'): - s = s + ' colspan=%d'%(attrs['morecols']+1) - if attrs.has_key('morerows'): - s = s + ' rowspan=%d'%(attrs['morerows']+1) - self.w('<td valign="top" align="left"%s>'%s) - if cell.children: self.format(cell) - self.w('</td>\n') - self.w('</tr>\n') + def visit_row(self, node): + self.body.append(self.starttag(node, 'tr', '')) - # - # Inline Elements - # - # Inline elements occur within the text contents of body elements. Some - # nesting of inline elements is allowed by these definitions, with the - # following caveats: - # - An inline element may not contain a nested element of the same type - # (e.g. <strong> may not contain another <strong>). - # - Nested inline elements may or may not be supported by individual - # applications using this DTD. - # - The inline elements <footnote_reference>, <literal>, and <image> do - # not support nesting. - # - # What that means is that all of these take (%text.model;) except: - # literal (#PCDATA) - # footnote_reference (#PCDATA) - # - # text.model: - # (#PCDATA | %inline.elements;)* - # - def format_emphasis(self, node): - ''' (%text.model;) - ''' + def depart_row(self, node): + self.body.append('</tr>\n') + + def visit_thead(self, node): + self.thead = 1 + def depart_thead(self, node): + self.thead = 0 + def visit_tbody(self, node): + self.thead = 1 + def depart_tbody(self, node): + self.thead = 0 + + def visit_entry(self, node): + if self.thead: + s = 'th ' + else: + s = 'td ' + attrs = node.attributes + if attrs.has_key('morecols'): + s = s + 'colspan=%d '%(attrs['morecols']+1) + if attrs.has_key('morerows'): + s = s + 'rowspan=%d '%(attrs['morerows']+1) + self.w('<%svalign="top" align="left">'%s) + + def depart_entry(self, node): + if self.thead: + self.w('</th>\n') + else: + self.w('</td>\n') + + def visit_emphasis(self, node): self.w('<i>') - if node.children: self.format(node) + def depart_emphasis(self, node): self.w('</i>') - def format_strong(self, node): - ''' (%text.model;) - ''' + def visit_strong(self, node): self.w('<b>') - if node.children: self.format(node) + def depart_strong(self, node): self.w('</b>') - def format_interpreted(self, node): - ''' (%text.model;) - type CDATA #IMPLIED - ''' + def visit_interpreted(self, node): + pass #raise NotImplementedError, node + def depart_interpreted(self, node): pass #raise NotImplementedError, node - def format_literal(self, node): - ''' (#PCDATA) - ''' + def visit_literal(self, node): self.w('<tt>') - for literal in node.children: - self.w(cgi.escape(literal.data)) + def depart_literal(self, node): self.w('</tt>') - def format_reference(self, node): - ''' (%text.model;) - %reference.atts; - %anonymous.att; - ''' + def visit_reference(self, node): attrs = node.attributes doc = self.document ok = 1 @@ -292,53 +247,56 @@ class DumbPythonPointFormatter: name = attrs['refname'] self.w('<a href="#%s">'%urllib.quote(name)) else: - ok = 0 self.w('<span class="formatter_error">target "%s" ' 'undefined</span>'%attrs['refname']) - if node.children: self.format(node) - if ok: - self.w('</a>') - - def format_footnote_reference(self, node): - ''' (#PCDATA) - %reference.atts; - %auto.att; - ''' + + def depart_reference(self, node): + self.w('</a>') + + def visit_footnote_reference(self, node): raise NotImplementedError, node - def format_substitution_reference(self, node): - ''' (%text.model;) - %refname.att; - ''' - #raise NotImplementedError, node + def visit_substitution_reference(self, node): + pass + def depart_substitution_reference(self, node): pass - def format_problematic(self, node): - ''' (%text.model;) - ''' + def visit_problematic(self, node): + raise NotImplementedError, node + def depart_problematic(self, node): raise NotImplementedError, node - def format_system_message(self, node): - ''' just print it to stderr - ''' + def visit_system_message(self, node): print >>sys.stderr, '%s: %s'%(node.attributes['type'], node[0][0].data) + def depart_system_message(self, node): + pass - # - # Finally, #text - # - def format__text(self, node): + def visit_comment(self, node): + pass + def depart_comment(self, node): + pass + + def visit_Text(self, node): self.w(cgi.escape(node.data)) + def depart_Text(self, node): + pass def main(filename, debug=0): - parser = get_parser_class('rest')() - input = open(filename).read() - document = docutils.utils.new_document(parser) - parser.parse(input, document) + pub = docutils.core.Publisher() + pub.set_reader('standalone', None, 'restructuredtext') + pub.writer = Writer() + pub.get_settings() + pub.settings._destination = '' + pub.source = docutils.io.StringInput(source=open(filename).read(), + encoding='latin-1') + pub.destination = docutils.io.StringOutput(encoding='latin-1') + document = pub.reader.read(pub.source, pub.parser, pub.settings) + pub.apply_transforms(document) + if debug == 1: print document.pformat() else: - formatter = DumbPythonPointFormatter() - print formatter.format_document(document) + print pub.writer.write(document, pub.destination) if __name__ == '__main__': if len(sys.argv) > 2: |