Python bleach 模块,ALLOWED_TAGS 实例源码

我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用bleach.ALLOWED_TAGS

项目:pretix-pages    作者:pretix    | 项目源码 | 文件源码
def get_context_data(self, **kwargs):
        ctx = super().get_context_data()
        page = self.get_page()
        ctx['page'] = page

        attributes = dict(bleach.ALLOWED_ATTRIBUTES)
        attributes['a'] = ['href', 'title', 'target']
        attributes['p'] = ['class']
        attributes['li'] = ['class']

        ctx['content'] = bleach.clean(str(page.text), tags=bleach.ALLOWED_TAGS + [
            'p',
            'br',
            's',
            'sup',
            'sub',
            'u',
            'h3',
            'h4',
            'h5',
            'h6'
        ], attributes=attributes)
        return ctx
项目:gennotes    作者:madprime    | 项目源码 | 文件源码
def markdown(value):
    """
    Translate markdown to a safe subset of HTML.
    """
    cleaned = bleach.clean(markdown_library.markdown(value),
                           tags=bleach.ALLOWED_TAGS +
                           ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'])

    linkified = bleach.linkify(cleaned)

    return mark_safe(linkified)
项目:oclubs    作者:SHSIDers    | 项目源码 | 文件源码
def format(rawstr):
        return bleach.clean(
            markdown(
                rawstr,
                output_format='html5',
                lazy_ol=False
            ),
            tags=ALLOWED_TAGS
        )
项目:PonyConf    作者:PonyConf    | 项目源码 | 文件源码
def markdown_to_html(md):
    html = markdown(md)
    allowed_tags = bleach.ALLOWED_TAGS + ['p', 'pre', 'span' ] + ['h%d' % i for i in range(1, 7) ]
    html = bleach.clean(html, tags=allowed_tags)
    return mark_safe(html)
项目:pretalx    作者:pretalx    | 项目源码 | 文件源码
def send(self):
        if self.sent:
            raise Exception('This mail has been sent already. It cannot be sent again.')

        body_md = bleach.linkify(bleach.clean(markdown.markdown(self.text), tags=bleach.ALLOWED_TAGS + [
            'p', 'pre'
        ]))
        html_context = {
            'body': body_md,
            'event': self.event,
            'color': self.event.primary_color or '#1c4a3b',
        }
        body_html = get_template('mail/mailwrapper.html').render(html_context)

        from pretalx.common.mail import mail_send_task
        mail_send_task.apply_async(
            kwargs={
                'to': self.to.split(','),
                'subject': self.subject,
                'body': self.text,
                'html': body_html,
                'sender': self.reply_to,
                'event': self.event.pk,
                'cc': (self.cc or '').split(','),
                'bcc': (self.bcc or '').split(','),
            }
        )

        self.sent = now()
        if self.pk:
            self.save()
项目:helios-server-mixnet    作者:RunasSudo    | 项目源码 | 文件源码
def description_bleached(self):
    return bleach.clean(self.description, tags = bleach.ALLOWED_TAGS + ['p', 'h4', 'h5', 'h3', 'h2', 'br', 'u'])
项目:site    作者:alphageek-xyz    | 项目源码 | 文件源码
def markup_markdown(md, allowed_tags=None, allowed_attrs=None):
    html = bleach.clean(markdown.markdown(
        md, extensions=[
            'markdown.extensions.tables',
            'markdown.extensions.abbr',
            'markdown.extensions.attr_list',
            'markdown.extensions.footnotes',
            'markdown.extensions.wikilinks',
            'markdown.extensions.smart_strong',
            'markdown.extensions.smarty',
            'markdown.extensions.extra',
            'pymdownx.magiclink',
            'pymdownx.betterem',
            'pymdownx.tilde',
            'pymdownx.superfences',
        ]),
        styles=['width', 'margin'],
        tags=allowed_tags or bleach.ALLOWED_TAGS + [
            'h1', 'h2', 'h3', 'h4', 'p',
            'div', 'pre', 'small', 'img'
        ],
        attributes=allowed_attrs or {
            'a':       ['href', 'title', 'target', 'class'],
            'abbr':    ['title'],
            'acronym': ['title'],
            'div':     ['class', 'name'],
            'p':       ['class', 'name'],
            'img':     ['alt', 'src', 'class', 'id', 'height', 'width'],
        }
    )

    soup = BeautifulSoup(
        '<div class="service-markup">\n%s\n</div>' % html,
        'html.parser'
    )
    for ul in soup.select('ul'):
        ul['class'] =  'list-group'
    for li in soup.select('ul li'):
        li['class'] = 'bullet-item'
    for h in ['h%d' % i for i in range(1, 5)]:
        for t in soup.select(h):
            t['class'] = 'text-primary'

    return str(soup)