[Zope3-checkins] CVS: Zope3/src/zope/app/renderer - interfaces.py:1.1 __init__.py:1.3 configure.zcml:1.7 plaintext.py:1.2 rest.py:1.6 stx.py:1.4 vocabulary.py:1.3 sourcetype.py:NONE

Stephan Richter srichter at cosmos.phy.tufts.edu
Tue Mar 2 09:25:16 EST 2004


Update of /cvs-repository/Zope3/src/zope/app/renderer
In directory cvs.zope.org:/tmp/cvs-serv21732/src/zope/app/renderer

Modified Files:
	__init__.py configure.zcml plaintext.py rest.py stx.py 
	vocabulary.py 
Added Files:
	interfaces.py 
Removed Files:
	sourcetype.py 
Log Message:


Reimplemented source types to use factories and the factory service instead
implementing its own service. The vocabulary was also updated accordingly.




=== Added File Zope3/src/zope/app/renderer/interfaces.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Renderer Interface Declarations

The source renderer takes a special type of string, an ISource, and is able to
produce 

$Id: interfaces.py,v 1.1 2004/03/02 14:24:45 srichter Exp $
"""
from zope.interface import Interface

class ISource(Interface):
    """Simple base interface for all possible Wiki Page Source types."""

class ISourceRenderer(Interface):
    """Object implementing this interface are responsible for rendering an
    ISource objects to an output format.

    This is the base class for all possible output types."""

    def __init__(self, source):
        """Initialize the renderer.

        The source argument is the source code that needs to be converted.
        """

    def render():
        """Renders the source into another format."""

class IHTMLRenderer(ISourceRenderer):
    """Renders an ISource object to HTML."""


=== Zope3/src/zope/app/renderer/__init__.py 1.2 => 1.3 ===
--- Zope3/src/zope/app/renderer/__init__.py:1.2	Wed Feb  4 08:09:12 2004
+++ Zope3/src/zope/app/renderer/__init__.py	Tue Mar  2 09:24:45 2004
@@ -1 +1,41 @@
-# make this directory a package
+##############################################################################
+#
+# Copyright (c) 2003 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Plain Text Renderer Classes
+
+$Id$
+"""
+from zope.component.interfaces import IFactory
+from zope.interface import implements, directlyProvides, Declaration
+from zope.publisher.browser import BrowserView
+from zope.app.interfaces.renderer import ISource, IHTMLRenderer
+
+
+class Source(unicode):
+    __provides__ = None
+
+
+class SourceFactory(object):
+    """Creates an ISource object."""
+    implements(IFactory)
+
+    def __init__(self, iface):
+        self._iface = iface
+
+    def getInterfaces(self):
+        return Declaration(self._iface).flattened()
+
+    def __call__(self, ustr):
+        source = Source(ustr)
+        directlyProvides(source, self._iface)
+        return source


=== Zope3/src/zope/app/renderer/configure.zcml 1.6 => 1.7 ===
--- Zope3/src/zope/app/renderer/configure.zcml:1.6	Thu Feb 19 14:59:38 2004
+++ Zope3/src/zope/app/renderer/configure.zcml	Tue Mar  2 09:24:45 2004
@@ -1,22 +1,12 @@
 <configure
    xmlns="http://namespaces.zope.org/zope"
-   xmlns:renderer="http://namespaces.zope.org/renderer">
-
-  <serviceType 
-      id="SourceTypeRegistry"
-      interface="zope.app.interfaces.renderer.ISourceTypeService" />
-
-  <service 
-      serviceType="SourceTypeRegistry"
-      permission="zope.View"
-      component=".sourcetype.SourceTypes" />
-
+   xmlns:browser="http://namespaces.zope.org/browser">
 
   <!-- Source Type Vocabulary Setup -->
 
   <vocabulary
-     name="SourceTypes"
-     factory=".vocabulary.SourceTypeVocabulary" />
+      name="SourceTypes"
+      factory=".vocabulary.SourceTypeVocabulary" />
 
   <content class=".vocabulary.SourceTypeVocabulary">
     <allow interface="zope.schema.interfaces.IVocabulary"/>
@@ -31,41 +21,47 @@
 
   <!-- Plain Text Support -->
 
-  <renderer:sourcetype
+  <factory
+      component=".plaintext.PlainTextSourceFactory"
+      id="zope.source.plaintext"
       title="Plain Text" 
-      interface="zope.app.interfaces.renderer.IPlainTextSource"
-      class=".plaintext.PlainTextSource" />
-
-  <renderer:renderer 
-      sourceType="zope.app.interfaces.renderer.IPlainTextSource"
-      for="zope.publisher.interfaces.browser.IBrowserRequest" 
-      factory=".plaintext.PlainTextToHTMLRenderer" />
+      description="Plain Text Source" 
+      permission="zope.Public"/>
 
+  <browser:view
+      name=""
+      for=".plaintext.IPlainTextSource"
+      class=".plaintext.PlainTextToHTMLRenderer"
+      permission="zope.Public" />
 
   <!-- STX support -->
 
-  <renderer:sourcetype 
+  <factory
+      component=".stx.StructuredTextSourceFactory"
+      id="zope.source.stx"
       title="Structured Text (STX)"
-      interface="zope.app.interfaces.renderer.IStructuredTextSource"
-      class=".stx.StructuredTextSource" />
-
-  <renderer:renderer 
-      sourceType="zope.app.interfaces.renderer.IStructuredTextSource"
-      for="zope.publisher.interfaces.browser.IBrowserRequest" 
-      factory=".stx.StructuredTextToHTMLRenderer" />
+      description="Structured Text (STX) Source" 
+      permission="zope.Public" />
 
+  <browser:view
+      name=""
+      for=".stx.IStructuredTextSource"
+      class=".stx.StructuredTextToHTMLRenderer"
+      permission="zope.Public" />
 
   <!-- ReST support -->
 
-  <renderer:sourcetype 
-      title="reStructured Text (reST)" 
-      interface="zope.app.interfaces.renderer.IReStructuredTextSource"
-      class=".rest.ReStructuredTextSource" />
-
-  <renderer:renderer 
-      sourceType="zope.app.interfaces.renderer.IReStructuredTextSource"
-      for="zope.publisher.interfaces.browser.IBrowserRequest" 
-      factory=".rest.ReStructuredTextToHTMLRenderer" />
-
+  <factory
+      component=".rest.ReStructuredTextSourceFactory"
+      id="zope.source.rest"
+      title="ReStructured Text (ReST)"
+      description="ReStructured Text (ReST) Source" 
+      permission="zope.Public" />
+
+  <browser:view
+      name=""
+      for=".rest.IReStructuredTextSource"
+      class=".rest.ReStructuredTextToHTMLRenderer"
+      permission="zope.Public" />
 
 </configure>


=== Zope3/src/zope/app/renderer/plaintext.py 1.1 => 1.2 ===
--- Zope3/src/zope/app/renderer/plaintext.py:1.1	Thu Jul 31 13:59:36 2003
+++ Zope3/src/zope/app/renderer/plaintext.py	Tue Mar  2 09:24:45 2004
@@ -16,42 +16,34 @@
 $Id$
 """
 from zope.interface import implements
-from zope.app.interfaces.renderer import IPlainTextSource, IHTMLRenderer
 from zope.publisher.browser import BrowserView
 
+from zope.app.renderer.interfaces import ISource, IHTMLRenderer
+from zope.app.renderer import SourceFactory
 
-class PlainTextSource(unicode):
-    """Represents Plain Text source code""" 
-    implements(IPlainTextSource)
-
-    def createComment(self, comment, number, user, date):
-        "See zope.app.interfaces.renderer.IPlainTextSource"
-        if number == 1:
-            return first_comment_template %(number, user, date, comment)
-        else:
-            return comment_template %(number, user, date, comment)    
-    
 
-class PlainTextToHTMLRenderer(BrowserView):
-    """An Adapter to convert from Plain Text to HTML.""" 
-    implements(IHTMLRenderer)
-    __used_for__ = IPlainTextSource
+class IPlainTextSource(ISource):
+    """Marker interface for a plain text source. Note that an implementation
+    of this interface should always derive from unicode or behave like a
+    unicode class."""
 
-    def render(self, context):
-        "See zope.app.interfaces.renderer.IHTMLRenderer"
-        html = self.context.replace('\n', '<br/>\n')
-        html = html.replace('----------<br/>',
-                            '<hr class="comments" size="1" NOSHADE>')
-        return html
+PlainTextSourceFactory = SourceFactory(IPlainTextSource)
 
 
-comment_template = '''
+class PlainTextToHTMLRenderer(BrowserView):
+    r"""A view to convert from Plain Text to HTML.
 
-Comment #%i by %s (%s)
-%s'''
+    Example::
 
-first_comment_template = '''
-----------
+      >>> from zope.publisher.browser import TestRequest
+      >>> source = PlainTextSourceFactory(u'This is source.\n')
+      >>> renderer = PlainTextToHTMLRenderer(source, TestRequest())
+      >>> renderer.render()
+      u'This is source.<br />\n'
+    """ 
+    implements(IHTMLRenderer)
+    __used_for__ = IPlainTextSource
 
-Comment #%i by %s (%s)
-%s'''
+    def render(self):
+        "See zope.app.interfaces.renderer.IHTMLRenderer"
+        return self.context.replace('\n', '<br />\n')


=== Zope3/src/zope/app/renderer/rest.py 1.5 => 1.6 ===
--- Zope3/src/zope/app/renderer/rest.py:1.5	Mon Nov  3 23:04:26 2003
+++ Zope3/src/zope/app/renderer/rest.py	Tue Mar  2 09:24:45 2004
@@ -22,7 +22,17 @@
 
 from zope.interface import implements
 from zope.publisher.browser import BrowserView
-from zope.app.interfaces.renderer import IReStructuredTextSource, IHTMLRenderer
+from zope.app.renderer.interfaces import ISource, IHTMLRenderer
+from zope.app.renderer import SourceFactory
+
+
+class IReStructuredTextSource(ISource):
+    """Marker interface for a restructured text source. Note that an
+    implementation of this interface should always derive from unicode or
+    behave like a unicode class."""
+
+
+ReStructuredTextSourceFactory = SourceFactory(IReStructuredTextSource)
 
 
 class Writer(writers.Writer):
@@ -103,25 +113,35 @@
             self.section_level -= (self.settings.base_section - 1)
     
     
+class ReStructuredTextToHTMLRenderer(BrowserView):
+    r"""An Adapter to convert from Restructured Text to HTML.
 
-class ReStructuredTextSource(unicode):
-    """Represents Restructured Text source code""" 
-    implements(IReStructuredTextSource)
-
-    def createComment(self, comment, number, user, date):
-        "See zope.app.interfaces.renderer.IReStructuredTextSource"
-        if number == 1:
-            return first_comment_template %(number, user, date, comment)
-        else:
-            return comment_template %(number, user, date, comment)
+    Examples::
+
+      >>> from zope.publisher.browser import TestRequest
+      >>> source = ReStructuredTextSourceFactory(u'''
+      ... This is source.
+      ... 
+      ... Header 3
+      ... --------
+      ... This is more source.
+      ... ''')
+      >>> renderer = ReStructuredTextToHTMLRenderer(source, TestRequest())
+      >>> print renderer.render().strip()
+      <div class="document">
+      <p>This is source.</p>
+      <div class="section" id="header-3">
+      <h3><a name="header-3">Header 3</a></h3>
+      <p>This is more source.</p>
+      </div>
+      </div>
+    """ 
 
 
-class ReStructuredTextToHTMLRenderer(BrowserView):
-    """An Adapter to convert from Restructured Text to HTML.""" 
     implements(IHTMLRenderer)
     __used_for__ = IReStructuredTextSource
 
-    def render(self, context):
+    def render(self):
         "See zope.app.interfaces.renderer.IHTMLRenderer"
         settings_overrides = {
             'footnote_references': 'brackets',
@@ -135,17 +155,3 @@
             settings_overrides=settings_overrides,
             )
         return html
-
-
-comment_template = '''
-
-Comment #%i by **%s** (%s)
-
-%s'''
-
-first_comment_template = '''
-----------
-
-Comment #%i by **%s** (%s)
-
-%s'''


=== Zope3/src/zope/app/renderer/stx.py 1.3 => 1.4 ===
--- Zope3/src/zope/app/renderer/stx.py:1.3	Thu Feb 19 14:56:52 2004
+++ Zope3/src/zope/app/renderer/stx.py	Tue Mar  2 09:24:45 2004
@@ -17,30 +17,39 @@
 """
 import re
 
-from zope.structuredtext.document import Document
-from zope.structuredtext.html import HTML
 from zope.interface import implements
 from zope.publisher.browser import BrowserView
-from zope.app.interfaces.renderer import IStructuredTextSource, IHTMLRenderer
+from zope.structuredtext.document import Document
+from zope.structuredtext.html import HTML
+
+from zope.app.renderer.interfaces import ISource, IHTMLRenderer
+from zope.app.renderer import SourceFactory
+
+
+class IStructuredTextSource(ISource):
+    """Marker interface for a structured text source. Note that an
+    implementation of this interface should always derive from unicode or
+    behave like a unicode class."""
+
+StructuredTextSourceFactory = SourceFactory(IStructuredTextSource)
 
-class StructuredTextSource(unicode):
-    """Represents Structured Text source code""" 
-    implements(IStructuredTextSource)
-
-    def createComment(self, comment, number, user, date):
-        "See zope.app.interfaces.renderer.IStructuredTextSource"
-        if number == 1:
-            return first_comment_template %(number, user, date, comment)
-        else:
-            return comment_template %(number, user, date, comment)    
-    
 
 class StructuredTextToHTMLRenderer(BrowserView):
-    """An Adapter to convert from Plain Text to HTML.""" 
+    r"""A view to convert from Plain Text to HTML.
+
+    Example::
+
+      >>> from zope.publisher.browser import TestRequest
+      >>> source = StructuredTextSourceFactory(u'This is source.')
+      >>> renderer = StructuredTextToHTMLRenderer(source, TestRequest())
+      >>> renderer.render()
+      '<p>This is source.</p>\n'
+
+    """ 
     implements(IHTMLRenderer)
     __used_for__ = IStructuredTextSource
 
-    def render(self, context):
+    def render(self):
         "See zope.app.interfaces.renderer.IHTMLRenderer"
         doc = Document()(str(self.context))
         html = HTML()(doc)
@@ -49,21 +58,4 @@
         html = re.sub(
             r'(?sm)^<html.*<body.*?>\n(.*)</body>\n</html>\n',r'\1', html)
 
-        html = html.replace('<p>----------</p>',
-                            '<hr class="comments" size="1" NOSHADE>')
         return html
-
-
-comment_template = '''
-
-Comment #%i by **%s** (%s)
-
-%s'''
-
-first_comment_template = '''
-
-----------
-
-Comment #%i by **%s** (%s)
-
-%s'''


=== Zope3/src/zope/app/renderer/vocabulary.py 1.2 => 1.3 ===
--- Zope3/src/zope/app/renderer/vocabulary.py:1.2	Wed Oct 29 15:26:28 2003
+++ Zope3/src/zope/app/renderer/vocabulary.py	Tue Mar  2 09:24:45 2004
@@ -16,17 +16,23 @@
 $Id$
 """
 from zope.interface import implements
-from zope.component import getService
+from zope.proxy import removeAllProxies
 from zope.schema.interfaces import \
      ITokenizedTerm, IVocabulary, IVocabularyTokenized
 
+from zope.app import zapi
+from zope.app.browser.form.vocabularywidget import DropdownListWidget
+from zope.app.services.servicenames import Factories
+from zope.app.renderer.interfaces import ISource
+
 class SourceTypeTerm:
 
     implements(ITokenizedTerm)
 
-    def __init__(self, title):
-        self.value = self.token = title
-        self.title = title
+    def __init__(self, name, info):
+        self.token = self.value = name
+        self.title = info.title
+        self.description = info.description
 
 
 class SourceTypeVocabulary(object):
@@ -34,27 +40,40 @@
     implements(IVocabulary, IVocabularyTokenized)
 
     def __init__(self, context):
-        self.types = getService(context, 'SourceTypeRegistry')
-
+        factories = zapi.getService(context, Factories)
+        self.types = [(name, factories.getFactoryInfo(name)) \
+                      for name, fact in factories.queryFactoriesFor(ISource,
+                                                                    ())]
     def __contains__(self, value):
-        return value in self.types.getAllTitles()
+        return value in [name for name, info in self.types]
 
     def __iter__(self):
-        terms = map(lambda st: SourceTypeTerm(st), self.types.getAllTitles())
-        return iter(terms)
+        return iter([SourceTypeTerm(name, info) for name, info in self.types])
 
     def __len__(self):
-        return len(self.types.getAllTitles())
+        return len(self.types)
 
     def getQuery(self):
         return None
 
     def getTerm(self, value):
-        if value not in self:
-            raise KeyError, 'item (%s) not in vocabulary.' %value
-        return SourceTypeTerm(value)
+        for name, info in self.types:
+            if name == value:
+                return SourceTypeTerm(name, info)
+
+        raise KeyError, 'item (%s) not in vocabulary.' %value
 
     def getTermByToken(self, token):
-        if token not in self:
-            raise KeyError, 'item (%s) not in vocabulary.' %token
         return self.getTerm(token)
+
+
+class SourceTypeEditWidget(DropdownListWidget):
+
+    def __init__(self, field, request):
+        self.request = request
+        vocabs = zapi.getService(field, "Vocabularies")
+        self.vocabulary = vocabs.get(field, "SourceTypes")
+        self.setField(field)
+
+    def textForValue(self, term):
+        return term.title

=== Removed File Zope3/src/zope/app/renderer/sourcetype.py ===




More information about the Zope3-Checkins mailing list