Job Inquiries > Requests - No pay

Using Geshi Code Syntax Highlighter with sNews, please somebody help.


I'm in very much need to integrate GESHI code syntax highlighter with Snews. I came across this post.

Where bakercad told the method to  integrate a wordpress plugin with the snews. This works fine, but its very old plugin and does not support new features.

I want to integrate this wordpress plugin which uses Geshi liberary to highlight code.
This plugin can be downloaded from here.

here is code of the plugin page.

--- Code: ---add_filter('the_content', 'hsp_clean_and_codify', 1);
add_action('wp_head', 'hsp_add_css');

function hsp_add_css()
    print('<link rel="stylesheet" type="text/css" href="'
            . get_option('siteurl')
            . '/wp-content/plugins/highlight-source-pro/all.css" />');
    print('<!--[if IE]><link rel="stylesheet" type="text/css" href="'
            . get_option('siteurl')
            . '/wp-content/plugins/highlight-source-pro/ie.css" /><![endif]-->');

  * Highlights the text as code in the supplied language
  * @return string The marked up code
  * @param subject The text to markup
  * @param language The language to use for highlighting
function hsp_syntax_highlight($subject, $language, $offset=1, $title='', $encoded = false)
    /* Format the code with GeSHi */
    if ( $encoded )
        $subject = html_entity_decode($subject);
    $subject = $subject;
    $geshi= new GeSHi($subject, $language);
    if ( $offset != 0 )
        $geshi->set_overall_class('geshi '. $language);
    } else {
        $geshi->set_overall_class('geshi no '. $language);
    if ( !empty($title) )
    return str_replace("'", '&#39;', $geshi->parse_code());
  * Returns a cleaned and syntax-highlighted string of HTML
  * @return string Cleaned and codified text
  * @param subject The text to cut into code pieces
function hsp_clean_and_codify($subject)
    $original= $subject;
    $code_pieces= array();
//    $code_regex = '/[\[\<]pre\s*(lang|language)\=[\"\'](\w+)[\"\'][\]\>]([\D\S]+?)[\[\<]\/pre[\]\>]/';
//    $code_regex = '/[\[\<]pre\s*(lang|language)\=[\"\'](\w+)[\"\'](\s*class=[\"\']([0-9]+)[\"\'])?\s*[\]\>]([\D\S]+?)[\[\<]\/pre[\]\>]/';
//    $code_regex = '/[\[\<]pre\s*(lang|language)\=[\"\'](\w+)[\"\'](\s*class=[\"\']([0-9]+)[\"\'])?\s*[\]\>]([ ]*([^\\n]+))?([\D\S]+?)[\[\<]\/pre[\]\>]/';
    $code_regex = '/[\[\<]pre\s*(lang|language)\=[\"\'](enc__)?(\w+)[\"\'](\s*class=[\"\']([0-9]+)[\"\'])?\s*[\]\>]([ ]*([^\n]+))?\n([\D\S]+?)[\[\<]\/pre[\]\>]/';
    $code_delimiter= "CODECODECODE";
    /* First split the text into code and non-code blocks */
    while (preg_match($code_regex, $subject, $code_matches) == 1)
        $encoded_flag = (strtolower($code_matches[2]) == 'enc__');
        $language= trim(strtolower($code_matches[3])); // 0-index is the full match
        $start_line = $code_matches[5];
        $code_title = trim($code_matches[7]);
        $code_sample= $code_matches[8];
        $entire_code_string= $code_matches[0];
        $code_sample= str_replace("\t", " ", $code_sample); /* Replace tabs with spaces */
        $code_pieces[]= array('enc'=>$encoded_flag, 'lang'=>$language, 'text'=>$code_sample, 'offset'=>$start_line, 'title'=>$code_title);
        $subject= str_replace($entire_code_string, $code_delimiter, $subject);
        $code_matches= array(); //reset
      * Now $subject should contain CleanMarkup\n|||CODE|||\nCleanMarkup...
      * We now replace the code sections by passing an executable string
      * to the regex parser (the /e option) and using the syntax_highlight
      * function to do the grunt work
    $num_code_pieces= count($code_pieces);
    $i= 0;
    if ($num_code_pieces > 0)
        $replacement= "hsp_syntax_highlight(rtrim(\$code_pieces[\$i]['text']), \$code_pieces[\$i]['lang'], \$code_pieces[\$i]['offset'], \$code_pieces[\$i]['title'], \$code_pieces[\$i++]['enc']);";
        $subject= preg_replace('/' . $code_delimiter . '/e', $replacement, $subject);
    return $subject;

--- End code ---

If someone can help me, i will be very much thankful.


michael kennedy:
I'd recommend you use this JavaScript syntax highlighter. It's much easier to setup and won't interfere with the sNews PHP.


[0] Message Index

Go to full version