Beispiel #1
0
class BBCodeParser(BaseParser):
    widget_class = BBCodeWidget

    def _render_quote(self, name, value, options, parent, context):
        if options and 'quote' in options:
            origin_author_html = '<em>%s</em><br>' % options['quote']
        else:
            origin_author_html = ''
        return '<blockquote>%s%s</blockquote>' % (origin_author_html, value)

    def __init__(self):
        self._parser = Parser()
        self._parser.add_simple_formatter('img',
                                          '<img src="%(value)s">',
                                          replace_links=False)
        self._parser.add_simple_formatter('code',
                                          '<pre><code>%(value)s</code></pre>',
                                          render_embedded=False,
                                          transform_newlines=False,
                                          swallow_trailing_newline=True)
        self._parser.add_formatter('quote',
                                   self._render_quote,
                                   strip=True,
                                   swallow_trailing_newline=True)

    def format(self, text):
        return smile_it(self._parser.format(text))

    def quote(self, text, username=''):
        return '[quote="%s"]%s[/quote]\n' % (username, text)
Beispiel #2
0
class BBCodeParser(BaseParser):
    widget_class = BBCodeWidget

    def __init__(self):
        self._parser = Parser()
        self._parser.add_simple_formatter('img', '<img src="%(value)s">', replace_links=False)
        self._parser.add_simple_formatter('code', '<pre><code>%(value)s</code></pre>',
                                          render_embedded=False, transform_newlines=False,
                                          swallow_trailing_newline=True, replace_cosmetic=False)
        self._parser.add_formatter('quote', self._render_quote, strip=True, swallow_trailing_newline=True)

    def _render_quote(self, name, value, options, parent, context):
        if options and 'quote' in options:
            origin_author_html = '<em>%s</em><br>' % options['quote']
        else:
            origin_author_html = ''
        return '<blockquote>%s%s</blockquote>' % (origin_author_html, value)

    def format(self, text, instance=None):
        if instance and instance.pk:
            text = self.format_attachments(text, attachments=instance.attachments.all())
        return smile_it(self._parser.format(text))

    def quote(self, text, username=''):
        return '[quote="%s"]%s[/quote]\n' % (username, text)
Beispiel #3
0
def _add_quote_formatter(parser: Parser) -> None:
    """Render quotes with optional author."""
    def render_quote(name, value, options, parent, context):
        intro = ''
        if 'author' in options:
            author = escape(options['author'])
            intro = f'<p class="quote-intro"><cite>{author}</cite> schrieb:</p>\n'
        return f'{intro}<blockquote>{value}</blockquote>'

    parser.add_formatter('quote', render_quote, strip=True)
Beispiel #4
0
def _add_image_formatter(parser: Parser) -> None:
    """Replace image tags."""
    def render_image(name, value, options, parent, context):
        return '<img src="{}">'.format(value)

    parser.add_formatter('img', render_image, replace_links=False)