1<?php
2/**
3 * Add Javadoc link capability to dokuwiki
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Damien Coraboeuf <dcoraboeuf@yahoo.fr>
7 * @author     Stefan Rothe <info@stefan-rothe.ch>
8 */
9
10if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'syntax.php');
13
14class syntax_plugin_javadoc extends DokuWiki_Syntax_Plugin {
15
16    /**
17     * What kind of syntax are we?
18     */
19    function getType() {
20        return 'substition';
21    }
22
23    /**
24     * What about paragraphs?
25     */
26    function getPType() {
27        return 'normal';
28    }
29
30    function getAllowedTypes() {
31        return array('container','substition','protected','disabled','formatting','paragraphs');
32    }
33
34    /**
35     * Where to sort in?
36     */
37    function getSort() {
38        return 194;
39    }
40
41    /**
42     * Allow nesting, i.e. let the plugin accept its own entry syntax
43     */
44    function accepts($mode) {
45        if ($mode == substr(get_class($this), 7)) {
46            return true;
47        }
48        else {
49            return parent::accepts($mode);
50        }
51    }
52
53    /**
54     * Connect pattern to lexer
55     */
56    function connectTo($mode) {
57        $this->Lexer->addEntryPattern('<javadoc\b.*?>(?=.*?</javadoc>)',$mode,'plugin_javadoc');
58    }
59
60    /**
61     * Add exit pattern to lexer
62     */
63    function postConnect() {
64        $this->Lexer->addExitPattern('</javadoc>','plugin_javadoc');
65    }
66
67    /**
68     * Handle the match
69     */
70    function handle($match, $state, $pos, Doku_Handler $handler){
71        switch ($state) {
72            case DOKU_LEXER_ENTER :
73                $site = trim(substr($match,8,-1));
74                if (strlen($site) == 0) {
75                    return array($state, "jdk");
76                }
77                // Backwards compatibility
78                else if ($site == 'jdk6') {
79                    return array($state, 'jdk');
80                }
81                else {
82                    return array($state, $site);
83                }
84            case DOKU_LEXER_UNMATCHED :
85                return array($state, $match);
86            default:
87                return array($state);
88        }
89    }
90
91    /**
92     * Create output
93     */
94    function render($mode, Doku_Renderer $renderer, $indata) {
95        $sites = array(
96            'jdk' => 'http://docs.oracle.com/javase/7/docs/api',
97            // Backwards compatibility
98            'doolin' => 'http://www.doolin-guif.net/reports/apidocs',
99            // Backwards compatibility
100            'commons-beanutil' => 'commons.apache.org/proper/commons-beanutils/api'
101        );
102        // Add configured default Javadoc URL to sites
103        $jdkurl = $this->getConf('jdk');
104        if (!empty($jdkurl)) {
105            $sites['jdk'] = $jdkurl;
106        }
107
108        // Add configured user Javadoc URLs to sites
109        for ($i = 1; $i <= 5; $i++) {
110            $key = 'user'.$i;
111            $config = explode(' ', $this->getConf($key));
112            if (count($config) == 1) {
113                $url = $config[0];
114            }
115            else {
116                // Alias defined, use it instead of key
117                $key = $config[0];
118                $url = $config[1];
119            }
120
121            if (!empty($url)) {
122                $sites[$key] = $url;
123            }
124        }
125
126        if ($this->getConf('show_icon')) {
127            $icon = ' icon';
128        }
129        else {
130            $icon = '';
131        }
132
133        if ($mode == 'xhtml'){
134            list($state, $data) = $indata;
135            switch ($state) {
136                case DOKU_LEXER_ENTER :
137                    $prefix =  $sites[$data];
138                    $renderer->doc .= '<a class="javadoc'.$icon.'" target="_blank" href="'.$prefix;
139                    break;
140                case DOKU_LEXER_UNMATCHED :
141                    // Get the token and the text, separated by an optional "|"
142                    $indexOfTextSeparator = strrpos($data,"|");
143                    if ($indexOfTextSeparator === false) {
144                        $token = $data;
145                        $text = $renderer->_xmlEntities(str_replace("#", ".", $data));
146                    }
147                    else {
148                        $token = substr($data, 0, $indexOfTextSeparator);
149                        $text = substr($data, $indexOfTextSeparator + 1);
150                    }
151                    // Get the class name and the method
152                    $indexOfMethodSep = strrpos($token,"#");
153                    if ($indexOfMethodSep === false) {
154                        $url = "/".str_replace(".", "/", $token).'.html';
155                    }
156                    else {
157                        $className = substr($token, 0, $indexOfMethodSep);
158                        $className = str_replace(".", "/", $className);
159                        $methodName = substr($token, $indexOfMethodSep + 1);
160                        $url = "/".$className.".html#".$methodName;
161                    }
162                    $renderer->doc .= $url.'">'.$text;
163                    break;
164                case DOKU_LEXER_EXIT :
165                    $renderer->doc .= "</a>";
166                    break;
167            }
168            return true;
169        }
170        // unsupported $mode
171        return false;
172    }
173}
174
175?>
176