2 namespace sfjp\Wiki\Formatter;
3 class HTML extends Base {
4 static public $singleton_tags = array('line', 'hr', 'image', 'img', 'newline', 'br');
8 function __construct() {
13 "underline" => array('span', array('style' => 'text-decoration: underline;')),
15 "superscript" => "sup",
21 "list_roma" => array("ol", array('style' => 'list-style-type: lower-roman;')),
22 "list_ROMA" => array("ol", array('style' => 'list-style-type: upper-roman;')),
23 "list_alpha" => array("ol", array('style' => 'list-style-type: lower-alpha;')),
24 "list_ALPHA" => array("ol", array('style' => 'list-style-type: upper-alpha;')),
26 "quote" => array("blockquote", array("class" => "citation")),
35 "indent" => array('div', array('class' => 'indent')),
38 "error" => array("span", array('class' => 'wiki-system-error')),
42 public function reset() {
46 public function cleanup() {
50 public function raw_node($string) {
54 public function text_node($text) {
55 return htmlspecialchars($text);
58 public function open_element($name, $opt=null) {
59 if (!isset($opt)) $opt = array();
62 return self::tag_builder("strong", $opt)."<em>";
65 self::tag_builder("table",
66 array_merge(array("class" => "wikitable"),
70 return self::tag_builder("a", $opt);
72 if (array_key_exists($name, $this->tagmap)) {
73 $taginfo = self::merge_tagopt($this->tagmap[$name], $opt);
74 return self::tag_builder($taginfo[0], $taginfo[1]);
76 return self::merge_tagopt($name, $opt);
81 public function close_element($name, $opt=array()) {
82 if (in_array($name, static::$singleton_tags))
83 return ''; # no close tag.
86 return "</em></strong>";
88 return "</tbody></table>";
92 if (array_key_exists($name, $this->tagmap)) {
93 $taginfo = self::merge_tagopt($this->tagmap[$name], $opt);
94 return self::tag_builder($taginfo[0], $taginfo[1], false);
101 public function element($name, $text = '', $opts = array()) {
102 return $this->open_element($name, $opts) .
103 $this->text_node($text) .
104 $this->close_element($name, $opts);
107 static protected function merge_tagopt($taginfo, $newopt) {
108 if (!is_array($taginfo))
109 $taginfo = array($taginfo, array());
110 if (!isset($newopt)) return $taginfo;
111 $taginfo[1] = $taginfo[1] + $newopt;
115 static function tag_builder($name, $opt=null, $open=true) {
116 $ret = "<".($open ? '' : '/').htmlspecialchars($name);
118 if ($name == "a" && array_key_exists("href", $opt) &&
119 !(array_key_exists("pass_unsecure", $opt) && $opt["pass_unsecure"])) {
120 $opt["href"] = preg_replace('/^(javascript|telnet|tel):/i', '', $opt["href"]);
123 if ($open && isset($opt) && is_array($opt) && count($opt) > 0 ) {
124 foreach($opt as $pname => $pvalue) {
125 if ($pname === "id") {
126 $pvalue = self::escape_id_value($pvalue);
128 if (isset($pvalue)) {
129 $ret .= " " . htmlspecialchars($pname) . '="'. htmlspecialchars($pvalue) .'"';
131 $ret .= " " . htmlspecialchars($pname);
134 } else if ($open && isset($opt) && !empty($opt)) {
138 $close = in_array($name, static::$singleton_tags) ? ' />' : '>';
139 return $ret . $close;