Edgewall Software

Changes between Version 13 and Version 14 of WorkInProgress/PluggableDirectivesLibraries


Ignore:
Timestamp:
Jun 17, 2010, 11:46:59 PM (14 years ago)
Author:
Carsten Klein <carsten.klein@…>
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • WorkInProgress/PluggableDirectivesLibraries

    v13 v14  
    4949{{{
    5050#!python
     51try:
     52    from setuptools import setup, Feature
     53    from setuptools.command.bdist_egg import bdist_egg
     54except ImportError:
     55    from distutils.core import setup
     56    Feature = None
     57    bdist_egg = None
    5158
     59setup(
     60    name = 'GenshiSampleDirectivesLibrary',
     61    version = '1.0.0proto_proto',
     62    description = 'A sample genshi directives library',
     63    long_description = \
     64"""""",
     65    author = 'axn-software.de, Carsten Klein',
     66    author_email = 'info@axn-software.de',
     67    license = 'BSD',
     68    url = 'http://genshi.edgewall.org/wiki/WorkInProgress/PluggableDirectivesLibraries',
     69    #download_url = 'http://genshi.edgewall.org/wiki/Download',
     70
     71    classifiers = [
     72        'Development Status :: 2 - Prototype',
     73        'Environment :: Web Environment',
     74        'Intended Audience :: Developers',
     75        'License :: OSI Approved :: BSD License',
     76        'Operating System :: OS Independent',
     77        'Programming Language :: Python',
     78        'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
     79        'Topic :: Software Development :: Libraries :: Python Modules',
     80        'Topic :: Text Processing :: Markup :: HTML',
     81        'Topic :: Text Processing :: Markup :: XML'
     82    ],
     83    keywords = ['python.templating.engines'],
     84    packages = ['sample'],
     85    #test_suite = 'genshi.tests.suite',
     86
     87    entry_points = """
     88    [genshi.libraries.directives]
     89    genshi-sample = sample.directives_library:SampleDirectivesLibrary
     90    """
     91)
    5292}}}
    5393
     
    5898#!python
    5999
     100from genshi.core import Markup, TEXT
     101from genshi.template.base import TemplateSyntaxError, _apply_directives
     102from genshi.template.default_directives_library import WhenDirective
     103from genshi.template.directives import Directive, DirectivesLibrary
     104
     105
     106NAMESPACE = "http://genshi.edgewall.org/directives/sample/"
     107
     108
     109class SampleDirectivesLibrary(DirectivesLibrary):
     110
     111    def __init__(self):
     112        DirectivesLibrary.__init__(self,
     113                                   [CommentDirective,
     114                                    #ElementDirective
     115                                    #CDATADirective
     116                                   ],
     117                                   default_namespace = NAMESPACE)
     118
     119
     120# taken from ticket #321 comment directive patch
     121class CommentDirective(Directive):
     122        """Implementation of the ``sample:comment`` template directive.
     123         
     124        This directive replaces the content of the element with the result of
     125        evaluating the value of the ``sample:comment`` attribute:
     126         
     127        >>> from genshi.template import MarkupTemplate
     128        >>> tmpl = MarkupTemplate('''<div xmlns:sample="http://genshi.edgewall.org/directives/sample">
     129        ...   <sample:comment>${2 + 2}</sample:comment>
     130        ... </div>''')
     131        >>> print tmpl.generate(bar='Bye')
     132        <div>
     133          <!--4-->
     134        </div>
     135        """
     136        __slots__ = []
     137
     138        @classmethod
     139        def attach(cls, template, stream, value, namespaces, pos):
     140            if type(value) is not dict:
     141                raise TemplateSyntaxError('The comment directive can only be used '
     142                                          'as an element', template.filepath,
     143                                          *pos[1:])
     144            return super(CommentDirective, cls).attach(template, stream,
     145                                                       value, namespaces, pos)
     146
     147        def __call__(self, stream, directives, ctxt, **vars):
     148            stream = list(stream)
     149           
     150            stream.insert(0, (TEXT, Markup("<!--"), (None, 1, 0)))
     151            stream.append((TEXT, Markup("-->"), (None, 1, 0)))
     152           
     153            return _apply_directives(stream, directives, ctxt, vars)
     154
     155        @classmethod
     156        def get_execute_before(cls):
     157            return WhenDirective
    60158}}}