.
The
header is the default. If you're familiar with HTML you'll know that whitespace is rendered "as is" by a element. The advantage for you is that if you use the whitespace you use will appear pretty much exactly how it is in the source, and what's more GeSHi won't have to add a whole lot of
's and non-breaking spaces ( ) to your code to indent it. This saves you source code (and your valuable visitors waiting time and your bandwidth).
But if you don't like or it looks stupid in your browser no matter what styles you try to apply to it or something similar, you might want to use a instead. A
will result in more source - GeSHi will have to insert whitespace markup - but in return you can wrap long lines of code that would otherwise have your browser's horizontal scrollbar appear. Of course with
you can *not* wrap lines if you please. The highlighter demo at the GeSHi home page uses the
approach for this reason.
At this stage there isn't an option to wrap the code in
tags (unless you use the function geshi_highlight), partly because of the inconsistent and unexpected ways stuff in tags is highlighted. Besides, is an inline element. But this may become an option in future versions.
As of GeSHi 1.0.7.2 there is a new header type, that specifies that the code should not be wrapped in anything at all.
Another requested addition has been made in GeSHi 1.0.7.20 to force GeSHi to create a block around the highlighted source even if this wasn't necessary, thus styles that are applied to the output of GeSHi can directly influence the code only even if headers and footers are present.
To change/set the header to use, you call the set_header_type() method:
$geshi->set_header_type(GESHI_HEADER_DIV);
// or...
$geshi->set_header_type(GESHI_HEADER_PRE); // or...
$geshi->set_header_type(GESHI_HEADER_NONE);
Those are the only three arguments you should pass to set_header_type. Passing anything else may cause inconsistencies in what is used as the Code Container (although it *should* simply use a ). Better not to risk it.
Note:
GESHI_HEADER_DIV, GESHI_HEADER_PRE and GESHI_HEADER_NONE are constants, so don't put them in strings!
Caution:
The default styles for the and will be different, especially if you use line numbers!. I have found that a
results in code that is smaller than for that of a , you should rectify this difference by using set_overall_style() if you need to. But be aware of this difference for if you are changing the header type!
3.2: Line Numbers
Top | Contents | Next | Previous
GeSHi has the ability to add line numbers to your code (see the demo available at http://qbnz.com/highlighter/demo.php to see what can be achieved). Line numbers are a great way to make your code look professional, especially if you use the fancy line numbers feature.
3.2.1: Enabling Line Numbers
Top | Contents | Next | Previous
To highlight a source with line numbers, you call the enable_line_numbers() method:
$geshi->enable_line_numbers($flag);
Where $flag is one of the following:
* GESHI_NORMAL_LINE_NUMBERS - Use normal line numbering
* GESHI_FANCY_LINE_NUMBERS - Use fancy line numbering
* GESHI_NO_LINE_NUMBERS - Disable line numbers (default)
Normal line numbers means you specify a style for them, and that style gets applied to all of them. Fancy line numbers means that you can specify a different style for each nth line number. You change the value of n (default 5):
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 37);
The second parameter is not used in any other mode. Setting it to 0 is the same as simply using normal line numbers. Setting it to 1 applies the fancy style to every line number.
Note:
The values above are CONSTANTS - so don't put them in strings!
3.2.2 Styling Line Numbers
Top | Contents | Next | Previous
As of GeSHi 1.0.2, line numbers are added by the use of ordered lists. This solves the old issues of line number styles inheriting from styles meant for the code. Also, this solves an important issue about selecting code. For example, line numbers look nice, but when you go to select the code in your browser to copy it? You got the line numbers too! Not such a good thing, but thankfully this issue is now solved. What is the price? Unfortunately the whole way that styles are inherited/used has changed for those of you who were familiar with 1.0.1, and there is quite a bit more HTML involved. So think carefully about these things before you enable line numbers.
Now, onto how to style line numbers:
Styles are set for line numbers using the set_line_style() method:
$geshi->set_line_style('background: #fcfcfc;');
If you're using Fancy Line Numbers mode, you pass a second string for the style of the nth line number:
$geshi->set_line_style('background: #fcfcfc;', 'background: #f0f0f0;');
The second style will have no effect if you're not using Fancy Line Numbers mode.
By default, the styles you pass overwrite the current styles. Add a boolean "true" after the styles you specify to combine them with the current styles:
$geshi->set_line_style('background: red;', true);
// or, for fancy line numbers
$geshi->set_line_style('background: red;', 'background: blue;', true);
Note:
Due to a bug with Firefox the issue that should have been fixed with 1.0.2 has reappeared in another form as Firefox includes extra text\markup into plaintext versions of webpage copies. This can sometimes be useful (actually it's used to get the plaintext version of this documentation), but more often is quite annoying. Best practice so far is to either not use line numbers, or offer the visitor of your page a plaintext version of your source. To learn more have a look at the SF.net BugTracker Issue #1651996. This will hopefully be fixed in GeSHi version 1.2 or as soon as Firefox provides webdevelopers with adequate ways to control this feature - whichever comes first!
Caution:
When you set line number styles, the code will inherit those styles! This is the main issue to come out of the 1.0.2 release. If you want your code to be styled in a predictable manner, you'll have to call the set_code_style() method to rectify this problem.
Note also that you cannot apply background colours to line numbers unless you use set_overall_style(). Here's how you'd style:
1. Use set_overall_style() to style the overall code block. For example, you can set the border style/colour, any margins and padding etc. using this method. In addition: set the background colour for all the line numbers using this method.
2. Use set_line_style() to style the foreground of the line numbers. For example, you can set the colour, weight, font, padding etc. of the line numbers using this method.
3. Use set_code_style() to explicitly override the styles you set for line numbers using set_line_style. For example, if you'd set the line numbers to be bold (or even if you'd only set the fancy line number style to be bold), and you didn't actually want your code to be bold, you'd make sure that font-weight: normal; was in the stylesheet rule you passed to set_code_style
This is the one major change from GeSHi 1.0.1 - make sure you become familiar with this, and make sure that you check any code you have already styled with 1.0.1 when you upgrade to make sure nothing bad happens to it.
3.2.3: Choosing a Start Number
Top | Contents | Next | Previous
As of GeSHi 1.0.2, you can now make the line numbers start at any number, rather than just 1. This feature is useful if you're highlighting code from a file from around a certain line number in that file, as an additional guide to those who will view the code. You set the line numbers by calling the start_line_numbers_at() method:
$geshi->start_line_numbers_at($number);
$number must be a positive integer (or zero). If it is not, GeSHi will convert it anyway.
If you have not enabled line numbers, this will have no effect.
Caution:
Although I'd like GeSHi to have XHTML strict compliance, this feature will break compliancy (however transitional compliancy remains). This is because the only widely supported way to change the start value for line numbers is by using the start="number" attribute of the
tag. Although CSS does provide a mechanism for doing this, it is only supported in Opera versions 7.5 and above (not even Firefox supports this).
3.3: Using CSS Classes
Top | Contents | Next | Previous
Using CSS to highlight your code instead of in-lining the styles is a definate bonus. Not only is it more compliant (the w3c is deprecating the style attribute in XHTML 2.0) but it results in far less outputted code - up to a whopping 90% saving - which makes a *huge* difference to those unlucky of us on modems!
3.3.1: Enabling CSS Classes
Top | Contents | Next | Previous
By default, GeSHi doesn't use the classes, so it's easy just to whack out some highlighted code if you need without worrying about stylesheets. However, if you're a bit more organised about it, you should use the classes ;). To turn the use of classes on, you call the enable_classes() method:
$geshi->enable_classes();
If you want to turn classes OFF for some reason later:
$geshi->enable_classes(false);
If classes are enabled when parse_code() is called, then the resultant source will use CSS classes in the output, otherwise it will in-line the styles. The advantages of using classes are great - the reduction in source will be very noticeable, and what's more you can use one stylesheet for several different highlights on the same page. In fact, you can even use an external stylesheet and link to that, saving even more time and source (because stylesheets are cached by browsers).
Note:
There have been problems with inline styles and the Symbol Highlighting added in 1.0.7.21. If you can you should therefore turn CSS classes ON to avoid those issues.
Caution:
This should be the very first method you call after creating a new GeSHi object! That way, various other methods can act upon your choice to use classes correctly. In theory, you could call this method just before parsing the code, but this may result in unexpected behaviour.
3.3.2: Setting the CSS class and ID
Top | Contents | Next | Previous
You can set an overall CSS class and id for the code. This is a good feature that allows you to use the same stylesheet for many different snippets of code. You call set_overall_class() and set_overall_id to accomplish this:
$geshi->set_overall_class('mycode');
$geshi->set_overall_id('dk48ck');
The default classname is the name of the language being used. This means you can use just the one stylesheet for all sources that use the same language, and incidentally means that you probably won't have to call these methods too often.
CSS IDs are supposed to be unique, and you should use them as such. Basically, you can specify an ID for your code and then use that ID to highlight that code in a unique way. You'd do this for a block of code that you expressly wanted to be highlighted in a different way (see the section below on gettting the stylesheet for your code for an example).
3.3.3: Getting the stylesheet for your code
Top | Contents | Next | Previous
The other half of using CSS classes is getting the stylesheet for use with the classes. GeSHi makes it very easy to get a stylesheet for your code, with one easy method call:
$geshi->enable_classes();
// Here we have code that will spit out a header for
// a stylesheet. For example:
echo '
Code
';
The get_stylesheet() method gets the stylesheet for your code in one easy call. All you need to do is output it in the correct place. As you can also see, you don't even have to enable class usage to get the stylesheet nessecary either - however not enabling classes but using the stylesheet may result in problems later.
By default, get_stylesheet() tries to echo the least amount of code possible. Although currently it doesn't check to see if a certain lexic is even in the source, you can expect this feature in the future. At least for the present however, if you explicitly disable the highlighting of a certain lexic, or disable line numbers, the related CSS will not be outputted. This may be a bad thing for you perhaps you're going to use the stylesheet for many blocks of code, some with line numbers, others with some lexic enabled where this source has it disabled. Or perhaps you're building an external stylesheet and want all lexics included. So to get around this problem, you do this:
$geshi->get_stylesheet(false);
This turns economy mode off, and all of the stylesheet will be outputted regardless.
Now lets say you have several snippets of code, using the same language. In most of them you don't mind if they're highlighted the same way (in fact, that's exactly what you want) but in one of them you'd like the source to be highlighted differently. Here's how you can do that:
// assume path is the default "geshi/" relative to the current directory
$geshi1 = new GeSHi($source1, $lang);
$geshi2 = new GeSHi($source2, $lang);
$geshi3 = new GeSHi($source3, $lang);
// Turn classes on for all sources
$geshi1->enable_classes();
$geshi2->enable_classes();
$geshi3->enable_classes();
// Make $geshi3 unique
$geshi3->set_overall_id('different');
//
// Methods are called on $geshi3 to change styles...
//
echo '
Code
';
echo 'Code snippet 1:';
echo $geshi1->parse_code();
echo 'Code snippet 2 (same highlighting as 1):';
echo $geshi2->parse_code();
echo 'Code snippet 3 (DIFFERENT highlighting):';
echo $geshi3->parse_code();
echo '';
Before version 1.0.2, you needed to set the class of the code you wanted to be unique to the empty string. This limitation has been removed in version 1.0.2 - if you set the ID of a block of code, all styling will be done based on that ID alone.
3.3.4: Using an External Stylesheet
Top | Contents | Next | Previous
An external stylesheet can reduce even more the amount of code needed to highlight some source. However there are some drawbacks with this. To use an external stylesheet, it's up to you to link it in to your document, normally with the following HTML:
In your external stylesheet you put CSS declarations for your code. Then just make sure you're using the correct class (use set_overall_class() to ensure this) and this should work fine.
This method is great if you don't mind the source always being highlighted the same (in particular, if you're making a plugin for a forum/wiki/other system, using an external stylesheet is a good idea!). It saves a small amount of code and your bandwidth, and it's relatively easy to just change the stylesheet should you need to. However, using this will render the methods that change the styles of the code useless, because of course the stylesheet is no longer being dynamically generated. You can still disable highlighting of certain lexics dynamically, however.
Note:
As of version 1.0.2, GeSHi comes with a contrib/ directory, which in it contains a "wizard" script for creating a stylesheet. Although this script is by no means a complete solution, it will create the necessary rules for the basic lexics - comments, strings for example. Things not included in the wizard include regular expressions for any language that uses them (PHP and XML are two languages that use them), and keyword-link styles. However, this script should take some of the tedium out of the job of making an external stylesheet. Expect a much better version of this script in version 1.2!
3.4: Changing Styles
Top | Contents | Next | Previous
One of the more powerful features of GeSHi is the ability to change the style of the output dynamically. Why be chained to the boring styles the language authors make up? You can change almost every single aspect of highlighted code - and can even say whether something is to be highlighted at all.
If you're confused about "styles", you probably want to have a quick tutorial in them so you know what you can do with them. Checkout the homepage of CSS at http://www.w3.org/Style/CSS.
3.4.1: The Overall Styles
Top | Contents | Next | Previous
The code outputted by GeSHi is either in a or a
(see the section entitled "The Code Container"), and this can be styled.
$geshi->set_overall_style('... styles ...');
Where styles is a string containing valid CSS declarations. By default, these styles overwrite the current styles, but you can change this by adding a second parameter:
$geshi->set_overall_style('color: blue;', true);
The default styles "shine through" wherever anything isn't highlighted. Also, you can apply more advanced styles, like position: (fixed|relative) etc, because a /
is a block level element.
Note:
Remember that a will by default have a larger font size than a
, as discussed in the section "The Code Container".
3.4.2: Line Number Styles
Top | Contents | Next | Previous
You may wish to refer to the section Styling Line Numbers before reading this section.
As of version 1.0.2, the way line numbers are generated is different, so therefore the way that they are styled is different. In particular, now you cannot set the background style of the fancy line numbers to be different from that of the normal line numbers.
Line number styles are set by using the method set_line_style:
$geshi->set_line_style($style1, $style2);
$style1 is the style of the line numbers by default, and $style2 is the style of the fancy line numbers.
Caution:
Things have changed since 1.0.1! This note is very important - please make sure you check this twice before complaining about line numbers!
Because of the way that ordered lists are done in HTML, there really isn't normally a way to style the actual numbers in the list. I've cheated somewhat with GeSHi - I've made it possible to use CSS to style the foreground of the line numbers. So therefore, you can change the color, font size and type, and padding on them. If you want to have a pretty background, you must use set_overall_style() to do this, and use set_code_style() to style the actual code! This is explained in the section above: Styling Line Numbers.
In addition, the styles for fancy line numbers is now the difference between the normal styles and the styles you want to achieve. For example, in GeSHi prior to 1.0.2 you may have done this to style line numbers:
$geshi->set_line_style('color: red; font-weight: bold;', 'color: green; font-weight: bold');
Now you instead can do this:
$geshi->set_line_style('color: red; font-weight: bold;', 'color: green;');
The font-weight: bold; will automatically carry through to the fancy styles. This is actually a small saving in code - but the difference may be confusing for anyone using 1.0.1 at first.
3.4.3: Setting Keyword Styles
Top | Contents | Next | Previous
Perhaps the most regular change you will make will be to the styles of a keyword set. In order to change the styles for a particular set, you'll have to know what the set is called first. Sets are numbered from 1 up. Typically, set 1 contains keywords like if, while, do, for, switch etc, set 2 contains null, false, true etc, set 3 contains function inbuilt into the language (echo, htmlspecialchars etc. in PHP) and set 4 contains data types and similar variable modifiers: int, double, real, static etc. However these things are not fixed, and you should check the language file to see what key you want. Having a familiarity with a language file is definately a plus for using it.
To change the styles for a keyword set, call the set_keyword_group_style() method:
$geshi->set_keyword_group_style($group, $styles);
Where $group is the group to change the styles for and $styles is a string containing the styles to apply to that group.
By default, the styles you pass overwrite the current styles. Add a boolean true after the styles you specify to combine them with the current styles:
$geshi->set_keyword_group_style(3, 'color: white;', true);
3.4.4: Setting Comment Styles
Top | Contents | Next | Previous
To change the styles for a comment group, call the set_comments_style() method:
$geshi->set_comments_style($group, $styles);
Where $group is either a number corresponding to a single-line comment, or the string 'MULTI' to specify multiline comments:
$geshi->set_comments_style(1, 'font-style: italic;');
$geshi->set_comments_style('MULTI', 'display: hidden;');
By default, the styles you pass overwrite the current styles. Add a boolean true after the styles you specify to combine them with the current styles:
$geshi->set_comments_style(1, 'font-weight: 100;', true);
Note:
In 1.0.7.22 a new kind of Comments called "COMMENT_REGEXP" has been added. Those are handled by setting single line comment styles.
3.4.5: Setting Other Styles
Top | Contents | Next | Previous
GeSHi can highlight many other aspects of your source other than just keywords and comments. Strings, Numbers, Methods and Brackets among other things can all also be highlighted. Here are the related methods:
$geshi->set_escape_characters_style($styles[, $preserve_defaults]);
$geshi->set_symbols_style($styles[, $preserve_defaults]);
$geshi->set_strings_style($styles[, $preserve_defaults]);
$geshi->set_numbers_style($styles[, $preserve_defaults]);
$geshi->set_methods_style($key, $styles[, $preserve_defaults]);
$geshi->set_regexps_style($key, $styles[, $preserve_defaults]);
$styles is a string containing valid stylesheet declarations, while $preserve_defaults should be set to true if you want your styles to be merged with the previous styles. In the case of set_methods_style, you should select a group to set the styles of, check the language files for the number used for each "object splitter".
Like this was possible for set_method_style a new parameter has been introduced for set_symbols_style too which allows you to select the group of symbols for which you'd like to change your style. $geshi->set_symbols_style($styles[, $preserve_defaults[, $group]]);
If the third parameter is not given, group 0 is assumed. Furthermore you should note that any changes to group 0 are also reflected in the bracket style, i.e. a pass-through call to set_bracket_style is made.
3.5: Case Sensitivity and Auto Casing
Top | Contents | Next | Previous
Controlling the case of the outputted source is an easy job with GeSHi. You can control which keywords are converted in case, and also control whether keywords are checked in a case sensitive manner.
3.5.1: Auto-Caps/Nocaps
Top | Contents | Next | Previous
Auto-Caps/Nocaps is a nifty little feature that capitalises or lowercases automatically certain lexics when they are styled. I dabble in QuickBASIC, a dialect of BASIC which is well known for it's capatalisation, and SQL is another language well known for using caps for readability.
To change what case lexics are rendered in, you call the set_case_keywords() method:
$geshi->set_case_keywords($caps_modifier);
The valid values to pass to this method are:
* GESHI_CAPS_NO_CHANGE - Don't change the case of any lexics, leave as they are found
* GESHI_CAPS_UPPER - Uppercase all lexics found
* GESHI_CAPS_LOWER - Lowercase all lexics found
Caution:
When I say "lexic", I mean "keywords". Any keyword in any keyword array will be modified using this option! This is one small area of inflexibility I hope to fix in 1.2.X.
I suspect this will only be used to specify GESHI_CAPS_NO_CHANGE to turn off autocaps for languages like SQL and BASIC variants, like so:
$geshi = new GeSHi($source, 'sql');
$geshi->set_case_keywords(GESHI_CAPS_NO_CHANGE); // don't want keywords capatalised
All the same, it can be used for some interesting effects:
$geshi = new GeSHi($source, 'java');
// Anyone who's used java knows how picky it is about CapitalLetters...
$geshi->set_case_keywords(GESHI_CAPS_LOWER);
// No *way* the source will look right now ;)
3.5.2: Setting Case Sensitivity
Top | Contents | Next | Previous
Some languages, like PHP, don't mind what case function names and keywords are in, while others, like Java, depend on such pickiness to maintain their bad reputations ;). In any event, you can use the set_case_sensitivity to change the case sensitiveness of a particular keyword group from the default:
$geshi->set_case_sensitivity($key, $sensitivity);
Where $key is the key of the group for which you wish to change case sensitivness for (see the language file for that language), and $sensitivity is a boolean value - true if the keyword is case sensitive, and false if not.
3.6: Changing the Source, Language, Config Options
Top | Contents | Next | Previous
What happens if you want to change the source to be highlighted on the fly, or the language. Or if you want to specify any of those basic fields after you've created a GeSHi object? Well, that's where these methods come in.
3.6.1: Changing the Source Code
Top | Contents | Next | Previous
To change the source code, you call the set_source() method:
$geshi->set_source($newsource);
Example:
$geshi = new GeSHi($source1, 'php');
// Method calls to specify various options...
$code1 = $geshi->parse_code();
$geshi->set_source($source2);
$code2 = $geshi->parse_code();
3.6.2: Changing the Language
Top | Contents | Next | Previous
What happens if you want to change the language used for highlighting? Just call set_language():
$geshi->set_language('newlanguage');
Example:
$geshi = new GeSHi($source, 'php');
$code = $geshi->parse_code();
// Highlight GeSHi's output
$geshi->set_source($code);
$geshi->set_language('html4strict');
$geshi->enable_classes(false);
echo $geshi->parse_code();
Note:
Names are case-insensitive - they will be converted to lower case to match a language file however. So if you're making a language file, remember it should have a name in lower case.
Note:
What you pass to this method is the name of a language file, minus the .php extension. If you're writing a plugin for a particular application, it's up to you to somehow convert user input into a valid language name.
Caution:
GeSHi include()s the language file, so be careful to make sure that users can't pass some wierd language name to include any old script! GeSHi tries to strip non-valid characters out of a language name, but you should always do this your self anyway. In particular, language files are always lower-case, with either alphanumeric characters, dashes or underscores in their name.
At the very least, strip "/" characters out of a language name.
3.6.3: Changing the Language Path
Top | Contents | Next | Previous
What happens if all of a sudden you want to use language files from a different directory from the current language file location? You call the set_language_path() method:
$geshi->set_language_path($newpath);
It doesn't matter whether the path has a trailing slash after it or not - only that it points to a valid folder. If it doesn't, that's your tough luck ;)
3.6.4: Changing the Character Set
Top | Contents | Next | Previous
Note:
As of GeSHi 1.0.7.18, you don't need to use this, as htmlspecialchars is not being used due to a security flaw in it (that is unpatched in even the most recent PHP4 versions, and in PHP5 < 5.2). As long as you set the encoding properly with a php header() call, your foreign characters will be displayed correctly.
As of version 1.0.3, you can use the method set_encoding to specify the character set that your source is in. Valid names are those names that are valid for the PHP function htmlentities():
$geshi->set_encoding($encoding);
There is a table of valid strings for $encoding at the php.net manual linked to above. If you do not specify an encoding, or specify an invalid encoding, the character set used is ISO-8859-1.
Using load_from_file to Change the Language and Source Code
Top | Contents | Next | Previous
As of GeSHi 1.0.5, you can use the method load_from_file to load the source code and language from a file. Simply pass this method a file name and it will attempt to load the source and set the language.
$geshi->load_from_file($file_name, $lookup);
$file_name is the file name to use, and $lookup is an optional parameter that contains a lookup array to use for deciding which language to choose. You can use this to override GeSHi's default lookup array, which may not contain the extension of the file you're after, or perhaps does have your extension but under a different language. The lookup array is of the form:
array(
* 'lang_name' => array('extension', 'extension', ...),
* 'lang_name' ...
* );
Also, you can use the method get_language_name_from_extension if you need to convert a file extension to a valid language name. This method will return the empty string if it could not find a match in the lookup, and like load_from_file it accepts an optional second parameter that contains a lookup array.
3.7: Error Handling
Top | Contents | Next | Previous
What happens if you try to highlight using a language that doesn't exist? Or if GeSHi can't read a required file? The results you get may be confusing. You may check your code over and over, and never find anything wrong. GeSHi provides ways of finding out if GeSHi itself found anything wrong with what you tried to do. After highlighting, you can call the error() method:
$geshi = new GeSHi('hi', 'thisLangIsNotSupported');
echo $geshi->error(); // echoes error message
The error message you will get will look like this:
GeSHi Error: GeSHi could not find the language thisLangIsNotSupported (using path geshi/) (code 2)
The error outputted will be the last error GeSHi came across, just like how mysql_error() works.
3.8: Disabling styling of some Lexics
Top | Contents | Next | Previous
One disadvantage of GeSHi is that for large source files using complex languages, it can be quite slow with every option turned on. Although future releases will concentrate on the speed/resource side of highlighting, for now you can gain speed increases by disabling some of the highlighting options. This is done by using a series of set_*_highlighting methods:
* set_keyword_group_highlighting($group, $flag): Sets whether a particular $group of keywords is to be highlighted or not. Consult the necessary language file(s) to see what $group should be for each group (typically a positive integer). $flag is false if you want to disable highlighting of this group, and true if you want to re-enable higlighting of this group. If you disable a keyword group then even if the keyword group has a related URL one will not be generated for that keyword.
* set_comments_highlighting($group, $flag): Sets whether a particular $group of comments is to be highlighted or not. Consult the necessary language file(s) to see what $group should be for each group (typically a positive integer, or the string 'MULTI' for multiline comments. $flag is false if you want to disable highlighting of this group, and true if you want to re-enable highlighting of this group.
* set_regexps_highlighting($regexp, $flag): Sets whether a particular $regexp is to be highlighted or not. Consult the necessary language file(s) to see what $regexp should be for each regexp (typically a positive integer, or the string 'MULTI' for multiline comments. $flag is false if you want to disable highlighting of this group, and true if you want to re-enable highlighting of this group.
* The following methods:
o set_escape_characters_highlighting($flag)
o set_symbols_highlighting($flag)
o set_strings_highlighting($flag)
o set_numbers_highlighting($flag)
o set_methods_highlighting($flag)
Work on their respective lexics (e.g. set_methods_highlighting will disable/enable highlighting of methods). For each method, if $flag is false then the related lexics will not be highlighted at all (this means no HTML will surround the lexic like usual, saving on time and bandwidth.
In case all highlighting should be disabled or reenabled GeSHi provides two methods called disable_highlighting() and enable_highlighting($flag). The optional paramter $flag has been added in 1.0.7.21 and specifies the desired state, i.e. true (default) to turn all highlighting on, or false to turn all highlighting off. Since 1.0.7.21 the method disnable_highlighting() has become deprecated.
3.9: Setting the Tab Width
Top | Contents | Next | Previous
If you're using the header, tabs are handled automatically by your browser, and in general you can count on good results. However, if you're using the header, you may want to specify a tab width explicitly.
Note that tabs created in this fashion won't be like normal tabs - there won't be "tab-stops" as such, instead tabs will be replaced with the specified number of spaces.
To change the tab width, you call the set_tab_width() method:
$geshi->set_tab_width($width);
Where $width is the width in spaces that you'd like tabs to be.
3.10: Using Strict Mode
Top | Contents | Next | Previous
Some languages like to get tricky, and jump in and out of the file that they're in. For example, the vast majority of you reading this will have used a PHP file. And you know that PHP code is only executed if it's within delimiters like (there are others of course...). So what happens if you do the following in a php file?
Well normally using GeSHi with PHP, or using a bad highlighter, you'll end up with this:
What a mess! Especially if you're being slack about where you're putting your quotes, you could end up with the rest of your file as bright red. Fortunately, you can tell GeSHi to be "strict" about just when it highlights and when it does not, using the enable_strict_mode method:
$geshi->enable_strict_mode($mode);
Where $mode is true or not specified to enable strict mode, or false to disable strict mode if you've already turned it and don't want it now.
Here's the result: much better!
3.11: Adding/Removing Keywords
Top | Contents | Next | Previous
Lets say that you're working on a large project, with many files, many classes and many functions. Perhaps also you have the source code on the web and highlighted by GeSHi, perhaps as a front end to CVS, as a learning tool, something to refer to, whatever. Well, why not highlight the names of the functions and classes *your* project uses, as well as the standard functions and classes? Or perhaps you're not interested in highlighting certain functions, and would like to remove them? Or maybe you don't mind if an entire function group goes west in the interest of speed? GeSHi can handle all of this!
3.11.1: Adding a Keyword
Top | Contents | Next | Previous
If you want to add a keyword to an existing keyword group, you use the add_keyword method:
$geshi->add_keyword($key, $word);
Where $key is the index of the group of keywords you want to add this keyword to, and $word is the word to add.
This implies knowledge of the language file to know the correct index.
Note:
Keywords should contain at least two alphabetical characters (lower or upper case letters only). This is to enable GeSHi to work much faster by not bothering to try to detect keywords in parts of your source where there is no alphabetical characters.
3.11.2: Removing a Keyword
Top | Contents | Next | Previous
Perhaps you want to remove a keyword from an existing group. Maybe you don't use it and want to save yourself some time. Whatever the reason, you can remove it using the remove_keyword method:
$geshi->remove_keyword($key, $word);
Where $key is the index of the gropu of keywords that you want to remove this keyword from, and $word is the word to remove.
This implies knowledge of the language file to know the correct index - most of the time the keywords you'll want to remove will be in group 3, but this is not guaranteed and you should check the language file first.
This function is silent - if the keyword is not in the group you specified, nothing awful will happen ;)
3.11.3: Adding a Keyword Group
Top | Contents | Next | Previous
Lets say for your big project you have several main functions and classes that you'd like highlighted. Why not add them as their own group instead of having them highlighted the same way as other keywords? Then you can make them stand out, and people can instantly see which functions and classes are user defined or inbuilt. Furthermore, you could set the URL for this group to point at the API documentation of your project.
You add a keyword group by using the add_keyword_group method:
$geshi->add_keyword_group($key, $styles, $case_sensitive, $words);
Where $key is the key that you want to use to refer to this group, $styles is the styles that you want to use to style this group, $case_sensitive is true or false depending on whether you want this group of keywords to be case sensitive or not and $words is an array of words (or a string) of which words to add to this group. For example:
$geshi->add_keyword_group(10, 'color: #600000;', false, array('myfunc_1', 'myfunc_2', 'myfunc_3'));
Adds a keyword group referenced by index 10, of which all keywords in the group will be dark red, each keyword can be in any case and which contains the keywords "myfunc_1", "myfunc_2" and "myfunc_3".
After creating such a keyword group, you may call other GeSHi methods on it, just as you would for any other keyword group.
Caution:
If you specify a $key for which there is already a keyword group, the old keyword group will be overwritten! Most language files don't use numbers larger than 5, so I recommend you play it safe and use a number like 10 or 42.
3.11.4: Removing a Keyword Group
Top | Contents | Next | Previous
Perhaps you *really* need speed? Why not just remove an entire keyword group? GeSHi won't have to loop through each keyword checking for its existance, saving much time. You remove a keyword group by using the remove_keyword_group method:
$geshi->remove_keyword_group($key);
Where $key is the key of the group you wish to remove. This implies knowleged of the language file.
3.12: Headers and Footers for Your Code
Top | Contents | Next | Previous
So you want to add some special information to the highlighted source? GeSHi can do that too! You can specify headers and footers for your code, style them, and insert information from the highlighted source into your header or footer.
3.12.1: Keyword Substitution
Top | Contents | Next | Previous
In your header and footer, you can put special keywords that will be replaced with actual configuration values for this GeSHi object. The keywords you can use are:
*