1<?php 2 3/** 4 * DokuWiki Plugin latexit (Syntax Component) 5 * This file is based on mathjax syntax plugin https://www.dokuwiki.org/plugin:mathjax 6 * 7 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 8 * @author Mark Liffiton <liffiton@gmail.com> 9 * @author Adam Kučera <adam.kucera@wrent.cz> 10 */ 11// must be run within Dokuwiki 12if (!defined('DOKU_INC')) 13 die(); 14 15class syntax_plugin_latexit_mathjax extends DokuWiki_Syntax_Plugin { 16 17 /** 18 * Order in which this Syntax plugin will be called. 19 * @var int 20 */ 21 protected $sort; 22 23 # We need to grab any math before dokuwiki tries to parse it. 24 # Once it's 'claimed' by this plugin (type: protected), it won't be altered. 25 # Set of environments that this plugin will protect from Dokuwiki parsing 26 # * is escaped to work in regexp below 27 # Note: "math", "displaymath", and "flalign" environments seem to not be 28 # recognized by Mathjax... They will still be protected from Dokuwiki, 29 # but they will not be rendered by MathJax. 30 protected static $ENVIRONMENTS = array( 31 "math", 32 "displaymath", 33 "equation", 34 "equation\*", 35 "eqnarray", 36 "eqnarray\*", 37 "align", 38 "align\*", 39 "flalign", 40 "flalign\*", 41 "alignat", 42 "alignat\*", 43 "multline", 44 "multline\*", 45 "gather", 46 "gather\*", 47 ); 48 49 /** 50 * @return string Syntax mode type 51 */ 52 public function getType() { 53 return 'protected'; 54 } 55 56 /** 57 * @return int Sort order - Low numbers go before high numbers 58 */ 59 public function getSort() { 60 if (!isset($this->sort)) { 61 return 65; 62 } else { 63 return $this->sort; 64 } 65 } 66 67 /** 68 * Set sort order of the syntax component 69 * @param int $sort Sort order. 70 */ 71 public function _setSort($sort) { 72 $this->sort = $sort; 73 } 74 75 /** 76 * Connect lookup pattern to lexer. 77 * regexp patterns adapted from jsMath plugin: http://www.dokuwiki.org/plugin:jsmath 78 * 79 * @param string $mode Parser mode 80 */ 81 public function connectTo($mode) { 82 $this->Lexer->addEntryPattern('(?<!\\\\)\$(?=[^\$][^\r\n]*?\$)', $mode, 'plugin_latexit_mathjax'); 83 $this->Lexer->addEntryPattern('\$\$(?=.*?\$\$)', $mode, 'plugin_latexit_mathjax'); 84 $this->Lexer->addEntryPattern('\\\\\((?=.*?\\\\\))', $mode, 'plugin_latexit_mathjax'); 85 $this->Lexer->addEntryPattern('\\\\\[(?=.*?\\\\])', $mode, 'plugin_latexit_mathjax'); 86 foreach (self::$ENVIRONMENTS as $env) { 87 $this->Lexer->addEntryPattern('\\\\begin{' . $env . '}(?=.*?\\\\end{' . $env . '})', $mode, 'plugin_latexit_mathjax'); 88 } 89 } 90 91 public function postConnect() { 92 $this->Lexer->addExitPattern('\$(?!\$)', 'plugin_latexit_mathjax'); 93 $this->Lexer->addExitPattern('\\\\\)', 'plugin_latexit_mathjax'); 94 $this->Lexer->addExitPattern('\\\\\]', 'plugin_latexit_mathjax'); 95 foreach (self::$ENVIRONMENTS as $env) { 96 $this->Lexer->addExitPattern('\\\\end{' . $env . '}', 'plugin_latexit_mathjax'); 97 } 98 } 99 100 /** 101 * This syntax plugin should be used as a singleton. 102 * (so it can change its sort, when latex will be rendered) 103 * @return boolean 104 */ 105 public function isSingleton() { 106 return true; 107 } 108 109 /** 110 * Handle matches of the latexit syntax 111 * 112 * @param string $match The match of the syntax 113 * @param int $state The state of the handler 114 * @param int $pos The position in the document 115 * @param Doku_Handler $handler The handler 116 * @return array Data for the renderer 117 */ 118 public function handle($match, $state, $pos, Doku_Handler $handler) { 119 // Just pass it through... 120 return $match; 121 } 122 123 /** 124 * Render xhtml output or metadata 125 * 126 * @param string $mode Renderer mode (supported modes: xhtml) 127 * @param Doku_Renderer $renderer The renderer 128 * @param array $data The data from the handler() function 129 * @return bool If rendering was successful. 130 */ 131 public function render($mode, Doku_Renderer $renderer, $data) { 132 if ($mode == 'latex') { 133 $renderer->_mathMode($data); 134 return true; 135 } 136 return false; 137 } 138 139} 140 141// vim:ts=4:sw=4:et: 142