def present(self, presenters, wrapper=None, sep=None): if not is_seq_not_string(presenters): presenters = [presenters] # is there a better way to do this with itertools? maybe but it seems like not for the sep part output = [] for p in presenters: if hasattr(p, 'render_html'): rendered = p.render_html() else: rendered = p if wrapper: rendered = wrapper(rendered) if is_seq_not_string(rendered) and not isinstance(rendered, HtmlElement): output.extend(rendered) else: output.append(rendered) if sep: output.append(sep) if sep: output = output[:-1] return output
def __init__(self, data=None): super(BridgeJSON, self).__init__() if not data: return for k, v in data.iteritems(): if isinstance(v, collections.Mapping): self[k] = BridgeJSON(v) elif v and is_seq_not_string(v) and isinstance(v[0], collections.Mapping): self[k] = [BridgeJSON(i) for i in v] else: self[k] = v
def render_etree(tree): # HtmlElement is a seq, but we want to treat seqs of HtmlElements differently than HtmlElements # XXX verify that this is a list of HtmlElements? we'll see after we audit redner_etree_to_string if tree is not None and is_seq_not_string(tree) and not isinstance(tree, HtmlElement): return jinja2.Markup(''.join(map(render_etree_to_string, tree))) elif tree is not None and not isinstance(tree, jinja2.runtime.Undefined): # etrees evaluate to false, so we check is not None, but jinja2.runtime.Undefined could get passed in here return jinja2.Markup(lxml.html.tostring(tree)) elif isinstance(tree, basestring): return tree else: return ''
def render_template_string(request, ctx, template, minify_html=None, extra_ctx=None): with graphite_timer('jinja2.render_template_string'): if minify_html is None: minify_html = template.endswith('.html') if extra_ctx: if not is_seq_not_string(extra_ctx): extra_ctx = [extra_ctx] new_ctx = {} page_load_hooks = ctx.get('__js_page_load_hooks', []) for ctx_callable in extra_ctx: ctx_item = ctx_callable(request) if ctx_item: if '__js_page_load_hooks' in ctx_item: page_load_hooks.extend( ctx_item['__js_page_load_hooks']) del ctx_item['__js_page_load_hooks'] new_ctx.update(ctx_item) new_ctx.update(ctx) new_ctx['__js_page_load_hooks'] = page_load_hooks ctx = new_ctx with graphite_timer('jinja2.render_to_string'): output = render_to_string(template, ctx, context_instance=RequestContext(request)) if isinstance(output, unicode): output = output.encode('utf-8') if ctx and ctx.get('__no_minify'): minify_html = False if minify_html: with graphite_timer('jinja2.minify_html'): output = slimmer.html_slimmer(output) return output
def render_template_string(request, ctx, template, minify_html=None, extra_ctx=None): with graphite_timer('jinja2.render_template_string'): if minify_html is None: minify_html = template.endswith('.html') if extra_ctx: if not is_seq_not_string(extra_ctx): extra_ctx = [extra_ctx] new_ctx = {} page_load_hooks = ctx.get('__js_page_load_hooks', []) for ctx_callable in extra_ctx: ctx_item = ctx_callable(request) if ctx_item: if '__js_page_load_hooks' in ctx_item: page_load_hooks.extend(ctx_item['__js_page_load_hooks']) del ctx_item['__js_page_load_hooks'] new_ctx.update(ctx_item) new_ctx.update(ctx) new_ctx['__js_page_load_hooks'] = page_load_hooks ctx = new_ctx with graphite_timer('jinja2.render_to_string'): output = render_to_string(template, ctx, context_instance=RequestContext(request)) if isinstance(output, unicode): output = output.encode('utf-8') if ctx and ctx.get('__no_minify'): minify_html = False if minify_html: with graphite_timer('jinja2.minify_html'): output = slimmer.html_slimmer(output) return output
def extract_classes(attrs): class_ = attrs.pop('class_', ()) if not is_seq_not_string(class_): class_ = (class_,) return (class_, attrs)