[Zope3-checkins] CVS: Zope3/src/zope/app/renderer - __init__.py:1.1 configure.zcml:1.1 meta.zcml:1.1 metaconfigure.py:1.1 plaintext.py:1.1 rest.py:1.1 sourcetype.py:1.1 stx.py:1.1

Stephan Richter srichter@cosmos.phy.tufts.edu
Thu, 31 Jul 2003 13:59:45 -0400


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

Added Files:
	__init__.py configure.zcml meta.zcml metaconfigure.py 
	plaintext.py rest.py sourcetype.py stx.py 
Log Message:
First checkin of the ZWiki Renderer package into the core. The renderer
package can convert some sort of code to a particular presentation. Examples
include ReST --> HTML or Plain Text to HTML. However, it could be also used
to convert Python code --> Pretty HTML Python code and so on. 

To Do:

- Make a vocabulary, so that it is easier to list all the available source 
  types in an HTML form.

- Update Wiki code. (This actually already done...)



=== Added File Zope3/src/zope/app/renderer/__init__.py ===


=== Added File Zope3/src/zope/app/renderer/configure.zcml ===
<zopeConfigure
   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" />


  <!-- Plain Text Support -->

  <renderer:sourcetype
      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.IBrowserPresentation" 
      factory=".plaintext.PlainTextToHTMLRenderer" />


  <!-- STX support -->

  <!--renderer:sourcetype 
      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.IBrowserPresentation" 
      factory=".stx.StructuredTextToHTMLRenderer" /-->


  <!-- 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.IBrowserPresentation" 
      factory=".rest.ReStructuredTextToHTMLRenderer" />


</zopeConfigure>


=== Added File Zope3/src/zope/app/renderer/meta.zcml ===
<zopeConfigure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:meta="http://namespaces.zope.org/meta">

  <meta:directive
     namespace="http://namespaces.zope.org/renderer"
     name="sourcetype"
     schema=".metaconfigure.ISourceTypeDirective"
     handler=".metaconfigure.sourcetype" />

  <meta:directive
     namespace="http://namespaces.zope.org/renderer"
     name="renderer"
     schema=".metaconfigure.IRendererDirective"
     handler=".metaconfigure.renderer" />

</zopeConfigure>

=== Added File Zope3/src/zope/app/renderer/metaconfigure.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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 configuration code

$Id: metaconfigure.py,v 1.1 2003/07/31 17:59:36 srichter Exp $
"""
from zope.app.component.metaconfigure import handler
from zope.app.renderer.sourcetype import SourceTypes 
from zope.configuration.fields import GlobalObject
from zope.interface import Interface, implements, classProvides
from zope.schema import TextLine

class ISourceTypeDirective(Interface):
    """The renderers directive specifies how a particular source text can
    be rendered for various view types. It also generates a registry
    of available source types."""

    interface = GlobalObject(
        title=u"Interface",
        description=u"Specifies an interface for of a particular source type.",
        required=True)

    class_ = GlobalObject(
        title=u"Class",
        description=u"Specifies the class that is implementing this " \
                     "source type.",
        required=True)

    title = TextLine(
        title=u"Title",
        description=u"Provides a title for the source type.",
        required=False)


class IRendererDirective(Interface):
    """Register a renderer for a paricular output interface, such as
    IBrowserView."""

    sourceType = GlobalObject(
        title=u"Source Type Interface",
        description=u"Specifies an interface for of a particular source type.",
        required=True)

    for_ = GlobalObject(
        title=u"Interface of the output type",
        description=u"Specifies the interface of the output type (i.e. "
                    u"browser) for which this view is being registered.",
        required=True)

    factory = GlobalObject(
        title=u"Factory",
        description=u"Specifies the factory that is used to create the "
                    u"view on the source.",
        required=True)


def renderer(_context, sourceType, for_, factory):
    if for_ == '*':
        for_ = None
    _context.action(
        discriminator = ('view', sourceType, None, for_, 'default'),
        callable = handler,
        args = ('Views', 'provideView',
                sourceType, None, for_, factory, 'default')
        )


def sourcetype(_context, interface, class_, title=u''):
    _context.action(
        discriminator = ('source type', title, interface),
        callable = SourceTypes.provide,
        args = (title, interface, class_)
        )


=== Added File Zope3/src/zope/app/renderer/plaintext.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.
#
##############################################################################
"""Plain Text Renderer Classes

$Id: plaintext.py,v 1.1 2003/07/31 17:59:36 srichter Exp $
"""
from zope.interface import implements
from zope.app.interfaces.renderer import IPlainTextSource, IHTMLRenderer
from zope.publisher.browser import BrowserView


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

    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


comment_template = '''

Comment #%i by %s (%s)
%s'''

first_comment_template = '''
----------

Comment #%i by %s (%s)
%s'''


=== Added File Zope3/src/zope/app/renderer/rest.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.
#
##############################################################################
"""ReStructured Text Renderer Classes

$Id: rest.py,v 1.1 2003/07/31 17:59:36 srichter Exp $
"""
import re
import docutils.core, docutils.io
from datetime import datetime

from zope.interface import implements
from zope.publisher.browser import BrowserView
from zope.app.interfaces.renderer import IReStructuredTextSource, IHTMLRenderer


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)


class ReStructuredTextToHTMLRenderer(BrowserView):
    """An Adapter to convert from Restructured Text to HTML.""" 
    implements(IHTMLRenderer)
    __used_for__ = IReStructuredTextSource

    def render(self, context):
        "See zope.app.interfaces.renderer.IHTMLRenderer"
        # format with strings
        pub = docutils.core.Publisher()
        pub.set_reader('standalone', None, 'restructuredtext')
        pub.set_writer('html')

        # go with the defaults
        pub.get_settings()

        # this is needed, but doesn't seem to do anything
        pub.settings._destination = ''

        # use the Zope 3 stylesheet
        pub.settings.stylesheet = 'zope3.css'

        # set the reporting level to something sane (1 being the smallest)
        pub.settings.report_level = 1

        # don't break if we get errors
        pub.settings.halt_level = 6

        # input
        pub.source = docutils.io.StringInput(source=self.context)

        # output - not that it's needed
        pub.destination = docutils.io.StringOutput(encoding='UTF-8')

        # parse!
        document = pub.reader.read(pub.source, pub.parser, pub.settings)

        # transform
        pub.apply_transforms(document)

        # do the format
        html = pub.writer.write(document, pub.destination)
        html = re.sub(
            r'(?sm)^<\?xml.*<html.*<body.*?>\n(.*)</body>\n</html>\n',r'\1',
            html)
        return html
    

comment_template = '''

Comment #%i by **%s** (%s)

%s'''

first_comment_template = '''
----------

Comment #%i by **%s** (%s)

%s'''


=== Added File Zope3/src/zope/app/renderer/sourcetype.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.
#
##############################################################################
"""Source Types Service

Allows us to register source types.

$Id: sourcetype.py,v 1.1 2003/07/31 17:59:36 srichter Exp $
"""
from zope.interface import implements
from zope.app.interfaces.renderer import IGlobalSourceTypeService

class GlobalSourceTypeService:
    __doc__ = IGlobalSourceTypeService.__doc__

    implements(IGlobalSourceTypeService)

    def __init__(self):
        self.__types = {}

    def provide(self, title, iface, klass):
        "See zope.app.interfaces.renderer.IGlobalSourceTypeService"
        self.__types[title] = (iface, klass)

    def get(self, title, default=None):
        "See zope.app.interfaces.renderer.IGlobalSourceTypeService"
        res = self.__types.get(title, default)
        if res is not default:
            res = res[0]
        return res

    def query(self, title):
        "See zope.app.interfaces.renderer.IGlobalSourceTypeService"
        return self.__types[title][0]

    def getAllTitles(self):
        "See zope.app.interfaces.renderer.IGlobalSourceTypeService"
        return self.__types.keys()

    def createObject(self, title, source):
        "See zope.app.interfaces.renderer.IGlobalSourceTypeService"
        klass = self.__types[title][1]
        return klass(source)


SourceTypes = GlobalSourceTypeService()


=== Added File Zope3/src/zope/app/renderer/stx.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.
#
##############################################################################
"""Structured Text Renderer Classes

$Id: stx.py,v 1.1 2003/07/31 17:59:36 srichter Exp $
"""
import re

from datetime import datetime
from zope.interface import implements
from zope.publisher.browser import BrowserView
from zope.app.interfaces.renderer import IStructuredTextSource, IHTMLRenderer

from StructuredText import HTML


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.""" 
    implements(IHTMLRenderer)
    __used_for__ = IStructuredTextSource

    def render(self, context):
        "See zope.app.interfaces.renderer.IHTMLRenderer"
        html = HTML(str(self.context))

        # strip html & body added by some zope versions
        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'''