move link processing from separate tree processor to writer/translator

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@3209 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2009-02-06 09:01:31 +00:00
parent 75b59f8308
commit 495f048fbb
8 changed files with 47 additions and 108 deletions

View file

@ -75,7 +75,7 @@ Links to not yet existing pages
<p>This is the cybertools demo wiki.</p>
<p><a class="reference"
href="http://127.0.0.1/demo_wiki/start_page">Back to the Start Page</a></p>
<p><a class="reference"
<p><a class="reference create"
href="http://127.0.0.1/demo_wiki/create.html?linkid=0000002">?More...</a></p>

View file

@ -56,9 +56,8 @@ class WikiConfiguration(BaseConfiguration):
""" A global utility providing the default settings.
"""
writer = 'docutils.html'
parser = 'docutils.rstx'
processor = 'standard'
writer = 'docutils.html'
linkManager = 'basic'
nodeProcessors = dict(reference=['default'])

View file

@ -105,11 +105,12 @@ class LinkProcessor(object):
def __init__(self, context):
self.node = self.context = context
self.parent = context.document
def getProperties(self):
raise ValueError("Method 'getProperties()' must be implemented by subclass.")
def process(self):
def process(self, atts):
#print 'processing reference:', self.node
props = self.getProperties()
source = self.parent.context
@ -135,10 +136,12 @@ class LinkProcessor(object):
absoluteURL(wiki, request), link.identifier)
else:
link.refuri = absoluteURL(target, request)
self.setProperty('refuri', link.refuri)
#self.setProperty('href', link.refuri)
atts['href'] = link.refuri
if target is None:
# change CSS class, link text
# needs overriding of HTMLTranslator.visit_reference()
self.setProperty('class', 'create') # no direct effect
#self.setProperty('class', 'create') # no direct effect
atts['class'] += ' create'
self.node.insert(0, Text('?'))

View file

@ -1,70 +0,0 @@
#
# 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
#
"""
Tree processor implementation
$Id$
"""
from zope.interface import implements
from zope.component import adapts
from zope import component
from cybertools.wiki.interfaces import ITreeProcessor, INodeProcessor
from cybertools.wiki.interfaces import IWikiPage
class TreeProcessor(object):
""" The standard tree processor walking the tree and processing
the tree's nodes.
"""
implements(ITreeProcessor)
adapts(IWikiPage)
tree = request = None
visitor = None
def __init__(self, context):
self.context = context
def process(self):
self.visitor = visitor = Visitor(self)
self.tree.walk(visitor)
class Visitor(object):
def __init__(self, context):
self.context = context # the tree processor
self.document = context.tree # needed internally
self.processorNames = self.context.context.getConfig('nodeProcessors')
def dispatch_visit(self, node):
#print 'visiting', node.tagname
tag = node.tagname
procs = []
procNames = self.processorNames.get(tag, [])
for n in procNames:
proc = component.queryAdapter(node, INodeProcessor, name=n)
if proc is not None:
proc.parent = self.context
procs.append(proc)
for p in procs:
p.process()

View file

@ -28,7 +28,7 @@ from zope.app.intid.interfaces import IIntIds
from cybertools.wiki.interfaces import IWikiConfiguration
from cybertools.wiki.interfaces import IWikiManager, IWiki, IWikiPage
from cybertools.wiki.interfaces import IParser, IWriter, ITreeProcessor
from cybertools.wiki.interfaces import IParser, IWriter
from cybertools.wiki.base.config import BaseConfiguration
@ -124,7 +124,8 @@ class WikiPage(BaseConfiguration):
def render(self, request=None):
source = self.preprocess(self.text)
tree = self.parse(source)
self.process(tree, request)
tree.context = self
tree.request = request
result = self.write(tree)
return self.postprocess(result)
@ -141,13 +142,6 @@ class WikiPage(BaseConfiguration):
def preprocess(self, source):
return source
def process(self, tree, request=None):
processor = component.getAdapter(self, ITreeProcessor,
name=self.getConfig('processor'))
processor.tree = tree
processor.request = request
processor.process()
def postprocess(self, result):
return result

View file

@ -23,10 +23,12 @@ $Id$
"""
from docutils.core import publish_from_doctree
from docutils import nodes
from docutils.writers.html4css1 import HTMLTranslator, Writer as HTMLWriter
from zope import component
from zope.interface import implements
from cybertools.wiki.interfaces import IWriter
from cybertools.wiki.interfaces import INodeProcessor, IWriter
class Writer(object):
@ -45,3 +47,35 @@ class HTMLBodyTranslator(HTMLTranslator):
def astext(self):
return u''.join(self.body_pre_docinfo + self.docinfo + self.body)
def visit_reference(self, node):
# copied from docutils.writers.html4css1
if node.has_key('refuri'):
href = node['refuri']
if (self.settings.cloak_email_addresses
and href.startswith('mailto:')):
href = self.cloak_mailto(href)
self.in_mailto = 1
else:
assert node.has_key('refid'), \
'References must have "refuri" or "refid" attribute.'
href = '#' + node['refid']
atts = {'href': href, 'class': 'reference'}
if not isinstance(node.parent, nodes.TextElement):
assert len(node) == 1 and isinstance(node[0], nodes.image)
atts['class'] += ' image-reference'
# wiki processing:
node.document = self.document
self.processNode(node, atts)
self.body.append(self.starttag(node, 'a', '', **atts))
def processNode(self, node, atts):
procs = []
processorNames = self.document.context.getConfig('nodeProcessors')
procNames = processorNames.get(node.tagname, [])
for n in procNames:
proc = component.queryAdapter(node, INodeProcessor, name=n)
if proc is not None:
procs.append(proc)
for p in procs:
p.process(atts)

View file

@ -134,11 +134,6 @@ class IWikiPage(Interface):
""" Modify the source text of the page before parsing it and return it.
"""
def process(tree, request=None):
""" Scan the tree, changing it if necessary and collecting
interesting information about the nodes, e.g. about links.
"""
def postprocess(result):
""" Modify the output of the write process and return it.
"""
@ -147,7 +142,7 @@ class IWikiPage(Interface):
# wiki plugins
class IParser(Interface):
""" Converts from (plain text) input format to internal format.
""" Converts from (plain text) input format to internal tree format.
"""
def parse(text):
@ -163,20 +158,6 @@ class IWriter(Interface):
"""
class ITreeProcessor(Interface):
""" Processes a document tree.
"""
context = Attribute('The wiki page from which the tree was generated.')
tree = Attribute('The tree to be processed.')
request = Attribute('An optional request object, needed e.g. for '
'rendering absolute URLs.')
def process():
""" Do what is necessary.
"""
class INodeProcessor(Interface):
""" Processes a document tree.
"""

View file

@ -16,7 +16,6 @@ from zope.traversing.browser.interfaces import IAbsoluteURL
from cybertools.relation.tests import IntIdsStub
from cybertools.wiki.base.config import WikiConfiguration
from cybertools.wiki.base.process import TreeProcessor
from cybertools.wiki.base.link import LinkManager
from cybertools.wiki.dcu.html import Writer as DocutilsHTMLWriter
from cybertools.wiki.dcu.rstx import Parser as DocutilsRstxParser
@ -57,7 +56,6 @@ def setUp(testCase):
component.provideUtility(WikiConfiguration())
component.provideUtility(DocutilsHTMLWriter(), name='docutils.html')
component.provideUtility(DocutilsRstxParser(), name='docutils.rstx')
component.provideAdapter(TreeProcessor, name='standard')
component.provideAdapter(process.Reference, name='default')
component.provideUtility(LinkManager(), name='basic')