1<?php
2/**
3 * Plugin chiplink: Provides automatic links to HTML reports on your server, by searching the wikitext.
4 *
5 *  Thanks to Gregg Berkholtz for the rtlink plugin, on which this plugin is based.
6 *
7 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author     Jonas Berg
9 */
10
11 // must be run within Dokuwiki
12if (!defined('DOKU_INC')) die();
13
14if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
15if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
16if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
17
18require_once DOKU_PLUGIN.'syntax.php';
19
20class syntax_plugin_chiplink extends DokuWiki_Syntax_Plugin {
21
22    public function getInfo(){
23        return array(
24            'author' => 'Jonas Berg',
25            'email'  => 'jonas.s.berg@home.se',
26            'date'   => '2012-03-28',
27            'name'   => 'chiplink plugin',
28            'desc'   => 'Provides automatic links to HTML reports on your server, by searching the wikitext.',
29            'url'    => 'http://www.dokuwiki.org/plugin:chiplink',
30        );
31    }
32
33    public function getType(){
34        return 'substition';
35    }
36
37    public function getSort(){
38        return 920;
39    }
40
41    /**
42     * Search for the pattern
43     */
44    public function connectTo($mode) {
45      $this->Lexer->addSpecialPattern('[sS][wW][0-9]+[pP][0-9]+', $mode, 'plugin_chiplink'); // For example 'SW1613p8'
46    }
47
48    /**
49     * Read the information from the matched wiki text
50     */
51    public function handle($match, $state, $pos, &$handler){
52        $stripped = substr($match, 2); // Remove initial 'SW'
53        $splitted = preg_split("/[pP]/", $stripped); // Split '1613p8' into '1613' and '8'.
54        list($wafer, $chip) = $splitted;
55
56        return array($wafer, $chip);
57    }
58
59    /**
60     * Generate HTML output
61     */
62    public function render($mode, &$renderer, $data) {
63        if($mode != 'xhtml'){
64            return false;
65        }
66
67        list($wafer, $chip) = $data;
68        $relativelocation = $this->getConf('relativelocation');
69
70        $url = "/".$relativelocation."/"."SW".$wafer.".htm#p".$chip; // Relative link, servername is not necessary.
71        $description = "SW".$wafer."p".$chip;
72
73        $renderer->doc .= "<a href=\"".$url."\">".$description."</a>";
74        return true;
75    }
76}
77
78// vim:ts=4:sw=4:et:
79