Syntax highlighting in articles using GeSHi
On Habré, GeSHi was mentioned in the comments, but there were no articles on its use. We will consider the use of GeSHi for automatic syntax highlighting in the text of articles or posts on the site.
GeSHi (Generic Syntax Highlighter) allows you to highlight the syntax of code written in any of more than 80 languages. GeSHi uses plug-ins with language syntax descriptions, making it easy to add new descriptions. GeSHi is written in PHP.
Download GeSHi 1.0.7.21 from SourceForge. We will write a function that provides highlighting of the code located inside the special BBCode tag [syntax]. For example, the code inside [syntax = php] [/ syntax] will be highlighted using PHP syntax. In addition, special characters can be used in the language name: # - in the highlighted code, the lines will be numbered, * - the code will be highlighted in inline mode (span instead of div). Examples of use:
1. Highlighting the SQL query [syntax = * mysql] SHOW PROCESSLIST [/ syntax] inside the text.
2. Highlighting sample code with line numbering:
[syntax = # php]
function hellow () {
echo "Hello, world!";
}
?>
Among the highlighted languages by default are bash, c, c ++, css, delphi, diff, fortran, html, java, javascript, latex, matlab, mysql, oracle, perl, php, python, rails, ruby, smarty, sql, tcl , xml and others. Language files are located in the geshi directory of the distribution. We will perform highlighting using CSS, for this we need to generate css using the contib / cssgen.php included in the distribution. cssgen.php will ask you to select the languages to include in the style file and the colors to highlight the various elements. Do not forget to include the link to the generated css-file in the header of the site pages.
The code below defines the syntax_filter () function that highlights the code inside the [syntax] [/ syntax] tags. This function should be included in the chain of functions that process the materials of the site when forming the HTML page. Please note that in the case of HTML highlighting, the code inside the tags [syntax] [/ syntax] must be written in its pure form, GeSHi independently converts it into HTML entities.
require_once ("geshi / geshi.php");
function syntax_filter ($ text) {
$ search = '/\►syntax=(.*?)\\\r?\n?(.*?)\r?\n?\\\/syntax\\/is' ;
return preg_replace_callback ($ search, 'syntax_filter_callback', $ text);
}
function syntax_filter_callback ($ data) {
$ linenumbers = false;
$ urls = false;
$ inline = false;
$ indentsize = 4;
if (isset ($ data [2])) {
$ syntax = $ data [1];
$ code = $ data [2];
if (strstr ($ syntax, '*')) {
$ inline = true;
$ syntax = str_replace ('*', '', $ syntax);
}
if (strstr ($ syntax, '#')) {
$ linenumbers = true;
$ syntax = str_replace ('#', '', $ syntax);
}
if ($ syntax == 'html') {
$ syntax = 'html4strict';
}
if ($ syntax == 'js') {
$ syntax = 'javascript';
}
$ geshi = & new GeSHi ($ code, $ syntax);
$ geshi-> set_header_type (GESHI_HEADER_DIV);
$ geshi-> set_overall_style ('font-family: monospace;');
if ($ linenumbers) {
$ geshi-> enable_line_numbers (GESHI_FANCY_LINE_NUMBERS, 5);
$ geshi-> set_line_style ('color: # 222;', 'color: # 888;');
$ geshi-> set_overall_style ('font-size: 14px; font-family: monospace;', true);
}
if (! $ urls) {
for ($ i = 0; $ i <5; $ i ++) {
$ geshi-> set_url_for_keyword_group ($ i, '');
}
}
if ($ indentsize) {
$ geshi-> set_tab_width ($ indentsize);
}
$ parsed = $ geshi-> parse_code ();
if ($ inline) {
$ parsed = preg_replace ('/ ^
GeSHi (Generic Syntax Highlighter) allows you to highlight the syntax of code written in any of more than 80 languages. GeSHi uses plug-ins with language syntax descriptions, making it easy to add new descriptions. GeSHi is written in PHP.
Download GeSHi 1.0.7.21 from SourceForge. We will write a function that provides highlighting of the code located inside the special BBCode tag [syntax]. For example, the code inside [syntax = php] [/ syntax] will be highlighted using PHP syntax. In addition, special characters can be used in the language name: # - in the highlighted code, the lines will be numbered, * - the code will be highlighted in inline mode (span instead of div). Examples of use:
1. Highlighting the SQL query [syntax = * mysql] SHOW PROCESSLIST [/ syntax] inside the text.
2. Highlighting sample code with line numbering:
[syntax = # php]
function hellow () {
echo "Hello, world!";
}
?>
Among the highlighted languages by default are bash, c, c ++, css, delphi, diff, fortran, html, java, javascript, latex, matlab, mysql, oracle, perl, php, python, rails, ruby, smarty, sql, tcl , xml and others. Language files are located in the geshi directory of the distribution. We will perform highlighting using CSS, for this we need to generate css using the contib / cssgen.php included in the distribution. cssgen.php will ask you to select the languages to include in the style file and the colors to highlight the various elements. Do not forget to include the link to the generated css-file in the header of the site pages.
The code below defines the syntax_filter () function that highlights the code inside the [syntax] [/ syntax] tags. This function should be included in the chain of functions that process the materials of the site when forming the HTML page. Please note that in the case of HTML highlighting, the code inside the tags [syntax] [/ syntax] must be written in its pure form, GeSHi independently converts it into HTML entities.
require_once ("geshi / geshi.php");
function syntax_filter ($ text) {
$ search = '/\►syntax=(.*?)\\\r?\n?(.*?)\r?\n?\\\/syntax\\/is' ;
return preg_replace_callback ($ search, 'syntax_filter_callback', $ text);
}
function syntax_filter_callback ($ data) {
$ linenumbers = false;
$ urls = false;
$ inline = false;
$ indentsize = 4;
if (isset ($ data [2])) {
$ syntax = $ data [1];
$ code = $ data [2];
if (strstr ($ syntax, '*')) {
$ inline = true;
$ syntax = str_replace ('*', '', $ syntax);
}
if (strstr ($ syntax, '#')) {
$ linenumbers = true;
$ syntax = str_replace ('#', '', $ syntax);
}
if ($ syntax == 'html') {
$ syntax = 'html4strict';
}
if ($ syntax == 'js') {
$ syntax = 'javascript';
}
$ geshi = & new GeSHi ($ code, $ syntax);
$ geshi-> set_header_type (GESHI_HEADER_DIV);
$ geshi-> set_overall_style ('font-family: monospace;');
if ($ linenumbers) {
$ geshi-> enable_line_numbers (GESHI_FANCY_LINE_NUMBERS, 5);
$ geshi-> set_line_style ('color: # 222;', 'color: # 888;');
$ geshi-> set_overall_style ('font-size: 14px; font-family: monospace;', true);
}
if (! $ urls) {
for ($ i = 0; $ i <5; $ i ++) {
$ geshi-> set_url_for_keyword_group ($ i, '');
}
}
if ($ indentsize) {
$ geshi-> set_tab_width ($ indentsize);
}
$ parsed = $ geshi-> parse_code ();
if ($ inline) {
$ parsed = preg_replace ('/ ^
$ parsed = preg_replace ('/ <\ / div> $ /', '', $ parsed);
}
}
return $ parsed;
}
?>
Crosspost Syntax highlighting in articles using GeSHi with webew.ru .
}
}
return $ parsed;
}
?>
Crosspost Syntax highlighting in articles using GeSHi with webew.ru .