<?php
/**
 * Tooltip Plugin: Create tooltips for sections of wikitext.
 *
 *   syntax: <ttip::text>tooltip content<ttip>
 *
 * @file       tooltip/syntax/tooltip.php
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Luis Machuca B. <luis [dot] machuca [at] gulix [dot] cl>
 * @brief      CSS-based inline toltips
 * @version    1.8
 *
 *   This plugin allows to insert "tooltips" in DokuWiki text, so that 
 *  hovering the mouse over the mrked text will reveal the tooltip.
 *  The wikitext that composes the tooltip is parsed for basic 
 *  formatting only (underlines, italics and bolds). Otherwise,
 *  the plugin is perfectly usable.
 *   This plugin uses CSS only; no JavaScript or server side help
 *  (beyond the actual tag parsing) is used to harm any animal, 
 *  in any way.
 *
 *   For a live demo check instructions in the lugin page.
 *
 *  Greetings.
 *        - Luis Machuca Bezzaza ('ryan.chappelle' at the Wiki)
 */

if(!defined('DW_LF')) define('DW_LF',"\n");
 
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');
 
 
class syntax_plugin_tooltip_tooltip extends DokuWiki_Syntax_Plugin
{
  function getInfo()
  {
    return array (
           'author' => 'Luis Machuca Bezzaza',
           'email'  => 'luis.machuca [at] gulix.cl',
           'date'   => @file_get_contents(DOKU_PLUGIN . 'tooltip/VERSION'),
           'name'   => 'Tooltip Plugin ('. $this->getLang('comp.tooltip'). ')',
           'desc'   => $this->getLang('descr.tooltip'). 
                       ': <ttip:text> tooltip </ttip>',
           'url'    => 'http://www.dokuwiki.org/plugin:tooltip',
           );
  }

  function getType()  { 
    return 'formatting'; 
  }

  function getAllowedTypes()  {
    return array('substition','disabled','formatting');
  }

  function getPType() { 
    return 'normal'; 
  }

  function getSort() { 
    return 400; 
  } // get by exp
 
  function connectTo($mode) {
    $this->Lexer->addEntryPattern( 
                  "<ttip:.+?>(?=.+?</ttip>)", 
                  $mode, 
                  'plugin_tooltip_tooltip');
  }

  function postConnect()  {
    $this->Lexer->addExitPattern(
                  "</ttip>",
                  'plugin_tooltip_tooltip');
  }

  function handle($match, $state, $pos, &$handler) {
        
    $data= array();
    $data['state']= $state;

    switch ($state)  {
      case DOKU_LEXER_ENTER: {
        // here, we first look for an available ':' that
        // would indicate a 'style' parameter
        $match= substr($match, 6, -1);
        list($e1, $e2)= explode( ":", $match);
        // one ':' indicates only text folows, whereas two ':'
        // indicate there is also a style definition
        if ( empty($e2) ) {
          $e2= $e1;
          $e1= "default";
        }
        else { // we have to look for things now
        }
        $data['class'] = $e1;
        $data['text']  = $e2;
        break;
      }
      case DOKU_LEXER_UNMATCHED: {
        // here, we simply pass the match because we want it to be 
        // handled direclty by the DW parser
        $data['ttip']= $match;
        break;
      }
    } // end switch ($state)
    return $data;
  } // end function

  function render($mode, &$renderer, $data) {
    $state= $data['state'];
    $style= "tooltip_". $data['class'];
    $text= $data['text'];
    $ttip= $data['ttip'];

    if($mode == 'xhtml') {
      /* let's do the jon thingie, that is, 
         create the HTML code that reveals the tooltip
       */
      switch ($state) {
        case DOKU_LEXER_ENTER: {
          $renderer->doc.= '<span class="'. $style. '"><span>'. $text;
          $renderer->doc.= '<span class="tip">';
          break;
        }
        case DOKU_LEXER_UNMATCHED: {
          $renderer->doc.= $renderer->_xmlEntities($ttip);
          break;
        }
        case DOKU_LEXER_EXIT: {
          $renderer->doc.= '</span>';
          $renderer->doc.= '</span></span><!-- end tooltip -->'. DW_LF ; //end mark
          break;
        }
      } // end switch
      return true;
    } // end if (xhtml)

    else if ($mode == 'text') {
      // simple for the plaintext renderer (for things like search)
      switch ($state) {
        case DOKU_LEXER_ENTER: {
          $renderer->doc.= $mark. ' ';
          break;
        }
        case DOKU_LEXER_UNMATCHED: {
          $renderer->doc.= '[Tooltip: '. $content. '] ';
          break;
        }
        case DOKU_LEXER_EXIT: {
          break;
        }
      } // end switch
      return true;
      
    } // end if (text)

    // no more renderers
    return false;
  }

}
