示例#1
0
    def _render_json(self, path, stylesname, templatename, jsondata, extras,
                     searcher):
        # Render the page template
        kwargs = {
            "path": path,
            "basepath": paths.basepath(path),
            "is_index_page": self.is_index_page(path),
            "rel": util.make_rel_fn(path, self.index_page_name),
            "searcher": searcher,
        }
        if extras:
            kwargs.update(extras)

        # Create a function to render JSON to HTML
        stylesname = stylesname or self.page_style
        styleobj = self.style(stylesname)
        stylectx, render = styleobj.context_and_function(path, jsondata, kwargs)

        # Create a function to apply the stylesheet to a given object
        def render_styles(obj):
            return render(stylectx, obj)

        # Get the page template
        template = self._template_for_page(templatename, jsondata)

        html = template.render(docroot=jsondata, render_styles=render_styles,
                               **kwargs)
        return html
示例#2
0
文件: styles.py 项目: mchaput/bookish
    def context_and_function(self, basepath, jsondata, extras=None):
        """
        Returns a Jinja context function you can use to transform a JSON
        document using the rules contained in this style's template.
        """

        template = self.template()

        # Create the render function
        @jinja2.contextfunction
        def render(jinctx, obj):
            if isinstance(obj, dict):
                # Look for a rule named <type>_rule, then <role>_rule
                rule = None
                for prefix in (obj.get("type"), obj.get("role")):
                    if not prefix:
                        continue

                    macroname = "%s_rule" % prefix
                    if macroname in jinctx.exported_vars:
                        rule = jinctx.vars[macroname]
                        break

                if rule is None:
                    if "default" in jinctx.exported_vars:
                        rule = jinctx.vars["default"]
                    else:
                        return self.default_rule(jinctx, obj, render)

                return rule(obj)

            elif isinstance(obj, (list, tuple)) or inspect.isgenerator(obj):
                return functions.string(render(jinctx, o) for o in obj)
            else:
                return escape(functions.string(obj))

        kwargs = {
            "rel": util.make_rel_fn(basepath, self.index_page_name),
            "render": render,
            "docroot": jsondata,
        }
        if extras:
            kwargs.update(extras)

        # Create a new Jinja context
        jinjactx = template.new_context(vars=kwargs)
        # Run the style template to evaluate the rule definitions
        list(template.root_render_func(jinjactx))

        return jinjactx, render
示例#3
0
def tag_page(tag):
    pages = get_wikipages()
    indexer = get_indexer()

    pagepath = "/tags/" + tag
    spath = pages.source_path(pagepath)
    if pages.exists(spath):
        return show(pagepath)
    else:
        searcher = indexer.searcher()
        docs = searcher.documents(tags=tag)
        tagcloud = searcher.tag_cloud()
        rel = util.make_rel_fn(pagepath, pages.index_page_name)
        return flask.render_template("/templates/tag.jinja2", rel=rel, tag=tag,
                                     docs=docs, tagcloud=tagcloud)