OSDN Git Service

Import current code.
[osdn-codes/wiki-parser.git] / sfjp / wiki / plugin / pageoutline.php
1 <?php
2 namespace sfjp\Wiki\Plugin;
3 use sfjp\Wiki\Exception\Plugin_Error;
4 class PageOutline extends Base {
5         public $is_vary = false;
6         public $is_block = true;
7         public function process($args) {
8                 $proc = $this->processor;
9                 $opt = array('start' => 1, 'depth' => 3, 'type' => 'ordered');
10                 $opt['title'] = $this->__('Outline');
11                 $counter = array();
12
13                 foreach ($args as $arg) {
14                         if (!strpos($arg, '='))
15                                 return $this->error("unknown argument $arg");
16                         list($key, $value) = explode('=', $arg, 2);
17                         if (!$key || !array_key_exists($key, $opt))
18                                 return $this->error("unknown argument $arg");
19                         $opt[$key] = $value;
20                 }
21
22                 $text  = "";
23                 $match = array();
24                 $orig  = $proc->getText();
25                 $orig  = preg_replace('/^{{{.*?^}}}/sm', '', $orig);
26                 foreach($proc->getBlockstyleRules() as $r) {
27                         if ($r[0] != "heading")
28                                 continue;
29                         $headre = $r[1];
30                         break;
31                 }
32                 $parser = new \sfjp\Wiki\Parser(array());
33                 preg_match_all("/$headre/m", $orig, $match);
34                 for($h = 0; array_key_exists($h, $match[1]); $h++) {
35                         $level  = strlen($match[1][$h]);
36                         $label  = preg_replace('/<.*?>/', '', $parser->parse(preg_replace('/\[\[.*?\]\]/', '', $match[0][$h])));
37                         if (isset($match[3][$h]) && strlen($match[3][$h])) {
38                                 $linkid = trim($match[3][$h]);
39                         } else {
40                                 $linkid = "h" . ($level + intval($proc->getContext('head_excess'))) . "-";
41                                 $linkid .= preg_replace_callback($parser->processor->getInlinestyleRegex(),
42                                                                  create_function('$m', 'return empty($m[1]) ? "" : substr($m[0], 1);'),
43                                                                  trim($match[2][$h]));
44                         }
45                         if (!isset($counter)) $counter[$linkid] = 0;
46                         $c =& $counter[$linkid];
47                         if (++$c > 1) $linkid .= "-{$c}";
48                         $linkid = str_replace('%', '.', rawurlencode($linkid));
49
50                         $outlevel = $level - ($opt['start']-1);
51                         if ($outlevel > 0 && $outlevel <= $opt['depth'])
52                                 $text .= sprintf("%".($outlevel*2)."s%s [#%s %s]\n", '',
53                                                  ($opt['type'] == 'unordered' ? '*' : '1.'),
54                                                  $linkid, htmlspecialchars_decode($label));
55                 }
56                 return $this->getFormatter()->raw_node(
57                                                        '<div class="pageoutline"><div class="pageoutline-title">'
58                                                        . '<div class="action">'
59                                                        . '<button type="button" onClick="javascript:togglePageOutline(this)">'
60                                                        . '<img src="//static.sourceforge.jp/wiki/images/icons/roll-up.gif" border="0"></button>'
61                                                        . '</div>'
62                                                        . $this->__($opt['title']) 
63                                                        . '</div>' . $parser->parse($text) . '</div>');
64         }
65
66
67 }