[ Index ]
 

Code source de Serendipity 1.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/bundled-libs/Text/Wiki/Rule/ -> phpcode.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: phpcode.php,v 1.3 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_phpcode 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 = '/^(\<php\>)\n(.+)\n(\<\/php\>)(\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          $options = array('text' => $matches[2]);
  72          return $this->addToken($options) . $matches[4];
  73      }
  74      
  75      
  76      /**
  77      * 
  78      * Renders a token into text matching the requested format.
  79      * 
  80      * @access public
  81      * 
  82      * @param array $options The "options" portion of the token (second
  83      * element).
  84      * 
  85      * @return string The text rendered from the token options.
  86      * 
  87      */
  88      
  89      function renderXhtml($options)
  90      {
  91          // add the PHP tags
  92          $text = "<?php\n" . $options['text'] . "\n?>"; // <?php
  93          
  94          // convert tabs to four spaces
  95          $text = str_replace("\t", "    ", $text);
  96          
  97          // colorize the code block (also converts HTML entities and adds
  98          // <code>...</code> tags)
  99          ob_start();
 100          highlight_string($text);
 101          $text = ob_get_contents();
 102          ob_end_clean();
 103          
 104          // replace <br /> tags with simple newlines
 105          //$text = str_replace("<br />", "\n", $text);
 106          
 107          // replace non-breaking space with simple spaces
 108          //$text = str_replace("&nbsp;", " ", $text);
 109          
 110          // replace <br /> tags with simple newlines
 111          // replace non-breaking space with simple spaces
 112          // translate old HTML to new XHTML
 113          // courtesy of research by A. Kalin :-)
 114          $map = array(
 115              '<br />'  => "\n",
 116              '&nbsp;'  => ' ',
 117              '<font'   => '<span',
 118              '</font>' => '</span>',
 119              'color="' => 'style="color:'
 120          );
 121          $text = strtr($text, $map);
 122         
 123          // get rid of the last newline inside the code block
 124          // (becuase higlight_string puts one there)
 125          if (substr($text, -8) == "\n</code>") {
 126              $text = substr($text, 0, -8) . "</code>";
 127          }
 128          
 129         // done
 130          return "\n<pre>$text</pre>\n";
 131      }
 132  }
 133  ?>


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