Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / Printer.php
blob14135fd8db0e2849ea5b49ff7b1717a3ee9f80bf
1 <?php
3 require_once 'HTMLPurifier/Generator.php';
4 require_once 'HTMLPurifier/Token.php';
5 require_once 'HTMLPurifier/Encoder.php';
7 class HTMLPurifier_Printer
10 /**
11 * Instance of HTMLPurifier_Generator for HTML generation convenience funcs
13 var $generator;
15 /**
16 * Instance of HTMLPurifier_Config, for easy access
18 var $config;
20 /**
21 * Initialize $generator.
23 function HTMLPurifier_Printer() {
24 $this->generator = new HTMLPurifier_Generator();
27 /**
28 * Main function that renders object or aspect of that object
29 * @param $config Configuration object
31 function render($config) {}
33 /**
34 * Returns a start tag
35 * @param $tag Tag name
36 * @param $attr Attribute array
38 function start($tag, $attr = array()) {
39 return $this->generator->generateFromToken(
40 new HTMLPurifier_Token_Start($tag, $attr ? $attr : array())
44 /**
45 * Returns an end teg
46 * @param $tag Tag name
48 function end($tag) {
49 return $this->generator->generateFromToken(
50 new HTMLPurifier_Token_End($tag)
54 /**
55 * Prints a complete element with content inside
56 * @param $tag Tag name
57 * @param $contents Element contents
58 * @param $attr Tag attributes
59 * @param $escape Bool whether or not to escape contents
61 function element($tag, $contents, $attr = array(), $escape = true) {
62 return $this->start($tag, $attr) .
63 ($escape ? $this->escape($contents) : $contents) .
64 $this->end($tag);
67 /**
68 * Prints a simple key/value row in a table.
69 * @param $name Key
70 * @param $value Value
72 function row($name, $value) {
73 if (is_bool($value)) $value = $value ? 'On' : 'Off';
74 return
75 $this->start('tr') . "\n" .
76 $this->element('th', $name) . "\n" .
77 $this->element('td', $value) . "\n" .
78 $this->end('tr')
82 /**
83 * Escapes a string for HTML output.
84 * @param $string String to escape
86 function escape($string) {
87 $string = HTMLPurifier_Encoder::cleanUTF8($string);
88 $string = htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
89 return $string;
92 /**
93 * Takes a list of strings and turns them into a single list
94 * @param $array List of strings
95 * @param $polite Bool whether or not to add an end before the last
97 function listify($array, $polite = false) {
98 if (empty($array)) return 'None';
99 $ret = '';
100 $i = count($array);
101 foreach ($array as $value) {
102 $i--;
103 $ret .= $value;
104 if ($i > 0 && !($polite && $i == 1)) $ret .= ', ';
105 if ($polite && $i == 1) $ret .= 'and ';
107 return $ret;
111 * Retrieves the class of an object without prefixes, as well as metadata
112 * @param $obj Object to determine class of
113 * @param $prefix Further prefix to remove
115 function getClass($obj, $sec_prefix = '') {
116 static $five = null;
117 if ($five === null) $five = version_compare(PHP_VERSION, '5', '>=');
118 $prefix = 'HTMLPurifier_' . $sec_prefix;
119 if (!$five) $prefix = strtolower($prefix);
120 $class = str_replace($prefix, '', get_class($obj));
121 $lclass = strtolower($class);
122 $class .= '(';
123 switch ($lclass) {
124 case 'enum':
125 $values = array();
126 foreach ($obj->valid_values as $value => $bool) {
127 $values[] = $value;
129 $class .= implode(', ', $values);
130 break;
131 case 'composite':
132 $values = array();
133 foreach ($obj->defs as $def) {
134 $values[] = $this->getClass($def, $sec_prefix);
136 $class .= implode(', ', $values);
137 break;
138 case 'multiple':
139 $class .= $this->getClass($obj->single, $sec_prefix) . ', ';
140 $class .= $obj->max;
141 break;
143 $class .= ')';
144 return $class;