(photos (map life (merge lluvia jose)))

Codevim

Codevim: Go to highlighter

<?php /* This highlighter use vim for the colors and the css's
    below for the color.
    Paste you code here, then write the lang for the code,
    like ruby, python, etc.
    For more info just use vim :-)  */ ?>
<?php
/* Author: Jose Figueroa Martinez <coloso at gmail dot com>
 * Date: 2007-05-16
 *
 * Use the file codevim_vim_location.php for set the variable
 * $vim_location
 * and set another vim executable.
 *
 * Requires vim7
 *
 * Updated on 2008-03-12.
 */

require('config.php');

class CodeVim{
  private $maxSize;

  function __construct(){
    global $vim_location, $tmp_dir;

    $this->maxSize  = 358400; // 350Kb
    $this->vim      = 'vim';  // vim executable
    $this->base     = dirname(__FILE__).DIRECTORY_SEPARATOR;
    $this->tmp      = '/tmp';

    $this->show_lines = 0;      // do not show line numbers

    if ($vim_location != ''){
        $this->vim = $vim_location;
    }
    if ($tmp_dir != ''){
        $this->tmp = $tmp_dir;
    }
  }

  // The principal function for highlight
  // Options: number_lines
  function scan($text = '', $lang = '', $show_lines = 0){
    if (strlen($text) > $this->maxSize){
      $lang = '';
    }
    $this->show_lines = intval($show_lines);
    return $this->process($text, $lang);
  }

  // Just a wrapper for the scan function
  function highlight($text = '', $lang = ''){
    return $this->scan($text, $lang);
  }

  private function process($text = '', $lang = ''){
    $this->runtime = $this->base.'runtime'.DIRECTORY_SEPARATOR;
    $code = '';

    // Fix lang
    $lang = substr($lang, 0, 20);

    while (preg_match('/\W/', $lang) > 0){
      $lang = preg_replace( '/\W/', '', $lang );
    }
    $conf = ":set runtimepath=$this->runtime\n:colors codevim\n:set filetype=$lang\n:TOhtml\n:wq\n:q\n:q!";

    $code_file     = tempnam($this->tmp, 'codevim_');
    $conf_file     = tempnam($this->tmp, 'codevim_conf_');
    file_put_contents($code_file, $text);
    file_put_contents($conf_file, $conf);

    $vim_call  = "$this->vim -f --nofork -X -n -u $this->runtime"."vimrc -s $conf_file $code_file";
    exec($vim_call);

    $code = file_get_contents($code_file.'.html');
    $code = $this->cutCode($code, $lang);

    unlink($code_file);
    unlink($code_file.'.html');
    unlink($conf_file);

    return $code;
  }

  private function cutCode($code, $lang){
    $lines = explode("\n", $code);
    unset($code);

    while (preg_match('/^<pre>$/',$lines[0] ) < 1){
      array_shift($lines);
    }
    array_shift($lines);

    $i = count($lines) - 1;
    while (preg_match('/^<\/pre>$/',$lines[$i] ) < 1){
      array_pop($lines);
      $i--;
    }
    array_pop($lines);

    // Adding line numbers
    if ($this->show_lines > 0) {
      $nlines  = count($lines);
      $max = strlen(strval($nlines));
      for ($i=0; $i<$nlines; $i++){
        $lines[$i] = $this->lineNumber(strval($i+1), $max).$lines[$i];
      }
    }

    array_unshift($lines, "<pre class=\"codevim\" lang=\"$lang\">");
    return implode("\n", $lines).'</pre>';
  }

  private function lineNumber($n, $max){
    $spaces = str_repeat(" ", $max - strlen($n));
    $n = "$spaces"."$n";
    return "<span class=\"LineNr\" title=\"$n\"></span>";
  }
}

?>