/plugins/wkp_LatexImg.php

https://github.com/pastak/jichikai_web · PHP · 146 lines · 70 code · 21 blank · 55 comment · 6 complexity · 8c92f4e13cda3eb12e8375c77a89d6e6 MD5 · raw file

  1. <?php
  2. /*
  3. * LatexImg plugin for LionWiki, (c) Matthew Leifer <matt@mattleifer.info>, 2009
  4. * Licensed under GNU GPLv2.0
  5. *
  6. * LatexImg plugin provides ability to insert snippets of LaTeX into a page that are
  7. * rendered as either .gif or .png images. This is useful for adding mathematical
  8. * equations to a wiki page.
  9. *
  10. * LatexImg plugin is built upon LaTeX Rendering Class 0.8, (c) 2003/2004 Benjamin Zeiss
  11. * See the file "LatexImg/class.latexrender.php" for licensing and further details.
  12. *
  13. * Syntax: insert {tex}Some Latex markup, e.g. E = mc^2{/tex}
  14. *
  15. * Explanation of error codes:
  16. *
  17. * 0 OK
  18. * 1 Formula longer than 500 characters
  19. * 2 Includes a blacklisted tag
  20. * 3 (Not used) Latex rendering failed
  21. * 4 Cannot create DVI file
  22. * 5 Picture larger than 500 x 500 followed by x x y dimensions
  23. * 6 Cannot copy image to pictures directory
  24. *
  25. */
  26. class LatexImg {
  27. // Description array for the ListPlugins plugin
  28. var $desc = array(
  29. array("LatexImgv0.1", "supports the insertion of snippets of LaTeX into a wiki page that are rendered as .gif or .png images.")
  30. );
  31. // This holds the LatexRender object
  32. var $latex;
  33. // Data array to hold details of Latex snippets between first and second parse
  34. var $latex_data = array();
  35. /* Constructor */
  36. function LatexImg()
  37. {
  38. global $REAL_PATH, $PLUGINS_DIR, $PLUGINS_DATA_DIR;
  39. // Setup important directories for LatexImg
  40. $lateximg_dir = $PLUGINS_DIR . 'LatexImg';
  41. $lateximg_data_dir = $PLUGINS_DATA_DIR . 'LatexImg';
  42. $pictures_dir = $lateximg_data_dir . '/pictures';
  43. $tmp_dir = $lateximg_data_dir . '/tmp';
  44. // Include LatexImg configuration variables
  45. include_once($REAL_PATH . $lateximg_dir . '/config.php');
  46. // Include the LatexRender class
  47. include_once($REAL_PATH . $lateximg_dir . '/class.latexrender.php');
  48. // Check existence of data and tmp directories
  49. foreach(array($lateximg_data_dir , $tmp_dir) as $DIR)
  50. if(!file_exists($DIR)) {
  51. mkdir($DIR, 0777);
  52. $f = fopen($REAL_PATH . $DIR . "/.htaccess", "w");
  53. fwrite($f, "deny from all");
  54. fclose($f);
  55. }
  56. // Check existence of pictures directory. Has to allow http requests.
  57. if(!file_exists($pictures_dir)) {
  58. mkdir($pictures_dir, 0777);
  59. $f = fopen($REAL_PATH . $pictures_dir . "/.htaccess", "w");
  60. fwrite($f, "allow from all");
  61. fclose($f);
  62. }
  63. // Get a new latex renderer
  64. $this->latex = new LatexRender($REAL_PATH . $pictures_dir, $pictures_dir, $REAL_PATH . $tmp_dir);
  65. // Set output image format
  66. $this->latex->_image_format = $LATEXIMG_IMAGE_FORMAT;
  67. // Set up the paths to the utilities that latexrender needs to run
  68. $this->latex->_latex_path = $LATEXIMG_LATEX_PATH;
  69. $this->latex->_dvips_path = $LATEXIMG_DVIPS_PATH;
  70. $this->latex->_convert_path = $LATEXIMG_CONVERT_PATH;
  71. $this->latex->_identify_path = $LATEXIMG_IDENTIFY_PATH;
  72. }
  73. /* Template hook */
  74. function template()
  75. {
  76. global $HEAD, $PLUGINS_DIR;
  77. // Include css for minimal styling on Latex images
  78. $HEAD .= '<style type="text/css" media="all">@import url("' . $PLUGINS_DIR . 'LatexImg/LatexImg.css");</style>';
  79. }
  80. /* subPagesLoaded hook.
  81. * Main processing of LaTeX markup. This is done before the main LionWiki syntax parsing
  82. * because many of the elements of LionWiki syntax constructions, e.g. { ,^ and }, commonly
  83. * occur inside LaTeX markup and would be misinterpreted. */
  84. function subPagesLoaded()
  85. {
  86. // Import the content to be processed
  87. global $CON;
  88. // Detect all latex markup
  89. preg_match_all("/(?<!\^)\{tex\}(.*?)\{\/tex\}/s", $CON, $tex_matches);
  90. // Go through each instance of latex markup, latex it, store data
  91. // and replace it with {tex} placeholder
  92. $this->latex_data = array();
  93. for ($i = 0, $c = count($tex_matches[0]); $i < $c; $i++) {
  94. $pos = strpos($CON, $tex_matches[0][$i]);
  95. $latex_formula = $tex_matches[1][$i];
  96. // Get a link to the image (this also makes the image if it doesn't exist yet)
  97. $this->latex_data[0][$i] = $this->latex->getFormulaURL($latex_formula);
  98. // Build a string for the alt attribute that displays the original LaTeX markup
  99. $alt_latex_formula = htmlentities($latex_formula, ENT_QUOTES);
  100. $alt_latex_formula = str_replace("\r", "&#13;", $alt_latex_formula);
  101. $alt_latex_formula = str_replace("\n", "&#10;", $alt_latex_formula);
  102. $this->latex_data[1][$i] = $alt_latex_formula;
  103. // Check whether latexrenderer successfully returned an image
  104. if ($this->latex_data[0][$i] != false)
  105. // If so, replace the LaTeX markup with {tex} placeholder
  106. $CON = substr_replace($CON, "{TEX}", $pos, strlen($tex_matches[0][$i]));
  107. else
  108. // If not, display the error code
  109. $CON = substr_replace($CON, "[LatexImg Error" . $this->latex->_errorcode . " " . $this->latex->_errorextra . "]", $pos, strlen($tex_matches[0][$i]));
  110. }
  111. }
  112. /* formatEnd hook. Replaces {tex} placeholders with images */
  113. function formatEnd()
  114. {
  115. // Import the content to be processed
  116. global $CON;
  117. // Detect all {TEX} placeholders
  118. preg_match_all("/\{TEX\}/", $CON, $tex_matches);
  119. // Go through each {TEX} placeholder and replace it with an image
  120. for ($i = 0; $i < count($tex_matches[0]); $i++) {
  121. $pos = strpos($CON, $tex_matches[0][$i]);
  122. $CON = substr_replace($CON, "<img class = 'latex' src='" . $this->latex_data[0][$i] . "' title='" . $this->latex_data[1][$i] . "' alt='" . $this->latex_data[1][$i] ."' />", $pos, strlen($tex_matches[0][$i]));
  123. }
  124. }
  125. }