本文整理汇总了Python中bleach.linkify方法的典型用法代码示例。如果您正苦于以下问题:Python bleach.linkify方法的具体用法?Python bleach.linkify怎么用?Python bleach.linkify使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bleach
的用法示例。
在下文中一共展示了bleach.linkify方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: preview_body
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import linkify [as 别名]
def preview_body(target, value, oldvalue, initiator):
allowed_tags = [
'a', 'abbr', 'acronym', 'b', 'img', 'blockquote', 'code',
'em', 'i', 'li', 'ol', 'pre', 'strong', 'ul', 'h1', 'h2',
'h3', 'p'
]
target.body_html = bleach.linkify(bleach.clean(
markdown(value, output_format='html'),
tags=allowed_tags, strip=True,
attributes={
'*': ['class'],
'a': ['href', 'rel'],
'img': ['src', 'alt'], # 支持标签和属性
}
))
# 把文章转换成JSON格式的序列化字典
示例2: pre_save
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import linkify [as 别名]
def pre_save(self, model_instance, add):
instance = model_instance
value = None
for attr in self.source.split('.'):
value = getattr(instance, attr)
instance = value
if value is None or value == '':
return value
extensions = [
'markdown.extensions.extra',
'markdown.extensions.codehilite',
]
md = markdown.Markdown(extensions=extensions)
value = md.convert(value)
value = bleach_value(value)
value = bleach.linkify(value)
setattr(model_instance, self.attname, value)
return value
示例3: convert_markdown
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import linkify [as 别名]
def convert_markdown(text):
# https://pythonadventures.wordpress.com/tag/markdown/
allowed_tags = [
'a', 'abbr', 'acronym', 'b',
'blockquote', 'code', 'em',
'i', 'li', 'ol', 'pre', 'strong',
'ul', 'h1', 'h2', 'h3', 'p', 'br', 'ins', 'del',
]
unsafe_html = markdown.markdown(
text,
extensions=["markdown.extensions.fenced_code"],
)
html = bleach.linkify(bleach.clean(unsafe_html, tags=allowed_tags))
return Markup(html)
# Timezones. Be cautious with using tzinfo argument. http://pytz.sourceforge.net/
# "tzinfo argument of the standard datetime constructors 'does not work'
# with pytz for many timezones."
示例4: process_text_links
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import linkify [as 别名]
def process_text_links(text):
"""Process links in text, adding some attributes and linkifying textual links."""
link_callbacks = [callbacks.nofollow, callbacks.target_blank]
def link_attributes(attrs, new=False):
"""Run standard callbacks except for internal links."""
href_key = (None, "href")
if attrs.get(href_key).startswith("/"):
return attrs
# Run the standard callbacks
for callback in link_callbacks:
attrs = callback(attrs, new)
return attrs
return bleach.linkify(
text,
callbacks=[link_attributes],
parse_email=False,
skip_tags=["code"],
)
示例5: post_receive
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import linkify [as 别名]
def post_receive(self) -> None:
"""
Make linkified tags normal tags.
"""
super().post_receive()
def remove_tag_links(attrs, new=False):
rel = (None, "rel")
if attrs.get(rel) == "tag":
return
return attrs
self.raw_content = bleach.linkify(
self.raw_content,
callbacks=[remove_tag_links],
parse_email=False,
skip_tags=["code", "pre"],
)
示例6: markitup
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import linkify [as 别名]
def markitup(text):
"""
把Markdown转换为HTML
"""
# 删除与段落相关的标签,只留下格式化字符的标签
# allowed_tags = ['a', 'abbr', 'acronym', 'b', 'blockquote', 'code',
# 'em', 'i', 'li', 'ol', 'pre', 'strong', 'ul',
# 'h1', 'h2', 'h3', 'p', 'img']
return bleach.linkify(markdown(text, ['extra'], output_format='html5'))
# return bleach.linkify(bleach.clean(
# # markdown默认不识别三个反引号的code-block,需开启扩展
# markdown(text, ['extra'], output_format='html5'),
# tags=allowed_tags, strip=True))
# 权限
示例7: on_changed_body
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import linkify [as 别名]
def on_changed_body(target, value, oldvalue, initiator):
allowed_tags = ['a', 'abbr', 'acronym', 'b', 'blockquote', 'code',
'em', 'i', 'li', 'ol', 'pre', 'strong', 'ul',
'h1', 'h2', 'h3', 'p']
target.body_html = bleach.linkify(bleach.clean(
markdown(value, output_format='html'),
tags=allowed_tags, strip=True))
示例8: on_changed_body
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import linkify [as 别名]
def on_changed_body(target, value, oldvalue, initiator):
allowed_tags = [
'a', 'abbr', 'acronym', 'b', 'code', 'em', 'img', 'i', 'strong'
]
target.body_html = bleach.linkify(bleach.clean(
markdown(value, output_format='html'),
tags=allowed_tags, strip=True
))
示例9: linkify_text
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import linkify [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 ""
示例10: add_link
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import linkify [as 别名]
def add_link(html_doc):
return bleach.linkify(html_doc)
示例11: bleach_linkify
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import linkify [as 别名]
def bleach_linkify(value):
"""
Convert URL-like strings in an HTML fragment to links
This function converts strings that look like URLs, domain names and email
addresses in text that may be an HTML fragment to links, while preserving:
1. links already in the string
2. urls found in attributes
3. email addresses
"""
if value is None:
return None
return bleach.linkify(value, parse_email=True)
示例12: htmlize
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import linkify [as 别名]
def htmlize(text):
"""
This helper method renders Markdown then uses Bleach to sanitize it as
well as convert all links to actual links.
"""
text = bleach.clean(text, strip=True) # Clean the text by stripping bad HTML tags
text = markdown(text) # Convert the markdown to HTML
text = bleach.linkify(text) # Add links from the text and add nofollow to existing links
return text
示例13: linkfy_subject
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import linkify [as 别名]
def linkfy_subject(self):
"""Linkifies the subject body."""
return bleach.linkify(escape(self.body))
示例14: __init__
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import linkify [as 别名]
def __init__(self):
self.results: Dict[str, Result] = {}
self.env = Environment(
loader=PackageLoader("arche", "templates"),
autoescape=select_autoescape(["html"]),
extensions=["jinja2.ext.loopcontrols"],
)
self.env.filters["linkify"] = linkify
示例15: markdown
# 需要导入模块: import bleach [as 别名]
# 或者: from bleach import linkify [as 别名]
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)