GeSHi - Generic Syntax Highlighter

  Previous article Next article  

GeSHi - Generic Syntax Highlighter

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 2.2.3.1
  Last updated: 18-11-2017
  Comments: 6
  http://cms.ms/3l1G

Add the Generic Syntax Highlighter (GeSHi) to the frontend of your website.
The idea for this article originates from the former CMS Made Simple Wiki and the original author was tsw.


  How to use

1. Create a file named block.cms_geshi.php with the following content:

block.cms_geshi.php
<?php
/**
 * GeSHi - Generic Syntax Highlighter
 * -------------------------------------------------------------------
 * File:     block.cms_geshi.php
 * Type:     block
 * Purpose:  highlight a block of text using GeSHi
 * https://www.cmscanbesimple.org/blog/geshi-generic-syntax-highlighter
 * -------------------------------------------------------------------
 */

include_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cms_geshi' . DIRECTORY_SEPARATOR . 'geshi.php');

function smarty_block_cms_geshi($params, $content, &$smarty)
{
  if (isset($content))
  {
    $lang = isset($params['lang']) ? $params['lang'] : 'php';
    $styles = isset($params['styles']) ? $params['styles'] : '';

    $geshi = new GeSHi($content, $lang,  '', true);

    // Show title in header
      $title = isset($params['title']) ? $params['title'] : '';
      if (!empty($title)) { $geshi->set_header_content($title); }

    // Show linenumbers
      $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);

    // Show CSS classes
      $geshi->enable_classes(true);

    // Show text indent, etc.
      $geshi->set_header_type(GESHI_HEADER_DIV);

    // Show external links to PHP documentation
      $geshi->enable_keyword_links(false);

    // Set target to external links
      $geshi->set_link_target('_blank', $styles);

    echo $geshi->parse_code();
  }
}
?>

2. Download the GeSHi files and unzip the archive.

3. Create a new folder in the core plugins directory named cms_geshi and upload the GeSHi folders and files in it.

4. Move the created block.cms_geshi.php file to the plugins folder.

.../assets/plugins/block.cms_geshi.php
/assets/plugins.../cms_geshi/contrib/...
/assets/plugins/cms_geshi.../docs/...
/assets/plugins/cms_geshi.../geshi/...
/assets/plugins/cms_geshi.../geshi.php

5. Copy the GeSHi stylesheet content into a new CMSMS stylesheet and attach it to the template.

Refer to GeSHi documentation on GeSHi's capabilities GeSHi doc.


A2 Webhosting CMS Made Simple

Now you can have highlighted code by calling this block from your page with:

{cms_geshi}
  <?php
    echo 'foo bar';
  ?>
{/cms_geshi}
{cms_geshi}{literal}
  <?php
    if ($test == true) { echo 1; }
  ?>
{/literal}{/cms_geshi}
{cms_geshi lang='php' title='foo'}{literal}
  <?php
    if ($test == true) { echo 1; }
  ?>
{/literal}{/cms_geshi}

Note that you need {literal}{/literal} tags around your code when braces are used inside.

  Working example

Just look above, I use it here at the website! :)



A2 Webhosting CMS Made Simple

  Comments

  Click here to leave a comment






This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)
  « Enter the CAPTCHA code (lower case)

Your IP address will be stored on submit. I reserve the right to change or delete your response without notice!
In case of abuse your IP address will be blocked!


Can I have your attention for a moment?

Did this tutorial help you solving a problem at your (clients) website and it saved you many hours of work? Great!!
Consider buying Rolf a cup of coffee in return! [ Click here ]


  6 Comments

CMS Made Simple 2.2.3.1 - Tutorials, tips and tricks - CMSMS 2.2.3.1

GeSHi - Generic Syntax Highlighter

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 2.2.3.1
  Last updated: 18-11-2017
  Comments: 6
  http://cms.ms/3l1G

A2 Webhosting CMS Made Simple