forked from capnrefsmmat/ipbLatex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipbLatex.php
More file actions
64 lines (57 loc) · 2.12 KB
/
Copy pathipbLatex.php
File metadata and controls
64 lines (57 loc) · 2.12 KB
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
<?php
/**
* IPB LaTeX plugin. Requires writable directories in which to write its images.
* Author: Alex Reinhart
*/
require_once 'class.latex-vb.php';
if( !class_exists('bbcode_parent_main_class') )
{
require_once( IPS_ROOT_PATH . 'sources/classes/text/parser/bbcode/defaults.php' );
}
class bbcode_plugin_latex extends bbcode_parent_main_class
{
public function __construct(ipsRegistry $registry, $_parent = null)
{
$this->currentBbcode = 'latex';
parent::__construct($registry, $_parent);
}
protected function _replaceText($txt)
{
$_tags = $this->_retrieveTags();
foreach($_tags as $_tag)
{
$_tag = strtolower($_tag);
switch ($_tag)
{
case "ce": $this->_latexMode = LATEX_INPUT_CHEM; break;
case "imath": $this->_latexMode = LATEX_INPUT_INLINE; break;
case "pgf": $this->_latexMode = LATEX_INPUT_PGF; break;
default: $this->_latexMode = LATEX_INPUT_NORM;
}
$this->curTagType = $_tag;
$txt = preg_replace_callback("/\[{$_tag}\](.*)\[\/{$_tag}\]/isU",
array(&$this, '_createImg'), $txt);
}
return $txt;
}
protected function _createImg($toTex)
{
$find = array("\", "&", "<br />", "<", ">",
""", "'", "!", '<p>', '</p>', "[", " ");
$replace = array("\\", "&", "", "<", ">", '"', "'", "!", "", "", "[", " ");
$formula_text = str_replace($find, $replace, $toTex[1]);
$latex = new Latex($this->_latexMode);
$img = $latex->renderLatex($formula_text);
if (is_array($img)) {
$imgsrc = "<img src=\"".$img[0][0]."\" width=\"".$img[1]."\" height=\"".$img[2]."\" alt=\"" . htmlentities($formula_text) . "\"";
if (count($img[0]) == 2)
{
$imgsrc .= " srcset=\"".$img[0][1]." 2x\"";
}
return $imgsrc." />";
} else {
return '[<strong>LaTeX Error:</strong> '.substr($img, 7).']';
}
}
}
?>