work in progress: wiki-based link rendering for text resources

git-svn-id: svn://svn.cy55.de/Zope3/src/loops/trunk@3247 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2009-02-22 15:24:18 +00:00
parent 204e66f791
commit c3a5462a6c
11 changed files with 221 additions and 17 deletions

View file

@ -536,9 +536,20 @@ view for rendering.)
>>> doc1.data = u'Test data\n\nAnother paragraph'
>>> view.renderTarget()
u'<pre>Test data\n\nAnother paragraph</pre>'
>>> doc1.contentType = 'text/restructured'
>>> doc1.data = u'Test data\n\nAnother `paragraph <para>`_'
>>> from loops.wiki.base import wikiLinksActive
>>> view.renderTarget()
u'<p>Test data</p>\n<p>Another paragraph</p>\n'
u'<p>Test data</p>\n<p>Another <a class="reference" href="para">paragraph</a></p>\n'
u'<p>Test data</p>\n<p>Another <a class="reference create"
href="http://127.0.0.1/loops/wiki/create.html?linkid=0000001">?paragraph</a></p>\n'
>>> #links = loopsRoot.getRecordManager()['links']
>>> #links['0000001']
<Link ['42', 1, '', '... ...', u'para', None]: {}>
If the target object is removed from its container all references
to it are removed as well. (To make this work we have to handle

View file

@ -55,7 +55,10 @@ from loops.media.interfaces import IMediaAsset
from loops.organize.stateful.browser import statefulActions
from loops.versioning.browser import version_macros
from loops.versioning.interfaces import IVersionable
from loops import util
from loops.util import _
from loops.wiki.base import wikiLinksActive
from loops.wiki.base import LoopsWikiManager, LoopsWiki, LoopsWikiPage
resource_macros = ViewPageTemplateFile('resource_macros.pt')
@ -149,7 +152,6 @@ class ResourceView(BaseView):
return component.queryMultiAdapter((context, self.request),
name=viewName)
ct = context.contentType
#if ct.startswith('text/') and ct != 'text/rtf':
ti = IType(context).typeInterface
if (not ti or issubclass(ti, ITextDocument)
or (ct.startswith('text/') and ct != 'text/rtf')):
@ -166,10 +168,8 @@ class ResourceView(BaseView):
data = context.data
response = self.request.response
ct = context.contentType
#if useAttachment or (not ct.startswith('image/') and ct != 'application/pdf'):
if useAttachment:
filename = adapted(self.context).localFilename or getName(self.context)
#filename = urllib.quote(filename)
filename = NameChooser(getParent(self.context)).normalizeName(filename)
response.setHeader('Content-Disposition',
'attachment; filename=%s' % filename)
@ -180,6 +180,21 @@ class ResourceView(BaseView):
response.setHeader('Content-Type', ct)
return data
def renderText(self, text, contentType):
if contentType == 'text/restructured' and wikiLinksActive(self.loopsRoot):
# TODO: make this more flexible/configurable
wm = LoopsWikiManager(self.loopsRoot)
wiki = LoopsWiki('loops')
wiki.__parent__ = self.loopsRoot
wiki.__name__ = 'wiki'
wm.addWiki(wiki)
#wp = wiki.createPage(getName(self.context))
wp = wiki.addPage(LoopsWikiPage(self.context))
wp.text = text
#print wp.wiki.getManager()
#return util.toUnicode(wp.render(self.request))
return super(ResourceView, self).renderText(text, contentType)
def download(self):
""" Force download, e.g. of a PDF file """
return self.show(True)

View file

@ -478,6 +478,7 @@
<include package=".security" />
<include package=".system" />
<include package=".versioning" />
<include package=".wiki" />
<include package=".xmlrpc" />
</configure>

View file

@ -3,8 +3,7 @@
<configure
xmlns:zope="http://namespaces.zope.org/zope"
xmlns:browser="http://namespaces.zope.org/browser"
i18n_domain="zope"
>
i18n_domain="loops">
<!-- named templates -->

View file

@ -144,8 +144,6 @@ class Resource(Image, Contained):
self.resourceType = value
def _setData(self, data):
#if not data:
# return
dataFile = StringIO(data) # let File tear it into pieces
super(Resource, self)._setData(dataFile)
if not self.contentType:
@ -224,18 +222,12 @@ class Resource(Image, Contained):
def getSize(self):
if self._size:
return self._size
size = getattr(adapted(self), 'size', None)
adobj = adapted(self)
size = getattr(adobj, 'size', None)
if size is None:
return len(adapted(self).data)
return len(adobj.data)
return size
tp = IType(self, None)
if tp is not None:
ti = tp.typeInterface
if ti is not None:
return len(ti(self).data)
return len(self.data)
def sizeForSorting(self):
return 'byte', self.getSize()

View file

@ -44,6 +44,10 @@ from cybertools.relation.registry import invalidateRelations, removeRelation
from cybertools.stateful.interfaces import IStatefulIndexInfo
from cybertools.text.html import HtmlTransform
from cybertools.typology.interfaces import IType
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
from cybertools.wiki.tracking.link import LinkManager
from loops.base import Loops
from loops import util
@ -72,6 +76,8 @@ from loops.security.setter import BaseSecuritySetter
from loops.setup import SetupManager, addObject
from loops.type import LoopsType, ConceptType, ResourceType, TypeConcept
from loops.view import Node, NodeAdapter
from loops.wiki.link import LoopsLinkProcessor
from loops.wiki.setup import SetupManager as WikiSetupManager
class ClientIdManager(object):
@ -174,6 +180,13 @@ class TestSite(object):
component.provideAdapter(StatefulResourceIndexInfo)
component.provideHandler(handleTransition)
component.provideAdapter(WikiSetupManager, name='wiki')
component.provideUtility(WikiConfiguration())
component.provideAdapter(LinkManager)
component.provideUtility(DocutilsHTMLWriter(), name='docutils.html')
component.provideUtility(DocutilsRstxParser(), name='docutils.rstx')
component.provideAdapter(LoopsLinkProcessor, name='loops')
loopsRoot = self.site['loops'] = Loops()
setup = SetupManager(loopsRoot)
concepts, resources, views = setup.setup()

4
wiki/__init__.py Normal file
View file

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

74
wiki/base.py Normal file
View file

@ -0,0 +1,74 @@
#
# 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
#
"""
Wiki base implementation for loops.
$Id$
"""
from zope.traversing.api import getName
from cybertools.wiki.base.wiki import WikiManager, Wiki, WikiPage
from cybertools.wiki.interfaces import ILinkManager
from loops import util
def wikiLinksActive(loops):
records = loops.getRecordManager()
if records is not None:
links = records.get('links')
return links is not None
return False
class LoopsWikiManager(WikiManager):
""" An adapter...
"""
linkManager = 'tracking'
nodeProcessors = dict(reference=['loops'])
def __init__(self, context):
super(LoopsWikiManager, self).__init__()
self.context = context
def getPlugin(self, type, name):
if type == ILinkManager and name == 'tracking':
return ILinkManager(self.context.getRecordManager()['links'])
return super(LoopsWikiManager, self).getPlugin(type, name)
class LoopsWiki(Wiki):
def getPage(self, name):
if name.startswith('.target'):
return self.getManager().getObject(int(name[7:]))
class LoopsWikiPage(WikiPage):
""" An adapter...
"""
def __init__(self, context):
self.context = context
self.name = getName(context)
def getUid(self):
return util.getUidForObject(self.context)

24
wiki/configure.zcml Normal file
View file

@ -0,0 +1,24 @@
<!-- $Id$ -->
<configure
xmlns:zope="http://namespaces.zope.org/zope"
xmlns:browser="http://namespaces.zope.org/browser"
i18n_domain="loops">
<zope:utility factory="cybertools.wiki.base.config.WikiConfiguration" />
<zope:utility factory="cybertools.wiki.dcu.html.Writer"
name="docutils.html" />
<zope:utility factory="cybertools.wiki.dcu.rstx.Parser"
name="docutils.rstx" />
<zope:adapter factory="cybertools.wiki.tracking.link.LinkManager" />
<zope:adapter factory="loops.wiki.link.LoopsLinkProcessor"
name="loops" />
<zope:adapter factory="loops.wiki.setup.SetupManager"
name="wiki" />
</configure>

36
wiki/link.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
#
"""
Link manager implementation for loops.
$Id$
"""
from cybertools.wiki.dcu.process import Reference
class LoopsLinkProcessor(Reference):
def getTargetUri(self, obj):
ann = self.request.annotations.get('loops.view', {})
nodeView = self.viewAnnotations.get('nodeView')
if nodeView is not None:
return nodeView.getUrlForTarget(obj)
return super(LoopsLinkProcessor, self).getTargetUri(obj)

35
wiki/setup.py Normal file
View file

@ -0,0 +1,35 @@
#
# 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
#
"""
Automatic setup of a loops site for the wiki package.
$Id$
"""
from cybertools.tracking.btree import TrackingStorage
from cybertools.wiki.tracking.link import Link
from loops.setup import SetupManager as BaseSetupManager
class SetupManager(BaseSetupManager):
def setup(self):
records = self.context.getRecordManager()
links = self.addObject(records, TrackingStorage, 'links',
trackFactory=Link)