Text_Wiki
[ class tree: Text_Wiki ] [ index: Text_Wiki ] [ all elements ]

Source for file phpcode.php

Documentation is available at phpcode.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at                              |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Paul M. Jones <pmjones@ciaweb.net>                          |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: phpcode.php,v 1.1 2004/01/04 01:35:23 pmjones Exp $
  20.  
  21.  
  22. /**
  23. * This class implements a Text_Wiki_Rule to find sections marked as code
  24. * examples.  Blocks are marked as the string <code> on a line by itself,
  25. * followed by the inline code example, and terminated with the string
  26. * </code> on a line by itself.  The code example is run through the
  27. * native PHP highlight_string() function to colorize it, then surrounded
  28. * with <pre>...</pre> tags when rendered as XHTML.
  29. *
  30. @author Paul M. Jones <pmjones@ciaweb.net>
  31. *
  32. @package Text_Wiki
  33. *
  34. */
  35.  
  36.     
  37.     
  38.     /**
  39.     * 
  40.     * The regular expression used to find source text matching this
  41.     * rule.
  42.     * 
  43.     * @access public
  44.     * 
  45.     * @var string 
  46.     * 
  47.     */
  48.     
  49.     var $regex = '/^(\<php\>)\n(.+)\n(\<\/php\>)(\s|$)/Umsi';
  50.     
  51.     
  52.     /**
  53.     * 
  54.     * Generates a token entry for the matched text.  Token options are:
  55.     * 
  56.     * 'text' => The full matched text, not including the <code></code> tags.
  57.     * 
  58.     * @access public
  59.     *
  60.     * @param array &$matches The array of matches from parse().
  61.     *
  62.     * @return delimited token number to be used as a placeholder in
  63.     *  the source text.
  64.     *
  65.     */
  66.     
  67.     function process(&$matches)
  68.     {    
  69.         $options = array('text' => $matches[2]);
  70.         return $this->addToken($options$matches[4];
  71.     }
  72.     
  73.     
  74.     /**
  75.     * 
  76.     * Renders a token into text matching the requested format.
  77.     * 
  78.     * @access public
  79.     * 
  80.     * @param array $options The "options" portion of the token (second
  81.     *  element).
  82.     * 
  83.     * @return string The text rendered from the token options.
  84.     * 
  85.     */
  86.     
  87.     function renderXhtml($options)
  88.     {
  89.         // add the PHP tags
  90.         $text "<?php\n" $options['text'"\n?>"// <?php
  91.         
  92.         // convert tabs to four spaces
  93.         $text str_replace("\t""    "$text);
  94.         
  95.         // colorize the code block (also converts HTML entities and adds
  96.         // <code>...</code> tags)
  97.         ob_start();
  98.         highlight_string($text);
  99.         $text ob_get_contents();
  100.         ob_end_clean();
  101.         
  102.         // replace <br /> tags with simple newlines
  103.         $text str_replace("<br />""\n"$text);
  104.         
  105.         // replace non-breaking space with simple spaces
  106.         $text str_replace("&nbsp;"" "$text);
  107.         
  108.         // get rid of the last newline inside the code block
  109.         // (becuase higlight_string puts one there)
  110.         if (substr($text-8== "\n</code>"{
  111.             $text substr($text0-8"</code>";
  112.         }
  113.         
  114.         return "\n<pre>$text</pre>\n";
  115.     }
  116. }
  117. ?>

Documentation generated on Mon, 11 Mar 2019 10:14:14 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.