def test_literal(self): lit = literal("This string <>") other = literal("<other>") assert "This string <><other>" == lit + other assert type(lit + other) is literal assert ""<other>" == '"' + other assert "<other>"" == other + '"' mod = literal("<%s>ello") assert "<<H>>ello" == mod % "<H>" assert type(mod % "<H>") is literal assert HTML("<a>") == "<a>" assert type(HTML("<a>")) is literal
def link_to_unless(condition, label, url='', **attrs): """The opposite of ``link_to``. Return just the label if the condition is true. """ if not condition: return link_to(label, url, **attrs) else: return HTML(label)
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 link_to_if(condition, label, url='', **attrs): """Same as ``link_to`` but return just the label if the condition is false. This is useful in a menu when you don't want the current option to be a link. The condition will be something like: ``actual_value != value_of_this_menu_item``. """ if condition: return link_to(label, url, **attrs) else: return HTML(label)
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 _render(self, options, selected_values): tags = [] for opt in options: if isinstance(opt, OptGroup): content = self._render(opt, selected_values) tag = HTML.tag("optgroup", NL, content, label=opt.label) tags.append(tag) else: value = opt.value if opt.value is not None else opt.label selected = value in selected_values tag = HTML.tag("option", opt.label, value=opt.value, selected=selected) tags.append(tag) return HTML(*tags, nl=True)
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 text_to_html(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.tag("p", para) return literal("\n\n").join(paragraphs)
def test_one_arg_with_nl(self): a = HTML("A&B", nl=True) b = "A&B\n" self.check(a, b)
def test_raises_error_on_unknown_kwarg(self): with raises(TypeError): HTML("A", foo='bar')
def test_multi_args_with_nl_and_lit(self): a = HTML("A&B", "&C", nl=True, lit=True) b = "A&B\n&C\n" self.check(a, b)
def test_one_arg_with_nl_and_lit(self): a = HTML("A&B", nl=True, lit=True) b = "A&B\n" self.check(a, b)
def test_multi_args_with_lit(self): a = HTML("A&B", "&C", lit=True) b = "A&B&C" self.check(a, b)
def test_one_arg_with_lit(self): a = HTML("A&B", lit=True) b = "A&B" self.check(a, b)
def test_multi_args(self): a = HTML("A&B", "&C") b = "A&B&C" self.check(a, b)
def test_one_arg(self): a = HTML("A&B") b = "A&B" self.check(a, b)
def html_escape(s): return HTML(s)
def __html__(self): if not self.condition: return HTML(self.label) return HTML.tag("a", self.label, href=self.url, **self.attrs)
def test_multi_args_with_nl(self): a = HTML("A&B", "&C", nl=True) b = "A&B\n&C\n" self.check(a, b)