/*
mimeTeX Plugin
===================
This plugin lets you easily embed LaTeX math in your html pages.
Version: 0.2, 2007-04-01, Luca Allulli
Version: 0.2.1, 2007-02-01, Daniele "MadMage" Calisi
Version: 0.5.1, 2007-06-08, Americo Dias
Version: 0.5.2, 2007-11-01, Yacoby
Authors: Luca Allulli, Americo Dias
http://www.amdias.com
License: CC-GNU LGPL
http://creativecommons.org/licenses/LGPL/2.1/
MODx version: 0.9.5
See mimeTeX.txt for details.
*/
/* <?php */
// Please install mimetex CGI on your own server and update the following line.
$mimetex_url = 'http://www.example.com/cgi-bin/mimetex.cgi';
// Change the following line if you want to change the cache formulas. Don't forget to have write permitions!
$cache_folder = 'assets/cache/';
// Grab and evluate the document event.
$mdStrEvent = &$modx->Event;
if ( $mdStrEvent->name == "OnWebPagePrerender" ){
// Grab the raw document output.
$mdStrDocOutput = $modx->documentOutput;
// Search the raw document output for blocks to be parsed. Those blocks are marked with <markdown> and </markdown> tags.
preg_match_all( "|<pre class=\"math\">(.*)</pre>|Uis", $mdStrDocOutput, $mdArrParseBlocks);
// Get the parsed output for each block.
// If no blocks found, exit
if( count($mdArrParseBlocks) == 0 ){
return;
}
// Process the formulas...
for ( $i=0; $i<count( $mdArrParseBlocks[0] ); $i++ ){
$mdBlock = $mdArrParseBlocks[0][$i];
// Create a unique formula filename using the formula text and MD5
$formula_text = $mdArrParseBlocks[1][$i];
$formula_hash = md5($formula_text);
$formula_filename = 'etx_'.$formula_hash.'.gif';
$cache_formula_path = $modx->config['base_path'] . $cache_folder . $formula_filename;
$cache_formula_url = $modx->config['site_url'] . $cache_folder . $formula_filename;
// Is the formula image cached?
if (!is_file($cache_formula_path)){
// If not, create the image
$mimetex_host = curl_init($mimetex_url . '?formdata=' . urlencode($formula_text));
// and save it to the cache path
$cache_file = fopen($cache_formula_path, 'w');
curl_setopt($mimetex_host, CURLOPT_FILE, $cache_file);
curl_setopt($mimetex_host, CURLOPT_HEADER, 0);
curl_exec($mimetex_host);
curl_close($mimetex_host);
fclose($cache_file);
}
$mdBlockText = '<img src="' . $cache_formula_url . '" alt="'.$formula_text.'" border=0 align=middle>';
$mdStrDocOutput = str_replace( $mdBlock, $mdBlockText, $mdStrDocOutput );
}
// Overwrite the raw document output with parsed one.
$modx->documentOutput = $mdStrDocOutput;
}
/* ?> */