예제 #1
0
    def _render_default(self, parser, value, option=None, parent=None):
        placeholders = re.findall(placeholder_re, self.definition_string)
        # Get the format data
        fmt = {}
        if len(placeholders) == 1:
            fmt.update({placeholders[0]: value})
        elif len(placeholders) == 2:
            fmt.update({
                placeholders[1]:
                value,
                placeholders[0]:
                replace(option, bbcode_settings.BBCODE_ESCAPE_HTML)
                if option else ''  # noqa
            })

        # Semantic validation
        valid = self._validate_format(parser, fmt)
        if not valid and option:
            return self.definition_string.format(**fmt)
        elif not valid:
            return self.definition_string.format(**fmt).replace('=', '')

        # Before rendering, it's necessary to escape the included braces: '{' and '}' ; some of them
        # could not be placeholders
        escaped_format_string = self.format_string.replace('{', '{{').replace(
            '}', '}}')
        for placeholder in fmt.keys():
            escaped_format_string = escaped_format_string.replace(
                '{' + placeholder + '}', placeholder)

        # Return the rendered data
        return escaped_format_string.format(**fmt)
예제 #2
0
 def render(self, value, option=None, parent=None):
     href = replace(option, bbcode_settings.BBCODE_ESCAPE_HTML) if option else value
     if '://' not in href and self._domain_re.match(href):
         href = 'http://' + href
     content = value if option else href
     # Render
     return '<a href="{}">{}</a>'.format(href, content or href)
예제 #3
0
 def render(self, value, option=None, parent=None):
     href = replace(option, bbcode_settings.BBCODE_ESCAPE_HTML) if option else value
     if '://' not in href and self._domain_re.match(href):
         href = 'http://' + href
     content = value if option else href
     # Render
     return '<a href="{}">{}</a>'.format(href, content or href)
예제 #4
0
    def _render_default(self, parser, value, option=None, parent=None):
        placeholders = re.findall(placeholder_re, self.definition_string)
        # Get the format data
        fmt = {}
        if len(placeholders) == 1:
            fmt.update({placeholders[0]: value})
        elif len(placeholders) == 2:
            fmt.update({
                placeholders[1]: value,
                placeholders[0]: replace(option, bbcode_settings.BBCODE_ESCAPE_HTML) if option else ''  # noqa
            })

        # Semantic validation
        valid = self._validate_format(parser, fmt)
        if not valid and option:
            return self.definition_string.format(**fmt)
        elif not valid:
            return self.definition_string.format(**fmt).replace('=', '')

        # Before rendering, it's necessary to escape the included braces: '{' and '}' ; some of them
        # could not be placeholders
        escaped_format_string = self.format_string.replace('{', '{{').replace('}', '}}')
        for placeholder in fmt.keys():
            escaped_format_string = escaped_format_string.replace(
                '{' + placeholder + '}', placeholder)

        # Return the rendered data
        return escaped_format_string.format(**fmt)
예제 #5
0
    def _render_textual_content(self, data, replace_specialchars, replace_links, replace_smilies):
        """
        Given an input text, update it by replacing the HTML special characters, the links with
        their HTML corresponding tags and the smilies codes with the corresponding images.
        """
        if replace_specialchars:
            data = replace(data, self.replace_html)

        if replace_links:
            def linkrepl(match):
                url = match.group(0)
                href = url if '://' in url else 'http://' + url
                return '<a href="{0}">{1}</a>'.format(href, url)
            data = re.sub(url_re, linkrepl, data)

        if replace_smilies:
            data = replace(data, self.smilies.items())

        return data
예제 #6
0
    def render(self, value, option=None, parent=None):
        href = option if option else value
        href = replace(href, bbcode_settings.BBCODE_ESCAPE_HTML)
        value = replace(value, bbcode_settings.BBCODE_ESCAPE_HTML)
        if '://' not in href and self._domain_re.match(href):
            href = 'http://' + href
        v = URLValidator()

        # Validates and renders the considered URL.
        try:
            v(href)
        except ValidationError:
            rendered = '[url={}]{}[/url]'.format(href, value) if option else \
                '[url]{}[/url]'.format(value)
        else:
            content = value if option else href
            rendered = '<a href="{}">{}</a>'.format(href, content or href)

        return rendered
예제 #7
0
    def _render_textual_content(self, data, replace_specialchars, replace_links, replace_smilies):
        """
        Given an input text, update it by replacing the HTML special characters, the links with
        their HTML corresponding tags and the smilies codes with the corresponding images.
        """
        if replace_specialchars:
            data = replace(data, self.replace_html)

        if replace_links:
            def linkrepl(match):
                url = match.group(0)
                href = url if '://' in url else 'http://' + url
                return '<a href="{0}">{1}</a>'.format(href, url)
            data = re.sub(url_re, linkrepl, data)

        if replace_smilies:
            data = replace(data, sorted(self.smilies.items(), reverse=True))

        return data
예제 #8
0
    def render(self, value, option=None, parent=None):
        href = option if option else value
        href = replace(href, bbcode_settings.BBCODE_ESCAPE_HTML)
        if '://' not in href and self._domain_re.match(href):
            href = 'http://' + href
        v = URLValidator()

        # Validates and renders the considered URL.
        try:
            v(href)
        except ValidationError:
            rendered = '[url={}]{}[/url]'.format(href, value) if option else \
                '[url]{}[/url]'.format(value)
        else:
            content = value if option else href
            rendered = '<a href="{}">{}</a>'.format(href, content or href)

        return rendered