1<?php 2 3/** 4 * DokuWiki Plugin latexit (Helper Component) 5 * 6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 7 * @author Andreas Gohr <gohr@cosmocode.de> 8 */ 9// must be run within Dokuwiki 10if (!defined('DOKU_INC')) 11 die(); 12 13/** 14 * Latexit uses some function to load plugins. 15 */ 16require_once DOKU_INC . 'inc/pluginutils.php'; 17 18/** 19 * Helper component that keeps certain configurations for multiple instances of the renderer 20 */ 21class helper_plugin_latexit extends DokuWiki_Plugin { 22 23 /** @var array list of Packages to load */ 24 protected $packages; 25 26 /** @var array the list of preamble commands */ 27 protected $preamble; 28 29 /** 30 * Constructor 31 */ 32 public function __construct(){ 33 $this->packages = array(); 34 $this->preamble = array(); 35 } 36 37 /** 38 * Add a new entry to the preamble 39 * 40 * @param array|string $data Either the parameters for renderer_plugin_latexit::_c() or a string to be used as is 41 */ 42 public function addPreamble($data){ 43 // make sure data contains the right info 44 if(is_array($data)){ 45 if(!isset($data[0])) trigger_error('No command given', E_USER_ERROR); // command 46 if(!isset($data[1])) $data[1] = null; // text 47 if(!isset($data[2])) $data[2] = 1; // newlines 48 if(!isset($data[3])) $data[3] = null; // params 49 } else { 50 if(substr($data,-1) != "\n") $data .= "\n"; 51 } 52 53 $this->preamble[] = $data; 54 } 55 56 /** 57 * Return the setup preamble as array 58 * 59 * @return array returns a reference to the preamble (allows modifying) 60 */ 61 public function &getPreamble() { 62 return $this->preamble; 63 } 64 65 /** 66 * Add a package to the list of document packages 67 * 68 * @param Package $package 69 */ 70 public function addPackage(Package $package){ 71 $name = $package->getName(); 72 if(isset($this->packages[$name])) return; 73 $this->packages[$name] = $package; 74 } 75 76 /** 77 * Get all added document packages (sorted) 78 */ 79 public function getPackages() { 80 // sort the packages 81 usort($this->packages, array('Package', 'cmpPackages')); 82 return $this->packages; 83 } 84 85 /** 86 * Remove the given package from the package list 87 * 88 * @param string $name 89 */ 90 public function removePackage($name) { 91 if(isset($this->packages[$name])) unset($this->packages[$name]); 92 } 93 94 95 /** 96 * Escapes LaTeX special chars. 97 * Entities are in the middle of special tags so eg. MathJax texts are not escaped, but entities are. 98 * @param string $text Text to be escaped. 99 * @return string Escaped text. 100 */ 101 static public function escape($text) { 102 //find only entities in TEXT, not in eg MathJax 103 preg_match('#///ENTITYSTART///(.*?)///ENTITYEND///#si', $text, $entity); 104 //replace classic LaTeX escape chars 105 $text = str_replace( 106 array('\\', '{', '}', '&', '%', '$', '#', '_', '~', '^', '<', '>'), 107 array('\textbackslash', '\{', '\}', '\&', '\%', '\$', '\#', '\_', '\textasciitilde{}', '\textasciicircum{}', '\textless ', '\textgreater '), 108 $text); 109 //finalize escaping 110 $text = str_replace('\\textbackslash', '\textbackslash{}', $text); 111 //replace entities in TEXT 112 $text = preg_replace('#///ENTITYSTART///(.*?)///ENTITYEND///#si', $entity[1], $text); 113 return $text; 114 } 115} 116