dcwatson / bbcode

A pure python bbcode parser and formatter.
BSD 2-Clause "Simplified" License
68 stars 17 forks source link

render_embedded=False doesn't ignore opening tags #6

Closed reclosedev closed 11 years ago

reclosedev commented 11 years ago

Code with custom formater, which demonstrates the problem:

import bbcode

parser = bbcode.Parser()

def render_code(name, value, options, parent, context):
    if options:
        lang = parser._replace(options.keys()[0], parser.REPLACE_ESCAPE)
        highlight_class = "lang-%s" % lang
    else:
        highlight_class = 'no-highlight'
    return '<code class="%s">%s</code>' % (highlight_class, value)

parser.add_formatter(
    "code", render_code, 
    render_embedded=False, 
    transform_newlines=False, 
    replace_links=False,
    replace_cosmetic=False,
)

markup = """text before code:
[code python]
def test():
    print "test (c)", 'test'
    code = 123
    a = [code]
    b = 42
[/code] after code"""
print parser.format(markup)

Output:

text before code:<br /><code class="lang-python">def test():
    print &quot;test (c)&quot;, &#39;test&#39;
    code = 123
    a = [code]
    b = 42
[/code] after code</code>

Expected:

text before code:<br /><code class="lang-python">def test():
    print &quot;test (c)&quot;, &#39;test&#39;
    code = 123
    a = [code]
    b = 42
</code> after code

Postmarkup has the same issue.

dcwatson commented 11 years ago

Nice catch, this should do the trick. Let me know if it does, and I'll push an update to pypi.

reclosedev commented 11 years ago

:+1: Works great!

dcwatson commented 11 years ago

Great, just pushed version 1.0.10 to PyPI.