<?php
/**
 * mathtran Plugin
 * 
 * Make use of the MathTran public TeX engine
 * for the rendering of mathematical formulae: 
 *    http://www.mathtran.org/wiki/index.php/Main_Page
 *
 * TeX syntax:
 *    http://refcards.com/docs/silvermanj/tex/tex-refcard-letter.pdf
 *
 * Author: Olivier Delrieu , olivier@delrieu.org
 *
 */

 
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');

global $conf;

global $mathtranplugin_js;
	   $mathtranplugin_js = true;

/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_mathtran extends DokuWiki_Syntax_Plugin {

    var $enable = false;
    var $msg_disable = "mathtran plugin disabled: ";
    var $msg_sent = false;

    function syntax_plugin_mathtran() {
        $this->enable = $this->_requirements_ok();    
    }

    /**
     * return some info
     */
    function getInfo(){
    
      return array(
        'author' => 'Olivier Delrieu',
        'email'  => 'olivier@delrieu.org',
        'date'   => '2010 March 10',
        'name'   => 'Mathtran Plugin'.(!$this->enable ? ' (disabled)' : ''),
        'desc'   => 'Add mathematical formulae to dokuwiki
		             using the MathTran TeX engine
                     Syntax: <tex>math formulae</tex>'.
                     (!$this->enable ? "\n(".$this->msg_disable.")" : ''),
        'url'    => 'http://www.delrieu.org',
      );
    }

    function getType(){ return 'protected'; }
    function getPType(){ return 'normal'; }
    function getSort(){ return 208; }

    /**
     * Connect pattern to lexer
     */
    function connectTo($mode) {
      $this->Lexer->addEntryPattern('<tex>.*?',$mode,'plugin_mathtran');
    }

    function postConnect() {
      $this->Lexer->addExitPattern('</tex>','plugin_mathtran');
    } 

    /**
     * Handle the match
     */
    function handle($match, $state, $pos, &$handler){
    
      if ( $state == DOKU_LEXER_UNMATCHED ) {
	    $math = $match;		
        return (array(trim($math), $align));
      }      
      return false;
    }

    /**
     * Create output
     */
    function render($mode, &$renderer, $data) {
      global $mathtranplugin_js;
      
      if (!$data) return;   // skip rendering for the enter and exit patterns
      list($math, $align) = $data;
    
      if($mode == 'xhtml'){
          if ($this->enable) {
				if($mathtranplugin_js == true) {
				     // this is ugly but I can't make any local script.js file work.
					$math_html = '<script type="text/javascript" src="http://www.mathtran.org/js/mathtran_img.js"></script>' ;
					$mathtranplugin_js = false;
				} else {
					$math_html = '';
				}
				$math_html .= '<img alt="tex:'.$math.'" align="absmiddle">'; 
                $renderer->doc .= $math_html;
          } else {
            $this->_msg($this->msg_disable, -1);
          }
          return true;
      }
      return false;
    }
    
    
    // return true if php installation has required libraries/functions for mathpublisher
    function _requirements_ok() {
        return true;    
    }
    
    // used to avoid multiple messages
    function _msg($str, $lvl=0) {
        if ($this->msg_sent) return;
        
        msg($str, $lvl);
        $this->msg_sent = true;
    }
    
    // would like to see this function in io.php :)
    function _mkdir($d) {
        global $conf;
        
        umask($conf['dmask']);
        $ok = io_mkdir_p($d);
        umask($conf['umask']);
        return $ok;
    }    
    
}

//Setup VIM: ex: et ts=4 enc=utf-8 :
