1<?php
2
3///// Generic plugin for all the latex syntax plugins.
4//// Handles the rendering bits, so the syntax plugins just need to match syntax.
5
6
7if(!defined('DOKU_INC')) die();
8if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
9require_once(DOKU_PLUGIN.'syntax.php');
10
11require_once(dirname(__FILE__).'/class.latexrender.php');
12
13/**
14 * All DokuWiki plugins to extend the parser/rendering mechanism
15 * need to inherit from this class
16 */
17class syntax_plugin_latex_common extends DokuWiki_Syntax_Plugin {
18	var $_latex;
19
20   /**
21	* return some info
22	*/
23	function getInfo(){
24		// DokuWiki_Syntax_Plugin -> $renderer
25		//if(method_exists($renderer, "getInfo"))
26		//	 return parent::getInfo(); /// this will grab the data from the plugin.info.txt
27
28		// Otherwise return some hardcoded data for old dokuwikis
29		return array(
30			'author' => 'Wizardry and Steamworks (original by Alexander Kraus, Michael Boyle, and Mark Lundeberg)',
31			'email'  => 'wizardry.steamworks@outlook.com',
32			'date'   => '???',
33			'name'   => 'LaTeX plugin',
34			'desc'   => 'LaTeX rendering plugin; requires LaTeX, dvips, ImageMagick.',
35			'url'    => 'http://www.dokuwiki.org/plugin:latex-was'
36		);
37	}
38
39	/* common constructor -- get config settings */
40	//function syntax_plugin_latex_common()
41	function __construct()
42	{
43		global $conf;
44		$meddir = $conf['mediadir'] . '/' . strtr($this->getConf('latex_namespace'),':','/');
45		$this->_mkpath($meddir,$conf['dmode']);
46		$this->_mkpath($meddir.'/tmp',$conf['dmode']);
47		$latex = new LatexRender($meddir,
48						DOKU_BASE.'lib/exe/fetch.php?media='.$this->getConf('latex_namespace').':',
49						$meddir.'/tmp');
50		$latex->_latex_path = $this->getConf("latex_path");
51		$latex->_dvips_path = $this->getConf("dvips_path");
52		$latex->_convert_path = $this->getConf("convert_path").' '.$this->getConf("convert_options");
53		$latex->_identify_path = $this->getConf("identify_path");
54		$latex->_keep_tmp = false;
55		$latex->_image_format = $this->getConf("image_format");
56		$latex->_colour = $this->getConf("colour");
57		$latex->_xsize_limit = $this->getConf("xsize_limit");
58		$latex->_ysize_limit = $this->getConf("ysize_limit");
59		$latex->_string_length_limit = $this->getConf("string_length_limit");
60		$latex->_preamble = $this->getConf("preamble");
61		$latex->_postamble = $this->getConf("postamble");
62
63		$this->_latex = $latex;
64	}
65
66	function connectTo($mode) { }
67
68	function handle($match, $state, $pos, Doku_Handler $handler) {}
69
70	function getType(){return 'protected'; }
71
72	function getSort(){return 405; }
73
74	function render($mode, Doku_Renderer $renderer, $data) {
75//	  global $conf
76	  if($data[1] != DOKU_LEXER_UNMATCHED) return true; // ignore entry/exit states
77
78	  if($mode == 'xhtml') {
79			////////////////////////////////////
80			// XHTML                          //
81			////////////////////////////////////
82		  $url = $this->_latex->getFormulaURL($data[0]);
83			$this->_url = $url;
84		  $title = $data['title'];
85
86		  if(!$url){
87				// some kinda error.
88				$url = DOKU_BASE.'lib/plugins/latex/images/renderfail.png';
89				switch($this->_latex->_errorcode) {
90					case 1: $title = $this->getLang('fail1').$this->_latex->_errorextra.
91							$this->getLang('failmax').$this->_latex->_string_length_limit;
92						break;
93					case 2: $title = $this->getLang('fail2');
94						break;
95					case 4: $title = $this->getLang('fail4');
96						break;
97					case 5: $title = $this->getLang('fail5').$this->_latex->_errorextra.
98							$this->getLang('failmax').$this->_latex->_xsize_limit.'x'.$this->_latex->_ysize_limit.'px';
99						break;
100					case 6: $title = $this->getLang('fail6');
101						break;
102					case 7: $title = $this->getLang('fail7');
103						break;
104					default: $title = $this->getLang('failX');
105						break;
106				}
107		  }
108
109		  $renderer->doc .= '<img src="'.$url.'" class="'.$data['class'].'" alt="'.htmlspecialchars($data[0]).'" title="'.$title.'" />';
110
111		  $fname = $this->_latex->_filename;
112		  return true;
113
114
115	  } elseif ($mode == 'metadata') {
116		  // nothing to do in metadata mode.
117		  return true;
118
119
120	  } elseif ($mode == 'odt') {
121			////////////////////////////////////
122			// ODT                            //
123			////////////////////////////////////
124		  $url = $this->_latex->getFormulaURL($data[0]);
125		  $fname = dirname(__FILE__).'/images/renderfail.png';
126		  if($url) {
127				$fname = $this->_latex->_filename;
128		  }
129		  $info  = getimagesize($fname);
130		  // expand images sizes 20% larger than those in renderer->_odtGetImageSize .
131		  $width = ($info[0] * 0.03175)."cm";
132		  $height = ($info[1] * 0.03175)."cm";
133
134		  if($data['class'] == "latex_displayed")
135				// displayed math: 5 spaces seems to look okay.
136				$renderer->doc .= '<text:s text:c="5"/>';
137
138		  $renderer->_odtAddImage($fname,$width,$height);
139
140		  return true;
141
142
143	  } elseif ($mode == 'latex') {
144			////////////////////////////////////
145			// LATEX                          //
146			////////////////////////////////////
147		  if($data['class'] == "latex_displayed")
148				$renderer->doc .= "\n".$data[0]."\n";
149		  else
150				$renderer->doc .= $data[0];
151		  return true;
152	  }
153	  $renderer->doc .= htmlspecialchars($data[0]); /// unknown render mode, just fart out the latex code.
154	  return false;
155	}
156
157	function _mkpath($path,$dmask=0777)
158	{
159		if(@mkdir($path,$dmask) or file_exists($path)) return true;
160		return ($this->_mkpath(dirname($path),$dmask) and mkdir($path,$dmask));
161	}
162
163}
164