Python bleach.linkify() Examples

The following are 23 code examples of bleach.linkify(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module bleach , or try the search function .
Example #1
Source File: models.py    From flask-blog with MIT License 6 votes vote down vote up
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))


# 权限 
Example #2
Source File: models.py    From Simpleblog with MIT License 6 votes vote down vote up
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格式的序列化字典 
Example #3
Source File: models.py    From django-mptt-comments with MIT License 6 votes vote down vote up
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 
Example #4
Source File: entities.py    From federation with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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"],
        ) 
Example #5
Source File: text.py    From federation with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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"],
    ) 
Example #6
Source File: utils.py    From ok with Apache License 2.0 6 votes vote down vote up
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." 
Example #7
Source File: models.py    From circleci-demo-python-flask with MIT License 5 votes vote down vote up
def on_changed_body(target, value, oldvalue, initiator):
        allowed_tags = ['a', 'abbr', 'acronym', 'b', 'code', 'em', 'i',
                        'strong']
        target.body_html = bleach.linkify(bleach.clean(
            markdown(value, output_format='html'),
            tags=allowed_tags, strip=True)) 
Example #8
Source File: models.py    From flasky-with-celery with MIT License 5 votes vote down vote up
def on_changed_body(target, value, oldvalue, initiator):
        allowed_tags = ['a', 'abbr', 'acronym', 'b', 'code', 'em', 'i',
                        'strong']
        target.body_html = bleach.linkify(bleach.clean(
            markdown(value, output_format='html'),
            tags=allowed_tags, strip=True)) 
Example #9
Source File: models.py    From flasky-with-celery with MIT License 5 votes vote down vote up
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)) 
Example #10
Source File: utilities.py    From open-humans with MIT License 5 votes vote down vote up
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) 
Example #11
Source File: models.py    From flasky-first-edition with MIT License 5 votes vote down vote up
def on_changed_body(target, value, oldvalue, initiator):
        allowed_tags = ['a', 'abbr', 'acronym', 'b', 'code', 'em', 'i',
                        'strong']
        target.body_html = bleach.linkify(bleach.clean(
            markdown(value, output_format='html'),
            tags=allowed_tags, strip=True)) 
Example #12
Source File: models.py    From flasky-first-edition with MIT License 5 votes vote down vote up
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)) 
Example #13
Source File: report.py    From arche with MIT License 5 votes vote down vote up
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 
Example #14
Source File: models.py    From flask-pycon2014 with MIT License 5 votes vote down vote up
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)) 
Example #15
Source File: models.py    From elmer with MIT License 5 votes vote down vote up
def linkfy_subject(self):
        """Linkifies the subject body."""
        return bleach.linkify(escape(self.body)) 
Example #16
Source File: utils.py    From partisan-discourse with Apache License 2.0 5 votes vote down vote up
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 
Example #17
Source File: bleach_tags.py    From django-bleach with MIT License 5 votes vote down vote up
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) 
Example #18
Source File: html_helper.py    From DeerU with GNU General Public License v3.0 5 votes vote down vote up
def add_link(html_doc):
    return bleach.linkify(html_doc) 
Example #19
Source File: filters.py    From pagure with GNU General Public License v2.0 5 votes vote down vote up
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 "" 
Example #20
Source File: models.py    From Simpleblog with MIT License 5 votes vote down vote up
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
        )) 
Example #21
Source File: models.py    From circleci-demo-python-flask with MIT License 5 votes vote down vote up
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)) 
Example #22
Source File: views.py    From yournextrepresentative with GNU Affero General Public License v3.0 4 votes vote down vote up
def get_context_data(self, **kwargs):
        context = super(PhotoReview, self).get_context_data(**kwargs)
        self.queued_image = get_object_or_404(
            QueuedImage,
            pk=kwargs['queued_image_id']
        )
        context['queued_image'] = self.queued_image
        person = Person.objects.get(
            id=self.queued_image.person.id,
        )
        context['has_crop_bounds'] = int(self.queued_image.has_crop_bounds)
        max_x = self.queued_image.image.width - 1
        max_y = self.queued_image.image.height - 1
        guessed_crop_bounds = [
            value_if_none(self.queued_image.crop_min_x, 0),
            value_if_none(self.queued_image.crop_min_y, 0),
            value_if_none(self.queued_image.crop_max_x, max_x),
            value_if_none(self.queued_image.crop_max_y, max_y),
        ]
        context['form'] = PhotoReviewForm(
            initial = {
                'queued_image_id': self.queued_image.id,
                'decision': self.queued_image.decision,
                'x_min': guessed_crop_bounds[0],
                'y_min': guessed_crop_bounds[1],
                'x_max': guessed_crop_bounds[2],
                'y_max': guessed_crop_bounds[3],
                'moderator_why_allowed': self.queued_image.why_allowed,
                'make_primary': True,
            }
        )
        context['guessed_crop_bounds'] = guessed_crop_bounds
        context['why_allowed'] = self.queued_image.why_allowed
        context['moderator_why_allowed'] = self.queued_image.why_allowed
        # There are often source links supplied in the justification,
        # and it's convenient to be able to follow them. However, make
        # sure that any maliciously added HTML tags have been stripped
        # before linkifying any URLs:
        context['justification_for_use'] = \
            bleach.linkify(
                bleach.clean(
                    self.queued_image.justification_for_use,
                    tags=[],
                    strip=True
                )
            )
        context['google_image_search_url'] = self.get_google_image_search_url(
            person
        )
        context['google_reverse_image_search_url'] = \
            self.get_google_reverse_image_search_url(
                self.queued_image.image.url
        )
        context['person'] = person
        return context 
Example #23
Source File: jinja_tags.py    From canvas with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def ugc_text(text, max_length=knobs.POST_TEXT_TRUNCATION_LENGTH,
             should_oembed=False, linkify=True, truncation_markup=u'…'):
    # When using this, you can't specify keyword arguments (until Django 1.3), use them as positional args.
    # They serve only to provide defaults.
    def _linkify(text, href):
        safe_href = urllib.quote(unicode(href).encode('utf-8'), safe=_SAFE_URI_CHARS)
        attrs = {
            'href': safe_href,
            'title': safe_href,
            'target': '_blank',
        }
        return u'<a {0}>{1}</a>'.format(u' '.join(u'{0}="{1}"'.format(key, val) for key,val in attrs.iteritems()),
                                        text)

    def linkify_group(match):
        #TODO make group 404s show a page asking if you want to create the group.
        group = match.group(2)
        return match.group(1) + _linkify(u'#' + group, u'/x/' + group)

    # Remove <tag> <shenanigans/>
    text = strip_tags(text)

    # Escape any HTML entities.
    text = escape(text)

    if len(text) > max_length:
        #TODO split on a word.
        text = text[:max_length] + truncation_markup

    # Enter means newline bitch
    text = text.replace('\n', '<br>\n')

    # Linkify links.
    if linkify:
        text = bleach.linkify(text, nofollow=True, target='_blank')

        # Replace all #foobar forms with http://example.com/x/foobar,
        # but not '#', '#1', '#1-ocle', et cetera.
        text = _GROUP_LINK_PATTERN.sub(linkify_group, text)

        #TODO linkify @names

    # Escape Django template tokens for jinja_adapter funkiness. Nasty. Delete once we move all over to Jinja.
    text = text.replace('{{', '&#123;' * 2)
    text = text.replace('}}', '&#125;' * 2)
    text = text.replace('{%', '&#123;%')
    text = text.replace('%}', '%&#125;')
    text = text.replace('{#', '&#123;#')
    text = text.replace('#}', '#&#125;')

    ugc_text_id = 'ugc_text_' + uuid_.uuid4().hex

    span_classes = 'ugc_text'
    return Markup(u'<span id="{0}" class="{1}">{2}</span>'.format(ugc_text_id, span_classes, text))