OSDN Git Service

Import current code.
[osdn-codes/wiki-parser.git] / sfjp / wiki / plugin / base.php
1 <?php
2 namespace sfjp\Wiki\Plugin;
3 use sfjp\Wiki\Exception\Plugin_Error;
4 abstract class Base {
5         public $is_vary  = true;
6         public $is_block = false;
7         public $processor;
8
9         function __construct($processor) {
10                 $this->processor = $processor;
11         }
12
13         public function setContext($ctx) {
14                 return $this->processor->setContext($ctx);
15         }
16
17         public function getContext($key=null) {
18                 return $this->processor->getContext($key);
19         }
20
21         public function error($msg) {
22                 $this->setContext(array('whole_page_cachable' => false));
23                 throw new Plugin_Error($msg);
24         }
25
26         public function __($text, $args=array()) {
27                 if (!$this->processor->hasContext('i18n')) {
28                         return $text;
29                 } else {
30                         return $this->getContext('i18n')->__($text, $args);
31                 }
32         }
33
34         public function getProcessor() {
35                 return $this->processor;
36         }
37
38         public function getFormatter() {
39                 return $this->getProcessor()->getFormatter();
40         }
41
42         public function parseArgs($args, $list = array(), $default = null) {
43                 $opt = array();
44                 foreach ($args as $arg) {
45                         if (strpos($arg, '=')) {
46                                 list($key, $value) = explode('=', $arg, 2);
47                                 if (!$key)
48                                         continue;
49                         } elseif (isset($default) && !empty($default)) {
50                                 $key = $default;
51                                 $value = $arg;
52                         }
53                         if (!in_array($key, $list) && !array_key_exists($key, $list)) {
54                                 $this->error("unknown argument $arg");
55                         }
56                         $opt[$key] = $value;
57                 }
58                 return $opt;
59         }
60
61         public function render_table($data) {
62                 $fmt = $this->getFormatter();
63                 $ret = '';
64
65                 $ret .= $fmt->open_element('table');
66                 foreach ($data as $cols) {
67                         $ret .= $fmt->open_element('table_row');
68                         foreach ($cols as $col) {
69                                 $ret .= $fmt->open_element('table_col');
70                                 $ret .= $col;
71                                 $ret .= $fmt->close_element('table_col');
72                         }
73                         $ret .= $fmt->close_element('table_row');
74                 }
75                 $ret .= $fmt->close_element('table');
76                 return $fmt->raw_node($ret);
77         }
78
79         public function render_date_separated_list($data) {
80                 $wikistr = '';
81                 $last_date = null;
82
83                 foreach ($data as $item) {
84                         if ($last_date != date('Y-m-d', $item[0])) 
85                                 $wikistr .= "==== " . date('Y-m-d', $item[0]) . " ====\n";
86                         $last_date = date('Y-m-d', $item[0]);
87                         $wikistr .= " * " . $item[1];
88                 }
89                 $p = new \sfjp\Wiki\Parser($this->getContext());
90                 return $this->getFormatter()->raw_node($p->parse($wikistr));
91         }
92
93 }