Commit de0c3cdf authored by vermeul's avatar vermeul

finally managed to render math symbols from query results dynamically

parent 0e6655f5
......@@ -6,8 +6,7 @@
* @version 0.1
* @license GNU General Public Licence 2.0
*/
#use Math\MathRenderer;
use MathRenderer;
class YATA {
public static $annotations = "";
......@@ -50,16 +49,6 @@ class YATA {
//
public static function annotation_query( $parser, $querystring) {
#// render math symbols in output
#$mode = (int)$parser->getUser()->getOption( 'math' );
#// Indicate that this page uses math.
#// This affects the page caching behavior.
#if ( is_callable( 'ParserOptions::getMath' ) ) {
# $parser->getOptions()->getMath();
#} else {
# $parser->getOptions()->optionUsed( 'math' );
#}
$title = $parser->getTitle();
$wikiPage = new WikiPage( $title );
......@@ -284,6 +273,7 @@ class YATA {
$cat = $annotation->parent_category ? $annotation->parent_category . "/" . $annotation->category : $annotation->category;
$wikitext = $annotation->wiki_text;
$wikitext = str_replace('|style="text-align:right;"|', '', $wikitext);
$wikitext = htmlentities($wikitext, ENT_QUOTES);
$a = array('=', '{', '|', '}');
......@@ -297,12 +287,23 @@ class YATA {
$link = "[[".$annotation->title."]]";
}
$wikitext = html_entity_decode($wikitext);
$rendered_wikitext = preg_replace_callback(
'/(?P<math_element><math>(?:.*?)<\/math>)/s',
'self::render_math_content',
$wikitext
);
$rendered_wikitext = 'ENCODED_YATA_CONTENT '
. base64_encode($rendered_wikitext)
. ' END_ENCODED_YATA_CONTENT';
$table .= "<tr><td>".$cat
."</td><td>".$annotation->comment
."</td><td>". $link
."</td><td><pre>" . $wikitext . "</pre>"
."</td><td>". $rendered_wikitext
."</td>";
$p_annotations = self::get_page_annotations($dbr, $annotation->page_id);
$td = "<td>";
foreach ($p_annotations as $p_annotation) {
......@@ -1526,4 +1527,53 @@ EOT;
return $annot;
}
private static function render_math_content($matches){
global $wgDefaultUserOptions;
$mathMode = $wgDefaultUserOptions['math'];
$unescapedExtract = $matches["math_element"];
$unescapedExtract = Parser::extractTagsAndParams( array( 'math' ), $unescapedExtract, $mathTags );
foreach ( $mathTags as $id => $tag ) {
$content = $tag[1];
$attributes = $tag[2];
$fullElement = $tag[3];
$renderer = MathRenderer::getRenderer( $content, $attributes, $mathMode );
if ( preg_match( '#</\s*math\s*>$#', $fullElement) ) { // only full elements
$renderer = MathRenderer::getRenderer( $content, $attributes, $mathMode );
if ( $renderer->checkTex() && $renderer->render() ) {
$renderedMath = $renderer->getHtmlOutput();
$renderer->writeCache(); // Math rendering takes a while
return $renderedMath;
#$logger->info( "Display \\$$content\\$ in search results." );
}
else {
var_dump($renderer->getLastError());
#die("did not render!");
#$logger->warning( "Can not display \\$$content\\$ in search results. \n". var_export( $renderer->getLastError(), true ) );
}
}
// undo parser modifications to unclosed math tags or in the case of a rendering error
$unescapedExtract = str_replace( $id, $fullElement, $unescapedExtract );
}
return $unescapedExtract;
}
public static function onParserBeforeTidy( Parser &$parser, string &$text ) {
$text = preg_replace_callback(
'/ENCODED_YATA_CONTENT (?P<img64>[[0-9a-zA-Z\/+=]*) END_ENCODED_YATA_CONTENT/sm',
'self::decode_math_images',
$text
);
return true;
}
public static function decode_math_images($matches) {
return base64_decode($matches["img64"]);
}
}
......@@ -39,7 +39,8 @@
],
"PageContentSaveComplete": [
"YATA::onPageContentSaveComplete"
]
],
"ParserBeforeTidy": "YATA::onParserBeforeTidy"
},
"manifest_version": 1
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment