diff --git a/wiki/README.txt b/wiki/README.txt index a204dc7..ff1f528 100644 --- a/wiki/README.txt +++ b/wiki/README.txt @@ -5,31 +5,24 @@ Yet Another WikiWiki Framework ($Id$) -Links and Link Management -========================= +A Very Basic Wiki Format +======================== - >>> from cybertools.wiki.base import link - - >>> manager = link.LinkManager() - - >>> input = ('This is text with a [[wikilink Wiki Link]] and a ' - ... '`RestructuredText 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 +things during testing we just use a bare object. + >>> from cybertools.wiki.base.format import BasicFormat + >>> format = BasicFormat() >>> page = object() - >>> format = link.DoubleBracketLinkFormat(page) - >>> format.manager = manager + >>> instance = format.getInstance(page) - >>> format.unmarshall(input) - 'This is text with a [[##0000001##]] and a `RestructuredText Link `__' +Now we enter some simple text and request the format instance to +unmarshall it, i.e. to convert it from the editable to the internal +representation. - >>> link = manager.links['0000001'] - - >>> link.original - '[[wikilink Wiki Link]]' - - >>> link.target - 'wikilink' - - >>> link.label - 'Wiki Link' + >>> input = ('This is text with a [[Wiki Link]].\n\n' + ... 'It also contains a second line.') + >>> instance.unmarshall(input) + 'This is text with a [[${l0000001}]].\n\nIt also contains a second line.' diff --git a/wiki/base/format.py b/wiki/base/format.py new file mode 100644 index 0000000..7333096 --- /dev/null +++ b/wiki/base/format.py @@ -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 diff --git a/wiki/base/link.py b/wiki/base/link.py index 3641326..1a9657b 100644 --- a/wiki/base/link.py +++ b/wiki/base/link.py @@ -25,7 +25,7 @@ $Id$ import re from zope.interface import implements -from cybertools.wiki.interfaces import ILink, ILinkFormat, ILinkManager +from cybertools.wiki.interfaces import ILink class Link(object): @@ -40,66 +40,3 @@ class Link(object): self.source = source 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 - diff --git a/wiki/base/manager.py b/wiki/base/manager.py new file mode 100644 index 0000000..00d5bd0 --- /dev/null +++ b/wiki/base/manager.py @@ -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 diff --git a/wiki/interfaces.py b/wiki/interfaces.py index 3a3c0b5..3e5f36e 100644 --- a/wiki/interfaces.py +++ b/wiki/interfaces.py @@ -25,17 +25,15 @@ $Id$ from zope.interface import Interface, Attribute -# links - -class ILinkManager(Interface): - """Manages (and possibly contains) links of all kinds. +class IWikiManager(Interface): + """Manages (and possibly contains) all kinds of wiki-related objects. """ - def register(link): + def registerLink(link): """Register (store) a link. """ - def unregister(link): + def unregisterLink(link): """Remove a link. """ @@ -52,11 +50,14 @@ class ILink(Interface): target = Attribute('Link target.') -class ILinkFormat(Interface): +class IFormat(Interface): """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): """Analyse the text given extracting all links and registering them