MENU

GeSHi - Generic Syntax Highlighter

  Previous article Next article  

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
 * -------------------------------------------------------------------
 */


function smarty_block_cms_geshi($params, $content, &$smarty)
{
  if (isset($content))
  {
    include_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cms_geshi' . DIRECTORY_SEPARATOR . 'geshi.php');

    $lang = isset($params['lang']) ? $params['lang'] : 'php';
    $styles = isset($params['styles']) ? $params['styles'] : '';

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

    $uid = get_userid(FALSE);
    if ( !empty ($uid) ) echo $geshi->error();

    // 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.

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! :)



Buy Me A Coffee


  Comment Form

ReviewManager

Click here to open the form

ReviewManager

  7 Comments

Buy Me A Coffee

CMS Made Simple - Tutorials, Tips and Tricks - CMSMS

GeSHi - Generic Syntax Highlighter

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 2.2.19
  Last updated: 10-02-2019
  Comments: 7
  http://cms.ms/3l1G


Buy Me A Coffee




Advertisement


Ads help me to help you! Thanks!

Ads help me to help you! Buy products from these advertisers!