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 _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)
def get_bbparser(): p = Parser() p.add_simple_formatter( "video", '<iframe width="450" height="340" src="https://www.youtube.com/embed/%(value)s?feature=player_embedded" frameborder="0" allowfullscreen></iframe>', ) p.add_simple_formatter( "img", '<img style="max-width:480px; max-height:400px;" src="%(value)s" alt="" />', replace_links=False ) return p
def _create_parser() -> Parser: """Create a customized BBcode parser.""" parser = Parser(replace_cosmetic=False) _add_image_formatter(parser) _add_quote_formatter(parser) return parser
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)
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)
def add_comment(userid, thread, threadid, threadtitle, title, content): p = Parser() p.add_simple_formatter( "video", '<object width="480" height="360"><embed src="http://www.youtube.com/v/%(value)s" type="application/x-shockwave-flash" width="480" height="360"></embed></object>', ) p.add_simple_formatter( "img", '<img style="max-width:480px; max-height:400px;" src="%(value)s" alt="" />', replace_links=False ) ts = int(time.time()) DBSession.autoflush = False c = Comments(userid, thread, threadid, threadtitle, title, p.format(content), created=ts) DBSession.add(c) DBSession.flush() try: transaction.commit() result = 1 except: DBSession.rollback() result = 0 return result
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)
def bbcode_to_html(data, *args, **kw): parser = Parser(*args, **kw) return parser.format(data)