jBBCode icon indicating copy to clipboard operation
jBBCode copied to clipboard

similey replacement & other text changes

Open ajorias opened this issue 11 years ago • 3 comments

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

ajorias avatar Apr 26 '13 10:04 ajorias

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.

jbowens avatar Apr 28 '13 16:04 jbowens

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?

ajorias avatar May 02 '13 20:05 ajorias

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

prateem avatar Jan 01 '14 09:01 prateem