Changing the Source, Language, Config Options - GeSHi/geshi-1.0 GitHub Wiki
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.
Changing the Source Code
To change the source code, you call the
$geshi = new GeSHi($source1, 'php'); // Method calls to specify various options... $code1 = $geshi->parse_code(); $geshi->set_source($source2); $code2 = $geshi->parse_code();
Changing the Language
What happens if you want to change the language used for highlighting? Just call
$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();
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.
$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.
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.
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.
Since GeSHi 1.0.8 this function does not reset language settings for an already loaded language. If you want to highlight code in the same language with different settings add the optional
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.
Changing the Language Path
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
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
Although GeSHi itself does not require to know the exact charset of your source you will need to set this option when processing sources where multi-byte characters can occur. As of GeSHi 22.214.171.124 internally a rewrite of
htmlspecialcharsis used due to a security flaw in that function that is unpatched in even the most recent PHP4 versions and in PHP5 < 5.2. Although this does no longer explicitely require the charset it is required again as of GeSHi 1.0.8 to properly handle multi-byte characters (e.g. after an escape char).
As of GeSHi 1.0.8 the default charset has been changed to UTF-8.
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 mbstring library:
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.