php PHP Script
Download PyroCMS/CodeIgniter Geshi Helper FileDownload PyroCMS/CodeIgniter Geshi Helper File
Download Count: 131
Date Added: Sunday, 03-Jul-11 20:31:26 CDT
Tags: geshi, pyrocms, php, codeigniter

This is a helper file I made for PyroCMS/CodeIgniter that makes it easy to use GeSHi Highlighting on code snippets pulled from a database or on complete files read from the system. No need for hooks to pre-process everything, wasting valuable resources.

To use this helper script, you first need to follow these steps to setup GeSHi with PyroCMS/CodeIgniter. After you have GeSHi setup, place this file in your PyroCMS addons/helpers folder, or for CodeIgniter, copy it to your application/helpers folder, and you'll be all set to go

The file is actually a set of two functions that you can call from a controller, geshi() and geshi_code().

The geshi() function is for reading and highlighting code from a file on the system. It takes three variables - geshi($path, $name, $language):

  • $path - the absolute path to the file
  • $name - the name of the file (currently used in the code header)
  • $language - the coding language - used by GeSHi

The geshi_code() function is for reading and highlighting code from a database(or other source). It takes three variables - geshi_code( $source, $language, $title):

  • $source - the code from database
  • $language - the coding language - used by GeSHi
  • $title - what to display in the code header

Both functions return an array with two items, one is the css styles, and the other is the formatted code. That way you can place them in your view where you need them.

GeSHi Helper file:

File Name: geshi_helper.php - Code Type: PHP
<?php defined('BASEPATH') OR exit('No direct script access allowed.');
 
/**
  * GeSHi Highlighter PyroCMS/CodeIgniter Helper File
  * @author             Larry Price
  * @filename   geshi_helper.php
  * @title              Geshi Helper
  * @version    1.0
 **/
 
        /**
         * Usage:
         * For highlighting code straight from file
         * geshi($path, $name, $language)
         * @return      string
         */     
if ( ! function_exists('geshi'))
{
        function geshi($path, $name, $language) {
                $source = read_file($path);
                $geshi = new GeSHi($source, $language);
                $geshi->enable_classes();
                $geshi->set_overall_style('font-size:12px; padding:10px; color: #000000; border: 3px solid silver; width: 650px; background-color: #f9f9f9; margin: 0 0 1.5em 0;  overflow: auto;', true);
                $geshi->set_header_content($name.' <LANGUAGE>' );
                $geshi->set_header_content_style('font-family: Verdana, Arial, sans-serif; color: #808080; font-size: 70%; font-weight: bold; background-color: #f0f0ff; border-bottom: 1px solid #d0d0d0; padding: 2px;');
                $geshi->set_footer_content('Parsed in <TIME> seconds,  using GeSHi <VERSION>');
                $geshi->set_footer_content_style('font-family: Verdana, Arial, sans-serif; color: #808080; font-size: 70%; font-weight: bold; background-color: #f0f0ff; border-top: 1px solid #d0d0d0; padding: 2px;');
                $geshi->set_header_type(GESHI_HEADER_PRE_TABLE);
                $highlights['styles'] = $geshi->get_stylesheet();
                $highlights['code'] = $geshi->parse_code();
                return $highlights;
   }
}       
 
        /**
         * Usage:
         * For highlighting code in document
         * geshi_code( $source, $language, $title)
         * @return      string
         */
if ( ! function_exists('geshi_code'))
{
        function geshi_code( $source, $language, $title) {
                $geshi = new GeSHi($source, $language);
                $geshi->enable_classes();
                $geshi->set_overall_style('font-size:12px; padding:10px; color: #000000; border: 3px solid #666; width: 650px; background-color: #f9f9f9; margin: 0 0 1.5em 0;  overflow: auto;', true);
                $geshi->set_header_content($title);
                $geshi->set_header_content_style('font-family: Verdana, Arial, sans-serif; color: #808080; font-size: 70%; font-weight: bold; background-color: #f0f0ff; border-bottom: 1px solid #d0d0d0; padding: 2px;');
                $geshi->set_footer_content('Parsed in <TIME> seconds,  using GeSHi <VERSION>');
                $geshi->set_footer_content_style('font-family: Verdana, Arial, sans-serif; color: #808080; font-size: 70%; font-weight: bold; background-color: #f0f0ff; border-top: 1px solid #d0d0d0; padding: 2px;');
                $geshi->set_header_type(GESHI_HEADER_PRE_TABLE);
                $highlights['styles'] = $geshi->get_stylesheet();
                $highlights['code'] = $geshi->parse_code();
                return $highlights;
   }
        
}       
/* End of file geshi_helper.php */
Parsed in 0.176 seconds - Rate: 15.12 KB/s - GeSHi version: 1.0.8.10

Hope you find this helpful! If you have any questions, please leave a comment.


  • PHP
  • Scripts
  • MS
  • Office
  • Templates
  • and
  • Files
  • Assorted
  • Linux
  • Bash
  • Scripts
  • Perl
  • Scripts
  • PDF
  • FIles
  • MS
  • Excel
  • 2003
  • Template
  • employment
  • tools
  • job
  • search
  • tools
  • spreadsheet
  • Job
  • Search
  • Spreadsheet
  • Template
  • bash
  • shell
  • script
  • file
  • backup
  • linux
  • gzip
  • tar
  • File
  • Backup
  • Script
  • Transmission
  • shell
  • script
  • public
  • torrent
  • private
  • torrent
  • Bit
  • Torrent
  • Transmission
  • BitTorrent
  • Completion
  • Script
  • Transmission
  • shell
  • script
  • torrent
  • information
  • Bit
  • Torrent
  • Transmission
  • Bit
  • Torrent
  • Get
  • Torrent
  • Info
  • bash
  • shell
  • script
  • add
  • user
  • samba
  • samba
  • user
  • add
  • samba
  • user
  • linux
  • command
  • line
  • Add
  • System
  • and
  • Samba
  • User
  • geshi
  • pyrocms
  • php
  • codeigniter
  • PyroCMS/CodeIgniter
  • Geshi
  • Helper
  • File
  • geshi
  • pyrocms
  • php
  • codeigniter
  • GeSHi
  • Code
  • Highlighter
  • plugin
  • for
  • PyroCMS
  • perl
  • cgi
  • hit
  • counter
  • Simple
  • Perl
  • Hit
  • Counter
  • pyrocms
  • codeigniter
  • php
  • ip
  • address
  • session
  • userdata
  • PyroCMS
  • IP
  • Address
  • Plugin
  • Soyo
  • SY5SSM
  • Socket
  • 7
  • Motherboard
  • Quick
  • Start
  • Guide
  • Soyo
  • SY5SSM
  • Socket
  • 7
  • Motherboard
  • Quick
  • Start
  • Guide
  • Home
  • web
  • designit
  • supportlinuxphpbash
  • PRIVACY
  • Statement
  • LEGAL
  • Disclaimer
  • Ebay
  • Listings
  • Soyo
  • Motherboard
  • PDF
  • Why
  • I
  • Like
  • the
  • HoneyPi
  • Brand
  • IR
  • Receiver
  • Module
  • for
  • the
  • Raspberry
  • Pi
  • Using
  • GeSHi
  • Highlighter
  • with
  • PyroCMS/CodeIgniter
  • Easy
  • as
  • 1
  • 2
  • 3
  • PHP
  • Stuff
  • Raspberry
  • PI