work in progress: wiki framework, starting with link formatting and processing
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2742 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
411c130300
commit
74b9efffc3
5 changed files with 160 additions and 94 deletions
|
@ -5,31 +5,24 @@ Yet Another WikiWiki Framework
|
||||||
($Id$)
|
($Id$)
|
||||||
|
|
||||||
|
|
||||||
Links and Link Management
|
A Very Basic Wiki Format
|
||||||
=========================
|
========================
|
||||||
|
|
||||||
>>> from cybertools.wiki.base import link
|
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
|
||||||
>>> manager = link.LinkManager()
|
things during testing we just use a bare object.
|
||||||
|
|
||||||
>>> input = ('This is text with a [[wikilink Wiki Link]] and a '
|
|
||||||
... '`RestructuredText Link <rstxlink>`__')
|
|
||||||
|
|
||||||
|
>>> from cybertools.wiki.base.format import BasicFormat
|
||||||
|
>>> format = BasicFormat()
|
||||||
>>> page = object()
|
>>> page = object()
|
||||||
>>> format = link.DoubleBracketLinkFormat(page)
|
>>> instance = format.getInstance(page)
|
||||||
>>> format.manager = manager
|
|
||||||
|
|
||||||
>>> format.unmarshall(input)
|
Now we enter some simple text and request the format instance to
|
||||||
'This is text with a [[##0000001##]] and a `RestructuredText Link <rstxlink>`__'
|
unmarshall it, i.e. to convert it from the editable to the internal
|
||||||
|
representation.
|
||||||
|
|
||||||
>>> link = manager.links['0000001']
|
>>> input = ('This is text with a [[Wiki Link]].\n\n'
|
||||||
|
... 'It also contains a second line.')
|
||||||
>>> link.original
|
|
||||||
'[[wikilink Wiki Link]]'
|
|
||||||
|
|
||||||
>>> link.target
|
|
||||||
'wikilink'
|
|
||||||
|
|
||||||
>>> link.label
|
|
||||||
'Wiki Link'
|
|
||||||
|
|
||||||
|
>>> instance.unmarshall(input)
|
||||||
|
'This is text with a [[${l0000001}]].\n\nIt also contains a second line.'
|
||||||
|
|
82
wiki/base/format.py
Normal file
82
wiki/base/format.py
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
#
|
||||||
|
# Copyright (c) 2008 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 Wiki formatting.
|
||||||
|
|
||||||
|
$Id$
|
||||||
|
"""
|
||||||
|
|
||||||
|
import re
|
||||||
|
from zope.interface import implements
|
||||||
|
|
||||||
|
from cybertools.wiki.base.link import Link
|
||||||
|
from cybertools.wiki.base.manager import SampleManager
|
||||||
|
from cybertools.wiki.interfaces import IFormat, IFormatInstance
|
||||||
|
|
||||||
|
|
||||||
|
class FormatInstance(object):
|
||||||
|
|
||||||
|
#implements(IFormatInstance)
|
||||||
|
|
||||||
|
parent = None
|
||||||
|
|
||||||
|
def __init__(self, context):
|
||||||
|
self.context = context
|
||||||
|
|
||||||
|
def unmarshall(self, text):
|
||||||
|
return self.parent.linkRegexp.sub(self.processLink, text)
|
||||||
|
|
||||||
|
def processLink(self, match):
|
||||||
|
ref, label = match.group(1).split(' ', 1)
|
||||||
|
link = Link(self.context, ref)
|
||||||
|
link.original = match.group(0)
|
||||||
|
link.label = label
|
||||||
|
self.parent.manager.registerLink(link)
|
||||||
|
return self.parent.linkFormat % (self.parent.internalFormat % link.identifier)
|
||||||
|
|
||||||
|
def marshall(self, text):
|
||||||
|
return text
|
||||||
|
|
||||||
|
def display(self, text):
|
||||||
|
return text
|
||||||
|
|
||||||
|
|
||||||
|
class BasicFormat(object):
|
||||||
|
|
||||||
|
#implements(IFormat)
|
||||||
|
|
||||||
|
name = 'basic'
|
||||||
|
instanceFactory = FormatInstance
|
||||||
|
|
||||||
|
internalRegexp = re.compile(r'!\\\$([_A-Z0-9a-z]+)|{(.+)}')
|
||||||
|
internalFormat = '${%s}'
|
||||||
|
linkRegexp = re.compile(r'\[\[(.+)\]\]')
|
||||||
|
linkFormat = '[[%s]]'
|
||||||
|
|
||||||
|
repository = None
|
||||||
|
|
||||||
|
def __init__(self, manager=None):
|
||||||
|
if manager is None:
|
||||||
|
manager = SampleManager()
|
||||||
|
self.manager = manager
|
||||||
|
|
||||||
|
def getInstance(self, context):
|
||||||
|
instance = self.instanceFactory(context)
|
||||||
|
instance.parent = self
|
||||||
|
return instance
|
|
@ -25,7 +25,7 @@ $Id$
|
||||||
import re
|
import re
|
||||||
from zope.interface import implements
|
from zope.interface import implements
|
||||||
|
|
||||||
from cybertools.wiki.interfaces import ILink, ILinkFormat, ILinkManager
|
from cybertools.wiki.interfaces import ILink
|
||||||
|
|
||||||
|
|
||||||
class Link(object):
|
class Link(object):
|
||||||
|
@ -40,66 +40,3 @@ class Link(object):
|
||||||
self.source = source
|
self.source = source
|
||||||
self.target = target
|
self.target = target
|
||||||
|
|
||||||
|
|
||||||
class BaseLinkFormat(object):
|
|
||||||
|
|
||||||
implements(ILinkFormat)
|
|
||||||
|
|
||||||
manager = None
|
|
||||||
|
|
||||||
name = 'base'
|
|
||||||
internalFormat = '##%s##'
|
|
||||||
internalRegexp = re.compile(r'##(.+)##(.+)##')
|
|
||||||
|
|
||||||
def __init__(self, context):
|
|
||||||
self.context = context
|
|
||||||
|
|
||||||
def unmarshall(self, text):
|
|
||||||
return self.externalRegexp.sub(self.processLink, text)
|
|
||||||
|
|
||||||
def marshall(self, text):
|
|
||||||
return text
|
|
||||||
|
|
||||||
def display(self, text):
|
|
||||||
return text
|
|
||||||
|
|
||||||
def processLink(self, match):
|
|
||||||
ref, label = match.group(1).split(' ', 1)
|
|
||||||
link = Link(self.context, ref)
|
|
||||||
link.original = match.group(0)
|
|
||||||
link.label = label
|
|
||||||
self.manager.register(link)
|
|
||||||
return self.externalFormat % (self.internalFormat % link.identifier)
|
|
||||||
|
|
||||||
|
|
||||||
class DoubleBracketLinkFormat(BaseLinkFormat):
|
|
||||||
|
|
||||||
name = 'doublebracket'
|
|
||||||
externalFormat = '[[%s]]'
|
|
||||||
externalRegexp = re.compile(r'\[\[(.+)\]\]')
|
|
||||||
|
|
||||||
|
|
||||||
class LinkManager(object):
|
|
||||||
|
|
||||||
implements(ILinkManager)
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self.links = {}
|
|
||||||
|
|
||||||
def register(self, link):
|
|
||||||
if link.identifier is None:
|
|
||||||
self.generateIdentifier(link)
|
|
||||||
if link.identifier not in self.links:
|
|
||||||
self.links[link.identifier] = link
|
|
||||||
link.manager = self
|
|
||||||
|
|
||||||
def unregister(self, link):
|
|
||||||
if link.identifier in self.links:
|
|
||||||
del self.links[link.identifier]
|
|
||||||
link.manager = None
|
|
||||||
|
|
||||||
def generateIdentifier(self, link):
|
|
||||||
identifier = '%07i' % (max(self.links.keys() or [0]) + 1)
|
|
||||||
link.identifier = identifier
|
|
||||||
return identifier
|
|
||||||
|
|
||||||
|
|
53
wiki/base/manager.py
Normal file
53
wiki/base/manager.py
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
#
|
||||||
|
# Copyright (c) 2008 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, i.e. a repository for pages and other information like
|
||||||
|
links, images, etc.
|
||||||
|
|
||||||
|
$Id$
|
||||||
|
"""
|
||||||
|
|
||||||
|
from zope.interface import implements
|
||||||
|
|
||||||
|
from cybertools.wiki.interfaces import IWikiManager
|
||||||
|
|
||||||
|
|
||||||
|
class SampleManager(object):
|
||||||
|
""" A very basic Wiki manager implementation.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.links = {}
|
||||||
|
|
||||||
|
def registerLink(self, link):
|
||||||
|
if link.identifier is None:
|
||||||
|
self.generateLinkIdentifier(link)
|
||||||
|
if link.identifier not in self.links:
|
||||||
|
self.links[link.identifier] = link
|
||||||
|
link.manager = self
|
||||||
|
|
||||||
|
def unregisterLink(self, link):
|
||||||
|
if link.identifier in self.links:
|
||||||
|
del self.links[link.identifier]
|
||||||
|
link.manager = None
|
||||||
|
|
||||||
|
def generateLinkIdentifier(self, link):
|
||||||
|
identifier = 'l%07i' % (max(self.links.keys() or [0]) + 1)
|
||||||
|
link.identifier = identifier
|
||||||
|
return identifier
|
|
@ -25,17 +25,15 @@ $Id$
|
||||||
from zope.interface import Interface, Attribute
|
from zope.interface import Interface, Attribute
|
||||||
|
|
||||||
|
|
||||||
# links
|
class IWikiManager(Interface):
|
||||||
|
"""Manages (and possibly contains) all kinds of wiki-related objects.
|
||||||
class ILinkManager(Interface):
|
|
||||||
"""Manages (and possibly contains) links of all kinds.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def register(link):
|
def registerLink(link):
|
||||||
"""Register (store) a link.
|
"""Register (store) a link.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def unregister(link):
|
def unregisterLink(link):
|
||||||
"""Remove a link.
|
"""Remove a link.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -52,11 +50,14 @@ class ILink(Interface):
|
||||||
target = Attribute('Link target.')
|
target = Attribute('Link target.')
|
||||||
|
|
||||||
|
|
||||||
class ILinkFormat(Interface):
|
class IFormat(Interface):
|
||||||
"""Identifies links in texts and transforms text correspondingly.
|
"""Identifies links in texts and transforms text correspondingly.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
manager = Attribute('The link manager this link format is associated with.')
|
manager = Attribute('The Wiki manager this format is associated with.')
|
||||||
|
|
||||||
|
|
||||||
|
class IFormatInstance(Interface):
|
||||||
|
|
||||||
def unmarshall(text):
|
def unmarshall(text):
|
||||||
"""Analyse the text given extracting all links and registering them
|
"""Analyse the text given extracting all links and registering them
|
||||||
|
|
Loading…
Add table
Reference in a new issue