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