shBrushPhp.js shBrushJScript.js shBrushSql.js shBrushXml.js ...
How to use Google Syntax Highlighter (GSH) in WordPress?
WordPress plugin was originally written by Peter Ryan and you can find the source at WordPress Plugin for Google Syntax Highlighter page. After plugin activation I analysed the HTML produced by Google Syntax Highlighter plugin and here is the result: one CSS in the <head> section and all possible brushes (a lot of them) at the page bottom. Even in the post without any program code to highlight, all brushes will be loaded anyway. So I decided to make plugin modification with the following objectives:
- load only needed brushes
- skip brushes loading if WordPress post doesn’t contain any program code
To reduce brush list immediately, find and edit google_syntax_highlighter.php from the plugins directory. Remove brushes you will never need. In my example it looks like:
gsh_brush -> Php gsh_brush -> JScript
Here goes modified google_syntax_highlighter.php code:
Brush loading is moved to the head section of the HTML page. Plugin has two hooked functions and one function for getting values from the Custom fields. If you apply this modification, you will have option to create brush list per post. I will stop here, but you can go even further and automate all this process without need to fill the Custom fields. For example, you can modify PHP function to search for all <pre name=”code” language=”PHP|JScript|Xml|…”> tags and automatically include found brushes. This method would certainly add some server load because PHP engine will have to search entire post before sending page to the client.
5 thoughts on “Google syntax highlighter”
Hi! Thanks for the information, it was very useful for me in my web log.
Your code here contains the older unmaintained version 1.5 of SyntaxHighlighter. Were you aware that it has been completely rewritten and that Alex Gorbatchev now has it’s own site? There are also many more brushes available nowadays. Have a look at my list which shows both included and elsewhere found brushes: list of known and found syntaxes for syntaxhighlighter
— Abel —
Thank you for pointing to the newer version of SyntaxHighlighter. Anyway, my main intention was to modify WordPress plugin to include only needed brushes instead of wasting bandwidth with unnecessary brushes. As I can see, today you can find 3 WordPress plugins for SyntaxHighlighter. I peeked to source of one of them and saw loading all brushes :(
Hi! .. When you say “Google SyntaxHighligter” it sounds like it’s a Google product. It’s not. It’s called “SyntaxHighlighter” and created by a guy called Alex Gorbatchev:
@Bjørn Børresen – I’m not sure but SyntaxHighligter had different name two years ago. In that time it didn’t have – dynamic brush loading – for economize bandwidth usage. So I modified WordPress plugin and specified needed brushes per post. Anyway, this post is old and it’s ready to write again. Thank you for pointing to the newest SyntaxHighligter version.