jbowens / jBBCode

A lightweight but extensible BBCode parser
http://jbbcode.com
MIT License
164 stars 32 forks source link

similey replacement & other text changes #4

Open ajorias opened 11 years ago

ajorias commented 11 years ago

i'm enjoying jbbcode :)

i just had to hack the core for my needs to do some replacements on plain text. i had to parse such plain text to put in smileys and do some automagic link creation stuff.

for such i had to change TextNode like this:

    /**
     * Returns the text string value of this text node.
     *
     * @return string
     */
    public function getValue()
    {
        // I know, it hurts ...
        if ( get_class($this->getParent()) == 'JBBCode\DocumentElement' )
        {
            return nl2br(autoLinkUsers(parseSmileys($this->value)));
        }
        else
        {
            return $this->value;
        }
    }

if there is a cleaner solution to do this i would like to know. if not it may be good option to allow such stuff via an official api.

cheers, alexander

jbowens commented 11 years ago

Hi Alexander,

It's not a perfect solution, but I recommend using a visitor. I just updated the source to allow visitors to modify the value of text nodes. I added an example visitor: https://github.com/jbowens/jBBCode/blob/master/visitors/SmileyVisitor.php

Since this involves actually modifying the text value of the node, it's not a great solution. Calls to Parser.getAsText() will yield html. I agree this is a place where the api should be improved. I'll think about it and post again later.

ajorias commented 11 years ago

thanks for the heads up! I completely missed the visitor stuff and will give it a try now that you changed the core. i agree that the solution is still not optimal (html stuff returned by getAsText()).

initially i thought i would be able to register a pseudo CodeDefinition for "plain text" so i would be able to handle non-bbcode style transformations through it.

considering that TextNode plays an integral part and that it holds data for everything and of course should make no assumption about what type its parent is (as in my hack) I tend towards a solution where the caller is aware of the context for the stuff i need here.

considering on the other side that most of the parsed text will not have any bbcode it seems inefficent in terms of resource consumption and performance to create additional instances for plain texts.

maybe the option to register a callback to "postprocess" plain (non bbcode) text would be enough?

prateem commented 10 years ago

Has there been any update about converting to smileys without having to use visitors to rewrite the actual text?