Please login or register.

Login with username, password and session length
Advanced search  

News:

Latest sNews - sNews 1.7 - with its own forums - for discussion and user mods.

Author Topic: Using Geshi Code Syntax Highlighter with sNews, please somebody help.  (Read 6905 times)

sami

  • Newbie
  • *
  • Karma: 0
  • Posts: 2

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

http://snewscms.com/forum/index.php?topic=6585.msg44730

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.

http://wordpress.org/extend/plugins/highlight-source-pro/

here is code of the plugin page.

Code: [Select]
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 */
    include_once('geshi/geshi.php');
   
    if ( $encoded )
    {
        $subject = html_entity_decode($subject);
    }
    $subject = $subject;
    $geshi= new GeSHi($subject, $language);
    $geshi->set_header_type(GESHI_HEADER_DIV);
    $geshi->enable_classes();
    $geshi->enable_strict_mode($mode);
    $geshi->enable_keyword_links(false);
    $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
   
    if ( $offset != 0 )
    {
        $geshi->start_line_numbers_at($offset);
        $geshi->set_overall_class('geshi '. $language);
    } else {
        $geshi->set_overall_class('geshi no '. $language);
    }
   
    if ( !empty($title) )
    {
        $geshi->set_header_content($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;
}


?>

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

Thanks.
Logged

michael kennedy

  • Full Member
  • ***
  • Karma: 3
  • Posts: 207
    • SpektreDesign

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

http://alexgorbatchev.com/wiki/SyntaxHighlighter