Пример #1
0
 def test_literal(self):
     lit = literal("This string <>")
     other = literal("<other>")
     assert "This string <><other>" == lit + other
     assert type(lit + other) is literal
     
     assert "&#34;<other>" == '"' + other
     assert "<other>&#34;" == other + '"'
     
     mod = literal("<%s>ello")
     assert "<&lt;H&gt;>ello" == mod % "<H>"
     assert type(mod % "<H>") is literal
     assert HTML("<a>") == "&lt;a&gt;"
     assert type(HTML("<a>")) is literal
Пример #2
0
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)
Пример #3
0
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
Пример #4
0
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)
Пример #5
0
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
Пример #6
0
 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)
Пример #7
0
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
Пример #8
0
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)
Пример #9
0
 def test_one_arg_with_nl(self):
     a = HTML("A&B", nl=True)
     b = "A&amp;B\n"
     self.check(a, b)
Пример #10
0
 def test_raises_error_on_unknown_kwarg(self):
     with raises(TypeError):
         HTML("A", foo='bar')
Пример #11
0
 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)
Пример #12
0
 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)
Пример #13
0
 def test_multi_args_with_lit(self):
     a = HTML("A&B", "&C", lit=True)
     b = "A&B&C"
     self.check(a, b)
Пример #14
0
 def test_one_arg_with_lit(self):
     a = HTML("A&B", lit=True)
     b = "A&B"
     self.check(a, b)
Пример #15
0
 def test_multi_args(self):
     a = HTML("A&B", "&C")
     b = "A&amp;B&amp;C"
     self.check(a, b)
Пример #16
0
 def test_one_arg(self):
     a = HTML("A&B")
     b = "A&amp;B"
     self.check(a, b)
Пример #17
0
def html_escape(s):
    return HTML(s)
Пример #18
0
 def __html__(self):
     if not self.condition:
         return HTML(self.label)
     return HTML.tag("a", self.label, href=self.url, **self.attrs)
Пример #19
0
 def test_multi_args_with_nl(self):
     a = HTML("A&B", "&C", nl=True)
     b = "A&amp;B\n&amp;C\n"
     self.check(a, b)