Beispiel #1
0
def do_urlize(eval_ctx, value, trim_url_limit=None, nofollow=False,
              target=None, rel=None):
    """Converts URLs in plain text into clickable links.

    If you pass the filter an additional integer it will shorten the urls
    to that number. Also a third argument exists that makes the urls
    "nofollow":

    .. sourcecode:: jinja

        {{ mytext|urlize(40, true) }}
            links are shortened to 40 chars and defined with rel="nofollow"

    If *target* is specified, the ``target`` attribute will be added to the
    ``<a>`` tag:

    .. sourcecode:: jinja

       {{ mytext|urlize(40, target='_blank') }}

    .. versionchanged:: 2.8+
       The *target* parameter was added.
    """
    policies = eval_ctx.environment.policies
    rel = set((rel or '').split() or [])
    if nofollow:
        rel.add('nofollow')
    rel.update((policies['urlize.rel'] or '').split())
    if target is None:
        target = policies['urlize.target']
    rel = ' '.join(sorted(rel)) or None
    rv = urlize(value, trim_url_limit, rel=rel, target=target)
    if eval_ctx.autoescape:
        rv = Markup(rv)
    return rv
Beispiel #2
0
def do_lineprefix(s, prefix):
    """Return a copy of the string with each line prepended by the given string.
     :param prefix: String to prepend each line with
     .. versionadded:: 2.11
        Add block auto-indent feature
    """
    newline = u'\n'

    if isinstance(s, Markup):
        prefix = Markup(prefix)
        newline = Markup(newline)

    lines = s.splitlines()
    rv = newline.join(prefix + line if line else line for line in lines)

    return rv
Beispiel #3
0
def do_xmlattr(_eval_ctx, d, autospace=True):
    """Create an SGML/XML attribute string based on the items in a dict.
    All values that are neither `none` nor `undefined` are automatically
    escaped:

    .. sourcecode:: html+jinja

        <ul{{ {'class': 'my_list', 'missing': none,
                'id': 'list-%d'|format(variable)}|xmlattr }}>
        ...
        </ul>

    Results in something like this:

    .. sourcecode:: html

        <ul class="my_list" id="list-42">
        ...
        </ul>

    As you can see it automatically prepends a space in front of the item
    if the filter returned something unless the second parameter is false.
    """
    rv = u' '.join(
        u'%s="%s"' % (escape(key), escape(value))
        for key, value in iteritems(d)
        if value is not None and not isinstance(value, Undefined)
    )
    if autospace and rv:
        rv = u' ' + rv
    if _eval_ctx.autoescape:
        rv = Markup(rv)
    return rv
Beispiel #4
0
 def as_const(self, eval_ctx=None):
     eval_ctx = get_eval_context(self, eval_ctx)
     if eval_ctx.volatile:
         raise Impossible()
     if eval_ctx.autoescape:
         return Markup(self.data)
     return self.data
Beispiel #5
0
 def as_const(self, eval_ctx=None):
     eval_ctx = get_eval_context(self, eval_ctx)
     if eval_ctx.volatile:
         raise Impossible()
     expr = self.expr.as_const(eval_ctx)
     if eval_ctx.autoescape:
         return Markup(expr)
     return expr
Beispiel #6
0
def do_indent(
    s, width=4, first=False, blank=False, indentfirst=None
):
    """Return a copy of the string with each line indented by 4 spaces. The
    first line and blank lines are not indented by default.

    :param width: Number of spaces to indent by.
    :param first: Don't skip indenting the first line.
    :param blank: Don't skip indenting empty lines.

    .. versionchanged:: 2.10
        Blank lines are not indented by default.

        Rename the ``indentfirst`` argument to ``first``.
    """
    if indentfirst is not None:
        warnings.warning(DeprecationWarning(
            'The "indentfirst" argument is renamed to "first".'
        ), stacklevel=2)
        first = indentfirst

    indention = u' ' * width
    newline = u'\n'

    if isinstance(s, Markup):
        indention = Markup(indention)
        newline = Markup(newline)

    s += newline  # this quirk is necessary for splitlines method

    if blank:
        rv = (newline + indention).join(s.splitlines())
    else:
        lines = s.splitlines()
        rv = lines.pop(0)

        if lines:
            rv += newline + newline.join(
                indention + line if line else line for line in lines
            )

    if first:
        rv = indention + rv

    return rv
Beispiel #7
0
def markup_join(seq):
    """Concatenation that escapes if necessary and converts to unicode."""
    buf = []
    iterator = imap(soft_unicode, seq)
    for arg in iterator:
        buf.append(arg)
        if hasattr(arg, '__html__'):
            return Markup(u'').join(chain(buf, iterator))
    return concat(buf)
Beispiel #8
0
 async def async_call(self):
     rv = await concat_async(self._stack[self._depth](self._context))
     if self._context.eval_ctx.autoescape:
         rv = Markup(rv)
     return rv
Beispiel #9
0
 async def async_invoke(self, arguments, autoescape):
     rv = await self._func(*arguments)
     if autoescape:
         rv = Markup(rv)
     return rv
Beispiel #10
0
 def ngettext(__context, __singular, __plural, __num, **variables):
     variables.setdefault('num', __num)
     rv = __context.call(func, __singular, __plural, __num)
     if __context.eval_ctx.autoescape:
         rv = Markup(rv)
     return rv % variables
Beispiel #11
0
 def gettext(__context, __string, **variables):
     rv = __context.call(func, __string)
     if __context.eval_ctx.autoescape:
         rv = Markup(rv)
     return rv % variables
Beispiel #12
0
 def _invoke(self, arguments, autoescape):
     """This method is being swapped out by the async implementation."""
     rv = self._func(*arguments)
     if autoescape:
         rv = Markup(rv)
     return rv
Beispiel #13
0
 def __call__(self):
     rv = concat(self._stack[self._depth](self._context))
     if self._context.eval_ctx.autoescape:
         rv = Markup(rv)
     return rv
Beispiel #14
0
def do_mark_safe(value):
    """Mark the value as safe which means that in an environment with automatic
    escaping enabled this variable will not be escaped.
    """
    return Markup(value)
Beispiel #15
0
def do_striptags(value):
    """Strip SGML/XML tags and replace adjacent whitespace by one space.
    """
    if hasattr(value, '__html__'):
        value = value.__html__()
    return Markup(text_type(value)).striptags()
Beispiel #16
0
 def as_const(self, eval_ctx=None):
     eval_ctx = get_eval_context(self, eval_ctx)
     return Markup(self.expr.as_const(eval_ctx))