<?php
/**
 * Plugin websvn: Deep-links to files in a WebSVN repository
 *                      http://websvn.tigris.org/
 * 
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Stefan Hechenberger <foss@stefanix.net>
 */
 
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
 


//-----------------------------------CONFIGURE WEBSVN ROOT HERE---------
global $websvn_root_url;
$websvn_root_url = "http://svn.stefanix.net/";
//----------------------------------------------------------------------

 
 
/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_websvn extends DokuWiki_Syntax_Plugin {
 
    /**
     * return some info
     */
    function getInfo(){
        return array(
            'author' => 'Stefan Hechenberger',
            'email'  => 'foss@stefanix.net',
            'date'   => '2007-02-06',
            'name'   => 'websvn Plugin',
            'desc'   => 'Generates deep links to files in a WebSVN repository.',
            'url'    => 'http://wiki.splitbrain.org/plugin:websvn',
        );
    }
 
    /**
     * What kind of syntax are we?
     */
    function getType(){
        return 'substition';
    }
	
 
    /**
     * Where to sort in?
     */ 
    function getSort(){
        return 921;
    }
 
 
    /**
     * Connect pattern to lexer
     */
    function connectTo($mode) {
      $this->Lexer->addSpecialPattern('<websvn .*?>',$mode, substr(get_class($this), 7));
    }

 
    /**
     * Handle the match
     */
    function handle($match, $state, $pos, &$handler){
        $match = html_entity_decode(substr($match, 8, -1));
        if(substr($match, -1, 1) == ' ') {
            $iframe = 1;
            $match = substr($match, 0, -1);
        }else {
            $iframe = 0;
        }
        list($repository, $reppath) = explode('/', $match, 2);
        $reppath = '/'.$reppath;
        $sourcefilename = substr(strrchr($reppath, "/"), 1);
        $reppath = urlencode($reppath);
        return array($repository, $reppath, $sourcefilename, $iframe);
    }
 
    /**
     * Create output
     */
    function render($mode, &$renderer, $data) {
        global $websvn_root_url;
        list($repository, $reppath, $sourcefilename, $iframe) = $data;
        $url = $websvn_root_url."filedetails.php?repname=$repository&path=$reppath";
        if($mode == 'xhtml'){
            if($iframe) {
                $w = "100%";
                $h = "400px";
                $renderer->doc .= '<iframe title="'.$sourcefilename.'" src="'.$url.'" style="width:'.$w.'; height: '.$h.';">'.$sourcefilename.'</iframe>';                
            } else {
                $renderer->doc .= "<a href=\"".$url."\">$sourcefilename</a>";
            }
            return true;
        }
        return false;
    }
}
