Skip to content
Browse files

made basic plugin mechanism work again

Unfortunately something within phpmathpublisher seems to have broken
during my restructure O_o
  • Loading branch information...
1 parent 42935fd commit a5309b3498fde3192344fc6e276c5dd2844e6092 @splitbrain splitbrain committed Jan 19, 2012
Showing with 133 additions and 115 deletions.
  1. +36 −0 img.php
  2. +4 −0 lang/en/lang.php
  3. +19 −2 phpmathpublisher/mathpublisher.php
  4. +74 −113 syntax.php
View
36 img.php
@@ -0,0 +1,36 @@
+<?php
+/**
+ * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
+ * @author Andreas Gohr <gohr@cosmocode.de>
+ */
+
+if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../../');
+define('NOSESSION',true);
+require_once(DOKU_INC.'inc/init.php');
+
+// let the syntax plugin do the work
+$cache = getcachename($_GET['img'], '.mathpublish.png');
+if(!file_exists($cache)) _fail();
+
+header('Content-Type: image/png;');
+header('Expires: '.gmdate("D, d M Y H:i:s", time()+max($conf['cachetime'], 3600)).' GMT');
+header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600));
+header('Pragma: public');
+http_conditionalRequest($time);
+if (http_sendfile($cache)) exit;
+$fp = @fopen($cache,"rb");
+if($fp){
+ http_rangeRequest($fp,filesize($cache),'image/png');
+}else{
+ header("HTTP/1.0 500 Internal Server Error");
+ print "Could not read file - bad permissions?";
+}
+
+
+function _fail(){
+ header("HTTP/1.0 404 Not Found");
+ header('Content-Type: image/png');
+ echo io_readFile('broken.png',false);
+ exit;
+}
+
View
4 lang/en/lang.php
@@ -0,0 +1,4 @@
+<?php
+
+$lang['nopng'] = "PHP's gd library is missing or unable to create PNG images";
+$lang['noft'] = "PHP installation missing access to freetype library";
View
21 phpmathpublisher/mathpublisher.php
@@ -2,12 +2,12 @@
/**
* PHPMathPublisher main class
- *
+ *
* This was converted fromt he original release 0.3 into a class structure. I
* don't speak french, so I'm a bit at loss at what all the functions and
* parameters really do. Documentation should be updated, function and parameter
* names should be refactored into English names.
- *
+ *
* @license GPL 2
* @author Pascal Brachet <pbrachet [at] xm1math.net>
* @author Andreas Gohr <gohr@cosmocode.de>
@@ -62,6 +62,23 @@ protected function detectimg($n){
}
/**
+ * Renders the formula into an image file
+ *
+ * @author Andreas Gohr <gohr@cosmocode.de>
+ * @param string $text The formula in syntax
+ * @param int $size The glyphsize in pixels
+ * @param string $output Full path to the image file
+ * @return int The vertical alignment
+ */
+ public function renderimage($text, $size, $output){
+ $formula = new PMP_expression_math($this->tableau_expression(trim($text)));
+ $formula->dessine($size);
+ $alignment = imagesy($formula->image) + $formula->base_verticale + 3;
+ imagepng($formula->image, $output);
+ return $alignment;
+ }
+
+ /**
* Creates the formula image (if the image is not in the cache)
*
* returns the <img src=...></img> html code.
View
187 syntax.php
@@ -21,33 +21,6 @@
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
-require_once(DOKU_INC.'inc/io.php');
-
-global $conf;
-
-// -----------------------[ math plugin globals ]---------------------------------------
-global $mathplugin_size, $mathplugin_urlimg;
-
- // default base size (pixels) of glyphs in the formulae
- $mathplugin_size = 12;
-
- // base url to access images, should correspond to $dirimg below.
- // if left at default, it will be modified to add a subfolder to avoid filling
- // the root media folder with clutter, refer _cacheExists()
- $mathplugin_urlimg = DOKU_URL.'lib/exe/fetch.php?w=&amp;h=&amp;cache=cache&amp;media=';
-
-// -----------------------[ mathpublisher settings ]------------------------------------
-global $dirfonts,$dirimg;
-
- // absolute path to the fonts directory (must not have '/' at end)
- $dirfonts=dirname(__FILE__)."/phpmathpublisher/fonts";
-
- // absolute path to the img directory (must not have '/' at end)
- // if left at default, it will be modified to add a subfolder to avoid filling
- // the root media folder with clutter, refer _cacheExists()
- $dirimg=$conf['mediadir'];
-
-// ------------------------------------------------------------------------------------
/**
* All DokuWiki plugins to extend the parser/rendering mechanism
@@ -56,130 +29,118 @@
class syntax_plugin_mathpublish extends DokuWiki_Syntax_Plugin {
// FIXME localise
- var $str_nopng = "PHP's gd library is missing or unable to create PNG images";
- var $str_noft = "PHP installation missing access to freetype library";
var $enable = false;
- var $msg_disable = "math plugin disabled: ";
var $msg_sent = false;
- function syntax_plugin_math() {
+ public function __construct() {
$this->enable = $this->_requirements_ok();
}
- function getType(){ return 'protected'; }
- function getPType(){ return 'normal'; }
- function getSort(){ return 208; }
+ public function getType(){ return 'protected'; } #FIXME why not substition?
+ public function getPType(){ return 'normal'; }
+ public function getSort(){ return 208; }
/**
* Connect pattern to lexer
*/
- function connectTo($mode) {
- $this->Lexer->addEntryPattern('<m(?=[^\r\n]*?>.*?</m>)',$mode,'plugin_math');
+ public function connectTo($mode) {
+ $this->Lexer->addEntryPattern('<m(?=[^\r\n]*?>.*?</m>)',$mode,'plugin_mathpublish');
}
- function postConnect() {
- $this->Lexer->addExitPattern('</m>','plugin_math');
+ public function postConnect() {
+ $this->Lexer->addExitPattern('</m>','plugin_mathpublish');
}
/**
* Handle the match
*/
- function handle($match, $state, $pos, &$handler){
- global $mathplugin_size;
-
- if ( $state == DOKU_LEXER_UNMATCHED ) {
- list($size, $math) = preg_split('/>/u', $match, 2); // will split into size & math formulae
- if (!is_numeric($size)) $size = $mathplugin_size;
-
- if (strlen($math) > 1) {
- $c_first = $math{0};
- $c_last = $math{strlen($math)-1};
+ public function handle($match, $state, $pos, &$handler){
+ if ( $state == DOKU_LEXER_UNMATCHED ) {
+ list($size, $math) = preg_split('/>/u', $match, 2); // will split into size & math formulae
+ if (!is_numeric($size)) $size = 12; // default size in pixels
+
+ if (strlen($math) > 1) {
+ $c_first = $math{0};
+ $c_last = $math{strlen($math)-1};
+
+ if($c_first == ' '){
+ if($c_last == ' '){
+ $align = 'center';
+ }else{
+ $align = 'right';
+ }
+ }else{
+ if($c_last == ' '){
+ $align = 'left';
+ }else{
+ $align ='normal';
+ }
+ };
+ } else {
+ $align = 'normal';
+ }
- $align = ($c_first == ' ') ? ($c_last == ' ' ? 'center' : 'right') : ($c_last == ' ' ? 'left' : 'normal');
- } else {
- $align = 'normal';
+ return (array($size, trim($math), $align));
}
-
- return (array($size, trim($math), $align));
- }
- return false;
+ return false;
}
/**
* Create output
*/
- function render($mode, &$renderer, $data) {
- global $mathplugin_urlimg;
-
- if (!$data) return; // skip rendering for the enter and exit patterns
- list($size, $math, $align) = $data;
-
- if($mode == 'xhtml'){
- // phpmathpublisher generates many E_NOTICE errors, ensure error_reporting doesn't include E_NOTICE.
- $error_level = error_reporting();
- error_reporting($error_level & ~E_NOTICE);
-
- // check we have ability to create png images
- if ($this->enable) {
- // check we have somewhere to create our images & make them
- if ($this->_cacheExists()) {
- require_once(dirname(__FILE__).'/phpmathpublisher/mathpublisher.php');
- $math_html = mathimage($math, $size, $mathplugin_urlimg);
+ function render($mode, &$R, $data) {
+ if(!$data) return; // skip rendering for the enter and exit patterns #FIXME
+ if(!$this->enable) return;
+ if($mode != 'xhtml') return;
+
+ list($size, $math, $align) = $data;
+ $ident = md5($math.'-'.$size);
+
+
+ // check if we have a cached version available
+ $valignfile = getcachename($ident, '.mathpublish.valign');
+ if(file_exists($valignfile)){
+ $valign = (int) io_readFile($valignfile);
+ }else{
+ $imagefile = getcachename($ident, '.mathpublish.png');
+ require_once(dirname(__FILE__).'/phpmathpublisher/mathpublisher.php');
+ $pmp = new phpmathpublisher();
+ $valign = $pmp->renderImage($math,$size,$imagefile);
+ io_saveFile($valignfile,$valign);
+ }
- if ($align != 'normal') {
- $math_html = preg_replace('/<img /i','\0 class="media'.$align.'" ',$math_html);
- }
+ // output aligned image
+ $R->doc .= '<img src="'.DOKU_BASE.'lib/plugins/mathpublish/img.php?img='.$ident.'"
+ class="media'.$align.' mathpublish"
+ alt="'.hsc($math).'"
+ title="'.hsc($math).'"
+ style="display: inline-block; vertical-align:'.$valign.'px" />';
- $renderer->doc .= $math_html;
- } else {
- $this->_msg("math plugin img folder is not writable", -1);
- }
- } else {
- $this->_msg($this->msg_disable, -1);
- }
-
- // return to previous error reporting level
- error_reporting($error_level);
- return true;
- }
- return false;
+ return false;
}
- function _cacheExists() {
- global $dirimg, $mathplugin_urlimg, $conf;
-
- // check for default setting
- if (!isset($dirimg) || !$dirimg) { $dirimg = $conf['mediadir']; }
- if ($dirimg == $conf['mediadir']) {
- // we don't want to clutter the root media dir, so create our own subfolder
- $dirimg .= "/cache_mathplugin";
- $mathplugin_urlimg .= "cache_mathplugin%3a";
-
- if (!@is_dir($dirimg)) {
- $this->_mkdir($dirimg);
- }
- }
-
- return @is_writable($dirimg);
- }
- // return true if php installation has required libraries/functions for mathpublisher
- function _requirements_ok() {
+ /**
+ * check if php installation has required libraries/functions
+ */
+ private function _requirements_ok() {
if (!function_exists('imagepng')) {
- $this->msg_disable .= $this->str_nopng;
- return false;
+ $this->msg($this->getLang('nopng'),-1);
+ return false;
}
if (!function_exists('imagettftext')) {
- $this->msg_disable .= $this->str_noft;
- return false;
+ $this->msg($this->getLang('noft'),-1);
+ return false;
}
return true;
}
- // used to avoid multiple messages
- function _msg($str, $lvl=0) {
+ /**
+ * used to avoid multiple messages
+ */
+ private function _msg($str, $lvl=0) {
if ($this->msg_sent) return;
msg($str, $lvl);

0 comments on commit a5309b3

Please sign in to comment.
Something went wrong with that request. Please try again.