--- /usr/local/lib/python2.3/site-packages/MoinMoin/parser/rst.py.bak Tue Jun 8 17:35:43 2004 +++ /usr/local/lib/python2.3/site-packages/MoinMoin/parser/rst.py.firstmod Tue Jun 8 22:01:31 2004 @@ -11,6 +11,7 @@ import cgi, sys from docutils import core, nodes, utils from docutils.parsers import rst +from docutils.transforms import references, Transformer ############################################################################# @@ -32,13 +33,26 @@ """ #core.publish(source=self.raw, destination=sys.stdout, writer_name='html') + transforms = \ + ( references.Substitutions + , references.ChainedTargets + , references.AnonymousHyperlinks + , references.IndirectHyperlinks + , references.Footnotes + , references.ExternalTargets + , references.InternalTargets + ) + parser = rst.Parser() document = utils.new_document(None) + transformer = Transformer (document) + transformer.add_transforms (transforms) if hasattr(document, 'settings'): document.settings.tab_width = 8 document.settings.pep_references = None document.settings.rfc_references = None parser.parse(self.raw, document) + transformer.apply_transforms () if 1: document.walkabout(MoinTranslator(self.request, formatter, document)) @@ -55,22 +69,48 @@ print '' +class LinkContext : + def __init__ (self, formatter, href, text=None) : + self.formatter = formatter + self.href = href + self.text = [] + if text : + self.text.append(text) + + def format (self) : + repl = "TO_BE_REPLACED" + # avoid escaping generated commands: + if self.formatter : + if self.text : + output = self.formatter (self.href, repl) + return output.replace (repl, "".join (self.text)) + else : + return self.formatter (self.href) + return "" + class MoinTranslator(nodes.GenericNodeVisitor): def __init__(self, request, formatter, document): nodes.NodeVisitor.__init__(self, document) self.request = request self.formatter = formatter + self.refs = [] self.level = 0 + def gen_output (self, text) : + if self.refs : + self.refs [-1].text.append (text) + else : + self.request.write (text) + def default_visit(self, node): #self.default_visit(node) - self.request.write(self.formatter.highlight(1)) - self.request.write(self.formatter.preformatted(1)) - self.request.write(self.formatter.text(node.pformat('\xA0\xA0\xA0\xA0', 1))) - self.request.write(self.formatter.preformatted(0)) - self.request.write(self.formatter.text(node.astext())) - self.request.write(self.formatter.highlight(0)) + self.gen_output(self.formatter.highlight(1)) + self.gen_output(self.formatter.preformatted(1)) + self.gen_output(self.formatter.text(node.pformat('\xA0\xA0\xA0\xA0', 1))) + self.gen_output(self.formatter.preformatted(0)) + self.gen_output(self.formatter.text(node.astext())) + self.gen_output(self.formatter.highlight(0)) def default_departure(self, node): pass @@ -85,13 +125,15 @@ self.level -= 1 def visit_Text(self, node): - self.request.write(self.formatter.text(node.astext())) + self.gen_output(self.formatter.text(node.astext())) def visit_title(self, node): - self.request.write(self.formatter.heading(self.level, '', on=1)) + self.gen_output(self.formatter.heading(self.level, '', on=1)) + if node.parent.hasattr ('id') : + self.gen_output(self.formatter.anchordef(node.parent ['id'])) def depart_title(self, node): - self.request.write(self.formatter.heading(self.level, '', on=0)) + self.gen_output(self.formatter.heading(self.level, '', on=0)) # @@ -99,22 +141,22 @@ # def visit_emphasis(self, node): - self.request.write(self.formatter.emphasis(1)) + self.gen_output(self.formatter.emphasis(1)) def depart_emphasis(self, node): - self.request.write(self.formatter.emphasis(0)) + self.gen_output(self.formatter.emphasis(0)) def visit_strong(self, node): - self.request.write(self.formatter.strong(1)) + self.gen_output(self.formatter.strong(1)) def depart_strong(self, node): - self.request.write(self.formatter.strong(0)) + self.gen_output(self.formatter.strong(0)) def visit_literal(self, node): - self.request.write(self.formatter.code(1)) + self.gen_output(self.formatter.code(1)) def depart_literal(self, node): - self.request.write(self.formatter.code(0)) + self.gen_output(self.formatter.code(0)) # @@ -123,19 +165,19 @@ def visit_paragraph(self, node): #if self.topic_class != 'contents': - self.request.write(self.formatter.paragraph(1)) + self.gen_output(self.formatter.paragraph(1)) def depart_paragraph(self, node): #if self.topic_class == 'contents': - # self.request.write(self.formatter.linebreak()) + # self.gen_output(self.formatter.linebreak()) #else: - self.request.write(self.formatter.paragraph(0)) + self.gen_output(self.formatter.paragraph(0)) def visit_literal_block(self, node): - self.request.write(self.formatter.preformatted(1)) + self.gen_output(self.formatter.preformatted(1)) def depart_literal_block(self, node): - self.request.write(self.formatter.preformatted(0)) + self.gen_output(self.formatter.preformatted(0)) # @@ -143,22 +185,22 @@ # def visit_bullet_list(self, node): - self.request.write(self.formatter.bullet_list(1)) + self.gen_output(self.formatter.bullet_list(1)) def depart_bullet_list(self, node): - self.request.write(self.formatter.bullet_list(0)) + self.gen_output(self.formatter.bullet_list(0)) def visit_enumerated_list(self, node): - self.request.write(self.formatter.number_list(1, start=node.get('start', None))) + self.gen_output(self.formatter.number_list(1, start=node.get('start', None))) def depart_enumerated_list(self, node): - self.request.write(self.formatter.number_list(0)) + self.gen_output(self.formatter.number_list(0)) def visit_list_item(self, node): - self.request.write(self.formatter.listitem(1)) + self.gen_output(self.formatter.listitem(1)) def depart_list_item(self, node): - self.request.write(self.formatter.listitem(0)) + self.gen_output(self.formatter.listitem(0)) # @@ -166,25 +208,25 @@ # def visit_definition_list(self, node): - self.request.write(self.formatter.definition_list(1)) + self.gen_output(self.formatter.definition_list(1)) def visit_definition_list_item(self, node): pass def visit_term(self, node): - self.request.write('
') + self.gen_output('
') def depart_term(self, node): - self.request.write('
') + self.gen_output('') def visit_definition(self, node): - self.request.write('
') + self.gen_output('
') def depart_definition(self, node): - self.request.write('
') + self.gen_output('') def depart_definition_list(self, node): - self.request.write(self.formatter.definition_list(0)) + self.gen_output(self.formatter.definition_list(0)) # @@ -192,10 +234,10 @@ # def visit_block_quote(self, node): - self.request.write(self.formatter.definition_list(1)) + self.gen_output(self.formatter.definition_list(1)) def depart_block_quote(self, node): - self.request.write(self.formatter.definition_list(0)) + self.gen_output(self.formatter.definition_list(0)) # @@ -203,25 +245,25 @@ # def visit_field_list(self, node): - self.request.write(self.formatter.bullet_list(1)) + self.gen_output(self.formatter.bullet_list(1)) def visit_field(self, node): - self.request.write(self.formatter.listitem(1)) + self.gen_output(self.formatter.listitem(1)) def visit_field_name(self, node): - self.request.write(self.formatter.strong(1)) + self.gen_output(self.formatter.strong(1)) def depart_field_name(self, node): - self.request.write(': ' + self.formatter.strong(0)) + self.gen_output(': ' + self.formatter.strong(0)) def visit_field_body(self, node): pass def depart_field(self, node): - self.request.write(self.formatter.listitem(0)) + self.gen_output(self.formatter.listitem(0)) def depart_field_list(self, node): - self.request.write(self.formatter.bullet_list(0)) + self.gen_output(self.formatter.bullet_list(0)) # @@ -229,12 +271,32 @@ # def visit_reference(self, node): - self.request.write(self.formatter.highlight(1)) - self.request.write('<<<') + href = '' + formatter = self.formatter.anchorlink + if node.has_key('refuri'): + href = node['refuri'] + formatter = self.formatter.url + elif node.has_key('refid'): + href = node['refid'] + elif node.has_key('refname'): + href = self.document.nameids[node['refname']] + if href : + self.refs.append (LinkContext(formatter, href)) + + def visit_target(self, node): + if not (node.has_key('refuri') or node.has_key('refid') + or node.has_key('refname')): + self.refs.append(LinkContext (self.formatter.anchordef, self.name)) + else: + self.refs.append(LinkContext (None, None)) + + def depart_target (self, node): + if self.refs : + self.gen_output (self.refs.pop ().format ()) def depart_reference(self, node): - self.request.write('>>>') - self.request.write(self.formatter.highlight(0)) + if self.refs : + self.gen_output (self.refs.pop ().format ()) # @@ -242,10 +304,10 @@ # def visit_warning(self, node): - self.request.write(self.formatter.highlight(1)) + self.gen_output(self.formatter.highlight(1)) def depart_warning(self, node): - self.request.write(self.formatter.highlight(0)) + self.gen_output(self.formatter.highlight(0)) # @@ -253,8 +315,8 @@ # def visit_system_message(self, node): - self.request.write(self.formatter.highlight(1)) - self.request.write('[%s]' % node.astext()) - self.request.write(self.formatter.highlight(0)) + self.gen_output(self.formatter.highlight(1)) + self.gen_output('[%s]' % node.astext()) + self.gen_output(self.formatter.highlight(0))