changeset 1908:8bd4b581cc60

make converter pluggable, dynamically load correct converter, move loading of parser to send_page_content
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 25 Mar 2007 01:34:47 +0100
parents 3799c27e664a
children be140a589f11
files MoinMoin/Page.py MoinMoin/PageGraphicalEditor.py MoinMoin/_tests/broken/test_converter_text_html_text_moin_wiki.py MoinMoin/action/edit.py MoinMoin/converter/__init__.py MoinMoin/converter/text_html_text_moin_wiki.py MoinMoin/error.py MoinMoin/wikiutil.py wiki/data/plugin/converter/__init__.py
diffstat 9 files changed, 41 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/Page.py	Sat Mar 24 22:25:00 2007 +0100
+++ b/MoinMoin/Page.py	Sun Mar 25 01:34:47 2007 +0100
@@ -1101,9 +1101,6 @@
                                     allow_doubleclick=1, trail=trail,
                                     )
 
-        # Load the parser
-        Parser = wikiutil.searchAndImportPlugin(request.cfg, "parser", pi['format'])
-
         # new page?
         if not page_exists and (not content_only or (content_only
                                                      and send_missing_page)):
@@ -1124,7 +1121,8 @@
             request.write(self.formatter.startContent(content_id))
 
             # parse the text and send the page content
-            self.send_page_content(request, Parser, body,
+            self.send_page_content(request, body,
+                                   format=pi['format'],
                                    format_args=pi['formatargs'],
                                    do_cache=do_cache,
                                    start_line=pi['lines'])
@@ -1196,17 +1194,18 @@
             return getattr(parser, 'caching', False)
         return False
 
-    def send_page_content(self, request, Parser, body, format_args='',
-                          do_cache=1, **kw):
+    def send_page_content(self, request, body, format='wiki', format_args='', do_cache=1, **kw):
         """ Output the formatted wiki page, using caching if possible
 
         @param request: the request object
-        @param Parser: Parser class
         @param body: text of the wiki page
+        @param format: format of content, default 'wiki'
         @param format_args: #format arguments, used by some parsers
         @param do_cache: if True, use cached content
         """
         request.clock.start('send_page_content')
+        # Load the parser
+        Parser = wikiutil.searchAndImportPlugin(request.cfg, "parser", format)
         parser = Parser(body, request, format_args=format_args, **kw)
 
         if not (do_cache and self.canUseCache(Parser)):
--- a/MoinMoin/PageGraphicalEditor.py	Sat Mar 24 22:25:00 2007 +0100
+++ b/MoinMoin/PageGraphicalEditor.py	Sun Mar 25 01:34:47 2007 +0100
@@ -13,7 +13,7 @@
 from MoinMoin.widget import html
 from MoinMoin.widget.dialog import Status
 from MoinMoin.util import web
-from MoinMoin.parser.text_moin_wiki import Parser
+from MoinMoin.parser.text_moin_wiki import Parser as WikiParser
 
 def execute(pagename, request):
     if not request.user.may.write(pagename):
@@ -28,7 +28,7 @@
 
     def word_rule(self):
         regex = re.compile(r"\(\?<![^)]*?\)")
-        word_rule = regex.sub("", Parser.word_rule)
+        word_rule = regex.sub("", WikiParser.word_rule)
         return repr(word_rule)[1:]
 
     def sendEditor(self, **kw):
@@ -233,6 +233,10 @@
         # Send revision of the page our edit is based on
         request.write('<input type="hidden" name="rev" value="%d">' % (rev,))
 
+        # Add src format (e.g. 'wiki') into a hidden form field, so that
+        # we can load the correct converter after POSTing.
+        request.write('<input type="hidden" name="format" value="%s">' % self.pi['format'])
+        
         # Create and send a ticket, so we can check the POST
         request.write('<input type="hidden" name="ticket" value="%s">' % wikiutil.createTicket(request))
 
@@ -318,7 +322,7 @@
         from MoinMoin.formatter.text_gedit import Formatter
         self.formatter = Formatter(request)
         self.formatter.page = self
-        output = request.redirectedOutput(self.send_page_content, request, Parser, raw_body, do_cache=False)
+        output = request.redirectedOutput(self.send_page_content, request, raw_body, format=self.pi['format'], do_cache=False)
         output = repr(output)
         if output[0] == 'u':
             output = output[1:]
--- a/MoinMoin/_tests/broken/test_converter_text_html_text_moin_wiki.py	Sat Mar 24 22:25:00 2007 +0100
+++ b/MoinMoin/_tests/broken/test_converter_text_html_text_moin_wiki.py	Sun Mar 25 01:34:47 2007 +0100
@@ -14,9 +14,10 @@
 from MoinMoin.parser.text_moin_wiki import Parser
 from MoinMoin.formatter.text_gedit import Formatter
 from MoinMoin.request import Clock
+from MoinMoin.error import import ConvertError
 
 convert = converter.convert
-error = converter.ConvertError
+error = ConvertError
 
 
 class BaseTests(unittest.TestCase):
--- a/MoinMoin/action/edit.py	Sat Mar 24 22:25:00 2007 +0100
+++ b/MoinMoin/action/edit.py	Sun Mar 25 01:34:47 2007 +0100
@@ -85,10 +85,16 @@
             pg.send_page(msg=msg)
             return
 
-    # convert input from Graphical editor
-    from MoinMoin.converter.text_html_text_moin_wiki import convert, ConvertError
+    from MoinMoin.error import ConvertError
     try:
         if lasteditor == 'gui':
+            # convert input from Graphical editor
+            format = request.form.get('format', ['wiki'])[0]
+            if format == 'wiki':
+                converter_name = 'text_html_text_moin_wiki'
+            else:
+                converter_name = 'undefined' # XXX we don't have other converters yet
+            convert = wikiutil.searchAndImportPlugin(request.cfg, "converter", converter_name)
             savetext = convert(request, pagename, savetext)
 
         # IMPORTANT: normalize text from the form. This should be done in
--- a/MoinMoin/converter/__init__.py	Sat Mar 24 22:25:00 2007 +0100
+++ b/MoinMoin/converter/__init__.py	Sun Mar 25 01:34:47 2007 +0100
@@ -6,3 +6,9 @@
     @license: GNU GPL, see COPYING for details.
 """
 
+# -*- coding: iso-8859-1 -*-
+
+from MoinMoin.util import pysupport
+
+modules = pysupport.getPackageModules(__file__)
+
--- a/MoinMoin/converter/text_html_text_moin_wiki.py	Sat Mar 24 22:25:00 2007 +0100
+++ b/MoinMoin/converter/text_html_text_moin_wiki.py	Sun Mar 25 01:34:47 2007 +0100
@@ -10,6 +10,7 @@
 from xml.dom import Node
 
 from MoinMoin import config, wikiutil, error
+from MoinMoin.error import ConvertError
 
 # Portions (C) International Organization for Standardization 1986
 # Permission to copy in any form is granted for use with
@@ -404,11 +405,6 @@
             qpagename = "'%s'" % pagename
     return pagename, qpagename
 
-class ConvertError(error.FatalError):
-    """ Raise when html to wiki conversion fails """
-    name = "MoinMoin Convert Error"
-
-
 class visitor(object):
     def do(self, tree):
         self.visit_node_list(tree.childNodes)
--- a/MoinMoin/error.py	Sat Mar 24 22:25:00 2007 +0100
+++ b/MoinMoin/error.py	Sun Mar 25 01:34:47 2007 +0100
@@ -109,3 +109,8 @@
     """ we didn't find a configuration for this URL """
     pass
 
+class ConvertError(FatalError):
+    """ Raise when html to storage format (e.g. 'wiki') conversion fails """
+    name = "MoinMoin Convert Error"
+
+
--- a/MoinMoin/wikiutil.py	Sat Mar 24 22:25:00 2007 +0100
+++ b/MoinMoin/wikiutil.py	Sun Mar 25 01:34:47 2007 +0100
@@ -1113,6 +1113,7 @@
 def searchAndImportPlugin(cfg, type, name, what=None):
     type2classname = {"parser": "Parser",
                       "formatter": "Formatter",
+                      "converter": "convert",
     }
     if what is None:
         what = type2classname[type]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wiki/data/plugin/converter/__init__.py	Sun Mar 25 01:34:47 2007 +0100
@@ -0,0 +1,5 @@
+# -*- coding: iso-8859-1 -*-
+
+from MoinMoin.util import pysupport
+
+modules = pysupport.getPackageModules(__file__)