<?php
/**
 * @file       hide/syntax.php
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Luis Machuca B. <luis.machuca [at] gulix.cl>
 * @brief      hides text by making it invisible (using CSS)
 * @version    1.0
 *
 *   syntax: {?:hidden text:?}
 *
 *   For a live demo check instructions in the plugin page.
 *
 *  Greetings.
 *        - Luis Machuca B. <a.k.a. 'ryan.chappelle'>
 */

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_hide extends DokuWiki_Syntax_Plugin
{
  function getInfo()
  {
    return array(
           'author' => 'Luis Machuca B.',
           'email'  => 'luis.machuca@gulix.cl',
           'date'   => '2009-07-07',
           'name'   => 'Hide Plugin',
           'desc'   => 'Reveals text when user selects',
           'url'    => 'http://www.dokuwiki.org/plugin:hide',
           );
  }

  function getType()    { 
    return 'formatting'; 
  }

  function getAllowedTypes()  {
    return array('substition','disabled','formatting');
  }

  function getPType()  { 
    return 'normal'; 
  }

  function getSort() { 
    return 500; 
  } // get by exp
 
  function connectTo($mode)  {
    $this->Lexer->addEntryPattern(
                  '\{\?(?=.+?\?\})',
                  $mode, 
                  'plugin_hide' );
  }

  function postConnect() {
    $this->Lexer->addExitPattern(
                  '\?\}',
                  'plugin_hide');
  }

  function handle($match, $state, $pos, &$handler) {
    // there is not much to do here...
    $data= array();
    $data['state']= $state;
    switch ($state)  {
      case DOKU_LEXER_UNMATCHED: {
        $data['content']= $match;
        break;
      }
    } // end switch ($state)
    return $data;
  } // end function

  function render($mode, &$renderer, $data) {
    $state= $data['state'];
    $content= $data['content'];

    if($mode == 'xhtml') {
      /* let's do the jon thingie, that is, 
         create the HTML code that reveals the hoverview
       */
      switch ($state) {
        case DOKU_LEXER_ENTER: {
          $renderer->doc.= '<span class="selectview">';
          break;
        }
        case DOKU_LEXER_UNMATCHED: {
          $renderer->doc.= $renderer->_xmlEntities($content);
          break;
        }
        case DOKU_LEXER_EXIT: {
          $renderer->doc.= '</span>';
          break;
        }
      } // end switch
      return true;
    } // end if (xhtml)
    // no more renderers, maybe Text renderer in the future?
    return false;
  }

}
