def test_lit_re(): lit = literal("This is a <string>") unlit = "This is also a <string>" result = lit_sub(r"<str", literal("<b"), lit) assert u"This is a <bing>" == escape(result) result = lit_sub(r"a <str", "a <b> <b", unlit) assert u"This is also a <b> <bing>" == escape(result)
def test_lit_re(): lit = literal('This is a <string>') unlit = 'This is also a <string>' result = lit_sub(r'<str', literal('<b'), lit) eq_(u'This is a <bing>', escape(result)) result = lit_sub(r'a <str', 'a <b> <b', unlit) eq_(u'This is also a <b> <bing>', escape(result))
def paginate(page, **attrs): p = page.pager('$link_previous ~2~ $link_next', symbol_first=u'First', symbol_previous=u'Previous', symbol_next=u'Next', symbol_last=u'Last', show_if_single_page=False, link_attr={'class': None}, curpage_attr={'class': 'active'}, dotdot_attr={'class': 'disabled'}, **attrs) p = lit_sub(u'<a', literal('<li><a'), p) p = lit_sub(patterns[0], patterns[1], p) return literal(u'<ul>') + p + literal(u'</ul>')
def paginate(page, **attrs): p = page.pager('$link_previous ~2~ $link_next', symbol_first=u'First', symbol_previous=u'Anterior', symbol_next=u'Próxima', symbol_last=u'Last', show_if_single_page=False, link_attr={'class':None}, curpage_attr={'class': 'active'}, dotdot_attr={'class': 'disabled'}, **attrs ) p = lit_sub(u'<a', literal('<li><a'), p) p = lit_sub(patterns[0], patterns[1], p) return literal(u'<ul>')+p+literal(u'</ul>')
def format_paragraphs(text): """Convert text to HTML paragraphs. ``text``: the text to convert. Split into paragraphs at blank lines (i.e., wherever one or more consecutive newlines appear), and wrap each paragraph in a <p>. """ if text is None: return literal("") text = lit_sub(_universal_newline_rx, "\n", text) # ensure all newlines are double text = lit_sub(newlines_re, "\n\n", text) return wh_format_paragraphs(text)
def _legacy_highlight(text, phrase, highlighter, flags): """WebHelpers 0.6 style highlight with deprecated ``highlighter arg.""" warnings.warn("the ``highlighter`` argument is deprecated", DeprecationWarning) pat = "(%s)" % re.escape(phrase) rx = re.compile(pat, flags) highlighter = literal(highlighter) return lit_sub(rx, highlighter, text)
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 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 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 "\n\n".join(paragraphs)
def _auto_link_email_addresses(text): return lit_sub(r'([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)', r'<a href="mailto:\1">\1</a>', text)
def _auto_link_email_addresses(text): return lit_sub(r'([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)', literal(r'<a href="mailto:\1">\1</a>'), text)
def highlight(text, phrase, highlighter=None, case_sensitive=False, class_="highlight", **attrs): """Highlight all occurrences of ``phrase`` in ``text``. This inserts "<strong class="highlight">...</strong>" around every occurrence. Arguments: ``text``: The full text. ``phrase``: A phrase to find in the text. This may be a string, a list of strings, or a compiled regular expression. If a string, it's regex-escaped and compiled. If a list, all of the strings will be highlighted. This is done by regex-escaping all elements and then joining them using the regex "|" token. ``highlighter``: Deprecated. A replacement expression for the regex substitution. This was deprecated because it bypasses the HTML builder and creates tags via string mangling. The previous default was '<strong class="highlight">\\1</strong>', which mimics the normal behavior of this function. ``phrase`` must be a string if ``highlighter`` is specified. Overrides ``class_`` and ``attrs_`` arguments. ``case_sensitive``: If false (default), the phrases are searched in a case-insensitive manner. No effect if ``phrase`` is a regex object. ``class_``: CSS class for the <strong> tag. ``**attrs``: Additional HTML attributes for the <strong> tag. Changed in WebHelpers 1.0b2: new implementation using HTML builder. Allow ``phrase`` to be list or regex. Deprecate ``highlighter`` and change its default value to None. Add ``case_sensitive``, ``class_``, and ``**attrs`` arguments. """ if not phrase or not text: return text text = escape(text) if case_sensitive: flags = 0 # No flags. else: flags = re.IGNORECASE if highlighter: return _legacy_highlight(text, phrase, highlighter, flags) if isinstance(phrase, basestring): pat = re.escape(phrase) rx = re.compile(pat, flags) elif isinstance(phrase, (list, tuple)): parts = [re.escape(x) for x in phrase] pat = "|".join(parts) rx = re.compile(pat, flags) else: rx = phrase def repl(m): return HTML.strong(m.group(), class_=class_, **attrs) return lit_sub(rx, repl, text)