本文整理汇总了Python中bleach.clean方法的典型用法代码示例。如果您正苦于以下问题:Python bleach.clean方法的具体用法?Python bleach.clean怎么用?Python bleach.clean使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bleach
的用法示例。
在下文中一共展示了bleach.clean方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: strip_invalid_html
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import clean [as 别名]
def strip_invalid_html(content):
''' strips invalid tags/attributes '''
allowed_tags = ['a', 'abbr', 'acronym', 'address', 'b', 'br', 'div', 'dl', 'dt',
'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img',
'li', 'ol', 'p', 'pre', 'q', 's', 'small', 'strike', 'strong',
'span', 'sub', 'sup', 'table', 'tbody', 'td', 'tfoot', 'th',
'thead', 'tr', 'tt', 'u', 'ul']
allowed_attrs = {
'a': ['href', 'target', 'title'],
'img': ['src', 'alt', 'width', 'height'],
}
cleaned = bleach.clean(content,
tags=allowed_tags,
attributes=allowed_attrs,
strip=True)
# handle malformed html after running through bleach
tree = BeautifulSoup(cleaned, "lxml")
return str(tree.html)
示例2: clean_html
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import clean [as 别名]
def clean_html(html, with_media=False):
authorized_tags = [
'p', 'a', 'ul', 'ol', 'li', 'blockquote',
'h1', 'h2', 'h3', 'h4', 'h5',
'strong', 'em',
'br',
]
authorized_attributes = {
'a': ['href', 'title'],
'img': ['src', 'width', 'height', 'alt'],
'iframe': ['src', 'width', 'height', 'allowfullscreen'],
'video': [
'controls', 'width', 'height', 'allowfullscreen', 'preload',
'poster'],
'audio': ['controls', 'preload'],
'source': ['src']
}
if with_media:
authorized_tags += ['img', 'iframe', 'video', 'audio', 'source']
return bleach.clean(
html, authorized_tags, authorized_attributes, strip=True)
示例3: markdown_and_sanitize
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import clean [as 别名]
def markdown_and_sanitize(markdown_string):
"""Takes a markdown string and converts it into sanitized html.
It uses the table extension; while that's not a part of standard
markdown, it is sure to be useful for TensorBoard users.
The sanitizer uses the allowed_tags and attributes specified above. Mostly,
we ensure that our standard use cases like tables and links are supported.
Args:
markdown_string: Markdown string to sanitize
Returns:
a string containing sanitized html for input markdown
"""
# Convert to utf-8 whenever we have a binary input.
if isinstance(markdown_string, six.binary_type):
markdown_string = markdown_string.decode('utf-8')
string_html = markdown.markdown(
markdown_string, extensions=['markdown.extensions.tables'])
string_sanitized = bleach.clean(
string_html, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES)
return string_sanitized
示例4: __init__
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import clean [as 别名]
def __init__(self, markdown_file: Path):
super().__init__()
self.markdown_file = markdown_file
self.html = bleach.clean(
markdown.markdown(
get_path(self.markdown_file).read_text(),
extensions=[
"tables",
"sane_lists",
_WebvizMarkdownExtension(base_path=markdown_file.parent),
],
),
tags=Markdown.ALLOWED_TAGS,
attributes=Markdown.ALLOWED_ATTRIBUTES,
styles=Markdown.ALLOWED_STYLES,
)
# Workaround for upstream issue https://github.com/plotly/dash-core-components/issues/746,
# where we convert void html tags from <tag> to <tag/>.
self.html = re.sub("<img (.*?[^/])>", r"<img \1/>", self.html)
self.html = self.html.replace("<br>", "<br/>").replace("<hr>", "<hr/>")
示例5: render_markdown
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import clean [as 别名]
def render_markdown(data, auto_link=True, allow_html=False):
''' Returns the data as rendered markdown
:param auto_link: Should ckan specific links be created e.g. `group:xxx`
:type auto_link: bool
:param allow_html: If True then html entities in the markdown data.
This is dangerous if users have added malicious content.
If False all html tags are removed.
:type allow_html: bool
'''
if not data:
return ''
if allow_html:
data = markdown(data.strip())
else:
data = RE_MD_HTML_TAGS.sub('', data.strip())
data = clean_html(
markdown(data), strip=True,
tags=MARKDOWN_TAGS, attributes=MARKDOWN_ATTRIBUTES)
# tags can be added by tag:... or tag:"...." and a link will be made
# from it
if auto_link:
data = html_auto_link(data)
return literal(data)
示例6: render
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import clean [as 别名]
def render(text, truncate_words=None):
html = markdown.markdown(
text,
extensions=[
EmojiExtension(emoji_index=twemoji),
SuperFencesCodeExtension(),
MagiclinkExtension(),
DeleteSubExtension(subscript=False),
Nl2BrExtension(),
]
)
markdown_attrs['img'].append('class')
markdown_tags.append('pre')
clean_html = bleach.clean(html, markdown_tags, markdown_attrs)
if truncate_words:
clean_html = Truncator(clean_html).words(num=truncate_words, html=True)
return clean_html
示例7: _validate
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import clean [as 别名]
def _validate(non_bleached_value, bleached_value):
"""Validates whether the specified non-bleached value ended up being
correctly bleached.
Arguments:
non_bleached_value:
The value before bleaching.
bleached_value:
The value after bleaching.
"""
for lang_code, _ in settings.LANGUAGES:
if not non_bleached_value.get(lang_code):
assert not bleached_value.get(lang_code)
continue
expected_value = bleach.clean(
non_bleached_value.get(lang_code), get_bleach_default_options()
)
assert bleached_value.get(lang_code) == expected_value
示例8: create
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import clean [as 别名]
def create(self, validated_data):
enterprise_customer = self.context['view'].kwargs.get('enterprise_customer')
email_type = validated_data['email_type']
email_greeting = bleach.clean(validated_data.get('email_greeting', ''))
email_closing = bleach.clean(validated_data.get('email_closing', ''))
create_data = dict(
enterprise_customer=enterprise_customer,
email_type=email_type,
email_greeting=email_greeting,
email_closing=email_closing,
)
if 'name' in validated_data:
create_data['name'] = validated_data.get('name')
instance = OfferAssignmentEmailTemplates.objects.create(**create_data)
# deactivate old templates for enterprise for this specific email type
OfferAssignmentEmailTemplates.objects.filter(
enterprise_customer=enterprise_customer,
email_type=email_type,
).exclude(pk=instance.pk).update(active=False)
return instance
示例9: format_email
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import clean [as 别名]
def format_email(template, placeholder_dict, greeting, closing):
"""
Arguments:
template (String): Email template body
placeholder_dict (SafeDict): Safe dictionary of placeholders and their values
greeting (String): Email greeting (prefix)
closing (String): Email closing (suffix)
Apply placeholders to the email template.
Safely handle placeholders in the template without matching tokens (just emit the placeholders).
Reference: https://stackoverflow.com/questions/17215400/python-format-string-unused-named-arguments
"""
if greeting is None:
greeting = ''
if closing is None:
closing = ''
greeting = bleach.clean(greeting)
closing = bleach.clean(closing)
email_body = string.Formatter().vformat(template, SafeTuple(), placeholder_dict)
return greeting + email_body + closing
示例10: get_result_label_html
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import clean [as 别名]
def get_result_label_html(self, result):
standard_html = '<strong>%s </strong>%s' % (
bleach.clean(result.get_ticket_identifier()),
bleach.clean(result.title)
)
# mark 'Done' issues
if result.kanbancol.type == "Done":
standard_html = '<del>%s</del><small style="padding-left: 1em">[%s]</small>' % (
standard_html,
result.kanbancol.get_type_display()
)
# mark archived issues
if result.archived:
standard_html = '<em class="text-muted">%s</em><small style="padding-left: 1em">[%s]</small>' % (
standard_html,
_("Archived")
)
return standard_html
示例11: safe_text
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import clean [as 别名]
def safe_text(text):
"""Clean text, stripping all tags, attributes and styles."""
return bleach.clean(text, tags=[], attributes=[], styles=[], strip=True)
示例12: sanitize_html
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import clean [as 别名]
def sanitize_html(html, restricted=False):
"""
Sanitize HTML we got from the user so it's safe to include in the page
"""
# TODO: document the HTML subset allowed (particularly <pre lang="python">)
allowed = allowed_tags_restricted if restricted else allowed_tags
return mark_safe(bleach.clean(html, tags=allowed, attributes=allowed_attributes, strip=True))
示例13: clean
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import clean [as 别名]
def clean(self, value):
content, markup, math = super(MarkupContentField, self).clean(value)
if markup == 'html-wysiwyg':
# the editor is a UI nicety only
markup = 'html'
if markup == 'html':
content = sanitize_html(content, restricted=self.restricted)
math = math and self.widget.allow_math
return content, markup, math
示例14: linkify_text
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import clean [as 别名]
def linkify_text(text):
""" escape all html tags with bleach, then use bleach to linkify
"""
if text:
cleaned = bleach.clean(text, tags=[], attributes=[])
return bleach.linkify(cleaned)
else:
return ""
示例15: bleach_value
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import clean [as 别名]
def bleach_value(value, tags=BLEACH_ALLOWED_TAGS, attributes=BLEACH_ALLOWED_ATTRIBUTES):
return bleach.clean(value, tags=tags, attributes=attributes)