posted by kevin on May 19, 2009

I'll take you through what I have done to add code highlighting to my site.

First download GeSHi - Generic Syntax Highlighter

After opening the compressed file you'll see the file/folder structure to look like:

 
/contrib/
/docs/
/geshi/
/geshi.php
 

Copy:
/geshi/
/geshi.php

Into your site:
/protected/components/geshi.php <-- Rename this to GeSHi.php
/protected/components/geshi/

Now since your configuration should already be autoloading your components you won't have to do anything special to load these.

You'll notice in the /protected/config/main.php file the import array:

 
    // autoloading model and component classes
    'import'=>array(
        'application.models.*',
        'application.components.*',
 
    ),
 

The application.components.* indicates to auto-load all of your components.

Now to put this component to work:

Open up your Post Model (/protected/models/Post.php)

Add this method to your model:

 
/**
 * Highlight Code Between <phpcode> and </phpcode> blocks.
 *
 * @param string $content
 */
protected function getContentForDisplay($content) {
    $new_content = false;
    while($start_position = strpos($content, "<phpcode>")) {
        if($new_content === false) {
            $new_content = "";
        }
        $end_position = strpos($content, "</phpcode>");
        $difference = $end_position - $start_position;
 
        $pre_content = substr($content, 0, $start_position);
        $content_to_highlight = substr($content, $start_position+9, $difference-9);
        $post_content = substr($content, $end_position+10);
        $geshi = new GeSHi($content_to_highlight, "php");
        $content = $pre_content.$geshi->parse_code().$post_content;
    }
    return $content;
}
 

This method will take your content and find all of the places where you have <phpcode> </phpcode> blocks, and takes your php code and highlights it and then saves the result as nice, formatted html.

Now we need to use this method to turn or normal posted code, into our formatted code. We'll do this in our beforeValidate method still in the Post Controller:

 
protected function beforeValidate($on)
{
    $parser=new CMarkdownParser;
    $this->contentDisplay = $this->getContentForDisplay($this->content);
    $this->contentDisplay=$parser->safeTransform($this->contentDisplay);
    if($this->isNewRecord)
    {
        $this->createTime=$this->updateTime=time();
        $this->authorId=Yii::app()->user->id;
    }
    else
        $this->updateTime=time();
    return true;
}
 

And you're done.

It's that simple. You'll come across some issues with your text being too long for the line, but those you'll have to solve on a case by case basis. Enjoy!

Let me know if you have any questions.

One comment to "Adding PHP Code Highlighting to Yii's demo blog."

#94
Rico says:
December 7, 2009 at 04:19 pm
Hi! I've followed your instructions but, after i click submit button in my form i get error: Fatal error: Call to undefined method GeSHi::parse_code() in C:\xampplite\htdocs\rBlog\protected\models\Post.php on line 194 You are familiar with this extension so maybe you can help me? Thx in advance!
Bookmark and Share

Leave a Comment

Your email address will not be published.

(You can enclose code in <php></php> blocks.)

You may use Markdown syntax.

Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.