This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

[resolved] code highlighter problem

Social code snippet repository

[resolved] code highlighter problem

by smallbeer » Tue Jul 04, 2006 10:17 am

Copying the code from the snippet app I'm trying to get a code highlighter working. I'm using the exact same code but...

Code: Select all
[code xml]
$this->getFoo();

[/code]

is saved in the db as

Code: Select all
<p>[xml]
$this->getFoo();</p>


Probably one for Francois but can anyone think what might be going on?
smallbeer
Faithful Member
 
Posts: 282
Joined: Sun Nov 27, 2005 3:23 pm

Re: code highlighter problem

by Draven » Tue Jul 04, 2006 3:13 pm

I've never looked at the code in question, but is there a chance it's expecting a closing tag with the XML part in it?

[/code xml]
Draven
Faithful Member
 
Posts: 760
Joined: Sun Jan 01, 2006 3:49 am

Re: code highlighter problem

by francois » Tue Jul 04, 2006 8:48 pm

Are you using the latest snipeet version? I updated it a week or so ago.

It should save something like
Code: Select all
<code>[xml]
$this->getFoo();</code>
francois
Faithful Member
 
Posts: 1638
Joined: Sat Oct 22, 2005 4:56 pm

Re: code highlighter problem

by smallbeer » Wed Jul 05, 2006 8:15 am

I've just borrowed code from the trac, I not using the full snippet app.

I'm going to have another look at it this afternoon.
smallbeer
Faithful Member
 
Posts: 282
Joined: Sun Nov 27, 2005 3:23 pm

Re: code highlighter problem

by smallbeer » Wed Jul 05, 2006 7:58 pm

This is driving me potty. I still can't get it to work as it should.

It either isn't getting as far as the geshiCall function or the geshi.php file isn't being included. I say this because if I include the geshi file in my model class I have to turn error reporting off because of all the deprecated var errors. I don't get than without including it in the model class though.

How can I tell if it's actually been included or not?

This is the code I'm using (exactly like the snipeet app):

Code: Select all
public function setBody($body)
{
  parent::setBody($body);
 
  $this->setHtmlBody(myToolkit::transformToHtml($body));
}


Code: Select all
 
class myToolkit
{
  private static function geshiCall($matches, $default = '')
  {
    require_once('geshi.php');
 
    if (preg_match('/^\[(.+?)\]\s*(.+)$/s', $matches[1], $match))
    {
      $geshi = new GeSHi(html_entity_decode($match[2]), $match[1]);
      $geshi->enable_classes();
      return @$geshi->parse_code();
    }
    else
    {
      if ($default)
      {
        $geshi = new GeSHi(html_entity_decode($matches[1]), $default);
        $geshi->enable_classes();
 
        return @$geshi->parse_code();
      }
      else
      {
        return "<pre><code>".$matches[1].'</pre></code>';
      }
    }
  }
 
  private static function geshiCallWithPHPAsDefault($matches)
  {
    return self::geshiCall($matches, 'php');
  }
 
  public static function transformToHtml($string)
  {
    $string = htmlentities($string, ENT_QUOTES, 'UTF-8');
    // transform [code] blocks to Markdown code blocks
    $lines  = explode(" ", $string);
    $incode = false;
    $string = '';
    foreach ($lines as $line)
    {
      if ($incode)
      {
        $line = '    '.html_entity_decode($line, ENT_QUOTES, 'UTF-8');
      }
      if (preg_match('/^\s*\[code\s*([^\]]*?)\]/', $line, $match))
      {
        $incode = true;
        $line   = $match[1] ? "      [".$match[1]."]" : "  ";
      }
      if (strpos($line, '[/code]') !== false)
      {
        $incode = false;
        $line   = ' ';
      }

      $string .= $line." ";
    }

    // Markdown formatting
    require_once('markdown.php');
    $html = markdown($string);

    // change class for command line stuff
    $html = preg_replace('/<pre><code>\$ /s', '<pre class="command-line"><code>$ ', $html);

    // change class for http:// link
    $html = preg_replace('#<pre><code>http\://#s', '<pre class="url"><code>http://', $html);

    // syntax highlighting
    $html = preg_replace_callback('#<pre><code>(.+?)</code></pre>#s', array('myToolkit', 'geshiCallWithPHPAsDefault'), $html);

    return $html;
  }
}
smallbeer
Faithful Member
 
Posts: 282
Joined: Sun Nov 27, 2005 3:23 pm

Re: code highlighter problem

by smallbeer » Fri Jul 07, 2006 10:59 am

it's now working as it should. I'm not 100% sure what fixed the problem but anyway... :)~ :-)~ :smile:
smallbeer
Faithful Member
 
Posts: 282
Joined: Sun Nov 27, 2005 3:23 pm