• jinja2.Markup 对HTML文本文件进行处理


    class jinja2.Markup([string])

    Marks a string as being safe for inclusion in HTML/XML output without needing to be escaped. This implements the __html__ interface a couple of frameworks and web applications use. Markup is a direct subclass of unicode and provides all the methods of unicode just that it escapes arguments passed and always returnsMarkup.

    The escape function returns markup objects so that double escaping can’t happen.

    The constructor of the Markup class can be used for three different things: When passed an unicode object it’s assumed to be safe, when passed an object with an HTML representation (has an __html__ method) that representation is used, otherwise the object passed is converted into a unicode string and then assumed to be safe:

    >>> Markup("Hello <em>World</em>!")
    Markup(u'Hello <em>World</em>!')
    >>> class Foo(object):
    ...  def __html__(self):
    ...   return '<a href="#">foo</a>'
    ... 
    >>> Markup(Foo())
    Markup(u'<a href="#">foo</a>')
    

    If you want object passed being always treated as unsafe you can use the escape()classmethod to create a Markup object:

    >>> Markup.escape("Hello <em>World</em>!")
    Markup(u'Hello &lt;em&gt;World&lt;/em&gt;!')
    

    Operations on a markup string are markup aware which means that all arguments are passed through the escape() function:

    >>> em = Markup("<em>%s</em>")
    >>> em % "foo & bar"
    Markup(u'<em>foo &amp; bar</em>')
    >>> strong = Markup("<strong>%(text)s</strong>")
    >>> strong % {'text': '<blink>hacker here</blink>'}
    Markup(u'<strong>&lt;blink&gt;hacker here&lt;/blink&gt;</strong>')
    >>> Markup("<em>Hello</em> ") + "<foo>"
    Markup(u'<em>Hello</em> &lt;foo&gt;')
    
    classmethod escape(s)

    Escape the string. Works like escape() with the difference that for subclasses ofMarkup this function would return the correct subclass.

    unescape()

    Unescape markup again into an unicode string. This also resolves known HTML4 and XHTML entities:

    >>> Markup("Main &raquo; <em>About</em>").unescape()
    u'Main \xbb <em>About</em>'
    
    striptags()

    Unescape markup into an unicode string and strip all tags. This also resolves known HTML4 and XHTML entities. Whitespace is normalized to one:

    >>> Markup("Main &raquo;  <em>About</em>").striptags()
    u'Main \xbb About'
    

    Note

    The Jinja2 Markup class is compatible with at least Pylons and Genshi. It’s expected that more template engines and framework will pick up the __html__ concept soon.

     
  • 相关阅读:
    visual studio 2019 sql server localdb 数据库中文乱码解决方法
    Ado.net总结
    Ado.Net总结
    提升DataGridView加载速度的三个属性设置
    winform程序:newtonsoft json 序列化时出现 “unterminated string. Excepted delimiter..."
    entityframewor core 不让属性生成数据库的列
    在winform中屏蔽双击最大化或最小化窗体(C#实现),禁用任务管理器结束程序
    使用cefsharp在winform中嵌套浏览器
    PIE SDK算法的同步调用
    PIE SDK小波变换
  • 原文地址:https://www.cnblogs.com/dwnblogs/p/2755912.html
Copyright © 2020-2023  润新知