OSDN Git Service

Import current code.
[osdn-codes/wiki-parser.git] / sfjp / wiki / formatter / html.php
1 <?php
2 namespace sfjp\Wiki\Formatter;
3 class HTML extends Base {
4         static public $singleton_tags = array('line', 'hr', 'image', 'img', 'newline', 'br');
5
6         public $tagmap;
7
8         function __construct() {
9                 $this->tagmap = array(
10                                       "bold"        => "strong",
11                                       "italic"      => "em",
12                                       "strike"      => "del",
13                                       "underline"   => array('span', array('style' => 'text-decoration: underline;')),
14                                       "monospace"   => "tt",
15                                       "superscript" => "sup",
16                                       "subscript"   => "sub",
17                                       "table_row"   => "tr",
18                                       "table_col"   => "td",
19                                       "list_mark"   => "ul",
20                                       "list_num"    => "ol",
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;')),
25                                       "list_item"   => "li",
26                                       "quote"       => array("blockquote", array("class" => "citation")),
27                                       "paragraph"   => "p",
28                                       "heading1"    => "h1",
29                                       "heading2"    => "h2",
30                                       "heading3"    => "h3",
31                                       "heading4"    => "h4",
32                                       "heading5"    => "h5",
33                                       "heading6"    => "h6",
34                                       "line"        => "hr",
35                                       "indent"      => array('div', array('class' => 'indent')),
36                                       "image"       => "img",
37                                       "newline"     => "br",
38                                       "error"       => array("span", array('class' => 'wiki-system-error')),
39                                       );
40         }
41
42         public function reset() {
43                 ; # nothing
44                           }
45
46         public function cleanup() {
47     
48         }
49
50         public function raw_node($string) {
51                 return $string;
52         }
53
54         public function text_node($text) {
55                 return htmlspecialchars($text);
56         }
57
58         public function open_element($name, $opt=null) {
59                 if (!isset($opt)) $opt = array();
60                 switch ($name) {
61                 case "bolditalic":
62                         return self::tag_builder("strong", $opt)."<em>";
63                 case "table":
64                         return
65                                 self::tag_builder("table",
66                                                   array_merge(array("class" => "wikitable"),
67                                                               $opt))
68                                 . '<tbody>';
69                 case "link":
70                         return self::tag_builder("a", $opt);
71                 default:
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]);
75                         } else {
76                                 return self::merge_tagopt($name, $opt);
77                         }
78                 }
79         }
80
81         public function close_element($name, $opt=array()) {
82                 if (in_array($name, static::$singleton_tags))
83                         return ''; # no close tag.
84                 switch ($name) {
85                 case "bolditalic":
86                 return "</em></strong>";
87                 case "table":
88                         return "</tbody></table>";
89                 case "link":
90                         return "</a>";
91                 default:
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);
95                         } else {
96                                 return "</$name>";
97                         }
98                 }
99         }
100
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);
105         }
106
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;
112                 return $taginfo;
113         }
114
115         static function tag_builder($name, $opt=null, $open=true) {
116                 $ret = "<".($open ? '' : '/').htmlspecialchars($name);
117
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"]);
121                 }
122
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);
127                                 }
128                                 if (isset($pvalue)) {
129                                         $ret .= " " . htmlspecialchars($pname) . '="'. htmlspecialchars($pvalue) .'"';
130                                 } else {
131                                         $ret .= " " . htmlspecialchars($pname);
132                                 }
133                         }
134                 } else if ($open && isset($opt) && !empty($opt)) {
135                         $ret .= " $opt";
136                 }
137                 
138                 $close = in_array($name, static::$singleton_tags) ? ' />' : '>';
139                 return $ret . $close;
140         }
141 }