384c9adfe0c65a6fa727685ddb464ec4ea9184c7
[atutor.git] / mods / wiki / plugins / markup / tex.php
1 <?php
2 /*
3    This plugin adds the <tex>...</tex> tags which allow you to
4    integrate formulas into wiki pages, if you have the MimeTeX
5    package (from John Forkosh) installed. You can get it from
6    [http://www.forkosh.com/mimetex.html] (source or as binary)
7    
8    The original idea and implementation of this plugin was done by
9    Francois Vanderseypen <illumineo*users·sf·net> as you can see at
10    [http://netron.sourceforge.net/ewiki/netron.php?id=MimeTeX]
11    
12    <tex> \aleph = \bigsum_{\alpha,\beta}\Bigint_{0}^{\infty}\:\Gamma_{\alpha\beta}(x)\,dx </tex> 
13 */
14
15 define("MIMETEX_BIN", "mimetex");
16    # the actual mimetex utility (on poorly configured UNIX boxes you would
17    # have to give the full path name here)
18    
19 define("MIMETEX_DIR", "/home/www/user28494/htdocs/ewiki/var/mimetex/");
20    # where generated images are thrown in (world-writeable!), you could
21    # use "/tmp" if _INLINE was ok for your users
22    
23 define("MIMETEX_PATH", "/ewiki/var/mimetex/");
24    # where to access the generated images then (prefix for the <img> URLs)
25    
26 define("MIMETEX_INLINE", 0);
27    # if you'd instead like data: URIs for images (does not work with IE <7)
28
29
30 $ewiki_plugins["format_block"]["tex"]= array("mimetex_format_block");
31 $ewiki_config["format_block"]["tex"] = array("&lt;tex&gt;", "&lt;/tex&gt;", false, 0x0410);
32
33
34 function mimetex_format_block(&$str, &$in, &$iii, &$s, $btype) {
35    $str = mimetex_generate($str);
36 }
37
38
39 /*
40    calls mimetex to create image or returns link to cached file
41 */
42 function mimetex_generate($formula) {
43
44    $formula = preg_replace("/[\s]+/", "", $formula);
45    $filename = md5($formula).".gif";
46    $fullname = MIMETEX_DIR."/$filename";
47    
48    $url = false;
49    if (is_file($fullname)) {
50       $url = MIMETEX_PATH."/$filename";
51    }
52    else {
53       $cmd = MIMETEX_BIN . " -e $fullname '" . escapeshellarg($formula) . "'";
54       system($cmd, $status);
55       if (!$status_code) {
56          $url = MIMETEX_PATH."/$filename";
57       }
58    }
59
60    if ($url) {
61       if (MIMETEX_INLINE) {
62          $url = "data:image/gif;base64," . base64_encode(implode("", file($fullname)));
63       }
64       return('<img src="'.$url.'" alt="'.htmlentities($formula).'" align="absmiddle" />');
65    }
66    else {
67       return("[MimeTex could not convert formula \"$formula\".]");
68    }
69 }
70
71
72 ?>