1<?php
2/**
3 * LaTeX common syntax base
4 *
5 * Primary Author: Wizardry and Steamworks <wizardry.steamworks@outlook.com>
6 * @license GPL 2
7 */
8
9if(!defined('DOKU_INC')) die();
10require_once(__DIR__ . '/class.latexrender.php');
11
12abstract class syntax_plugin_latexwas_common extends DokuWiki_Syntax_Plugin {
13    public $_latex;
14
15    public function __construct() {
16        global $conf;
17        $namespace = $this->getConf('latex_namespace');
18        $meddir = $conf['mediadir'] . '/' . strtr($namespace, ':', '/');
19
20        $this->_mkpath($meddir, $conf['dmode']);
21        $this->_mkpath($meddir . '/tmp', $conf['dmode']);
22
23        $this->_latex = new LatexRender(
24            $meddir,
25            DOKU_BASE . 'lib/exe/fetch.php?media=' . $namespace . ':',
26            $meddir . '/tmp'
27        );
28
29        $this->_latex->_latex_path = $this->getConf("latex_path");
30        $this->_latex->_dvips_path = $this->getConf("dvips_path");
31        $this->_latex->_convert_path = $this->getConf("convert_path") . ' ' . $this->getConf("convert_options");
32        $this->_latex->_identify_path = $this->getConf("identify_path");
33        $this->_latex->_image_format = $this->getConf("image_format");
34        $this->_latex->_xsize_limit = $this->getConf("xsize_limit");
35        $this->_latex->_ysize_limit = $this->getConf("ysize_limit");
36        $this->_latex->_string_length_limit = $this->getConf("string_length_limit");
37        $this->_latex->_preamble = $this->getConf("preamble");
38        $this->_latex->_postamble = $this->getConf("postamble");
39    }
40
41    public function getType() { return 'protected'; }
42    public function getSort() { return 405; }
43
44    protected function el($tag, $attr = [], $content = '', $selfClosing = false) {
45        $html = "<{$tag}";
46        foreach ($attr as $k => $v) { $html .= " {$k}=\"" . htmlspecialchars($v, ENT_QUOTES, 'UTF-8') . "\""; }
47        if ($selfClosing) return $html . ' />';
48        return $html . ">" . $content . "</{$tag}>";
49    }
50
51    protected function _mkpath($path, $dmask = 0777) {
52        if (@mkdir($path, $dmask) || file_exists($path)) return true;
53        return ($this->_mkpath(dirname($path), $dmask) && mkdir($path, $dmask));
54    }
55
56    public function render($mode, Doku_Renderer $renderer, $data) {
57        if ($data[1] !== DOKU_LEXER_UNMATCHED) return true;
58        if ($mode === 'xhtml') {
59            $url = $this->_latex->getFormulaURL($data[0]);
60            $title = $url ? $data['title'] : "Render Error " . $this->_latex->_errorcode;
61            if (!$url) $url = DOKU_BASE . 'lib/plugins/latexwas/images/renderfail.png';
62
63            $renderer->doc .= $this->el('img', [
64                'src' => $url,
65                'class' => $data['class'],
66                'alt' => $data[0],
67                'title' => $title
68            ], '', true);
69            return true;
70        }
71        return false;
72    }
73}
74