b2evolution PHP Cross Reference Blogging Systems

Source: /plugins/code_highlight_plugin/highlighters/php.highlighter.php - 162 lines - 6350 bytes - Summary - Text - Print

Description: This file is part of the AstonishMe Code plugin. This file is part of the b2evolution project - {@link http://b2evolution.net/}

   1  <?php
   2  /**
   3   * This file is part of the AstonishMe Code plugin.
   4   *
   5   * This file is part of the b2evolution project - {@link http://b2evolution.net/}
   6   *
   7   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}
   8   * Parts of this file are copyright (c)2005-2007 by Yabba/Scott - {@link http://astonishme.co.uk/contact/}.
   9   *
  10   * {@internal License choice
  11   * - If you have received this file as part of a package, please find the license.txt file in
  12   *   the same folder or the closest folder above for complete license terms.
  13   * - If you have received this file individually (e-g: from http://cvs.sourceforge.net/viewcvs.py/evocms/)
  14   *   then you must choose one of the following licenses before using the file:
  15   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  16   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  17   * }}
  18   *
  19   * {@internal Open Source relicensing agreement:
  20   * Yabba/Scott grant Francois PLANQUE the right to license
  21   * Yabba's/Scott's contributions to this file and the b2evolution project
  22   * under any OSI approved OSS license (http://www.opensource.org/licenses/).
  23   * }}
  24   *
  25   * @package plugins
  26   *
  27   * @author Yabba: Paul Jones - {@link http://astonishme.co.uk/}
  28   * @author Stk: Scott Kimler - {@link http://astonishme.co.uk/}
  29   *
  30   */
  31  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  32  
  33  /**
  34   * @package plugins
  35   * @todo
  36   * yabs > would like this to extend an "am_highlighter" class, but not sure how to handle $this->T_()
  37   * fp> is T_() actually used?
  38   * fp> you should probably just pass the plugin as argument to the constructor, this way you can call $plugin->T_()
  39   * yabs> it's only used in this one class, made the changes :)
  40   */
  41  class am_php_highlighter
  42  {
  43      /**
  44       * Array php functions
  45       *
  46       * @access private
  47       */
  48      var $php_functions = array();
  49  
  50      /**
  51       * Array php syntax highlighting colours
  52       *
  53       * @access private
  54       */
  55      var $highlight_colours = array();
  56  
  57  
  58      /**
  59       * Text name of language for display
  60       *
  61       * This is unused whilst "Experimental" as it requires a modification of the plugin
  62       * it would be used to replace the text output above the codeblock instead of ucfirst( language )
  63       *
  64       */
  65      var $language_title = 'PHP';
  66  
  67  
  68      /**
  69       * Boolean are we in strict mode ?
  70       *
  71       */
  72      var $strict_mode = false;
  73  
  74  
  75      /**
  76       * Called automatically on class innit
  77       *
  78       * @param object $parent
  79       * @return object am_php_highlighter
  80       */
  81  	function am_php_highlighter( & $parent )
  82      {
  83          $this->parent = & $parent;
  84          return $this;
  85      }
  86  
  87  
  88      /**
  89       * Highlights php ready for displaying
  90       * Links all known php functions to php.net documentation
  91       *
  92       * @param string $block - the code
  93       * @return string highlighted php code
  94       */
  95  
  96  	function highlight_code( $block )
  97      {
  98          // check if we've already grabbed existing php functions
  99          if( empty( $this->php_functions ) )
 100          {    // lets build a list of all native php functions
 101              $this->php_functions = get_defined_functions();
 102              // and add any missing ones
 103              // possible enhancement is to link all evo functions to it's own docs - yabs
 104              $this->php_functions['internal'] = array_merge( $this->php_functions['internal'], array( 'die', 'exit', 'array', 'require', 'require_once', 'include', 'include_once' ) );
 105          }
 106  
 107          // check if we've already grabbed the highlight colours
 108          if( empty( $this->highlight_colours ) )
 109          {    // get the users php_ini colours for highlight_string()
 110              $this->highlight_colours = array(
 111                      'highlight_bg' => '<span class="#FFFFFF',
 112                      'highlight_comment' => '<span class="#FF8000',
 113                      'highlight_default' => '<span class="#0000BB',
 114                      'highlight_html' => '<span class="#000000',
 115                      'highlight_keyword' => '<span class="#007700',
 116                      'highlight_string' => '<span class="#DD0000',
 117                  );
 118          }
 119          // lets sort out the code and highlighting
 120          $code = // take a deep breath and read this code upwards ;)
 121              // stitch it all back together again
 122              implode( "\n",
 123              // gets rid of the < ?php & * / that we added
 124              array_slice( explode( "\n",
 125              // clean it all up ready for numbering
 126              $this->parent->tidy_code_output(
 127              // find all potential php functions and link relevant ones to the documentation
 128              preg_replace_callback( '#([a-z0-9\-_]+?)(\</span>\<span class="amc_keyword">)?\(#i', array( $this, 'link_to_manual' ),
 129              // change the php.ini highlight colours to our class names
 130              str_replace( $this->highlight_colours, array( '<span class="amc_background', '<span class="amc_comment', '<span class="amc_default', '<span class="amc_html', '<span class="amc_keyword', '<span class="amc_string' ),
 131              // convert php 4's <font> tags to <span> & prepare to convert php 4 & 5 to our class names
 132              preg_replace( array( '#\<font color="\#([0-9a-f]+?)">#i', '#\</font>#', '#\<span style="color: \#([0-9a-f]+?)">#i' ), array( '<span class="#$1">', '</span>','<span class="#$1">' ),
 133              // remove <code></code> tags and \n's added by highlight_string
 134              str_replace( array( '<code>', '</code>', "\n" ), '',
 135              // top code with < ?php & to ensure highlight occurs
 136              highlight_string( '<?php'."\n"
 137              // convert relevant entities back for highlighting
 138              .str_replace( array( '&lt;', '&gt;', '&amp;', '&quot;' ), array( '<', '>', '&', '"' ),
 139              // get rid of empty start/end lines, and add */ to the end to overcome highlight_string() bug with unterminated comments
 140               trim( $block ) )."\n".'*/', true)
 141              ) ) ) ), '<br />' )
 142              // get rid of the first and last array elements
 143              ), 1, -1 ) );
 144          return $code;
 145      }
 146  
 147  
 148      /**
 149       * Links a php function to the documentation
 150       *
 151       * @param array $function ( 1 - function name, 2 - additional trailing html )
 152       * @return string php.net documentation link if appropriate
 153       */
 154  	function link_to_manual( $function )
 155      {    // check if $function is a native php function and provide a link to the documentation if true
 156          // if not in xhtml strict mode ( setting ) then add target="_blank"
 157          return ( in_array( trim( $function[1] ), $this->php_functions['internal'] ) ? sprintf( '<a href="http://www.php.net/function.%1$s" title=" %2$s : '.$function[1].'() "'.( $this->strict_mode ? '' : ' target="_blank"' ).' class="codeblock_external_link">%1$s</a>', trim( $function[1] ), $this->parent->T_( 'Read the PHP.net documentation for' ) ) : $function[1] ).( empty( $function[2] ) ? '' : $function[2] ).'(';
 158      }
 159  
 160  }
 161  
 162  ?>

title

Description

title

Description

title

Description

title

title

Body