Exemple #1
0
def compile_theme(rules_doc, theme_doc=None, css=True,
                  absolute_prefix=None, update=True, trace=False,
                  includemode=None, parser=None, compiler_parser=None):
    rules_doc = process_rules(
        rules_doc=rules_doc,
        theme=theme_doc,
        css=css,
        absolute_prefix=absolute_prefix,
        update=update,
        trace=trace,
        includemode=includemode,
    )

    # Build a document with all the <xsl:param /> values to set the defaults
    # for every value passed in as xsl_params
    known_params = compiler.build_xsl_params_document({})

    # Create a pseudo resolver for this
    known_params_url = 'file:///__diazo_known_params__'
    emit_stylesheet_resolver = utils.CustomResolver({
        known_params_url: etree.tostring(known_params)})
    emit_stylesheet_parser = etree.XMLParser()
    emit_stylesheet_parser.resolvers.add(emit_stylesheet_resolver)

    # Run the final stage compiler
    emit_stylesheet = utils.pkg_xsl(
        'emit-stylesheet.xsl', parser=emit_stylesheet_parser)
    compiled_doc = emit_stylesheet(rules_doc)
    compiled_doc = compiler.set_parser(
        etree.tostring(compiled_doc), parser, compiler_parser)

    return compiled_doc
Exemple #2
0
def compile_theme(rules_doc, theme_doc=None, css=True,
                  absolute_prefix=None, update=True, trace=False,
                  includemode=None, parser=None, compiler_parser=None):
    rules_doc = process_rules(
        rules_doc=rules_doc,
        theme=theme_doc,
        css=css,
        absolute_prefix=absolute_prefix,
        update=update,
        trace=trace,
        includemode=includemode,
    )

    # Build a document with all the <xsl:param /> values to set the defaults
    # for every value passed in as xsl_params
    known_params = compiler.build_xsl_params_document({})

    # Create a pseudo resolver for this
    known_params_url = 'file:///__diazo_known_params__'
    emit_stylesheet_resolver = utils.CustomResolver({
        known_params_url: etree.tostring(known_params)})
    emit_stylesheet_parser = etree.XMLParser()
    emit_stylesheet_parser.resolvers.add(emit_stylesheet_resolver)

    # Run the final stage compiler
    emit_stylesheet = utils.pkg_xsl(
        'emit-stylesheet.xsl', parser=emit_stylesheet_parser)
    compiled_doc = emit_stylesheet(rules_doc)
    compiled_doc = compiler.set_parser(
        etree.tostring(compiled_doc), parser, compiler_parser)

    return compiled_doc
Exemple #3
0
def set_parser(stylesheet, parser, compiler_parser=None):
    dummy_doc = etree.parse(open(pkg_resources.resource_filename('diazo', 'dummy.html')), parser=parser)
    name = 'file:///__diazo__'
    resolver = CustomResolver({name: stylesheet})
    if compiler_parser is None:
        compiler_parser = etree.XMLParser()
    compiler_parser.resolvers.add(resolver)
    identity = pkg_xsl('identity.xsl', compiler_parser)
    output_doc = identity(dummy_doc, docurl="'%s'"%name)
    compiler_parser.resolvers.remove(resolver)
    return output_doc
Exemple #4
0
def set_parser(stylesheet, parser, compiler_parser=None):
    dummy_doc = etree.parse(open(
        pkg_resources.resource_filename('diazo', 'dummy.html')), parser=parser)
    name = 'file:///__diazo__'
    resolver = CustomResolver({name: stylesheet})
    if compiler_parser is None:
        compiler_parser = etree.XMLParser()
    compiler_parser.resolvers.add(resolver)
    identity = pkg_xsl('identity.xsl', compiler_parser)
    output_doc = identity(dummy_doc, docurl="'%s'" % name)
    compiler_parser.resolvers.remove(resolver)
    return output_doc
Exemple #5
0
def set_parser(stylesheet, parser, compiler_parser=None):
    file_obj = pkg_resources.resource_filename('diazo', 'dummy.html')
    with open(file_obj) as file_handler:
        dummy_doc = etree.parse(
            file_handler,
            parser=parser,
        )
    name = 'file:///__diazo__'
    resolver = CustomResolver({name: stylesheet})
    if compiler_parser is None:
        compiler_parser = etree.XMLParser()
    compiler_parser.resolvers.add(resolver)
    identity = pkg_xsl('identity.xsl', compiler_parser)
    output_doc = identity(dummy_doc, docurl="'{name}'".format(name=name))
    compiler_parser.resolvers.remove(resolver)
    return output_doc
Exemple #6
0
def compile_theme(rules,
                  theme=None,
                  extra=None,
                  css=True,
                  xinclude=True,
                  absolute_prefix=None,
                  update=True,
                  trace=False,
                  includemode=None,
                  parser=None,
                  compiler_parser=None,
                  rules_parser=None,
                  access_control=None,
                  read_network=False,
                  indent=None,
                  xsl_params=None,
                  runtrace=False):
    """Invoke the diazo compiler.

    * ``rules`` is the rules file
    * ``theme`` is the theme file
    * ``extra`` is an optional XSLT file with Diazo extensions (depracated, use
      inline xsl in the rules instead)
    * ``css``   can be set to False to disable CSS syntax support (providing a
      moderate speed gain)
    * ``xinclude`` can be set to False to disable XInclude support during the
      compile phase (providing a moderate speed gain)
    * ``absolute_prefix`` can be set to a string that will be prefixed to any
      *relative* URL referenced in an image, link or stylesheet in the theme
      HTML file before the theme is passed to the compiler. This allows a
      theme to be written so that it can be opened and views standalone on the
      filesystem, even if at runtime its static resources are going to be
      served from some other location. For example, an
      ``<img src="images/foo.jpg" />`` can be turned into
      ``<img src="/static/images/foo.jpg" />`` with an ``absolute_prefix`` of
      "/static".
    * ``update`` can be set to False to disable the automatic update support
      for the old Deliverance 0.2 namespace (for a moderate speed gain)
    * ``trace`` can be set to True to enable compiler trace information
    * ``runtrace`` can be set to True to add tracing into the XSL output
    * ``includemode`` can be set to 'document', 'esi' or 'ssi' to change the
      way in which includes are processed
    * ``parser`` can be set to an lxml parser instance; the default is an
      HTMLParser
    * ``compiler_parser``` can be set to an lxml parser instance; the default
      is a XMLParser
    * ``rules_parser`` can be set to an lxml parser instance; the default is a
      XMLParser.
    * ``xsl_params`` can be set to a dictionary of parameters that will be
      known to the compiled theme transform. The keys should be the parameter
      names. Values are default values.
    """
    if access_control is not None:
        read_network = access_control.options['read_network']
    rules_doc = process_rules(
        rules=rules,
        theme=theme,
        extra=extra,
        css=css,
        xinclude=xinclude,
        absolute_prefix=absolute_prefix,
        update=update,
        trace=trace,
        includemode=includemode,
        parser=parser,
        rules_parser=rules_parser,
        read_network=read_network,
    )

    # Build a document with all the <xsl:param /> values to set the defaults
    # for every value passed in as xsl_params
    known_params = build_xsl_params_document(xsl_params)

    # Create a pseudo resolver for this
    known_params_url = 'file:///__diazo_known_params__'
    emit_stylesheet_resolver = CustomResolver(
        {known_params_url: etree.tostring(known_params)})
    emit_stylesheet_parser = etree.XMLParser()
    emit_stylesheet_parser.resolvers.add(emit_stylesheet_resolver)

    # Set up parameters
    params = {}
    if indent is not None:
        params['indent'] = indent and "'yes'" or "'no'"
    params['known_params_url'] = quote_param(known_params_url)
    params['runtrace'] = '1' if runtrace else '0'

    # Run the final stage compiler
    emit_stylesheet = pkg_xsl('emit-stylesheet.xsl',
                              parser=emit_stylesheet_parser)
    compiled_doc = emit_stylesheet(rules_doc, **params)
    compiled_doc = set_parser(etree.tostring(compiled_doc), parser,
                              compiler_parser)

    return compiled_doc
Exemple #7
0
#!/usr/bin/env python
import logging

from diazo.rules import process_rules
from diazo.utils import pkg_xsl

from lxml import etree

logger = logging.getLogger('diazo')

_runtrace_to_html = pkg_xsl('runtrace_to_html.xsl')


def log_to_xml_string(error_log):
    return """
<runtrace xmlns:css="http://namespaces.plone.org/diazo/css">%s</runtrace>
    """ % "".join(l.message for l in error_log
                  if l.message.startswith('<runtrace '))


def generate_runtrace(rules, error_log, rules_parser=None):
    """Annotate a rules file with the results of a transformation"""
    def condition_name(trace):
        """Generate attribute name for this entry"""
        for k in trace.attrib.keys():
            if(k == 'theme_xmlid'):
                continue
            if(k.startswith('{http://namespaces.plone.org/diazo/css}')):
                continue
            return "runtrace-" + k
Exemple #8
0
from lxml import etree
from urlparse import urljoin

from diazo.cssrules import convert_css_selectors
from diazo.utils import namespaces, fullname, AC_READ_NET, AC_READ_FILE, pkg_xsl, _createOptionParser

logger = logging.getLogger('diazo')

IMPORT_STYLESHEET = re.compile(
    r'''(?P<before>@import[ \t]+(?P<paren>url\([ \t]?)?(?P<quote>['"]?))(?P<url>\S+)(?P<after>(?P=quote)(?(paren)\)))''',
    re.IGNORECASE)
CONDITIONAL_SRC = re.compile(
    r'''(?P<before><[^>]*?(src|href)=(?P<quote>['"]?))(?P<url>[^ \t\n\r\f\v>]+)(?P<after>(?P=quote)[^>]*?>)''',
    re.IGNORECASE)

update_transform = pkg_xsl('update-namespace.xsl')
normalize_rules = pkg_xsl('normalize-rules.xsl')
apply_conditions = pkg_xsl('apply-conditions.xsl')
merge_conditions = pkg_xsl('merge-conditions.xsl')
annotate_themes = pkg_xsl('annotate-themes.xsl')
annotate_rules = pkg_xsl('annotate-rules.xsl')
apply_rules = pkg_xsl('apply-rules.xsl')
fixup_themes = pkg_xsl('fixup-themes.xsl')


def anchor_safe_urljoin(base, url):
    """Join the base with the url only when the url doesn't start with '#'"""
    if url.startswith('#'):
        return url
    else:
        return urljoin(base, url)
Exemple #9
0
def compile_theme(rules, theme=None, extra=None, css=True, xinclude=True,
                  absolute_prefix=None, update=True, trace=False,
                  includemode=None, parser=None, compiler_parser=None,
                  rules_parser=None, access_control=None, read_network=False,
                  indent=None, xsl_params=None, runtrace=False):
    """Invoke the diazo compiler.

    * ``rules`` is the rules file
    * ``theme`` is the theme file
    * ``extra`` is an optional XSLT file with Diazo extensions (depracated, use
      inline xsl in the rules instead)
    * ``css``   can be set to False to disable CSS syntax support (providing a
      moderate speed gain)
    * ``xinclude`` can be set to False to disable XInclude support during the
      compile phase (providing a moderate speed gain)
    * ``absolute_prefix`` can be set to a string that will be prefixed to any
      *relative* URL referenced in an image, link or stylesheet in the theme
      HTML file before the theme is passed to the compiler. This allows a
      theme to be written so that it can be opened and views standalone on the
      filesystem, even if at runtime its static resources are going to be
      served from some other location. For example, an
      ``<img src="images/foo.jpg" />`` can be turned into
      ``<img src="/static/images/foo.jpg" />`` with an ``absolute_prefix`` of
      "/static".
    * ``update`` can be set to False to disable the automatic update support
      for the old Deliverance 0.2 namespace (for a moderate speed gain)
    * ``trace`` can be set to True to enable compiler trace information
    * ``runtrace`` can be set to True to add tracing into the XSL output
    * ``includemode`` can be set to 'document', 'esi' or 'ssi' to change the
      way in which includes are processed
    * ``parser`` can be set to an lxml parser instance; the default is an
      HTMLParser
    * ``compiler_parser``` can be set to an lxml parser instance; the default
      is a XMLParser
    * ``rules_parser`` can be set to an lxml parser instance; the default is a
      XMLParser.
    * ``xsl_params`` can be set to a dictionary of parameters that will be
      known to the compiled theme transform. The keys should be the parameter
      names. Values are default values.
    """
    if access_control is not None:
        read_network = access_control.options['read_network']
    rules_doc = process_rules(
        rules=rules,
        theme=theme,
        extra=extra,
        css=css,
        xinclude=xinclude,
        absolute_prefix=absolute_prefix,
        update=update,
        trace=trace,
        includemode=includemode,
        parser=parser,
        rules_parser=rules_parser,
        read_network=read_network,
    )

    # Build a document with all the <xsl:param /> values to set the defaults
    # for every value passed in as xsl_params
    known_params = build_xsl_params_document(xsl_params)

    # Create a pseudo resolver for this
    known_params_url = 'file:///__diazo_known_params__'
    emit_stylesheet_resolver = CustomResolver({
        known_params_url: etree.tostring(known_params)})
    emit_stylesheet_parser = etree.XMLParser()
    emit_stylesheet_parser.resolvers.add(emit_stylesheet_resolver)

    # Set up parameters
    params = {}
    if indent is not None:
        params['indent'] = indent and "'yes'" or "'no'"
    params['known_params_url'] = quote_param(known_params_url)
    params['runtrace'] = '1' if runtrace else '0'

    # Run the final stage compiler
    emit_stylesheet = pkg_xsl(
        'emit-stylesheet.xsl', parser=emit_stylesheet_parser)
    compiled_doc = emit_stylesheet(rules_doc, **params)
    compiled_doc = set_parser(etree.tostring(compiled_doc), parser,
                              compiler_parser)

    return compiled_doc
Exemple #10
0
import re

from optparse import OptionParser
from lxml import etree
from urlparse import urljoin

from diazo.cssrules import convert_css_selectors
from diazo.utils import namespaces, fullname, AC_READ_NET, AC_READ_FILE, pkg_xsl, _createOptionParser

logger = logging.getLogger('diazo')

IMPORT_STYLESHEET = re.compile(r'''(?P<before>@import[ \t]+(?P<paren>url\([ \t]?)?(?P<quote>['"]?))(?P<url>\S+)(?P<after>(?P=quote)(?(paren)\)))''', re.IGNORECASE)
CONDITIONAL_SRC= re.compile(r'''(?P<before><[^>]*?(src|href)=(?P<quote>['"]?))(?P<url>[^ \t\n\r\f\v>]+)(?P<after>(?P=quote)[^>]*?>)''', re.IGNORECASE)


update_transform = pkg_xsl('update-namespace.xsl')
normalize_rules  = pkg_xsl('normalize-rules.xsl')
apply_conditions = pkg_xsl('apply-conditions.xsl')
merge_conditions = pkg_xsl('merge-conditions.xsl')
annotate_themes  = pkg_xsl('annotate-themes.xsl')
annotate_rules   = pkg_xsl('annotate-rules.xsl')
apply_rules      = pkg_xsl('apply-rules.xsl')
fixup_themes     = pkg_xsl('fixup-themes.xsl')

def anchor_safe_urljoin(base, url):
    """Join the base with the url only when the url doesn't start with '#'"""
    if url.startswith('#'):
        return url
    else:
        return urljoin(base, url)
Exemple #11
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from diazo.rules import process_rules
from diazo.utils import pkg_xsl
from lxml import etree

import logging

logger = logging.getLogger('diazo')

_runtrace_to_html = pkg_xsl('runtrace_to_html.xsl')


def log_to_xml_string(error_log):
    msgs = [l.message for l in error_log if l.message.startswith('<runtrace ')]
    return """
<runtrace xmlns:css="http://namespaces.plone.org/diazo/css">
    {message:s}
</runtrace>
""".format(message=''.join(msgs))


def generate_runtrace(rules, error_log, rules_parser=None):
    """Annotate a rules file with the results of a transformation"""
    def condition_name(trace):
        """Generate attribute name for this entry"""
        for k in trace.attrib.keys():
            if (k == 'theme_xmlid'):
                continue
            if (k.startswith('{http://namespaces.plone.org/diazo/css}')):
Exemple #12
0
from diazo.cssrules import convert_css_selectors
from diazo.utils import namespaces, fullname, AC_READ_NET, AC_READ_FILE, pkg_xsl, _createOptionParser

logger = logging.getLogger("diazo")

IMPORT_STYLESHEET = re.compile(
    r"""(?P<before>@import[ \t]+(?P<paren>url\([ \t]?)?(?P<quote>['"]?))(?P<url>\S+)(?P<after>(?P=quote)(?(paren)\)))""",
    re.IGNORECASE,
)
CONDITIONAL_SRC = re.compile(
    r"""(?P<before><[^>]*?(src|href)=(?P<quote>['"]?))(?P<url>[^ \t\n\r\f\v>]+)(?P<after>(?P=quote)[^>]*?>)""",
    re.IGNORECASE,
)


update_transform = pkg_xsl("update-namespace.xsl")
normalize_rules = pkg_xsl("normalize-rules.xsl")
apply_conditions = pkg_xsl("apply-conditions.xsl")
merge_conditions = pkg_xsl("merge-conditions.xsl")
annotate_themes = pkg_xsl("annotate-themes.xsl")
annotate_rules = pkg_xsl("annotate-rules.xsl")
apply_rules = pkg_xsl("apply-rules.xsl")
fixup_themes = pkg_xsl("fixup-themes.xsl")


def anchor_safe_urljoin(base, url):
    """Join the base with the url only when the url doesn't start with '#'"""
    if url.startswith("#"):
        return url
    else:
        return urljoin(base, url)