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

Source for file code.php

Documentation is available at code.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: code.php,v 1.4 2004/05/21 21:11:59 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 = '/^(\<code( .+)?\>)\n(.+)\n(\<\/code\>)(\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.         // are there additional arguments?
  70.         $args trim($matches[2]);
  71.         
  72.         if ($args == ''{
  73.             $options = array(
  74.                 'text' => $matches[3],
  75.                 'args' => array('type' => '')
  76.             );
  77.         else {
  78.             $options = array(
  79.                 'text' => $matches[3],
  80.                 'args' => $this->getMacroArgs($args)
  81.             );
  82.         }
  83.         
  84.         return $this->addToken($options$matches[5];
  85.     }
  86.     
  87.     
  88.     /**
  89.     * 
  90.     * Renders a token into text matching the requested format.
  91.     * 
  92.     * @access public
  93.     * 
  94.     * @param array $options The "options" portion of the token (second
  95.     *  element).
  96.     * 
  97.     * @return string The text rendered from the token options.
  98.     * 
  99.     */
  100.     
  101.     function renderXhtml($options)
  102.     {
  103.         $text $options['text'];
  104.         $args $options['args'];
  105.         
  106.         if (strtolower($args['type']== 'php'{
  107.             
  108.             // PHP code example
  109.             
  110.             // add the PHP tags
  111.             $text "<?php\n" $options['text'"\n?>"// <?php
  112.             
  113.             // convert tabs to four spaces
  114.             $text str_replace("\t""    "$text);
  115.             
  116.             // colorize the code block (also converts HTML entities and adds
  117.             // <code>...</code> tags)
  118.             ob_start();
  119.             highlight_string($text);
  120.             $text ob_get_contents();
  121.             ob_end_clean();
  122.             
  123.             // replace <br /> tags with simple newlines
  124.             //$text = str_replace("<br />", "\n", $text);
  125.             
  126.             // replace non-breaking space with simple spaces
  127.             //$text = str_replace("&nbsp;", " ", $text);
  128.             
  129.             // replace <br /> tags with simple newlines
  130.             // replace non-breaking space with simple spaces
  131.             // translate old HTML to new XHTML
  132.             // courtesy of research by A. Kalin :-)
  133.             $map = array(
  134.                 '<br />'  => "\n",
  135.                 '&nbsp;'  => ' ',
  136.                 '<font'   => '<span',
  137.                 '</font>' => '</span>',
  138.                 'color="' => 'style="color:'
  139.             );
  140.             $text strtr($text$map);
  141.            
  142.             // get rid of the last newline inside the code block
  143.             // (becuase higlight_string puts one there)
  144.             if (substr($text-8== "\n</code>"{
  145.                 $text substr($text0-8"</code>";
  146.             }
  147.             
  148.             // done
  149.             $text = "<pre>$text</pre>";
  150.         
  151.         elseif (strtolower($args['type']== 'html'{
  152.         
  153.             // HTML code example:
  154.             // add <html> opening and closing tags,
  155.             // convert tabs to four spaces,
  156.             // convert entities.
  157.             $text str_replace("\t""    "$text);
  158.             $text = "<html>\n$text\n</html>";
  159.             $text htmlentities($text);
  160.             $text = "<pre><code>$text</code></pre>";
  161.             
  162.         else {
  163.             // generic code example:
  164.             // convert tabs to four spaces,
  165.             // convert entities.
  166.             $text str_replace("\t""    "$text);
  167.             $text htmlentities($text);
  168.             $text = "<pre><code>$text</code></pre>";
  169.         }
  170.         
  171.         return "\n$text\n";
  172.     }
  173. }
  174. ?>

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