Exemple #1
0
def render_kajiki(template=None, info=None, format=None, fragment=False, mapping=None):
    global kajiki_loader
    if kajiki_loader is None:
        # Lazy imports of Kajiki
        from kajiki import PackageLoader
        kajiki_loader = PackageLoader()
            
    Template = kajiki_loader.import_(template)
       
    context = Bunch()
    context.update(stdvars())
    context.update(info)

    templ = Template(context)
    return templ.render()
Exemple #2
0
def render_kajiki(template=None,
                  info=None,
                  format=None,
                  fragment=False,
                  mapping=None):
    global kajiki_loader
    if kajiki_loader is None:
        # Lazy imports of Kajiki
        from kajiki import PackageLoader
        kajiki_loader = PackageLoader()

    Template = kajiki_loader.import_(template)

    context = Bunch()
    context.update(stdvars())
    context.update(info)

    templ = Template(context)
    return templ.render()
Exemple #3
0
def render_genshi(template=None,
                  info=None,
                  format=None,
                  fragment=False,
                  mapping=None):
    global genshi, genshi_loader
    if genshi is None:
        # Lazy imports of Genshi
        import genshi
        import genshi.template
        import genshi.output
        import genshi.input
        import genshi.filters

        def genshi_loader_callback(template):
            """This function will be called by genshi TemplateLoader after
            loading the template"""
            translator = genshi.filters.Translator(gettext)
            # Genshi 0.6 supports translation directives. Lets use them if available.
            if hasattr(translator, "setup"):
                translator.setup(template)
            else:
                template.filters.insert(0, translator)

        if config.get("i18n.run_template_filter", False):
            callback = genshi_loader_callback
        else:
            callback = None

        auto_reload = config.get("genshi.auto_reload", "1")
        if isinstance(auto_reload, basestring):
            auto_reload = auto_reload.lower() in ('1', 'on', 'yes', 'true')

        max_cache_size = config.get("genshi.max_cache_size", 25)

        genshi_loader = genshi.template.TemplateLoader(
            [""],
            auto_reload=auto_reload,
            callback=genshi_loader_callback,
            max_cache_size=max_cache_size,
        )

    # Choose Genshi template engine
    if format == "text":
        cls = genshi.template.NewTextTemplate
        default_extension = "txt"
    else:
        cls = None  # Default is Markup
        default_extension = "html"

    if "/" in template:
        # Path notation. Use first path part as module name
        module, pathname = template.split("/", 1)
        template = resource_filename(module, pathname)
    elif "." in template:
        # Dotted notation
        module, filename = template.rsplit(".", 1)
        filename = '%s.%s' % (filename, default_extension)
        template = resource_filename(module, filename)
    else:
        template = '%s.%s' % (template, default_extension)

    encoding = config.get("genshi.encoding", "utf-8")
    templ = genshi_loader.load(template, encoding=encoding, cls=cls)

    if format == 'html' and not fragment:
        mapping.setdefault(
            'doctype', config.get('genshi.default_doctype',
                                  'html-transitional'))

    serializer = genshi.output.get_serializer(format, **mapping)

    extras = {
        'XML': genshi.input.XML,
        'HTML': genshi.input.HTML,
        'ET': genshi.input.ET,
        '_': lazy_gettext
    }

    context = genshi.template.Context(**extras)
    context.push(stdvars())
    context.push(info)

    stream = templ.generate(context)
    if config.get('genshi.html_form_filler', False):
        stream = stream | genshi.filters.HTMLFormFiller(data=info)

    encode = genshi.output.encode
    return encode(serializer(stream), method=serializer, encoding=encoding)
Exemple #4
0
def render_genshi(template=None, info=None, format=None, fragment=False, mapping=None):
    global genshi, genshi_loader
    if genshi is None:
        # Lazy imports of Genshi
        import genshi
        import genshi.template
        import genshi.output
        import genshi.input
        import genshi.filters
    
        def genshi_loader_callback(template):
            """This function will be called by genshi TemplateLoader after
            loading the template"""
            translator = genshi.filters.Translator(gettext)
            # Genshi 0.6 supports translation directives. Lets use them if available.
            if hasattr(translator, "setup"):
                translator.setup(template)
            else:
                template.filters.insert(0, translator)
        
        if config.get("i18n.run_template_filter", False):
            callback = genshi_loader_callback
        else:
            callback = None
        
        auto_reload = config.get("genshi.auto_reload", "1")
        if isinstance(auto_reload, basestring):
            auto_reload = auto_reload.lower() in ('1', 'on', 'yes', 'true')

        max_cache_size = config.get("genshi.max_cache_size", 25)

        genshi_loader = genshi.template.TemplateLoader([""],
                auto_reload=auto_reload,
                callback=genshi_loader_callback,
                max_cache_size=max_cache_size,
        )

    # Choose Genshi template engine
    if format == "text":
        cls = genshi.template.NewTextTemplate
        default_extension = "txt"
    else:
        cls = None # Default is Markup
        default_extension = "html"
        
    if "/" in template:
        # Path notation. Use first path part as module name
        module, pathname = template.split("/", 1)
        template = resource_filename(module, pathname)
    elif "." in template:
        # Dotted notation
        module, filename = template.rsplit(".", 1)
        filename = '%s.%s' % (filename, default_extension)
        template = resource_filename(module, filename)
    else:
        template = '%s.%s' % (template, default_extension)
    
    encoding = config.get("genshi.encoding", "utf-8")
    templ = genshi_loader.load(template, encoding=encoding, cls=cls)

    if format == 'html' and not fragment:
        mapping.setdefault('doctype', config.get('genshi.default_doctype',
                                                 'html-transitional'))
                                                 
    serializer = genshi.output.get_serializer(format, **mapping)
    
    extras = { 
        'XML' : genshi.input.XML, 
        'HTML' : genshi.input.HTML,
        'ET' : genshi.input.ET,
        '_' : lazy_gettext
    }
    
    context = genshi.template.Context(**extras)
    context.push(stdvars())
    context.push(info)
    
    stream = templ.generate(context)
    if config.get('genshi.html_form_filler', False):
        stream = stream | genshi.filters.HTMLFormFiller(data=info)
    
    encode = genshi.output.encode
    return encode(serializer(stream), method=serializer, encoding=encoding)