dcwatson / bbcode

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

Unvalid BBCode Generates HTML #24

Closed erayerdin closed 7 years ago

erayerdin commented 7 years ago

I use this module in my Django project as below:

##################
# BBCode Parsers #
##################
import bbcode
from django.core.urlresolvers import reverse

# Parser Object
platform_parser = bbcode.Parser(
    install_defaults=False,
    replace_links=False,
    drop_unrecognized=True
)

# Parser Functions
def render_refer_entry_tag(tag_name, value, options, parent, context) -> str:
    return '<a href="{url}">#{pk}</a>'.format(
        url=reverse("entry", kwargs={"pk":value}),
        pk=value
    )

def render_refer_title_tag(tag_name, value, options, parent, context) -> str:
    return '<a href="{url}">{content}</a>'.format(
        url=reverse("title", kwargs={"label": value}),
        content=value
    )

# Parsers
platform_parser.add_formatter(
    "g",
    render_refer_entry_tag,
    strip=True,
    swallow_trailing_newline=True,
    same_tag_closes=True,
    newline_closes=True
)

platform_parser.add_formatter(
    "b",
    render_refer_title_tag,
    strip=True,
    swallow_trailing_newline=True,
    same_tag_closes=True,
    newline_closes=True
)

Following generates unexpected output while it is not valid at all:

AssertionError: '<a href="/g/4332/">#4332</a>' != '[g]4332'
- <a href="/g/4332/">#4332</a>
+ [g]4332

I want it to stay same. I could not find any valid option for Parser class in documentation.

dcwatson commented 7 years ago

I don't understand - I see that you're adding custom formatters, but don't see the actual call to platform_parser.format and the expected output, so I don't know if this is a bug with the library or not.

erayerdin commented 7 years ago

I am sorry, I just put testing results. It was a bit complicated (that I should have even give a couple of sources to demonstrate), so I wanted to put it simply. To demonstrate, I call the function as this:

parser.format("[g]4332") # which does not have a closing tag and supposed to return the same thing.

Instead, it acts like the string is valid and generates:

<a href="/g/4332/">#4332</a>

This is my problem, in short.

dcwatson commented 7 years ago

Tags are implicitly closed by the end of the text, so this is the expected behavior. There's even a test for it:

https://github.com/dcwatson/bbcode/blob/master/tests.py#L14

The idea is to be somewhat forgiving about unclosed tags, tags closed in the wrong order, etc. since bbcode is normally written by humans, who make mistakes :)