diff --git a/wiki/base/wiki.py b/wiki/base/wiki.py index 7b9506e..fce6ef4 100644 --- a/wiki/base/wiki.py +++ b/wiki/base/wiki.py @@ -30,6 +30,7 @@ from zope.traversing.browser import absoluteURL from cybertools.wiki.common import protocols, ExternalPage from cybertools.wiki.interfaces import IWikiConfiguration from cybertools.wiki.interfaces import IWikiManager, IWiki, IWikiPage +from cybertools.wiki.interfaces import IPreprocessor from cybertools.wiki.interfaces import IParser, IWriter from cybertools.wiki.base.config import BaseConfiguration @@ -198,8 +199,12 @@ class WikiPage(BaseConfiguration): writer = component.getUtility(IWriter, name=writerName) return writer.write(tree) - def preprocess(self, source): - return source + def preprocess(self, text): + preprocs = self.getConfig('preprocessor') or [] + for name in preprocs: + pp = component.getUtility(IPreprocessor, name=name) + text = pp(text) + return text def postprocess(self, result): return result diff --git a/wiki/generic/mixin.py b/wiki/generic/mixin.py index e7b02ec..f1642cb 100644 --- a/wiki/generic/mixin.py +++ b/wiki/generic/mixin.py @@ -76,6 +76,7 @@ class WikiManager(BaseWikiManager): plugins[(IIntIds, '')] = IntIds() plugins[(ILinkManager, 'internal')] = LinkManager(self) self.setConfig('linkManager', 'internal') + self.setConfig('preprocessor', ['mediawiki']) def addWiki(self, wiki): uid = self.getUid(wiki) diff --git a/wiki/interfaces.py b/wiki/interfaces.py index 01515bf..2abd410 100644 --- a/wiki/interfaces.py +++ b/wiki/interfaces.py @@ -314,3 +314,10 @@ class ILinkProcessor(INodeProcessor): def addText(text): """ Add additional text to the link on the rendered page. """ + + +class IPreprocessor(Interface): + """ A callable with one argument (a string or unicode object) that + processes this source and returns the resulting string or unicode + object. + """ diff --git a/wiki/preproc/README.txt b/wiki/preproc/README.txt new file mode 100644 index 0000000..c0a038a --- /dev/null +++ b/wiki/preproc/README.txt @@ -0,0 +1,17 @@ +============================ +Standard Wiki Pre-processors +============================ + + ($Id$) + + +MediaWiki Links +=============== + + >>> from cybertools.wiki.preproc.mediawiki import preprocess + + >>> src = '''Some text with [[a link]] and + ... [[link2 | another link]] with separate text.''' + + >>> preprocess(src) + 'Some text with `a link `__ and\n`another link `__ with separate text.' diff --git a/wiki/preproc/__init__.py b/wiki/preproc/__init__.py new file mode 100644 index 0000000..38314f3 --- /dev/null +++ b/wiki/preproc/__init__.py @@ -0,0 +1,3 @@ +""" +$Id$ +""" diff --git a/wiki/preproc/mediawiki.py b/wiki/preproc/mediawiki.py new file mode 100644 index 0000000..166f8f8 --- /dev/null +++ b/wiki/preproc/mediawiki.py @@ -0,0 +1,43 @@ +# +# Copyright (c) 2010 Helmut Merz helmutm@cy55.de +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +""" +Pre-processor for supporting the MediaWiki link format. + +$Id$ +""" + +import re + + +linkPattern = re.compile(r'\[\[(.+)\]\]') + + +def createRstxLink(match): + value = match.group(1) + parts = value.split('|') + text = name = parts[0].strip() + if len(parts) > 1: + text = parts[-1].strip() + return '`%s <%s>`__' % (text, name) + + +def preprocess(source): + result = linkPattern.sub(createRstxLink, source) + return result + diff --git a/wiki/tests.py b/wiki/tests.py index 56f3fd7..5910464 100755 --- a/wiki/tests.py +++ b/wiki/tests.py @@ -70,6 +70,7 @@ def test_suite(): return unittest.TestSuite(( unittest.makeSuite(Test), DocFileSuite('README.txt', optionflags=flags, setUp=setUp), + DocFileSuite('preproc/README.txt', optionflags=flags), )) if __name__ == '__main__':