1<?php
2/**
3 * youTrack-Links Plugin: Create hyperlinks to your youTrack system. You can specify how the plugin should recognize the ticket id.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Dominic Pöllath <dominic.poellath@ils-gmbh.net>
7 */
8
9if(!defined('DOKU_INC')) die();
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(DOKU_PLUGIN.'syntax.php');
12
13
14class syntax_plugin_youtracklinks extends DokuWiki_Syntax_Plugin
15{
16   /**
17    * Get the type of syntax this plugin defines.
18    *
19    * @param none
20    * @return String <tt>'substition'</tt> (i.e. 'substitution').
21    * @public
22    * @static
23    */
24    function getType(){
25        return 'substition';
26    }
27
28
29   /**
30    * Where to sort in?
31    *
32    * @param none
33    * @return Integer <tt>6</tt>.
34    * @public
35    * @static
36    */
37    function getSort(){
38        return 6;
39    }
40
41
42   /**
43    * Connect lookup pattern to lexer.
44    *
45    * @param $aMode String The desired rendermode.
46    * @return none
47    * @public
48    * @see render()
49    */
50    function connectTo($mode) {
51        $this->Lexer->addSpecialPattern('\b[A-Z]+[\-][0-9]+\b', $mode, 'plugin_youtracklinks');
52    }
53
54
55   /**
56    * Handler to prepare matched data for the rendering process.
57    *
58    * <p>
59    * The <tt>$aState</tt> parameter gives the type of pattern
60    * which triggered the call to this method:
61    * </p>
62    * <dl>
63    * <dt>DOKU_LEXER_ENTER</dt>
64    * <dd>a pattern set by <tt>addEntryPattern()</tt></dd>
65    * <dt>DOKU_LEXER_MATCHED</dt>
66    * <dd>a pattern set by <tt>addPattern()</tt></dd>
67    * <dt>DOKU_LEXER_EXIT</dt>
68    * <dd> a pattern set by <tt>addExitPattern()</tt></dd>
69    * <dt>DOKU_LEXER_SPECIAL</dt>
70    * <dd>a pattern set by <tt>addSpecialPattern()</tt></dd>
71    * <dt>DOKU_LEXER_UNMATCHED</dt>
72    * <dd>ordinary text encountered within the plugin's syntax mode
73    * which doesn't match any pattern.</dd>
74    * </dl>
75    * @param $aMatch String The text matched by the patterns.
76    * @param $aState Integer The lexer state for the match.
77    * @param $aPos Integer The character position of the matched text.
78    * @param $aHandler Object Reference to the Doku_Handler object.
79    * @return Integer The current lexer state for the match.
80    * @public
81    * @see render()
82    * @static
83    */
84    function handle($match, $state, $pos, &$handler){
85        try {
86            if (!$my =& plugin_load('helper', 'youtracklinks'))
87                throw new Exception("Helper missing.");
88            $issue = $my->fetchYouTrackIssue($match);
89            if (!$issue || $issue == 'Issue not found.')
90                throw new Exception("youTrack Issue not found: $match.");
91            $link = $my->generateLinkForIssue($issue);
92            return array('link' => $link);
93
94        } catch (Exception $e) {
95            echo $e->getMessage() ."<br>";
96        }
97        // can't handle -> return match to rerender it
98        return array('match' => $match);
99    }
100
101
102    /**
103    * Handle the actual output creation.
104    *
105    * <p>
106    * The method checks for the given <tt>$aFormat</tt> and returns
107    * <tt>FALSE</tt> when a format isn't supported. <tt>$aRenderer</tt>
108    * contains a reference to the renderer object which is currently
109    * handling the rendering. The contents of <tt>$aData</tt> is the
110    * return value of the <tt>handle()</tt> method.
111    * </p>
112    * @param $aFormat String The output format to generate.
113    * @param $aRenderer Object A reference to the renderer object.
114    * @param $aData Array The data created by the <tt>handle()</tt>
115    * method.
116    * @return Boolean <tt>TRUE</tt> if rendered successfully, or
117    * <tt>FALSE</tt> otherwise.
118    * @public
119    * @see handle()
120    */
121    function render($mode, &$renderer, $data) {
122        if ($mode == 'xhtml') {
123            if (array_key_exists('link', $data)) {
124                $renderer->doc .= $data['link'];
125                return true;
126            }
127        }
128        // rerender match, because no link was generated
129        if (array_key_exists('match', $data)) {
130            $renderer->doc .= $data['match'];
131            return true;
132        }
133        return false;
134    }
135}
136