MyBB Community Forums

Full Version: auto wrap custom mybb in spoiler tag if too long
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am looking for a way to modify (what i think should be) class_parser.php

I created a custom mycode to insert our own codebox with syntax highlighting. With the is new box though there is no limit to length, so if someone posted a code 150 lines long, it will show all 150 lines. 


[python]
LONG CODE
[/python]

Admins and mods have been manually editing these posts and wrapping these tags with the spoiler tags like


[spoiler=Click here to expand]
[python]
LONG CODE
[/python]
[/spoiler]

This is our custom mycode
[attachment=37716]

and then we had to modify class_parser nl2br call to handle the pre tag for this to stop adding html break tags within this pre tag for the code. 
function my2_nl2br($string){
    $string = str_replace("\n", "<br />", $string);
    if(preg_match_all('/\<pre class="brush: python"\>(.*?)\<\/pre\>/', $string, $match)){
        foreach($match as $a){
            foreach($a as $b){
                $string = str_replace('<pre class="brush: python">'.$b.'</pre>','<pre class="brush:      python">'.str_replace("<br />", "\n", $b)."</pre>", $string);
                        }
                    }
                }

                return $string;

}

I want to wrap this in a spoiler tag if the characters within the pre tag of a class brush python is over X length (250, maybe 500 characters or so). I know i can wrap the pre tags here in this nl2br function but that would be every code box, not restricted to long codes. 
What would be the php code for adding an if condition in this function?

My pseudo code for it would be something like....
function my2_nl2br($string){
    $cutoff = 250;
    $string = str_replace("\n", "<br />", $string);
    if(preg_match_all('/\<pre class="brush: python"\>(.*?)\<\/pre\>/', $string, $match)){
        foreach($match as $a){
            foreach($a as $b){
                $string = str_replace('<pre class="brush: python">'.$b.'</pre>','<pre class="brush:      python">'.str_replace("<br />", "\n", $b)."</pre>", $string);
                if (strlen($b) > $cutoff){
                        $string = "[spoiler]" . $string . "[/spoiler]";
                    }
                }
            }
        }
            
    return $string;

}      
Im not sure how to add the spoiler bbcodes as html though, or if this would even work.