Anonymous avatar Anonymous committed 08d0967

[svn r11148] XmlRpcPlugin: `wiki.getPageHTML()` now supports wiki manipulators that can possibly modify the wiki text content before rendering. With test.

Comments (0)

Files changed (2)

trunk/tracrpc/tests/wiki.py

         markup_2 = self.admin.wiki.getPageHTML('ImageTest')
         self.assertEquals(markup_2, markup_1)
 
+    def test_getPageHTMLWithManipulator(self):
+        self.admin.wiki.putPage('FooBar', 'foo bar', {})
+        # Enable wiki manipulator
+        plugin = os.path.join(rpc_testenv.tracdir, 'plugins', 'Manipulator.py')
+        open(plugin, 'w').write(
+        "from trac.core import *\n"
+        "from trac.wiki.api import IWikiPageManipulator\n"
+        "class WikiManipulator(Component):\n"
+        "    implements(IWikiPageManipulator)\n"
+        "    def prepare_wiki_page(self, req, page, fields):\n"
+        "        fields['text'] = 'foo bar baz'\n"
+        "    def validate_wiki_page(req, page):\n"
+        "        return []\n")
+        rpc_testenv.restart()
+        # Perform tests
+        self.assertEquals('<html><body><p>\nfoo bar baz\n</p>\n</body></html>',
+                          self.admin.wiki.getPageHTML('FooBar'))
+        # Remove plugin and restart
+        os.unlink(plugin)
+        rpc_testenv.restart()
 
 def test_suite():
     return unittest.makeSuite(RpcWikiTestCase)

trunk/tracrpc/wiki.py

 from trac.core import *
 from trac.mimeview import Context
 from trac.resource import Resource, ResourceNotFound
-from trac.wiki.api import WikiSystem
+from trac.wiki.api import WikiSystem, IWikiPageManipulator
 from trac.wiki.model import WikiPage
 from trac.wiki.formatter import wiki_to_html, format_to_html
 
 
     implements(IXMLRPCHandler)
 
+    manipulators = ExtensionPoint(IWikiPageManipulator)
+
     def __init__(self):
         self.wiki = WikiSystem(self.env)
 
         yield ('WIKI_VIEW', ((list, str),), self.listLinks)
         yield ('WIKI_VIEW', ((str, str),), self.wikiToHtml)
 
+    def _fetch_page(self, req, pagename, version=None):
+        # Helper for getting the WikiPage that performs basic checks
+        page = WikiPage(self.env, pagename, version)
+        req.perm(page.resource).require('WIKI_VIEW')
+        if page.exists:
+            return page
+        else:
+            msg = 'Wiki page "%s" does not exist' % pagename
+            if version is not None:
+                msg += ' at version %s' % version
+            raise ResourceNotFound(msg)
+
     def _page_info(self, name, when, author, version, comment):
         return dict(name=name, lastModified=when,
                     author=author, version=int(version), comment=comment)
 
     def getPage(self, req, pagename, version=None):
         """ Get the raw Wiki text of page, latest version. """
-        page = WikiPage(self.env, pagename, version)
-        req.perm(page.resource).require('WIKI_VIEW')
-        if page.exists:
-            return page.text
-        else:
-            msg = 'Wiki page "%s" does not exist' % pagename
-            if version is not None:
-                msg += ' at version %s' % version
-            raise ResourceNotFound(msg)
+        page = self._fetch_page(req, pagename, version)
+        return page.text
 
     def getPageHTML(self, req, pagename, version=None):
         """ Return page in rendered HTML, latest version. """
-        text = self.getPage(req, pagename, version)
-        resource = Resource('wiki', pagename, version)
-        context = Context.from_request(req, resource, absurls=True)
-        html = format_to_html(self.env, context, text)
+        page = self._fetch_page(req, pagename, version)
+        fields = {'text': page.text}
+        for manipulator in self.manipulators:
+            manipulator.prepare_wiki_page(req, page, fields)
+        context = Context.from_request(req, page.resource, absurls=True)
+        html = format_to_html(self.env, context, fields['text'])
         return '<html><body>%s</body></html>' % html
 
     def getAllPages(self, req):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.