Source for file Printer.php

Documentation is available at Printer.php

  1. <?php
  2.  
  3. // OUT OF DATE, NEEDS UPDATING!
  4. // USE XMLWRITER!
  5.  
  6. {
  7.     
  8.     /**
  9.      * Instance of HTMLPurifier_Generator for HTML generation convenience funcs
  10.      */
  11.     protected $generator;
  12.     
  13.     /**
  14.      * Instance of HTMLPurifier_Config, for easy access
  15.      */
  16.     protected $config;
  17.     
  18.     /**
  19.      * Initialize $generator.
  20.      */
  21.     public function __construct({
  22.     }
  23.     
  24.     /**
  25.      * Give generator necessary configuration if possible
  26.      */
  27.     public function prepareGenerator($config{
  28.         $all $config->getAll();
  29.         $context new HTMLPurifier_Context();
  30.         $this->generator = new HTMLPurifier_Generator($config$context);
  31.     }
  32.     
  33.     /**
  34.      * Main function that renders object or aspect of that object
  35.      * @note Parameters vary depending on printer
  36.      */
  37.     // function render() {}
  38.     
  39.     /**
  40.      * Returns a start tag
  41.      * @param $tag Tag name
  42.      * @param $attr Attribute array
  43.      */
  44.     protected function start($tag$attr array()) {
  45.         return $this->generator->generateFromToken(
  46.                     new HTMLPurifier_Token_Start($tag$attr $attr array())
  47.                );
  48.     }
  49.     
  50.     /**
  51.      * Returns an end teg
  52.      * @param $tag Tag name
  53.      */
  54.     protected function end($tag{
  55.         return $this->generator->generateFromToken(
  56.                     new HTMLPurifier_Token_End($tag)
  57.                );
  58.     }
  59.     
  60.     /**
  61.      * Prints a complete element with content inside
  62.      * @param $tag Tag name
  63.      * @param $contents Element contents
  64.      * @param $attr Tag attributes
  65.      * @param $escape Bool whether or not to escape contents
  66.      */
  67.     protected function element($tag$contents$attr array()$escape true{
  68.         return $this->start($tag$attr.
  69.                ($escape $this->escape($contents$contents.
  70.                $this->end($tag);
  71.     }
  72.     
  73.     protected function elementEmpty($tag$attr array()) {
  74.         return $this->generator->generateFromToken(
  75.             new HTMLPurifier_Token_Empty($tag$attr)
  76.         );
  77.     }
  78.     
  79.     protected function text($text{
  80.         return $this->generator->generateFromToken(
  81.             new HTMLPurifier_Token_Text($text)
  82.         );
  83.     }
  84.     
  85.     /**
  86.      * Prints a simple key/value row in a table.
  87.      * @param $name Key
  88.      * @param $value Value
  89.      */
  90.     protected function row($name$value{
  91.         if (is_bool($value)) $value $value 'On' 'Off';
  92.         return
  93.             $this->start('tr'"\n" .
  94.                 $this->element('th'$name"\n" .
  95.                 $this->element('td'$value"\n" .
  96.             $this->end('tr')
  97.         ;
  98.     }
  99.     
  100.     /**
  101.      * Escapes a string for HTML output.
  102.      * @param $string String to escape
  103.      */
  104.     protected function escape($string{
  105.         $string HTMLPurifier_Encoder::cleanUTF8($string);
  106.         $string htmlspecialchars($stringENT_COMPAT'UTF-8');
  107.         return $string;
  108.     }
  109.     
  110.     /**
  111.      * Takes a list of strings and turns them into a single list
  112.      * @param $array List of strings
  113.      * @param $polite Bool whether or not to add an end before the last
  114.      */
  115.     protected function listify($array$polite false{
  116.         if (empty($array)) return 'None';
  117.         $ret '';
  118.         $i count($array);
  119.         foreach ($array as $value{
  120.             $i--;
  121.             $ret .= $value;
  122.             if ($i && !($polite && $i == 1)) $ret .= ', ';
  123.             if ($polite && $i == 1$ret .= 'and ';
  124.         }
  125.         return $ret;
  126.     }
  127.     
  128.     /**
  129.      * Retrieves the class of an object without prefixes, as well as metadata
  130.      * @param $obj Object to determine class of
  131.      * @param $prefix Further prefix to remove
  132.      */
  133.     protected function getClass($obj$sec_prefix ''{
  134.         static $five null;
  135.         if ($five === null$five version_compare(PHP_VERSION'5''>=');
  136.         $prefix 'HTMLPurifier_' $sec_prefix;
  137.         if (!$five$prefix strtolower($prefix);
  138.         $class str_replace($prefix''get_class($obj));
  139.         $lclass strtolower($class);
  140.         $class .= '(';
  141.         switch ($lclass{
  142.             case 'enum':
  143.                 $values array();
  144.                 foreach ($obj->valid_values as $value => $bool{
  145.                     $values[$value;
  146.                 }
  147.                 $class .= implode(', '$values);
  148.                 break;
  149.             case 'css_composite':
  150.                 $values array();
  151.                 foreach ($obj->defs as $def{
  152.                     $values[$this->getClass($def$sec_prefix);
  153.                 }
  154.                 $class .= implode(', '$values);
  155.                 break;
  156.             case 'css_multiple':
  157.                 $class .= $this->getClass($obj->single$sec_prefix', ';
  158.                 $class .= $obj->max;
  159.                 break;
  160.         }
  161.         $class .= ')';
  162.         return $class;
  163.     }
  164.     
  165. }

Documentation generated on Thu, 19 Jun 2008 18:50:13 -0400 by phpDocumentor 1.4.2