Formatter: GeSHi Code Highlighting

Compatible with: R5.5
Current version: 0.2
Credits:

Repo: source:community/formatter/highlight/geshi.php


https://github.com/GeSHi/geshi-1.0
GeSHi Documentation

1. Usage

Call via (geshi css)


%%(geshi php numbers=1 start=35 lines=37) source%%

2. Install

  1. place the GeSHi lib under the lib/geshi/ folder
  2. add the GeSHi class to autoload.conf
    lib/geshi/geshi.php 			GeSHi

formatter/highlight/geshi.php


<?php

# require_once 'lib/geshi/geshi.php'; // -> autoload.conf

if ($options['_default'])
{
    
$language    $options['_default'];
    
$lines        = [];
    
$numbers    GESHI_NO_LINE_NUMBERS//
    
$start        = (int)($options['start'] ?? 1);
    
$header        GESHI_HEADER_PRE_VALID;

    
$geshi = new GeSHi($text$language);

    if (!empty(
$options['lines']))
    {
        
$lines array_map('intval'explode(','$options['lines']));
        
$lines array_unique($lines);
    }

    if (!empty(
$options['numbers']))
    {
        
$numbers $options['numbers'] ? GESHI_NORMAL_LINE_NUMBERS GESHI_NO_LINE_NUMBERS;

        switch (
$options['numbers'])
        {
            case 
1:
                
$header GESHI_HEADER_PRE_VALID;
                break;
            case 
2:
                
$header GESHI_HEADER_PRE_TABLE;
                break;
            default:
                
$header false;
                break;
        }
    }

    
$geshi->enable_classes();            // use classes for highlighting (must be first after creating object)
    
$geshi->set_overall_class('code');    // enables using a single stylesheet for multiple code fragments
    
$geshi->set_tab_width(4);            // default: 8

    
$geshi->set_header_type($header); // GESHI_HEADER_DIV GESHI_HEADER_PRE_VALID GESHI_HEADER_PRE_TABLE GESHI_HEADER_NONE

    
$geshi->enable_line_numbers((bool) $numbers); // GESHI_NORMAL_LINE_NUMBERS GESHI_FANCY_LINE_NUMBERS, 2 GESHI_NO_LINE_NUMBERS
    
$geshi->start_line_numbers_at((int) abs($start));
    
$geshi->highlight_lines_extra($lines);

    
#/*
    // get the stylesheet -> geshi.css
    
echo '<style type="text/css">';
    echo 
$geshi->get_stylesheet(false);
    echo 
'</style>';
    
#*/

    
echo '<!--notypo-->' "\n";
    echo 
$geshi->parse_code();
    echo 
'<!--/notypo-->' "\n";
}
else
{
    echo 
Ut::html($text);
}

2.1. External stylesheet

GeSHi comes with a contrib/ directory, which in it contains a “wizard” script for creating a stylesheet.


geshi.css (incomplete)

/* GeSHi – syntax highlighting code */

.code .de1, .code .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align\top;}
.code  {font-family:monospace;}
.code .imp {font-weight: bold; color: red;}
.code li, .code .li1 {font-weight: normal; vertical-align\top;}
.code .ln width:1px;text-align\right;margin:0;padding:0 2px;vertical-align\top;}
.code .li2 {font-weight: bold; vertical-align\top;}
.code .kw2 {color: #000000; font-weight: bold;}
.code .kw3 {color: #000066;}
.code .es0 {color: #000099; font-weight: bold;}
.code .br0 {color: #66cc66;}
.code .sy0 {color: #66cc66;}
.code .st0 {color: #ff0000;}
.code .nu0 {color: #cc66cc;}
.code .sc-2 {color: #404040;}
.code .sc-1 {color: #808080; font-style: italic;}
.code .sc0 {color: #00bbdd;}
.code .sc1 {color: #ddbb00;}
.code .sc2 {color: #009900;}
.code .ln-xtra, .code li.ln-xtra, .code div.ln-xtra {background-color: #ffc;}
.code span.xtra { display:block; } 

// add to theme/_common/_header.php
$this->add_html('header', '<link rel="stylesheet" href="' . $this->db->theme_url . 'css/geshi.css">');