From c3d47fd093524a1a20f2aa5f90d2b6900543c663 Mon Sep 17 00:00:00 2001 From: helmutm Date: Tue, 6 Jan 2009 19:31:29 +0000 Subject: [PATCH] set up basic plugin structure with utilities for configuration, rStx parsing and HTML writing git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@3121 fd906abe-77d9-0310-91a1-e0d9ade77398 --- wiki/README.txt | 9 +++---- wiki/base/config.py | 56 ++++++++++++++++++++++++++++++++++++++++++++ wiki/base/wiki.py | 45 +++++++++++++++++++++++------------ wiki/dcu/__init__.py | 3 +++ wiki/dcu/html.py | 47 +++++++++++++++++++++++++++++++++++++ wiki/dcu/rstx.py | 36 ++++++++++++++++++++++++++++ wiki/interfaces.py | 45 ++++++++++++++++++++++++++++++++++- wiki/tests.py | 13 +++++++++- 8 files changed, 233 insertions(+), 21 deletions(-) create mode 100644 wiki/base/config.py create mode 100644 wiki/dcu/__init__.py create mode 100644 wiki/dcu/html.py create mode 100644 wiki/dcu/rstx.py diff --git a/wiki/README.txt b/wiki/README.txt index 90e9c7e..08c0539 100644 --- a/wiki/README.txt +++ b/wiki/README.txt @@ -23,20 +23,21 @@ We format the content of the start page using the restructured text format. ... **Welcome to the Demo Wiki** ... ''' -As we have not yet registered any formatting plugins rendering the page -returns it content unchanged. +The parser for restructured text and a corresponding HTML writer are the +default plugins used, so we can already render the page as HTML. >>> tree = startPage.parse() >>> print startPage.render()

Welcome to the Demo Wiki

-**Welcome to the Demo Wiki** - A Very Basic Wiki Format ======================== +(this is probably obsolete, will be replaced by corresponding WikiPage +parsing and rendering functionality) + We first set up a format (a utility) and create a format instance from it. The instance needs a wiki page as its context - to simplify things during testing we just use a bare object. diff --git a/wiki/base/config.py b/wiki/base/config.py new file mode 100644 index 0000000..a168778 --- /dev/null +++ b/wiki/base/config.py @@ -0,0 +1,56 @@ +# +# Copyright (c) 2009 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 +# + +""" +Basic Configuration implementations. + +$Id$ +""" + +from zope.interface import implements + +from cybertools.wiki.interfaces import IWikiConfiguration + + +class BaseConfiguration(object): + """ The base class for all wiki configuration implementations. + """ + + implements(IWikiConfiguration) + + parent = None + + writer = parser = None + + def getConfig(self, functionality): + c = getattr(self, functionality, None) + if c is None: + return self.getParent().getConfig(functionality) + return c + + def getParent(self): + return self.parent + + +class WikiConfiguration(BaseConfiguration): + """ A global utility providing the default settings. + """ + + writer = 'docutils.html' + parser = 'docutils.rstx' + diff --git a/wiki/base/wiki.py b/wiki/base/wiki.py index e8ed80a..23711e6 100644 --- a/wiki/base/wiki.py +++ b/wiki/base/wiki.py @@ -1,5 +1,5 @@ # -# Copyright (c) 2008 Helmut Merz helmutm@cy55.de +# Copyright (c) 2009 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 @@ -22,14 +22,16 @@ A Wiki manager managing wikis and wiki-related objects, esp plugins. $Id$ """ -from docutils.core import publish_doctree, publish_from_doctree -from docutils.writers.html4css1 import HTMLTranslator, Writer +from zope import component from zope.interface import implements +from cybertools.wiki.interfaces import IWikiConfiguration from cybertools.wiki.interfaces import IWikiManager, IWiki, IWikiPage +from cybertools.wiki.interfaces import IParser, IWriter +from cybertools.wiki.base.config import BaseConfiguration -class WikiManager(object): +class WikiManager(BaseConfiguration): implements(IWikiManager) @@ -44,11 +46,18 @@ class WikiManager(object): wiki.manager = self return wiki + # configuration -class Wiki(object): + def getParent(self): + return component.getUtility(IWikiConfiguration) + + +class Wiki(BaseConfiguration): implements(IWiki) + manager = None + def __init__(self, name, title=None): self.name = name self.title = title or name @@ -61,11 +70,17 @@ class Wiki(object): page.wiki = self return page + # configuration -class WikiPage(object): + def getParent(self): + return self.manager + + +class WikiPage(BaseConfiguration): implements(IWikiPage) + wiki = None text = u'' def __init__(self, name, title=None): @@ -76,17 +91,17 @@ class WikiPage(object): return self.write(self.parse()) def parse(self): - # TODO: delegate to parser plugin - return publish_doctree(self.text) + parserName = self.getConfig('parser') + parser = component.getUtility(IParser, name=parserName) + return parser.parse(self.text) def write(self, tree): - # TODO: delegate to writer plugin, use translator plugins - writer = Writer() - writer.translator_class = HTMLBodyTranslator - return publish_from_doctree(tree, writer=writer) + writerName = self.getConfig('writer') + writer = component.getUtility(IWriter, name=writerName) + return writer.write(tree) + # configuration -class HTMLBodyTranslator(HTMLTranslator): + def getParent(self): + return self.wiki - def astext(self): - return u''.join(self.body_pre_docinfo + self.docinfo + self.body) diff --git a/wiki/dcu/__init__.py b/wiki/dcu/__init__.py new file mode 100644 index 0000000..38314f3 --- /dev/null +++ b/wiki/dcu/__init__.py @@ -0,0 +1,3 @@ +""" +$Id$ +""" diff --git a/wiki/dcu/html.py b/wiki/dcu/html.py new file mode 100644 index 0000000..eff81c8 --- /dev/null +++ b/wiki/dcu/html.py @@ -0,0 +1,47 @@ +# +# Copyright (c) 2009 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 +# + +""" +A writer implementation based on the docutils HTML writer. + +$Id$ +""" + +from docutils.core import publish_from_doctree +from docutils.writers.html4css1 import HTMLTranslator, Writer as HTMLWriter +from zope.interface import implements + +from cybertools.wiki.interfaces import IWriter + + +class Writer(object): + + implements(IWriter) + + def __init__(self): + self.writer = HTMLWriter() + self.writer.translator_class = HTMLBodyTranslator + + def write(self, tree): + return publish_from_doctree(tree, writer=self.writer) + + +class HTMLBodyTranslator(HTMLTranslator): + + def astext(self): + return u''.join(self.body_pre_docinfo + self.docinfo + self.body) diff --git a/wiki/dcu/rstx.py b/wiki/dcu/rstx.py new file mode 100644 index 0000000..eef50d0 --- /dev/null +++ b/wiki/dcu/rstx.py @@ -0,0 +1,36 @@ +# +# Copyright (c) 2009 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 +# + +""" +A Wiki manager managing wikis and wiki-related objects, esp plugins. + +$Id$ +""" + +from docutils.core import publish_doctree +from zope.interface import implements + +from cybertools.wiki.interfaces import IParser + + +class Parser(object): + + implements(IParser) + + def parse(self, text): + return publish_doctree(text) diff --git a/wiki/interfaces.py b/wiki/interfaces.py index 044dde0..3d7cc1e 100644 --- a/wiki/interfaces.py +++ b/wiki/interfaces.py @@ -1,5 +1,5 @@ # -# Copyright (c) 2008 Helmut Merz helmutm@cy55.de +# Copyright (c) 2009 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 @@ -25,6 +25,27 @@ $Id$ from zope.interface import Interface, Attribute +class IWikiConfiguration(Interface): + """ Provides information about the implementations to be used for + the various kinds of wiki plug-ins. + """ + + writer = Attribute('Plug-in component converting from internal tree ' + 'format to presentation format.') + parser = Attribute('Plug-in component converting from text input ' + 'format to internal tree format.') + + def getConfig(functionality): + """ Return the name of the plugin that should used for the + functionality given. + """ + + def getParent(): + """ Return the parent object in case this configuration does not + provide configuration information for a certain functionality. + """ + + class IWikiManager(Interface): """ Manages wikis and wiki-related objects, like plugins. """ @@ -83,6 +104,25 @@ class IWikiPage(Interface): """ +# wiki plugins + +class IParser(Interface): + """ Converts from (plain text) input format to internal format. + """ + + def parse(text): + """ Return internal tree structure. + """ + +class IWriter(Interface): + """ Converts from internal tree format to presentation format. + """ + + def write(tree): + """ Returns presentation format for the tree given. + """ + + # wiki elements class ILinkManager(Interface): @@ -98,6 +138,9 @@ class ILinkManager(Interface): """ +# TODO: convert the following stuff so that it fits in the parser/writer +# paradigm. + class ILink(Interface): """A hyperlink between two local or foreign objects. """ diff --git a/wiki/tests.py b/wiki/tests.py index 549519f..45fad68 100755 --- a/wiki/tests.py +++ b/wiki/tests.py @@ -8,6 +8,11 @@ $Id$ import unittest, doctest from zope.testing.doctestunit import DocFileSuite +from zope import component + +from cybertools.wiki.base.config import WikiConfiguration +from cybertools.wiki.dcu.html import Writer as DocutilsHTMLWriter +from cybertools.wiki.dcu.rstx import Parser as DocutilsRstxParser class Test(unittest.TestCase): @@ -17,11 +22,17 @@ class Test(unittest.TestCase): pass +def setUp(testCase): + component.provideUtility(WikiConfiguration()) + component.provideUtility(DocutilsHTMLWriter(), name='docutils.html') + component.provideUtility(DocutilsRstxParser(), name='docutils.rstx') + + def test_suite(): flags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS return unittest.TestSuite(( unittest.makeSuite(Test), - DocFileSuite('README.txt', optionflags=flags), + DocFileSuite('README.txt', optionflags=flags, setUp=setUp), )) if __name__ == '__main__':