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.  
  3.     
  4.     var $conf = array(
  5.         'css'      => null// class for <pre>
  6.         'css_code' => null// class for generic <code>
  7.         'css_php'  => null// class for PHP <code>
  8.         'css_html' => null // class for HTML <code>
  9.     );
  10.     
  11.     /**
  12.     * 
  13.     * Renders a token into text matching the requested format.
  14.     * 
  15.     * @access public
  16.     * 
  17.     * @param array $options The "options" portion of the token (second
  18.     *  element).
  19.     * 
  20.     * @return string The text rendered from the token options.
  21.     * 
  22.     */
  23.     
  24.     function token($options)
  25.     {
  26.         $text $options['text'];
  27.         $attr $options['attr'];
  28.         $type strtolower($attr['type']);
  29.         
  30.         $css      $this->formatConf(' class="%s"''css');
  31.         $css_code $this->formatConf(' class="%s"''css_code');
  32.         $css_php  $this->formatConf(' class="%s"''css_php');
  33.         $css_html $this->formatConf(' class="%s"''css_html');
  34.         
  35.         if ($type == 'php'{
  36.             
  37.             // PHP code example:
  38.             // add the PHP tags
  39.             $text "<?php\n" $options['text'"\n?>"// <?php
  40.             
  41.             // convert tabs to four spaces
  42.             $text str_replace("\t""    "$text);
  43.             
  44.             // colorize the code block (also converts HTML entities and adds
  45.             // <code>...</code> tags)
  46.             ob_start();
  47.             highlight_string($text);
  48.             $text ob_get_contents();
  49.             ob_end_clean();
  50.             
  51.             // replace <br /> tags with simple newlines.
  52.             // replace non-breaking space with simple spaces.
  53.             // translate HTML <font> and color to XHTML <span> and style.
  54.             // courtesy of research by A. Kalin :-).
  55.             $map = array(
  56.                 '<br />'  => "\n",
  57.                 '&nbsp;'  => ' ',
  58.                 '<font'   => '<span',
  59.                 '</font>' => '</span>',
  60.                 'color="' => 'style="color:'
  61.             );
  62.             $text strtr($text$map);
  63.            
  64.             // get rid of the last newline inside the code block
  65.             // (becuase higlight_string puts one there)
  66.             if (substr($text-8== "\n</code>"{
  67.                 $text substr($text0-8"</code>";
  68.             }
  69.             
  70.             // replace all <code> tags with classed tags
  71.             if ($css_php{
  72.                 $text str_replace('<code>'"<code$css_php>"$text);
  73.             }
  74.             
  75.             // done
  76.             $text = "<pre$css>$text</pre>";
  77.         
  78.         elseif ($type == 'html' || $type == 'xhtml'{
  79.         
  80.             // HTML code example:
  81.             // add <html> opening and closing tags,
  82.             // convert tabs to four spaces,
  83.             // convert entities.
  84.             $text str_replace("\t""    "$text);
  85.             $text = "<html>\n$text\n</html>";
  86.             $text htmlentities($text);
  87.             $text = "<pre$css><code$css_html>$text</code></pre>";
  88.             
  89.         else {
  90.             // generic code example:
  91.             // convert tabs to four spaces,
  92.             // convert entities.
  93.             $text str_replace("\t""    "$text);
  94.             $text htmlentities($text);
  95.             $text = "<pre$css><code$css_code>$text</code></pre>";
  96.         }
  97.         
  98.         return "\n$text\n\n";
  99.     }
  100. }
  101. ?>

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