本文整理匯總了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)