1<?php
2/**
3 * DokuWiki Plugin abbrlist (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Böhler <dev@aboehler.at>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'syntax.php');
14
15class syntax_plugin_abbrlist extends DokuWiki_Syntax_Plugin {
16
17
18    /**
19     * What kind of syntax are we?
20     */
21    function getType(){
22        return 'substition';
23    }
24
25    /**
26     * What about paragraphs?
27     */
28    function getPType(){
29        return 'normal';
30    }
31
32    /**
33     * Where to sort in?
34     */
35    function getSort(){
36        return 163;
37    }
38
39    /**
40     * Connect pattern to lexer
41     */
42    function connectTo($mode) {
43        $this->Lexer->addSpecialPattern('\{\{abbrlist>[^}]*\}\}',$mode,'plugin_abbrlist');
44    }
45
46    /**
47     * Handle the match
48     */
49    function handle($match, $state, $pos, &$handler){
50        global $ID;
51        $options = trim(substr($match,11,-2));
52        $options = explode(',', $options);
53        $data = array(
54            'nointernal' => false,
55            'sort' => false,
56            'acronyms' => array()
57        );
58
59        foreach($options as $option)
60        {
61            switch($option)
62            {
63                case 'nointernal':
64                    $data['nointernal'] = true;
65                break;
66
67                case 'sort':
68                    $data['sort'] = true;
69                break;
70            }
71        }
72
73        // Only parse the local config file if we should omit built-in ones
74        if($data['nointernal'] === true)
75        {
76            global $config_cascade;
77            if (!is_array($config_cascade['acronyms'])) trigger_error('Missing config cascade for "acronyms"',E_USER_WARNING);
78            if (!empty($config_cascade['acronyms']['local']))
79            {
80                foreach($config_cascade['acronyms']['local'] as $file)
81                {
82                    if(file_exists($file))
83                    {
84                        $data['acronyms'] = array_merge($data['acronyms'], confToHash($file));
85                    }
86                }
87            }
88        }
89        // otherwise, simply use retrieveConfig to fetch all acronyms
90        else
91        {
92            $data['acronyms'] = retrieveConfig('acronyms', 'confToHash');
93        }
94
95        // Sort the array, if requested
96        if($data['sort'] === true)
97        {
98            ksort($data['acronyms']);
99        }
100        return $data;
101    }
102
103    /**
104     * Create output
105     */
106    function render($format, &$R, $data) {
107        if($format == 'metadata')
108        {
109          if(!isset($renderer->meta['abbrlist']))
110            p_set_metadata($ID, array('abbrlist' => array()));
111          return true;
112        }
113        if(($format != 'xhtml') && ($format != 'odt')) return false;
114
115        $R->table_open();
116        $R->tablethead_open();
117        $R->tableheader_open();
118        $R->doc .= $this->getLang('key');
119        $R->tableheader_close();
120        $R->tableheader_open();
121        $R->doc .= $this->getLang('value');
122        $R->tableheader_close();
123        $R->tablethead_close();
124        foreach($data['acronyms'] as $key => $val)
125        {
126            $R->tablerow_open();
127            $R->tablecell_open();
128            $R->doc .= $key;
129            $R->tablecell_close();
130            $R->tablecell_open();
131            $R->doc .= $val;
132            $R->tablecell_close();
133            $R->tablerow_close();
134        }
135        $R->table_close();
136
137    }
138
139
140
141}
142
143// vim:ts=4:sw=4:et:enc=utf-8:
144