def test_literal(): lit = literal(u'This string <>') other = literal(u'<other>') eq_(u'This string <><other>', lit + other) assert type(lit + other) is literal eq_(u'"<other>', '"' + other) eq_(u'<other>"', other + '"') mod = literal('<%s>ello') eq_(u'<<H>>ello', mod % '<H>') assert type(mod % '<H>') is literal eq_(HTML('<a>'), '<a>') assert type(HTML('<a>')) is literal
def nl2br(text): """Insert a <br /> before each newline. """ if text is None: return literal("") text = lit_sub(_universal_newline_rx, "\n", text) text = HTML(text).replace("\n", br) return text
def required_legend(): """Return an inline HTML snippet explaining which fields are required. See webhepers/public/stylesheets/webhelpers.css for suggested styles. >>> required_legend() literal(u'<span class="required required-symbol">*</span> = required') """ return HTML( HTML.span("*", class_="required required-symbol"), " = required", )
def button(context, permision, caption, **kwargs): html = '' if context.has_permision(permision): caption = HTML.tag('span', c=caption) icon = '' if 'icon' in kwargs: icon = HTML.tag('span', class_=kwargs.pop('icon')) button_class = "button _action " + kwargs.pop('class', '') button_class = button_class.strip() html = HTML.tag('a', class_=button_class, c=HTML(icon, caption), **kwargs) return html
def content_tag(name, content, **options): """ Create a tag with content Takes the same keyword args as ``tag`` Examples:: >>> print content_tag("p", "Hello world!") <p>Hello world!</p> >>> print content_tag("div", content_tag("p", "Hello world!"), class_="strong") <div class="strong"><p>Hello world!</p></div> """ if content is None: content = '' tag = HTML.tag(name, _closed=False, **options) + HTML(content) + literal( '</%s>' % name) return tag
def format_paragraphs(text, preserve_lines=False): """Convert text to HTML paragraphs. ``text``: the text to convert. Split into paragraphs at blank lines (i.e., wherever two or more consecutive newlines appear), and wrap each paragraph in a <p>. ``preserve_lines``: If true, add <br /> before each single line break """ if text is None: return literal("") text = lit_sub(_universal_newline_rx, "\n", text) paragraphs = _paragraph_rx.split(text) for i, para in enumerate(paragraphs): if preserve_lines: para = HTML(para) para = para.replace("\n", br) paragraphs[i] = HTML.p(para) return literal("\n\n").join(paragraphs)
def html_escape(s): return HTML(s)