{% highlight 'print "Hello World"' python linenos=true %} """ if highlighter is None: return '<pre>%s</pre>' % code return highlighter(code or '', get_lexer_by_name(lexer), HtmlFormatter(**kwargs)) highlight = function(highlight, is_safe=True) def highlight_block(context, nodelist, lexer, **kwargs): """ Code is nodelist ``rendered`` in ``context`` Returns highlighted code ``div`` tag from ``HtmlFormatter`` Lexer is guessed by ``lexer`` name arguments are passed into the formatter Syntax:: {% highlight_block [lexer name] [formatter options] %} ... source code .. {% endhighlight_block %} Example:: {% highlight_block python linenos=true %} print '{{ request.path }}' {% endhighlight_block %} """ if highlighter is None: return '<pre>%s</pre>' % str(nodelist.render(context) or '') return highlighter(nodelist.render(context) or '', get_lexer_by_name(lexer), HtmlFormatter(**kwargs)) highlight_block = block(highlight_block, is_safe=True)
CMDS = ('title', 'axes.type', 'axes.label', 'type', 'encoding', 'fill', 'color', 'scale', 'legend') def parse_cmd(value): value = value.lstrip() for cmd in CMDS: if value.startswith(cmd): return cmd, value[len(cmd):].strip() return None, None def gchart(context, nodelist, type, dataset, **kwargs): G = GChart(type, dataset, encoding=kwargs.pop('encoding', 'text')) for node in nodelist: if isinstance(node, TextNode): for part in node.render(context).splitlines(): cmd, value = parse_cmd(part) if cmd is None: continue if cmd.startswith('axes'): cmd = getattr(G.axes, cmd[5:]) else: cmd = getattr(G, cmd) cmd(*value.split()) if 'instance' in kwargs: return G return G.img(**kwargs) gchart = block(gchart)
return '' do_set = function(do_set, takes_context=1, name='set') def do_del(context, *args): 'Deletes template variables from the context' for name in args: del context[name] return '' do_del = function(do_del, resolve=0, takes_context=1, name='del') def render_block(context, nodelist): 'Simply renders the nodelist with the current context' return nodelist.render(context) render_block = block(render_block) def template_string(context, template): 'Return the rendered template content with the current context' if not isinstance(context, Context): context = Context(context) return Template(template).render(context) template_string = function(template_string, takes_context=1) template_string.test = {'args':({'var':'T'},'W {{ var }} F'),'result':'W T F'} def template_block(context, nodelist): 'Return the rendered block\'s content with the current context' return Template(nodelist.render(context)).render(context) template_block = block(template_block)
from GChartWrapper import GChart from native_tags.decorators import block CMDS = ('title','axes.type','axes.label','type','encoding','fill','color','scale','legend') def parse_cmd(value): value = value.lstrip() for cmd in CMDS: if value.startswith(cmd): return cmd,value[len(cmd):].strip() return None, None def gchart(context, nodelist, type, dataset, **kwargs): G = GChart(type, dataset, encoding=kwargs.pop('encoding','text')) for node in nodelist: if isinstance(node, TextNode): for part in node.render(context).splitlines(): cmd,value = parse_cmd(part) if cmd is None: continue if cmd.startswith('axes'): cmd = getattr(G.axes, cmd[5:]) else: cmd = getattr(G, cmd) cmd(*value.split()) if 'instance' in kwargs: return G return G.img(**kwargs) gchart = block(gchart)