2017-09-28, 07:24 PM
(This post was last modified: 2017-09-28, 07:46 PM by A Future Pilot.)
Hello everyone!
I recently migrated my forum from SMF to MyBB, and I quickly discovered the default [ code ] tags are lacking some functionality. Here's a list of things I needed:
So, if you're in a similar boat as me, and need any or all of this functionality, here's what you do:
1. Allow [ code=Something ] tags where "Something" becomes the title of the code block
Replacement:
2. Preserve whitespace inside [ code ] tags
3. Don't add extra <br>s inside [ code ] tags
If you've done #2, you'll see that #3 is a problem. The following info is taken from a post by metulburr (https://community.mybb.com/thread-205148...pid1249392) so thanks to him!!
To fix this, we'll have to edit a core MyBB file, so be careful!
Now find the line:
and replace it with:
You're all done with #3
If you're not worried about #4, you can just save the file and re-upload it. Otherwise, continue on.
4. Don't convert tabs to spaces inside [ code ] tags
For #4 you'll need to edit class_parser.php again.
Find the lines:
And comment them out like so (note the double slashes at the beginning of each line):
You're now all done with #4. Just save class_parser.php and re-upload it.
If you have any questions just let me know!
I recently migrated my forum from SMF to MyBB, and I quickly discovered the default [ code ] tags are lacking some functionality. Here's a list of things I needed:
- Allow [ code=Something ] tags where "Something" would become the title of the code block.
- Preserve whitespace inside [ code ] tags
- Don't add extra <br>s inside [ code ] tags
- Don't convert tabs to spaces inside [ code ] tags
So, if you're in a similar boat as me, and need any or all of this functionality, here's what you do:
1. Allow [ code=Something ] tags where "Something" becomes the title of the code block
- Go to Admin CP -> Configuration -> MyCode (on the left sidebar).
- Click "Add new MyCode".
- Title it whatever you want (I used "Improved code tags"), and set the following:
\[code\=(.*?)\](?|\n(.*?)\n|\n(.*?)|(.*?)\n|(.*?))\[\/code\]
Replacement:
<div class="codeblock">
<div class="title">$1:</div>
<div class="body" dir="ltr"><code>$2</code></div>
</div>
- Under "Enabled" select "Yes" then click Save. You're all done with #1.
2. Preserve whitespace inside [ code ] tags
- Go to Admin CP -> Templates and Styles and click on your theme.
- At the top click "Add Stylesheet".
- Give it a name (I used "custom.css").
- Select "Globally" under "Attatched to" and click "Write my own content".
- Paste the following into the box:
.codeblock code {
white-space: pre;
}
- Click save.
- Go back to your theme and make sure that your new css file has the highest order
- Save the stylesheet ordering. You've now finished #2.
3. Don't add extra <br>s inside [ code ] tags
If you've done #2, you'll see that #3 is a problem. The following info is taken from a post by metulburr (https://community.mybb.com/thread-205148...pid1249392) so thanks to him!!
To fix this, we'll have to edit a core MyBB file, so be careful!
- Using the FTP client of your choice, go to your main MyBB directory, then to the "inc" subfolder, and edit the file "class_parser.php".
- At the top of the file you'll see "<?php" and underneath that you'll see a bunch of comments (inside /* */ symbols). After the comments, and above "class PostParser" (or whatever the first bit of code is) paste the following code:
function my_nl2br($string){
$string = str_replace("\n", "<br />", $string);
if(preg_match_all('/\<code\>(.*?)\<\/code\>/', $string, $match)){
foreach($match as $a){
foreach($a as $b){
$string = str_replace('<code>'.$b.'</code>', "<code>".str_replace("<br />", "\n", $b)."</code>", $string);
}
}
}
return $string;
}
Now find the line:
$message = nl2br($message);
and replace it with:
$message = my_nl2br($message);
You're all done with #3
If you're not worried about #4, you can just save the file and re-upload it. Otherwise, continue on.
4. Don't convert tabs to spaces inside [ code ] tags
For #4 you'll need to edit class_parser.php again.
Find the lines:
$code = str_replace("\t", ' ', $code);
$code = str_replace(" ", ' ', $code);
And comment them out like so (note the double slashes at the beginning of each line):
//$code = str_replace("\t", ' ', $code);
//$code = str_replace(" ", ' ', $code);
You're now all done with #4. Just save class_parser.php and re-upload it.
If you have any questions just let me know!