Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: geshi syntax highlight plugin  (Read 1259 times)

jsdakjsnd

  • Newbie
  • *
  • Karma: 0
  • Posts: 4
geshi syntax highlight plugin
« on: June 14, 2011, 11:05:50 am »

so i decided to write a geshi syntax highlight plugin for current snews.
first i read a little in this thread (http://snewscms.com/forum/index.php?topic=6585.0) and saw that most code was a little outdated, so i wrote my own plugin which i just placed at the end of geshi's main file "geshi.php":

Code: [Select]
function GeshiSyntaxColorer($text)
{
  if(preg_match_all('#<pre lang="(.*?)">(.*?)</pre>#s', $text, $matches, PREG_PATTERN_ORDER))
  {
    //$matches[1][0] == language
    //$matches[2][0] == codesnippet
    
    $str = md5(uniqid(rand())); //generate unique token
    //replace the whole codesnippet with the unique token
    $pattern = '<pre lang="'.$matches[1][0].'">'.$matches[2][0].'</pre>';
    $text = preg_replace("/". preg_quote($pattern, '/') ."/", $str, $text, 1, $count);
    //fire up geshi
    $geshi =& new GeSHi(htmlspecialchars_decode($matches[2][0]), $matches[1][0], 'geshi/');
    $geshi->set_overall_style('font: normal normal 100% \'Courier New\', Courier, monospace; color: #000066; border: 2px solid #d0d0d0; background-color: #f0f0f0; overflow: auto;', false);
    $geshi->set_footer_content("parsed in <TIME> seconds");
    $geshi->set_footer_content_style('font-family: Verdana, Arial, sans-serif; color: #808080; font-size: 80%; font-weight: bold; background-color: #f0f0ff; border-top: 1px solid #d0d0d0; padding: 2px;');
    $code = $geshi->parse_code(); //store the code
    //replace the token with the code
    $text = preg_replace("#".$str."#", $code, $text, 1);
    return $text;
  }
  //no codesnippet found in article
  else { return $text; }
}

..and i included the geshi.php in snews.php on top:
Code: [Select]
include_once('geshi.php');
and placed my function call on the same place as said in the tread above in snews.php:
Code: [Select]
.........
function file_include($text, $shorten)
{
   $fulltext = GeshiSyntaxColorer(substr($text, 0, $shorten));
   if(substr_count ($fulltext, '&')>0){$fulltext = str_replace('&', '&amp;', str_replace('&amp;', '&', $fulltext));}
if ($shorten < 9999000 && preg_match('<p>',$fulltext)) {
........


so now comes the tricky part:
the highlighting is working pretty good! BUT all special symbols get converted to htmlentities :( - and i have no idea why! i also tried to first html_entity_decode everything, but this isnt working either :(:(

can anyone please help me? i tried to explain it the best i could, if anything is still unclear dont hesitate to ask!
Logged

Kasey00

  • Newbie
  • *
  • Karma: 0
  • Posts: 4
Re: geshi syntax highlight plugin
« Reply #1 on: January 01, 2020, 10:28:45 pm »

so i decided to write a geshi syntax highlight mynikevisit-na plugin for current snews.
first i read a little in this thread (http://snewscms.com/forum/index.php?topic=6585.0) and saw that most code was a little outdated, so i wrote my own plugin which i just placed at the end of geshi's main file "geshi.php":

Code: [Select]
function GeshiSyntaxColorer($text)
{
  if(preg_match_all('#<pre lang="(.*?)">(.*?)</pre>#s', $text, $matches, PREG_PATTERN_ORDER))
  {
    //$matches[1][0] == language
    //$matches[2][0] == codesnippet
   
    $str = md5(uniqid(rand())); //generate unique token
    //replace the whole codesnippet with the unique token
    $pattern = '<pre lang="'.$matches[1][0].'">'.$matches[2][0].'</pre>';
    $text = preg_replace("/". preg_quote($pattern, '/') ."/", $str, $text, 1, $count);
    //fire up geshi
    $geshi =& new GeSHi(htmlspecialchars_decode($matches[2][0]), $matches[1][0], 'geshi/');
    $geshi->set_overall_style('font: normal normal 100% \'Courier New\', Courier, monospace; color: #000066; border: 2px solid #d0d0d0; background-color: #f0f0f0; overflow: auto;', false);
    $geshi->set_footer_content("parsed in <TIME> seconds");
    $geshi->set_footer_content_style('font-family: Verdana, Arial, sans-serif; color: #808080; font-size: 80%; font-weight: bold; background-color: #f0f0ff; border-top: 1px solid #d0d0d0; padding: 2px;');
    $code = $geshi->parse_code(); //store the code
    //replace the token with the code
    $text = preg_replace("#".$str."#", $code, $text, 1);
    return $text;
  }
  //no codesnippet found in article
  else { return $text; }
}

..and i included the geshi.php in snews.php on top:
Code: [Select]
include_once('geshi.php');
and placed my function call on the same place as said in the tread above in snews.php:
Code: [Select]
.........
function file_include($text, $shorten)
{
   $fulltext = GeshiSyntaxColorer(substr($text, 0, $shorten));
   if(substr_count ($fulltext, '&')>0){$fulltext = str_replace('&', '&amp;', str_replace('&amp;', '&', $fulltext));}
if ($shorten < 9999000 && preg_match('<p>',$fulltext)) {
........


so now comes the tricky part:
the highlighting is working pretty good! BUT all special bluestacks symbols get converted to htmlentities :( - and i have no idea why! i also tried to first html_entity_decode everything, but this isnt working either :(:(

can anyone please help me? i tried to explain it the best i could, if anything is still unclear dont hesitate to ask!

Goto dreamhost support panel. They have explained this very nicely.
« Last Edit: January 03, 2020, 07:46:18 am by Kasey00 »
Logged