1<?php
2/**
3 * DokuWiki Plugin shorty (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Fernando Ribeiro <pinguim.ribeiro@gmail.com>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12class syntax_plugin_shorty extends DokuWiki_Syntax_Plugin {
13    /**
14     * @return string Syntax mode type
15     */
16    public function getType() {
17        return 'substition';
18    }
19    /**
20     * @return string Paragraph type
21     */
22    public function getPType() {
23        return 'normal';
24    }
25    /**
26     * @return int Sort order - Low numbers go before high numbers
27     */
28    public function getSort() {
29        return 999;
30    }
31
32    /**
33     * Connect lookup pattern to lexer.
34     *
35     * @param string $mode Parser mode
36     */
37    public function connectTo($mode) {
38        // shorty plugin sintax:
39        // ~~shorty~~
40        // ~~shorty service~~
41        $this->Lexer->addSpecialPattern('~~shorty\b.*?~~',$mode,'plugin_shorty');
42    }
43
44
45    /**
46     * Handle matches of the shorty syntax
47     *
48     * @param string $match The match of the syntax
49     * @param int    $state The state of the handler
50     * @param int    $pos The position in the document
51     * @param Doku_Handler    $handler The handler
52     * @return array Data for the renderer
53     */
54    public function handle($match, $state, $pos, Doku_Handler $handler){
55        // disable bitly plugin sintax if we are parsing a submitted comment...
56        if (isset($_REQUEST['comment'])) return false;
57
58        $match = substr($match, 8, -2);         //strip ~~shorty from start and ~~ from end
59        $service = strtolower(trim($match));    //strip spaces
60
61        if (!$service) return $this->getConf('default_service');
62        return $service;
63    }
64
65    /**
66     * Render xhtml output or metadata
67     *
68     * @param string         $mode      Renderer mode (supported modes: xhtml)
69     * @param Doku_Renderer  $renderer  The renderer
70     * @param array          $data      The data from the handler() function
71     * @return bool If rendering was successful.
72     */
73    public function render($mode, Doku_Renderer $renderer, $data) {
74        global $ID;
75        if($data == false) return false;
76
77        if($mode != 'xhtml') return false;
78
79        $shorty =& plugin_load('helper', 'shorty');
80        if ($shorty) {
81            $shortUrl = $shorty->getShortUrl($ID, $data);
82            if ($shortUrl != false) {
83                $renderer->doc .= $renderer->externallink($shortUrl);
84            } else {
85                $renderer->doc .= "Shorty: error generating short URL.";
86            }
87        }
88        return true;
89    }
90}
91
92// vim:ts=4:sw=4:et:
93