MyBB Community Forums

Full Version: hack to get syntax highlighting code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So we took awhile trying to figure out how to get a working syntax highlighter on mybb 1.8.7. There are some that are available, but none provided python syntax and/or worked with 1.8.7.
There was only one syntax highlighter that already have a working python syntax, and that was 
https://mods.mybb.com/view/mybb-syntax-highlighter-v1-1


However that mod is using SyntaxHighlighter 3.x which has some problems escaping single quotes properly, and more. So to convert it to 4.x we had to download the latest syntaxhighlighter from their github
https://github.com/syntaxhighlighter/syntaxhighlighter


However there is no mod to just install when you update to the newer version

we then replaced all the previous mod's code with that newer version
 
Then we added a new BBCode from ACP MyCode wiht reg expression as
\[python\](.*?)\[/python\]
and replacement as
<pre class="brush: python">$1</pre>

We then installed myinsertbuttons mod to allow an easy button on the sceditor to push (optional)



The next problem was that the codebox's were adding <br> tags within the codebox...which took us awhile to figure out a solution for
class_parser.php -> create this new function before class postParser line

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;

}

then replace this line
$message = nl2br($message);
with this line

$message = my2_nl2br($message);
within the same file. 


Then you will have a syntax highlighted codebox that could in theory do any of the languages provided. 

After that youll have this
[attachment=37552]

Its using syntax highlighter version 4.x so there is no problem escaping characters. And its running in mybb 1.8.7. All the original langauges work. 

There is still a problem where copying code from it to advanced editor does not work (quick editor does though)...but i think we can fix that, and will update post.
awesome I was looking for just this, any news on the advanced editor? Did u manage to get it to work?
the problem we have with the advanced editor is just that editor is a WYSIWYG editor. People have to post in code tags with Ctrl + Shift + V, or right click and select "Paste as plain text". Otherwise it will grab text formatting html and include that into the code box. We just ended up enforcing a strict read the rules policy to help people understand this
https://python-forum.io/misc.php?action=help&hid=25


Or if we could disable the WYSIWYG editor....but we havent had any luck in that.

Here is the forum thread of ours we used to discuss back and forth the process
https://python-forum.io/Thread-syntax-highlighting


note the shcode tags as the old plugin, and "code" tags we converted to "python" tags by a new mycode to get rid of the default mybb codebox and only use the new syntax highlighter codebox. We also changed the CSS of the codebox to limit vertical height. 

Here are some other minor issues we had with it and their fixes
https://python-forum.io/Thread-More-code...ax+codebox

https://python-forum.io/Thread-Syntax-Hi...ax+codebox
https://python-forum.io/Thread-URL-not-s...ax+codebox
I believe you would have to remove the line:

$messagebox = build_mycode_inserter(...)

So that it doesn't get transformed into a sceditor box and perhaps set the textarea from hidden to block depending on how your styling it, then it should look just like the quick reply one.

Anyway thanks for the update on how you sorted it out, guess I will keep playing around with it.