Formatter: GeSHi Code Highlighting

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

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

1. Variant A

Here is how you can quickly add GeSHi code highlighting to your Wacko installation:

  1. cd formatter/highlight/
  2. wget https://sourceforge.net/projects/geshi/files/geshi/GeSHi%201.0.9.0/GeSHi-1.0.9.0.tar.gz
  3. tar xzvf GeSHi-1.0.9.0.tar.gz
  4. rm GeSHi-1.0.9.0.tar.gz
  5. mkdir old; mv {css,html,java,pascal,php,sql}.php old/ Get rid of older support.
  6. Build a list of names of GeSHi files, I did this with ls geshi/geshi -1 | awk -F. '{print $1}' > names
  7. You might want to open up names and change html4strict to just html.
  8. Run the below script in that directory, change as needed.
  9. You can then do things like %%(perl) %% and it will work more or less as expected.
  10. This is intended to get you there quickly, you may want to extend this further on your own.

#!/bin/sh

for x in `cat names`; do

cat < $x.php
\$geshi->set_header_type(GESHI_HEADER_DIV);
\$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
\$geshi->set_tab_width(4);
echo "";
echo "<div class="\"code\"">";
echo \$geshi->parse_code();
echo "</div>";
echo "";
?>
EOF

done

2. Variant B

Call via (geshi css)


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


  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">');