1<?php
2/**
3 * DokuWiki Plugin strata (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Brend Wanders <b.wanders@utwente.nl>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die('Meh.');
11
12/**
13 * Simple plugin to list the available types. This plugin uses
14 * the same syntax as the info plugin, but only accepts a specific
15 * info category.
16 */
17class syntax_plugin_strata_info extends DokuWiki_Syntax_Plugin {
18    public function __construct() {
19        $this->util =& plugin_load('helper', 'strata_util');
20    }
21
22    public function getType() {
23        return 'substition';
24    }
25
26    public function getPType() {
27        return 'block';
28    }
29
30    public function getSort() {
31        // sort just before info plugin
32        return 154;
33    }
34
35
36    public function connectTo($mode) {
37        $this->Lexer->addSpecialPattern('~~INFO:stratatypes~~',$mode,'plugin_strata_info');
38        $this->Lexer->addSpecialPattern('~~INFO:strataaggregates~~',$mode,'plugin_strata_info');
39    }
40
41    public function handle($match, $state, $pos, Doku_Handler $handler){
42        $data = array();
43        preg_match('/~~INFO:strata(type|aggregate)s~~/',$match, $captures);
44        list(,$kind) = $captures;
45
46        // get a list of all types...
47        foreach(glob(DOKU_PLUGIN."*/${kind}s/*.php") as $type) {
48            if(preg_match("@/([^/]+)/${kind}s/([^/]+)\.php@",$type,$matches)) {
49                // ...load each type...
50                switch($kind) {
51                    case 'type': $meta = $this->util->loadType($matches[2])->getInfo(); break;
52                    case 'aggregate': $meta = $this->util->loadAggregate($matches[2])->getInfo(); break;
53                }
54
55                // ...and check if it's synthetic (i.e., not user-usable)
56                if(!isset($meta['synthetic']) || !$meta['synthetic']) {
57                    $data[] = array(
58                        'name'=>$matches[2],
59                        'plugin'=>$matches[1],
60                        'meta'=>$meta
61                    );
62                }
63            }
64        }
65
66        usort($data, array($this,'_compareNames'));
67
68        return array($kind,$data);
69    }
70
71    function _compareNames($a, $b) {
72        return strcmp($a['name'], $b['name']);
73    }
74
75    public function render($mode, Doku_Renderer $R, $data) {
76        if($mode == 'xhtml' || $mode == 'odt') {
77            list($kind, $items) = $data;
78
79            $R->listu_open();
80            foreach($items as $data){
81                $R->listitem_open(1);
82                $R->listcontent_open();
83
84                $R->strong_open();
85                $R->cdata($data['name']);
86                $R->strong_close();
87
88                if($data['meta']['hint']) {
89                    $R->cdata(' ('.$kind.' hint: '. $data['meta']['hint'] .')');
90                }
91                // $R->emphasis_open();
92                // $R->cdata(' in '.$data['plugin'].' plugin');
93                // $R->emphasis_close();
94
95                $R->linebreak();
96                $R->cdata($data['meta']['desc']);
97
98                if(isset($data['meta']['tags']) && count($data['meta']['tags'])) {
99                    $R->cdata(' (');
100                    $R->emphasis_open();
101                    $R->cdata(implode(', ',$data['meta']['tags']));
102                    $R->emphasis_close();
103                    $R->cdata(')');
104                }
105
106                $R->listcontent_close();
107                $R->listitem_close();
108            }
109            $R->listu_close();
110            return true;
111        }
112
113        return false;
114    }
115}
116