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
This commit is contained in:
helmutm 2009-01-06 19:31:29 +00:00
parent 230f22ae08
commit c3d47fd093
8 changed files with 233 additions and 21 deletions

View file

@ -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()
<p><strong>Welcome to the Demo Wiki</strong></p>
**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.

56
wiki/base/config.py Normal file
View file

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

View file

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

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

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

47
wiki/dcu/html.py Normal file
View file

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

36
wiki/dcu/rstx.py Normal file
View file

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

View file

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

View file

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