1<?php
2/**
3 * mimetex-Plugin: Parses latex-blocks
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Michael Gritsaenko <michael.gritsaenko@gmail.com>
7 * @date       2011-02-01
8 */
9
10/**
11 * ChangeLog:
12 *
13 * [04/30/2015]: by LarsDW223
14 *               Added ODT support.
15 */
16
17// must be run within Dokuwiki
18if(!defined('DOKU_INC')) die();
19if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
20require_once(DOKU_PLUGIN.'syntax.php');
21
22/**
23 * All DokuWiki plugins to extend the parser/rendering mechanism
24 * need to inherit from this class
25 */
26class syntax_plugin_mimetex extends DokuWiki_Syntax_Plugin {
27
28    /**
29     * return some info
30     */
31    function getInfo(){
32        return array(
33            'author' => 'Michael Gritsaenko',
34            'email'  => 'michael.gritsaenko@gmail.com',
35            'date'   => '2015-05-07',
36            'name'   => 'mimetex Plugin',
37            'desc'   => 'mimetex Plugin based on mimeTeX
38                         parses latex blocks',
39            'url'    => 'http://www.dokuwiki.org/plugin:mimeTeX'
40        );
41    }
42
43    /**
44    * What kind of syntax are we?
45    */
46    function getType(){
47      return 'protected';
48    }
49
50    /**
51    * Where to sort in?
52    */
53    function getSort(){
54      return 100;
55    }
56
57    /**
58    * Connect pattern to lexer
59    */
60    function connectTo($mode) {
61      $this->Lexer->addEntryPattern('<latex(?=.*\x3C/latex\x3E)',$mode,'plugin_mimetex');
62    }
63
64    function postConnect() {
65      $this->Lexer->addExitPattern('</latex>','plugin_mimetex');
66    }
67
68    /**
69     * Handle the match
70    */
71    function handle($match, $state, $pos, Doku_Handler $handler) {
72      if ( $state == DOKU_LEXER_UNMATCHED ) {
73        $matches = preg_split('/>/u',$match,2);
74        $matches[0] = trim($matches[0]);
75        if ( trim($matches[0]) == '' ) {
76          $matches[0] = NULL;
77        }
78        return array($matches[1],$matches[0]);
79      }
80      return TRUE;
81    }
82    /**
83     * Create output
84    */
85    function render($mode, Doku_Renderer $renderer, $formula) {
86      global $conf;
87      if( ($mode == 'xhtml' || $mode == 'odt') && strlen($formula[0]) > 1) {
88        if ( !is_dir($conf['mediadir'] . '/latex') ) {
89          mkdir($conf['mediadir'] . '/latex', 0777-$conf['dmask']);
90        }
91
92        $hash = md5(serialize($formula));
93        $cachefilename = $conf['mediadir'] . '/latex/'.$hash.'.gif';
94        $cacheurl = DOKU_BASE.'lib/exe/fetch.php?media='.urlencode('latex:'.$hash.'.gif');
95
96        if( !is_readable($cachefilename) ) {
97
98          require_once(DOKU_PLUGIN.'mimetex/mimetexrender.php');
99          $mimetex = new mimetexRender();
100
101          if( !$mimetex->render($cachefilename, $formula[0]) ) {
102            $renderer->doc .= '**ERROR RENDERING LATEX**:<br/><b>'.$mimetex->_error."</b>";
103            return false;
104          }
105        }
106
107        switch ($mode) {
108            case 'odt':
109                $renderer->_odtAddImage ($cachefilename);
110                break;
111            default:
112                $renderer->doc .= '<img src="'.$cacheurl.'" class="media" title="mimeTeX" alt="mimeTeX" />';
113                break;
114        }
115        return true;
116      }
117      return false;
118    }
119}
120?>
121