I'm Dan Previte, a web developer and a geek in general living in Chicago and working for Caxy Inc.

New Syntax Highlighter


Well, WordPress got borked yesterday when I installed a bunch of plugin updates. I fixed the syntax highlighting by writing my own plugin. It just takes Geshi and does some extra regular expressions to make it look the same in WordPress as it does in E-Texteditor. Here's an example:


  /**
   * Parser Controller
   *
   **/

  class Parser extends Controller {

    /**
     * Parser Constructor
     *
     * @return void
     **/

    public function Parser() {
      parent::Controller();
      $this->load->library('highlighter');
    }

    /**
     * Default Parser Page
     *
     * @return void
     **/

    public function index() {
      // We're going to test it by having it render itself.
      $code = file_get_contents(
        'system/application/controllers/parser.php'
      );

      $this->load->view('highlight_test', array(
        'code' => $this->do_highlight($code, 'php')
      ));
    }

    /**
     * Do the syntax highlighting
     *
     * @return string The highlighted source code
     **/

    private function do_highlight($code, $lang) {
      $highlighted_code = $this->highlighter->syntax(
        $code,
        $lang
      );
      // Run extras that Geshi missed
      $highlighted_code = $this->do_highlight_extras($highlighted_code, $lang);
      return $highlighted_code;
    }

    /**
     * Since Geshi sucks a fat cock, I have to put these here. I couldn't
     * get Geshi to do this for me. This should be redone in Geshi if possible.
     *
     * @return string The highlighted source code with highlighted extras
     **/

    private function do_highlight_extras($code, $lang) {
      // Define the extra function to run based on the selected language
      switch($lang) {
        case 'php':
          $extras = array(
            'class_names',
            'extends_class_names',
            'scope_resolution_operators',
            'dash_arrows'
          );
        break;
        default:
          // We don't have any to take care of, so just exit out
          return $code;
      }
      // Loop through the extra functions, and if they exist, run em.
      foreach($extras as $extra) {
        $method = 'highlight_' . $lang . '_' . $extra;
        $code = $this->$method($code);
      }
      return $code;
    }

    /**
     * PHP Source Extra
     * Find all of the class names and highlight them.
     *
     * @return string The highlighted source code including this extra
     **/

    private function highlight_php_class_names($code) {
      $result = preg_replace(
        '/\>class\<\/span\> ([A-Za-z0-9_]+)/',
        '>class</span> <span class="re3">$1</span>',
        $code
      );
      return $result;
    }

    /**
     * PHP Source Extra
     * Find all of the class names that extend another class and highlight them.
     *
     * @return string The highlighted source code including this extra
     **/

    private function highlight_php_extends_class_names($code) {
      $result = preg_replace(
        '/\>extends\<\/span\> ([A-Za-z0-9_]+)/',
        '>extends</span> <span class="re3 it">$1</span>',
        $code
      );
      return $result;
    }

    /**
     * PHP Source Extra
     * Find all ::'s and highlight them.
     *
     * @return string The highlighted source code including this extra
     **/

    private function highlight_php_scope_resolution_operators($code) {
      $result = str_replace(
        '::',
        '<span class="kw2">::</span>',
        $code
      );
      return $result;
    }

    /**
     * PHP Source Extra
     * Find all of the ->'s and highlight them.
     *
     * @return string The highlighted source code including this extra
     **/

    private function highlight_php_dash_arrows($code) {
      $result = str_replace(
        '-&gt;',
        '<span class="kw2">-&gt;</span>',
        $code
      );
      return $result;
    }

  }
 

Leave a Reply