1<?php
2/**
3 * GIT commit link for gitweb plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Daniel-Constantin Mierla <miconda at gmail dot com>
7 */
8
9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(DOKU_PLUGIN.'syntax.php');
12
13//-----------------------------------CONFIGURE GITWEB ROOT HERE---------
14global $gitweb_root_url;
15$gitweb_root_url = "http://yourgitweb.com/cgi-bin/gitweb.cgi/project/";
16//----------------------------------------------------------------------
17
18
19/**
20 * All DokuWiki plugins to extend the parser/rendering mechanism
21 * need to inherit from this class
22 */
23class syntax_plugin_gitlink extends DokuWiki_Syntax_Plugin {
24
25    /**
26     * return some info
27     */
28    function getInfo(){
29        return array(
30            'author' => 'Daniel-Constantin Mierla',
31            'email'  => 'miconda@gmail.com',
32            'date'   => '2009-07-30',
33            'name'   => 'GITlink Plugin',
34            'desc'   => 'Enables links to GIT commits via gitweb',
35            'url'    => 'http://www.asipto.com/',
36        );
37    }
38
39    /**
40     * What kind of syntax are we?
41     */
42    function getType(){
43        return 'substition';
44    }
45
46    /**
47     * Where to sort in?
48     */
49    function getSort(){
50        return 812;
51    }
52
53    /**
54     * Connect pattern to lexer
55     */
56
57    function connectTo($mode) {
58        // Word boundaries?
59        $this->Lexer->addSpecialPattern('GIT#[0-9a-fA-F]{6,40}',$mode,'plugin_gitlink');
60        $this->Lexer->addSpecialPattern('GIT\?[0-9a-fA-F]{6,40}',$mode,'plugin_gitlink');
61    }
62
63    /**
64     * Handle the match
65     */
66    function handle($match, $state, $pos, &$handler){
67        return array($match, $state);
68    }
69
70    /**
71     * Create output
72     */
73	function render($mode, &$renderer, $data) {
74        global $gitweb_root_url;
75		$iframe = false;
76        if($mode == 'xhtml'){
77            $gitv = explode('#', $data[0]);
78			if(count($gitv) < 2) {
79				$iframe = true;
80                $gitv = explode('?', $data[0]);
81            }
82            $url = $gitweb_root_url."?a=commit&h=".$gitv[1];
83            if($iframe) {
84                $w = "75%";
85                $h = "400px";
86                $renderer->doc .= '<iframe title="GIT '.$gitv[1].'" src="'.$url.'" style="width:'.$w.'; height: '.$h.';">GIT '.$gitv[1].'</iframe>';
87            } else {
88                $renderer->doc .= "<a href=\"".$url."\">GIT ".$gitv[1]."</a>";
89            }
90            return true;
91        }
92        return false;
93    }
94
95}
96
97//Setup VIM: ex: et ts=4 enc=utf-8 :
98?>