provide preprocessor for MediaWiki format links

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@3973 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2010-08-23 13:37:53 +00:00
parent 76ab40712d
commit b3bec8feac
7 changed files with 79 additions and 2 deletions

View file

@ -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

View file

@ -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)

View file

@ -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.
"""

17
wiki/preproc/README.txt Normal file
View file

@ -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 <a link>`__ and\n`another link <link2>`__ with separate text.'

3
wiki/preproc/__init__.py Normal file
View file

@ -0,0 +1,3 @@
"""
$Id$
"""

43
wiki/preproc/mediawiki.py Normal file
View file

@ -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

View file

@ -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__':