<?php
/**
 *  Shortcut, anchor component: generates anchors for the shortcuts.
 *
 *  $Id: anchor.php 43 2007-02-04 17:01:50Z wingedfox $
 *  $HeadURL: https://svn.debugger.ru/repos/common/DokuWiki/Shortcut/tags/Shortcut.v0.1.0/syntax/anchor.php $
 *
 *  @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_anchor extends DokuWiki_Syntax_Plugin {

  /**
   * return some info
   */
  function getInfo() {
      preg_match("#^.*?Shortcut/([^\\/]+)#"," $HeadURL: https://svn.debugger.ru/repos/common/DokuWiki/Shortcut/tags/Shortcut.v0.1.0/syntax/anchor.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 anchor module {$v}.$b"
                   ,'desc'   => "Converts ordinary links to the shortcuts."
                   ,'url'    => 'http://wiki.splitbrain.org/plugin:shortcut'
                  );
  }
  
  /**
   * What kind of syntax are we?
   */
  function getType(){
    return 'substition';
  }

  function getPType(){
    return 'normal';
  } 
  /**
   * Where to sort in?
   */
  function getSort(){
    return 199;
  }
 
  /**
   * Connect pattern to lexer
   */
  function connectTo($mode) {
    $this->Lexer->addSpecialPattern('\[\[.+?\]\]',$mode,'plugin_shortcut_anchor');
  }

  /**
   * Handle the match
   */
  function handle($match, $state, $pos, &$handler){
    return $match;

  }  
 
  /**
   *  Render output
   */
  function render($mode, &$renderer, $data) {

      switch ($mode) {
          case 'xhtml' :
              $meta = p_get_metadata('shortcuts','shortcut');
              list($url, $title) = explode("|",trim($data,'[]'));
              $url = trim($url);
              $title = trim($title);
              $title = $title?$title
                             :($tgt['title']?$tgt['title']
                                            :$url);
              if ($tgt = $meta[cleanID($url)]) {
                  $renderer->doc .= '<a href="'.wl($tgt['target'])
                                    .'" id="shortcut_'.str_replace(":","_",cleanID($tgt['target']))
                                    .'" title="'.$title.'"><span>'
                                   .($title).'</span></a>';
                  $renderer->nest($tgt['desc']);
                  return true;
              } else {
                  $h = new Doku_Handler();
                  $h->internallink($data,"",0);
                  $renderer->nest($h->calls);
              }
              break;
          case 'metadata' :
              break;
    }
    return false;
  }
} // Shortcut_anchor class end