我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用django.utils.html.urlize()。
def html_doc(self): """Methods docstring, as HTML""" if not self.raw_docstring: result = '' elif settings.MODERNRPC_DOC_FORMAT.lower() in ('rst', 'restructred', 'restructuredtext'): from docutils.core import publish_parts result = publish_parts(self.raw_docstring, writer_name='html')['body'] elif settings.MODERNRPC_DOC_FORMAT.lower() in ('md', 'markdown'): import markdown result = markdown.markdown(self.raw_docstring) else: result = "<p>{}</p>".format(self.raw_docstring.replace('\n\n', '</p><p>').replace('\n', ' ')) return mark_safe(urlize(result))
def urlize_html(html): """ Returns urls found in an (X)HTML text node element as urls via Django urlize filter. """ try: from bs4 import BeautifulSoup except ImportError: if settings.DEBUG: raise template.TemplateSyntaxError( "Error in urlize_html The Python BeautifulSoup libraries aren't installed.") return html else: soup = BeautifulSoup(html, 'html.parser') text_nodes = soup.find_all(text=True) for text_node in text_nodes: urlized_text = urlize(text_node) text_node.replace_with(BeautifulSoup(urlized_text, 'html.parser')) return mark_safe(str(soup))
def urlize_target_blank(value, limit=None, autoescape=None): if not limit: limit = len(value) return mark_safe(urlize_impl(value, trim_url_limit=int(limit), nofollow=True, autoescape=autoescape).replace('<a', '<a target="_blank"'))
def render_activity(item): if not item.group: # not implemented return try: action_str = ACTIVITY_ACTION_STRINGS[item.type] except KeyError: logging.warning('Unknown activity type present: %s', item.type) return if item.type == Activity.CREATE_ISSUE: action_str = action_str.format(**item.data) elif item.type == Activity.ASSIGNED: if item.data['assignee'] == item.user_id: assignee_name = 'themselves' else: try: assignee = User.objects.get(id=item.data['assignee']) except User.DoesNotExist: assignee_name = 'unknown' else: assignee_name = assignee.get_display_name() action_str = action_str.format(user=assignee_name) output = '' if item.user: user = item.user name = user.name or user.email output += '<span class="avatar"><img src="%s"></span> ' % (get_gravatar_url(user.email, size=20),) output += '<strong>%s</strong> %s' % (escape(name), action_str) else: output += '<span class="avatar sentry"></span> ' output += 'The system %s' % (action_str,) output += ' <span class="sep">—</span> <span class="time">%s</span>' % (timesince(item.datetime),) if item.type == Activity.NOTE: output += linebreaks(urlize(escape(item.data['text']))) return mark_safe(output)