[ Index ]
 

Code source de Serendipity 1.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/bundled-libs/Text/Wiki/Rule/ -> code.php (source)

   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/12/02 10:54:32 nohn Exp $
  20  
  21  
  22  /**
  23  * 
  24  * This class implements a Text_Wiki_Rule to find sections marked as code
  25  * examples.  Blocks are marked as the string <code> on a line by itself,
  26  * followed by the inline code example, and terminated with the string
  27  * </code> on a line by itself.  The code example is run through the
  28  * native PHP highlight_string() function to colorize it, then surrounded
  29  * with <pre>...</pre> tags when rendered as XHTML.
  30  *
  31  * @author Paul M. Jones <pmjones@ciaweb.net>
  32  *
  33  * @package Text_Wiki
  34  *
  35  */
  36  
  37  class Text_Wiki_Rule_code extends Text_Wiki_Rule {
  38      
  39      
  40      /**
  41      * 
  42      * The regular expression used to find source text matching this
  43      * rule.
  44      * 
  45      * @access public
  46      * 
  47      * @var string
  48      * 
  49      */
  50      
  51      var $regex = '/^(\<code( .+)?\>)\n(.+)\n(\<\/code\>)(\s|$)/Umsi';
  52      
  53      
  54      /**
  55      * 
  56      * Generates a token entry for the matched text.  Token options are:
  57      * 
  58      * 'text' => The full matched text, not including the <code></code> tags.
  59      * 
  60      * @access public
  61      *
  62      * @param array &$matches The array of matches from parse().
  63      *
  64      * @return A delimited token number to be used as a placeholder in
  65      * the source text.
  66      *
  67      */
  68      
  69      function process(&$matches)
  70      {
  71          // are there additional arguments?
  72          $args = trim($matches[2]);
  73          
  74          if ($args == '') {
  75              $options = array(
  76                  'text' => $matches[3],
  77                  'args' => array('type' => '')
  78              );
  79          } else {
  80              $options = array(
  81                  'text' => $matches[3],
  82                  'args' => $this->getMacroArgs($args)
  83              );
  84          }
  85          
  86          return $this->addToken($options) . $matches[5];
  87      }
  88      
  89      
  90      /**
  91      * 
  92      * Renders a token into text matching the requested format.
  93      * 
  94      * @access public
  95      * 
  96      * @param array $options The "options" portion of the token (second
  97      * element).
  98      * 
  99      * @return string The text rendered from the token options.
 100      * 
 101      */
 102      
 103      function renderXhtml($options)
 104      {
 105          // trim opening and closing whitespace
 106          $text = trim($options['text']);
 107          $args = $options['args'];
 108          
 109          if (strtolower($args['type']) == 'php') {
 110              
 111              // PHP code example
 112              
 113              // add the PHP tags
 114              $text = "<?php\n" . $options['text'] . "\n?>"; // <?php
 115              
 116              // convert tabs to four spaces
 117              $text = str_replace("\t", "    ", $text);
 118              
 119              // colorize the code block (also converts HTML entities and adds
 120              // <code>...</code> tags)
 121              ob_start();
 122              highlight_string($text);
 123              $text = ob_get_contents();
 124              ob_end_clean();
 125              
 126              // replace <br /> tags with simple newlines
 127              //$text = str_replace("<br />", "\n", $text);
 128              
 129              // replace non-breaking space with simple spaces
 130              //$text = str_replace("&nbsp;", " ", $text);
 131              
 132              // replace <br /> tags with simple newlines
 133              // replace non-breaking space with simple spaces
 134              // translate old HTML to new XHTML
 135              // courtesy of research by A. Kalin :-)
 136              $map = array(
 137                  '<br />'  => "\n",
 138                  '&nbsp;'  => ' ',
 139                  '<font'   => '<span',
 140                  '</font>' => '</span>',
 141                  'color="' => 'style="color:'
 142              );
 143              $text = strtr($text, $map);
 144             
 145              // get rid of the last newline inside the code block
 146              // (becuase higlight_string puts one there)
 147              if (substr($text, -8) == "\n</code>") {
 148                  $text = substr($text, 0, -8) . "</code>";
 149              }
 150              
 151              // done
 152              $text = "<pre>$text</pre>";
 153          
 154          } elseif (strtolower($args['type']) == 'html') {
 155          
 156              // HTML code example:
 157              // add <html> opening and closing tags,
 158              // convert tabs to four spaces,
 159              // convert entities.
 160              $text = str_replace("\t", "    ", $text);
 161              $text = "<html>\n$text\n</html>";
 162              $text = htmlentities($text);
 163              $text = "<pre><code>$text</code></pre>";
 164              
 165          } else {
 166              // generic code example:
 167              // convert tabs to four spaces,
 168              // convert entities.
 169              $text = str_replace("\t", "    ", $text);
 170              $text = htmlentities($text);
 171              $text = "<pre><code>$text</code></pre>";
 172          }
 173          
 174          return "\n$text\n";
 175      }
 176  }
 177  ?>


Généré le : Sat Nov 24 09:00:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics