📜 ⬆️ ⬇️

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-in files with language syntax descriptions, which makes 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 highlights the code located inside the special [syntax] BBCode tag. 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 using:
')
1. Highlight SQL query [syntax = * mysql] SHOW PROCESSLIST [/ syntax] inside the text.
2. Posting example code with line numbering:
[syntax = # php]
function hellow () {
echo "Hello, world!";
}
?>

Among the languages ​​highlighted 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 you will need to generate css using contib / cssgen.php included in the distribution. cssgen.php will ask you to select languages ​​to include in the style file and colors for highlighting various elements. Do not forget to include a link to the generated css-file in the header of the site.

The code below defines the syntax_filter () function, which highlights the code inside the [syntax] [/ syntax] tags. This function should be included in the chain of functions that process the site materials when generating the HTML page. Please note that in the case of HTML highlighting, the code inside the [syntax] [/ syntax] tags should be written in its pure form, GeSHi automatically converts it into HTML entities.

<? php
require_once ("geshi / geshi.php");

function syntax_filter ($ text) {
$ search = '/\[syntax=(.*?)\\\\r?\n?(.*?)\r?\n?\[\/syntax\y/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-> enable_classes (true);
$ 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 ('/ ^ <div /', '<span', $ parsed);
$ parsed = preg_replace ('/ <\ / div> $ /', '', $ parsed);
}
}
return $ parsed;
}

?>


Crosspost Syntax highlighting in articles using GeSHi with webew.ru .

Source: https://habr.com/ru/post/25497/


All Articles