Changing Styles - GeSHi/geshi-1.0 GitHub Wiki
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.
The code outputted by GeSHi is either in a <div>
or a <pre>
(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 <div>
/<pre>
is a block level element.
👉 Note:
Remember that a
<div>
will by default have a larger font size than a<pre>
, as discussed in the section “The Code Container”.
You may wish to refer to the section [Styling Line Numbers][1] 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 useset_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.
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);
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.
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.
👉 Note:
Since GeSHi 1.0.8 multiple styles for strings and numbers are supported, though the API doesn’t provide full access yet.