Show TeX in wikibase diffs.

Diffs should show the rendered output as well as the TeX source,
so changes that do not affect the output noticably can still be
assessed by looking at the diff.

Bug: T125712
Change-Id: I098d30ea3158ec34723bfca313cf2bcc4cbc591f
This commit is contained in:
daniel 2016-02-25 12:55:36 +01:00
parent ac59fdcefc
commit 7018e85b7d
2 changed files with 56 additions and 10 deletions

View file

@ -62,15 +62,45 @@ class MathFormatter implements ValueFormatter {
return "<math>$tex</math>";
default:
$renderer = new MathMathML( $tex );
if ( $renderer->checkTex() && $renderer->render() ) {
return $renderer->getHtmlOutput();
$html = $renderer->getHtmlOutput();
} else {
$html = $renderer->getLastError();
}
if ( $this->format === SnakFormatter::FORMAT_HTML_DIFF ) {
$html = $this->formatDetails( $html, $tex );
}
// TeX string is not valid or rendering failed
return $renderer->getLastError();
return $html;
}
}
/**
* Constructs a detailed HTML rendering for use in diff views.
*
* @param string $valueHtml HTML
* @param string $tex TeX
*
* @return string HTML
*/
private function formatDetails( $valueHtml, $tex ) {
$html = '';
$html .= Html::rawElement( 'h4',
array( 'class' => 'wb-details wb-math-details wb-math-rendered' ),
$valueHtml
);
$html .= Html::rawElement( 'div',
array( 'class' => 'wb-details wb-math-details' ),
Html::element( 'code', array(), $tex )
);
return $html;
}
/**
* @return string One of the SnakFormatter::FORMAT_... constants.
*/

View file

@ -2,6 +2,7 @@
use DataValues\StringValue;
use DataValues\NumberValue;
use Wikibase\Lib\SnakFormatter;
/**
* Test the results of MathFormatter
@ -14,7 +15,7 @@ use DataValues\NumberValue;
*/
class MathFormatterTest extends MediaWikiTestCase {
const SOME_TEX = 'a^2+b^2=c^2';
const SOME_TEX = 'a^2+b^2 < c^2';
protected static $hasRestbase;
@ -36,16 +37,16 @@ class MathFormatterTest extends MediaWikiTestCase {
* @covers MathFormatter::__construct()
*/
public function testBasics() {
$formatter = new MathFormatter( 'text/plain' );
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
// check if the format input was corretly passed to the class
$this->assertSame( 'text/plain', $formatter->getFormat(), 'test getFormat' );
$this->assertSame( SnakFormatter::FORMAT_PLAIN, $formatter->getFormat(), 'test getFormat' );
}
/**
* @expectedException ValueFormatters\Exceptions\MismatchingDataValueTypeException
*/
public function testNotStringValue() {
$formatter = new MathFormatter( 'text/plain' );
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
$formatter->format( new NumberValue( 0 ) );
}
@ -53,7 +54,7 @@ class MathFormatterTest extends MediaWikiTestCase {
* @expectedException ValueFormatters\Exceptions\MismatchingDataValueTypeException
*/
public function testNullValue() {
$formatter = new MathFormatter( 'text/plain' );
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
$formatter->format( null );
}
@ -65,22 +66,37 @@ class MathFormatterTest extends MediaWikiTestCase {
}
public function testFormatPlain() {
$formatter = new MathFormatter( 'text/plain' );
$formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
$value = new StringValue( self::SOME_TEX );
$resultFormat = $formatter->format( $value );
$this->assertSame( self::SOME_TEX, $resultFormat );
}
public function testFormatHtml() {
$formatter = new MathFormatter( 'text/html' );
$formatter = new MathFormatter( SnakFormatter::FORMAT_HTML );
$value = new StringValue( self::SOME_TEX );
$resultFormat = $formatter->format( $value );
$this->assertContains( '</math>', $resultFormat, 'Result must contain math-tag' );
}
public function testFormatDiffHtml() {
$formatter = new MathFormatter( SnakFormatter::FORMAT_HTML_DIFF );
$value = new StringValue( self::SOME_TEX );
$resultFormat = $formatter->format( $value );
$this->assertContains( '</math>', $resultFormat, 'Result must contain math-tag' );
$this->assertContains( '</h4>', $resultFormat, 'Result must contain a <h4> tag' );
$this->assertContains( '</code>', $resultFormat, 'Result must contain a <code> tag' );
$this->assertContains( 'wb-details', $resultFormat, 'Result must contain wb-details class' );
$this->assertContains(
htmlspecialchars( self::SOME_TEX ),
$resultFormat,
'Result must contain the TeX source'
);
}
public function testFormatXWiki() {
$tex = self::SOME_TEX;
$formatter = new MathFormatter( 'text/x-wiki' );
$formatter = new MathFormatter( SnakFormatter::FORMAT_WIKI );
$value = new StringValue( self::SOME_TEX );
$resultFormat = $formatter->format( $value );
$this->assertSame( "<math>$tex</math>", $resultFormat, 'Tex wasn\'t properly wrapped' );