
import bbcode


def render_html(value):
    """Render text as HTML, interpreting BBcode."""
    parser = bbcode.Parser(replace_cosmetic=False)

    # 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)

    # Render quotes with optional author.
    def render_quote(name, value, options, parent, context):
        intro = ''
        if 'author' in options:
            author = options['author']
            intro = '<p class="quote-intro"><cite>{}</cite> schrieb:</p>\n' \
                .format(author)
        return '{}<blockquote>{}</blockquote>'.format(intro, value)
    parser.add_formatter('quote', render_quote, strip=True)

    return parser.format(value)


import bbcode


def create_parser():
    """Create a customized BBcode parser."""
    parser = bbcode.Parser(replace_cosmetic=False)

    # 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)

    # Render quotes with optional author.
    def render_quote(name, value, options, parent, context):
        intro = ''
        if 'author' in options:
            author = options['author']
            intro = '<p class="quote-intro"><cite>{}</cite> schrieb:</p>\n' \
                .format(author)
        return '{}<blockquote>{}</blockquote>'.format(intro, value)
    parser.add_formatter('quote', render_quote, strip=True)

    return parser


PARSER = create_parser()


def render_html(value):
    """Render text as HTML, interpreting BBcode."""
    return PARSER.format(value)

Create and reuse a single BBcode parser instance.
Create and reuse a single BBcode parser instance.

  
  import bbcode
  
  
- def render_html(value):
-     """Render text as HTML, interpreting BBcode."""
+ def create_parser():
+     """Create a customized BBcode parser."""
      parser = bbcode.Parser(replace_cosmetic=False)
  
      # 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)
  
      # Render quotes with optional author.
      def render_quote(name, value, options, parent, context):
          intro = ''
          if 'author' in options:
              author = options['author']
              intro = '<p class="quote-intro"><cite>{}</cite> schrieb:</p>\n' \
                  .format(author)
          return '{}<blockquote>{}</blockquote>'.format(intro, value)
      parser.add_formatter('quote', render_quote, strip=True)
  
-     return parser.format(value)
+     return parser
  
+ 
+ PARSER = create_parser()
+ 
+ 
+ def render_html(value):
+     """Render text as HTML, interpreting BBcode."""
+     return PARSER.format(value)
+ 
Create and reuse a single BBcode parser instance.
## Code Before:

import bbcode


def render_html(value):
    """Render text as HTML, interpreting BBcode."""
    parser = bbcode.Parser(replace_cosmetic=False)

    # 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)

    # Render quotes with optional author.
    def render_quote(name, value, options, parent, context):
        intro = ''
        if 'author' in options:
            author = options['author']
            intro = '<p class="quote-intro"><cite>{}</cite> schrieb:</p>\n' \
                .format(author)
        return '{}<blockquote>{}</blockquote>'.format(intro, value)
    parser.add_formatter('quote', render_quote, strip=True)

    return parser.format(value)

## Instruction:
Create and reuse a single BBcode parser instance.
## Code After:

import bbcode


def create_parser():
    """Create a customized BBcode parser."""
    parser = bbcode.Parser(replace_cosmetic=False)

    # 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)

    # Render quotes with optional author.
    def render_quote(name, value, options, parent, context):
        intro = ''
        if 'author' in options:
            author = options['author']
            intro = '<p class="quote-intro"><cite>{}</cite> schrieb:</p>\n' \
                .format(author)
        return '{}<blockquote>{}</blockquote>'.format(intro, value)
    parser.add_formatter('quote', render_quote, strip=True)

    return parser


PARSER = create_parser()


def render_html(value):
    """Render text as HTML, interpreting BBcode."""
    return PARSER.format(value)
