View Full Version : Syntax highlighting
Sarteck
02-04-2011, 09:17 PM
I am trying to change the default PHP Syntax highlighting for codes.
Looking at includes/class_bbcode.php at line 1736, I see that it uses the PHP function highlight_string() ( http://php.net/manual/en/function.highlight-string.php ).
As far as I can tell, this is supposed to get the values from my php.ini:
highlight.string = #DD0000
highlight.comment = #FF3399
highlight.keyword = #007700
highlight.bg = #FFFFFF
highlight.default = #4455DD
highlight.html = #024FB7
Those ones in particular. Just to see what is what and if it was working like I thought, I changed the Comment value from the default orange to a bright purple.
Next, I restarted my php-fastcgi.
I thought the changes would take, but when I loaded (even with a hard refresh) a page with posts that have PHP code, nothing was changed.
The reason I believe this might be a vB problem is because I then made a test file that used the highlight_string() function, and is spat out the comments as an ugly bright purple.
However, I cannot see how it could be a vB problem, since it uses the exact same function to retrieve the INI values. The only thing I can think of is, "is there somewhere in the vB files that sets those INI highlight settings?"
If that's the case, then it would make sense--the code-set INI settings would replace anything in my INI file, correct?
Could anyone tell me if that's the case?
Alternatively, is there a Mod out for syntax highlighting that you would recommend?
al3bed
02-04-2011, 10:38 PM
vb uses highlight_string function can be found in class_bbcode.php
you may need to Shift F5 refresh or to add php.ini inside forum directory
ForumsMods
02-04-2011, 11:02 PM
Be sure that options are not commented (by default) in php.ini
Take note if you are using post cache, you will see changes after rebuild it.
Try to post a new PHP Code.
Sarteck
02-05-2011, 02:15 AM
@al3bed:
Yeah, that's what I said. X3 And yeah, I also said I hard-refreshed (Ctrl+Shift+R in FireFox, loads page ignoring cache). Adding a php.ini file inside the forum directory shouldn't do anything--my webserver is set to use only one php.ini file, after all. XD
@ForumsMods:
No, the options are uncommented (semicolon removed from the start of the line). As I said, the changes did take in the test PHP file, but did not take in the vBulletin forum.
I'm not sure if I am using post cache. Where can I find out about that?
--------------- Added 1296890277 at 1296890277 ---------------
Solved. Kind of.
First, I downloaded GeSHi.
http://qbnz.com/highlighter/
I uploaded the geshi folder to my includes directory, along with the geshi.php file.
I created a custom BBCode, PHPS.
I added a new plugin at bbcode_create with the following:
$this->tag_list['no_option']['phps'] = array ();
$this->tag_list['no_option']['phps']['callback'] = 'handle_external';
$this->tag_list['no_option']['phps']['external_callback'] = 'handle_geshi_php';
if (!function_exists ('handle_geshi_php'))
{
function handle_geshi_php(&$theobj, &$value, &$option)
{
require_once(DIR . '/includes/geshi.php');
$geshi = new GeSHi($value, 'php');
return '<div class="bbcode_container"><div class="bbcode_description">PHP Code:</div><pre style="height:300px;" class="bbcode_code">'.$geshi->parse_code().'</pre></div>';
}
}
I'll likely use this for JavaScript and several things that vBulletin does not support by default.
I recommend it to you other Admins that need syntax highlighting, too. :>
--------------- Added 1296893984 at 1296893984 ---------------
THough it was "solved" above, I learned that I was having trouble with some HTML entities.
Below is the complete PHP for the bbcode_create hook which eliminates all those problems.
$this->tag_list['no_option']['phps'] = array ();
$this->tag_list['no_option']['phps']['callback'] = 'handle_external';
$this->tag_list['no_option']['phps']['external_callback'] = 'handle_geshi_php';
if (!function_exists ('handle_geshi_php'))
{
function handle_geshi_php(&$theobj, &$value, &$option)
{
require_once(DIR . '/includes/geshi.php');
if (!is_array($codefind1))
{
$codefind1 = array('<br>','<br />');
$codereplace1 = array('','');
$codefind2 = array('>','<','"','&','[',']',);
$codereplace2 = array('>','<','"','&','[',']',);
}
$code = rtrim(str_replace($codefind1, $codereplace1, $value));
$code = str_replace($codefind2, $codereplace2, $code); // finish replacements
$geshi = new GeSHi($code, 'php');
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
$code = $geshi->parse_code();
$code = preg_replace('/&#([0-9]+);/', '&#$1;', $code);
$code = str_replace(array('[', ']'), array('[', ']'), $code);
return '<div class="bbcode_container"><div class="bbcode_description">PHP Code:</div><div style="height: 300px;" class="bbcode_code">'.$code.'</div></div>';
}
}
Yay
CKahlhoefer
02-22-2011, 08:25 PM
thanks for this code snippet!
is it my configuration or does the output only has 2 digits for the line numbers?
e.g. 98,99,00,01
Dirk139
04-08-2011, 06:27 AM
Hi Sarteck,
in january I updatet my 3.6.4 vbulletin to version 4, so the geshi plugin I used does'nt work any longer.
Cause there is no plugin for version 4 available, I think so, I tried to getting geshi work by myself. I added code similar to your code to class_bbcode.php. Geshi works now, your solution is pretty better!.
I've one problem: When I edit the post, the tags (I use ist for C: Tag is Code_C) are gone, and the code is highlighted. Do you have an idea, how I can solve this problem?
Another question. It seems that there is an new Geshi plugin available, today I've found this Example: http://www.rigsofrods.com/threads/74105-GeSHi-Generic-Syntax-Highlighter-Demo
Do you have any information about a new plugin for vbulletin 4?
regards,
Dirk
Sarteck
05-22-2011, 10:36 AM
@CKahlhoefer, it seems that the full line numbers ARE actually there, it's just that the styling for the list item prevents their display. XD I'll have to take a deeper look at it to find out what it is taht should be changed.
I actually tried testing it our using my 4,756-line geshi.php config file as the test. WARNING! DON'T DO THAT! XD It seems that such a huge code snippet generates so many different HTML tags that it will slow your browser down immensely. Might even crash it, depending on how much is there, heh.
With that in mind, it might be a good idea to think about limiting the amount of lines that can be used in the tag, otherwise some users could really spam up some of your threads, while making it nearly impossible to load them in order to get rid of the offending posts. XP
When I do check out which CSS styling crap has to be adjusted to show more than two digits, I'll also probably include an adjustable limit to how many lines can be displayed in a single post. I'll have to sit and think about how to go about that, though, since I also have to consider that more than one tag could be submitted within a single post.
@Dirk139, I'm not sure what you mean by "the tags are gone and the code is highlighted." If you happen to drop by this thread again, could you explain a little further, and maybe give an example?
And nah, I have no idea about any GeSHi plugins for vB. If I run into any that are more-or-less "professionally" done (unlike my little snippet), I'll send ya a PM.
Disasterpiece
05-22-2011, 02:23 PM
Maybe too late for this now, but did you re-new the post-cache after modifying the php color output?
Sarteck
05-22-2011, 09:02 PM
@Disasterpiece, no, I did not. XD I later learned that was my issue, too. (Sorry, had thought I mentioned that, but suppose not.)
Disasterpiece
05-22-2011, 09:10 PM
I use geshi for parsing unreal script files, so it's possible to make it work, but really a stretch because I didn't get it to work with the extended editor. But it works...
If you only plan to use it for parsing php or html files, stay with the default syntax highlighting...
Sarteck
05-22-2011, 11:27 PM
Oh, I've got GeSHi working fine for PHP, HTML, CSS, Java, Tcl, JavaScript, C++, and SQL. I'll probably add some other languages, too, since some of my members are requesting them.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.