1<?php
2/**
3 * mathtran Plugin
4 *
5 * Make use of the MathTran public TeX engine
6 * for the rendering of mathematical formulae:
7 *    http://www.mathtran.org/wiki/index.php/Main_Page
8 *
9 * TeX syntax:
10 *    http://refcards.com/docs/silvermanj/tex/tex-refcard-letter.pdf
11 *
12 * Author: Olivier Delrieu , olivier@delrieu.org
13 *
14 */
15
16
17if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
18if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
19require_once(DOKU_PLUGIN.'syntax.php');
20
21global $conf;
22
23global $mathtranplugin_js;
24	   $mathtranplugin_js = true;
25
26/**
27 * All DokuWiki plugins to extend the parser/rendering mechanism
28 * need to inherit from this class
29 */
30class syntax_plugin_mathtran extends DokuWiki_Syntax_Plugin {
31
32    var $enable = false;
33    var $msg_disable = "mathtran plugin disabled: ";
34    var $msg_sent = false;
35
36    function syntax_plugin_mathtran() {
37        $this->enable = $this->_requirements_ok();
38    }
39
40    /**
41     * return some info
42     */
43    function getInfo(){
44
45      return array(
46        'author' => 'Olivier Delrieu',
47        'email'  => 'olivier@delrieu.org',
48        'date'   => '2010 March 10',
49        'name'   => 'Mathtran Plugin'.(!$this->enable ? ' (disabled)' : ''),
50        'desc'   => 'Add mathematical formulae to dokuwiki
51		             using the MathTran TeX engine
52                     Syntax: <tex>math formulae</tex>'.
53                     (!$this->enable ? "\n(".$this->msg_disable.")" : ''),
54        'url'    => 'http://www.delrieu.org',
55      );
56    }
57
58    function getType(){ return 'protected'; }
59    function getPType(){ return 'normal'; }
60    function getSort(){ return 208; }
61
62    /**
63     * Connect pattern to lexer
64     */
65    function connectTo($mode) {
66      $this->Lexer->addEntryPattern('<tex>.*?',$mode,'plugin_mathtran');
67    }
68
69    function postConnect() {
70      $this->Lexer->addExitPattern('</tex>','plugin_mathtran');
71    }
72
73    /**
74     * Handle the match
75     */
76    function handle($match, $state, $pos, &$handler){
77
78      if ( $state == DOKU_LEXER_UNMATCHED ) {
79	    $math = $match;
80        return (array(trim($math), $align));
81      }
82      return false;
83    }
84
85    /**
86     * Create output
87     */
88    function render($mode, &$renderer, $data) {
89      global $mathtranplugin_js;
90
91      if (!$data) return;   // skip rendering for the enter and exit patterns
92      list($math, $align) = $data;
93
94      if($mode == 'xhtml'){
95          if ($this->enable) {
96				if($mathtranplugin_js == true) {
97				     // this is ugly but I can't make any local script.js file work.
98					$math_html = '<script type="text/javascript" src="http://www.mathtran.org/js/mathtran_img.js"></script>' ;
99					$mathtranplugin_js = false;
100				} else {
101					$math_html = '';
102				}
103				$math_html .= '<img alt="tex:'.$math.'" align="absmiddle">';
104                $renderer->doc .= $math_html;
105          } else {
106            $this->_msg($this->msg_disable, -1);
107          }
108          return true;
109      }
110      return false;
111    }
112
113
114    // return true if php installation has required libraries/functions for mathpublisher
115    function _requirements_ok() {
116        return true;
117    }
118
119    // used to avoid multiple messages
120    function _msg($str, $lvl=0) {
121        if ($this->msg_sent) return;
122
123        msg($str, $lvl);
124        $this->msg_sent = true;
125    }
126
127    // would like to see this function in io.php :)
128    function _mkdir($d) {
129        global $conf;
130
131        umask($conf['dmask']);
132        $ok = io_mkdir_p($d);
133        umask($conf['umask']);
134        return $ok;
135    }
136
137}
138
139//Setup VIM: ex: et ts=4 enc=utf-8 :
140