forked from cosmocode/dokuwiki-plugin-mathpublish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.php
executable file
·168 lines (140 loc) · 4.96 KB
/
syntax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Math Plugin: incorporate mathematical formulae using MathPublisher into Dokuwiki
*
* Syntax: <m size>...mathematical formula..</m>
* size (optional) base glyph size in pixels,
* if not present will use the value of $mathplugin_size global, the value
* of which can be set below (default: 12)
*
* Formulae syntax: refer http://www.xm1math.net/phpmathpublisher/doc/help.html
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Christopher Smith <[email protected]>
* @date 2005-12-17
*
* phpmathpublisher
* @link http://www.xm1math.net/phpmathpublisher/
* @author Pascal Brachet
*/
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
/**
* All DokuWiki plugins to extend the parser/rendering mechanism
* need to inherit from this class
*/
class syntax_plugin_mathpublish extends DokuWiki_Syntax_Plugin {
// FIXME localise
var $enable = false;
var $msg_sent = false;
public function __construct() {
$this->enable = $this->_requirements_ok();
}
public function getType(){ return 'protected'; } #FIXME why not substition?
public function getPType(){ return 'normal'; }
public function getSort(){ return 208; }
/**
* Connect pattern to lexer
*/
public function connectTo($mode) {
$this->Lexer->addEntryPattern('<m(?=[^\r\n]*?>.*?</m>)',$mode,'plugin_mathpublish');
}
public function postConnect() {
$this->Lexer->addExitPattern('</m>','plugin_mathpublish');
}
/**
* Handle the match
*/
public function handle($match, $state, $pos, Doku_Handler $handler){
if ( $state == DOKU_LEXER_UNMATCHED ) {
list($size, $math) = preg_split('/>/u', $match, 2); // will split into size & math formulae
if (!is_numeric($size)) $size = 12; // default size in pixels
if (strlen($math) > 1) {
$c_first = $math{0};
$c_last = $math{strlen($math)-1};
if($c_first == ' '){
if($c_last == ' '){
$align = 'center';
}else{
$align = 'right';
}
}else{
if($c_last == ' '){
$align = 'left';
}else{
$align ='normal';
}
};
} else {
$align = 'normal';
}
return (array($size, trim($math), $align));
}
return false;
}
/**
* Create output
*/
function render($mode, Doku_Renderer $R, $data) {
if(!$data) return; // skip rendering for the enter and exit patterns #FIXME
if(!$this->enable) return;
if($mode != 'xhtml') return;
list($size, $math, $align) = $data;
$ident = md5($math.'-'.$size);
// check if we have a cached version available
$valignfile = getcachename($ident, '.mathpublish.valign');
$imagefile = getcachename($ident, '.mathpublish.png');
if(file_exists($valignfile)){
$valign = (int) io_readFile($valignfile);
}else{
require_once(dirname(__FILE__).'/phpmathpublisher/mathpublisher.php');
$pmp = new phpmathpublisher();
$valign = $pmp->renderImage($math,$size,$imagefile);
io_saveFile($valignfile,$valign);
}
// pass local files to PDF renderer
if(is_a($R,'renderer_plugin_dw2pdf')){
$img = 'dw2pdf://'.$imagefile;
}else{
$img = DOKU_BASE.'lib/plugins/mathpublish/img.php?img='.$ident;
}
// output aligned image
$R->doc .= '<img src="'.$img.'"
class="media'.$align.' mathpublish"
alt="'.hsc($math).'"
title="'.hsc($math).'"
style="display: inline-block; vertical-align:'.$valign.'px" />';
return false;
}
/**
* check if php installation has required libraries/functions
*/
private function _requirements_ok() {
if (!function_exists('imagepng')) {
$this->msg($this->getLang('nopng'),-1);
return false;
}
if (!function_exists('imagettftext')) {
$this->msg($this->getLang('noft'),-1);
return false;
}
return true;
}
/**
* used to avoid multiple messages
*/
private function _msg($str, $lvl=0) {
if ($this->msg_sent) return;
msg($str, $lvl);
$this->msg_sent = true;
}
// would like to see this function in io.php :)
function _mkdir($d) {
global $conf;
umask($conf['dmask']);
$ok = io_mkdir_p($d);
umask($conf['umask']);
return $ok;
}
}