<?php
/**
 *  Indexmenu2 plugin, Navigation component
 *
 *  $Id: shortcut.php 43 2007-02-04 17:01:50Z wingedfox $
 *  $HeadURL: https://svn.debugger.ru/repos/common/DokuWiki/Shortcut/tags/Shortcut.v0.1.0/syntax/shortcut.php $
 *
 *  Syntax:     <shortcut name|image> Description for the footnote </navigation>
 * 
 *  @lastmodified $Date: 2007-02-04 20:01:50 +0300 (Вск, 04 Фев 2007) $
 *  @license      LGPL 2 (http://www.gnu.org/licenses/lgpl.html)
 *  @author       Ilya Lebedev <ilya@lebedev.net>
 *  @version      $Rev: 43 $
 *  @copyright    (c) 2005-2007, Ilya Lebedev
 */

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');

/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_shortcut_shortcut extends DokuWiki_Syntax_Plugin {

    var $syntax = "";

    /**
     * return some info
     */
    function getInfo(){
        preg_match("#^.*?Shortcut/([^\\/]+)#"," $HeadURL: https://svn.debugger.ru/repos/common/DokuWiki/Shortcut/tags/Shortcut.v0.1.0/syntax/shortcut.php $ ", $v);
        $v = preg_replace("#.*?((trunk|.v)[\d.]+)#","\\1",$v[1]);
        $b = preg_replace("/\\D/","", " $Rev: 43 $ ");

        return array( 'author' => 'Ilya Lebedev'
                     ,'email'  => 'ilya@lebedev.net'
                     ,'date'   => preg_replace("#.*?(\d{4}-\d{2}-\d{2}).*#","\\1",'$Date: 2007-02-04 20:01:50 +0300 (Вск, 04 Фев 2007) $')
                     ,'name'   => "Shortcut definition module {$v}.$b"
                     ,'desc'   => 'Module is used to define the shortcut to the current page'
                     ,'url'    => 'http://wiki.splitbrain.org/plugin:shortcut'
                    );
    }

    function getType(){ return 'protected';}
    function getPType(){ return 'block'; }

    // must return a number lower than returned by native 'anchor' mode (210)
    function getSort(){ return 123; }


    /**
     * Connect pattern to lexer
     */
    function connectTo($mode) {
        $this->Lexer->addEntryPattern("<shortcut(?=[^\r\n]*?\x3E.*?\x3C/shortcut\x3E)",$mode,'plugin_shortcut_shortcut');
    }
    
    function postConnect() {
        $this->Lexer->addExitPattern('\x3C/shortcut\x3E', 'plugin_shortcut_shortcut');
    }
    
    /**
     * Handle the match
     */
    function handle($match, $state, $pos, &$handler){

        switch ($state) {
            case DOKU_LEXER_ENTER:
                $this->syntax = substr($match, 1);
                return false;
                                
            case DOKU_LEXER_UNMATCHED:
                // will include everything from <shortcut ... to ... </shortcut>
                // e.g. ... [name][|image]> [description]
                list($opts, $content) = preg_split('/>/u',trim($match),2);

                return array(true, $content, $opts);
        }      
        return false;
    }

    /**
     * Create output
     */
    function render($mode, &$renderer, $data) {
        switch ($mode) {
            case 'xhtml' :
                if (true === $data[0]) {
                    list(, $desc, $opts) = $data;
                    $opts = explode('|', $opts);
                    $opts[0] = cleanID($opts[0]);
                    $opts[1] = trim($opts[1]);


                    $id = getID();
                    $meta = p_get_metadata('shortcuts');
                    $m_s = $meta['shortcut'];
                    if (!is_array($m_s)) $m_s = array();
                    if (!$m_s[$opts[0]] || $m_s[$opts[0]]['target'] != $id) {
                        $instr = p_get_instructions("(($desc))");
                        $this->__cleanInstructions($instr);
                        /*
                        *  add metadata, if shortcut is not registered yet
                        *  or target differs from the current page
                        */
                        $m_s[$opts[0]] = array ( 'title' => p_get_first_heading($id)
                                                ,'target'=> $id
                                                ,'img'   => $opts[1]
                                                ,'desc'  => $instr
                                               );
                        if ($opts[1]) {
                                /*
                                *  update CSS
                                */
                                $f = @file_get_contents(DOKU_PLUGIN."/shortcut/style.css");
                                $c_id = 'shortcut_'.str_replace(":","_",cleanID($id));//preg_replace("/[^a-z0-9_]/","",$id);
                                $img = getimagesize(mediaFN('shortcuts:'.$opts[1]));
                                /*
                                *  replace structure
                                *  [css_comment_open] shortcut_id [css_comment_close]
                                *  [style_definition]
                                *  [css_comment_open] @ [css_comment_close]
                                */
                                $f = preg_replace("#/\\*\\s*".$c_id."[^@]+@\\s*\\*/\s*#sm","",$f);
                                $f .=   "/* $c_id */\n" .
                                                "a#$c_id {" .
                                                        "background: transparent url(\"".ml('shortcuts:'.$opts[1])."\") no-repeat center left; " .
                                                        "display: inline-block; " .
                                                        "height: ". $img[1] . "px;" .
                                                        "line-height: ". $img[1] . "px;" .
                                                        "margin-left: 2px;" .
                                                        "padding-left: ". ($img[0]+2) . "px;" .
                                                "}\n" .
                                                "a#$c_id span { display :none }\n".
                                                "/* @ */\n";
                                io_saveFile(DOKU_PLUGIN."/shortcut/style.css",$f,false);
                        }
                        $meta['shortcut'] = $m_s;
                        p_set_metadata('shortcuts', $meta, false, false);
                    }
//                    $renderer->doc .= syntax_plugin_indexmenu_indexmenu::getHTML ($opts[1], $instr);

                    return true;
                }
                break;
            default:
                break;
        }
        return false;
    }
    /**
     *  Clean instructions from unallowed stuff
     *
     *  @author Ilya Lebedev <ilya@lebedev.net>
     *  @param $instr mixed list of instructions
     *  @return mixed filtered list
     *  @access private
     */
    function __cleanInstructions (&$instr) {
        foreach ($instr as $k=>$v) {
            switch ($v[0]) {
                case "document_start":
                case "document_end":
                case "p_open":
                case "p_close":
                    unset($instr[$k]);
            }
        }
    }
}

//Setup VIM: ex: et ts=4 enc=utf-8 :
