2015-12-14 20:26:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use DataValues\StringValue;
|
|
|
|
use ValueFormatters\Exceptions\MismatchingDataValueTypeException;
|
|
|
|
use ValueFormatters\ValueFormatter;
|
|
|
|
use Wikibase\Lib\SnakFormatter;
|
|
|
|
|
2017-06-20 07:11:57 +00:00
|
|
|
/**
|
|
|
|
* Formats the tex string based on the known formats
|
|
|
|
* - text/plain: used in the value input field of Wikidata
|
|
|
|
* - text/x-wiki: wikitext
|
|
|
|
* - text/html: used in Wikidata to display the value of properties
|
|
|
|
* Formats can look like this: "text/html; disposition=widget"
|
|
|
|
* or just "text/plain"
|
|
|
|
*/
|
2015-12-14 20:26:29 +00:00
|
|
|
|
|
|
|
class MathFormatter implements ValueFormatter {
|
|
|
|
|
2016-01-25 15:38:53 +00:00
|
|
|
/**
|
|
|
|
* @var string One of the SnakFormatter::FORMAT_... constants.
|
|
|
|
*/
|
2015-12-14 20:26:29 +00:00
|
|
|
private $format;
|
|
|
|
|
2016-01-25 15:38:53 +00:00
|
|
|
/**
|
2015-12-14 20:26:29 +00:00
|
|
|
* Loads format to distinguish the type of formatting
|
|
|
|
*
|
2016-01-25 15:38:53 +00:00
|
|
|
* @param string $format One of the SnakFormatter::FORMAT_... constants.
|
2015-12-14 20:26:29 +00:00
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function __construct( $format ) {
|
|
|
|
switch ( $format ) {
|
2016-01-25 15:38:53 +00:00
|
|
|
case SnakFormatter::FORMAT_PLAIN:
|
|
|
|
case SnakFormatter::FORMAT_WIKI:
|
|
|
|
case SnakFormatter::FORMAT_HTML:
|
|
|
|
case SnakFormatter::FORMAT_HTML_DIFF:
|
|
|
|
case SnakFormatter::FORMAT_HTML_WIDGET:
|
2015-12-14 20:26:29 +00:00
|
|
|
$this->format = $format;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new InvalidArgumentException( 'Unsupported output format: ' . $format );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 15:38:53 +00:00
|
|
|
/**
|
2015-12-14 20:26:29 +00:00
|
|
|
* @param StringValue $value
|
|
|
|
*
|
2016-01-25 15:38:53 +00:00
|
|
|
* @throws MismatchingDataValueTypeException
|
2015-12-14 20:26:29 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function format( $value ) {
|
|
|
|
if ( !( $value instanceof StringValue ) ) {
|
|
|
|
throw new MismatchingDataValueTypeException( 'StringValue', get_class( $value ) );
|
|
|
|
}
|
2016-01-25 15:38:53 +00:00
|
|
|
|
2015-12-14 20:26:29 +00:00
|
|
|
$tex = $value->getValue();
|
|
|
|
|
|
|
|
switch ( $this->format ) {
|
2016-01-25 15:38:53 +00:00
|
|
|
case SnakFormatter::FORMAT_PLAIN:
|
|
|
|
return $tex;
|
|
|
|
case SnakFormatter::FORMAT_WIKI:
|
2015-12-14 20:26:29 +00:00
|
|
|
return "<math>$tex</math>";
|
2016-01-25 15:38:53 +00:00
|
|
|
default:
|
2015-12-14 20:26:29 +00:00
|
|
|
$renderer = new MathMathML( $tex );
|
2016-02-25 11:55:36 +00:00
|
|
|
|
2016-01-25 15:38:53 +00:00
|
|
|
if ( $renderer->checkTex() && $renderer->render() ) {
|
2016-02-25 11:55:36 +00:00
|
|
|
$html = $renderer->getHtmlOutput();
|
|
|
|
} else {
|
|
|
|
$html = $renderer->getLastError();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->format === SnakFormatter::FORMAT_HTML_DIFF ) {
|
|
|
|
$html = $this->formatDetails( $html, $tex );
|
2015-12-14 20:26:29 +00:00
|
|
|
}
|
2016-01-25 15:38:53 +00:00
|
|
|
|
2015-12-14 20:26:29 +00:00
|
|
|
// TeX string is not valid or rendering failed
|
2016-02-25 11:55:36 +00:00
|
|
|
return $html;
|
2015-12-14 20:26:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 11:55:36 +00:00
|
|
|
/**
|
|
|
|
* 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',
|
2016-04-12 20:53:25 +00:00
|
|
|
[ 'class' => 'wb-details wb-math-details wb-math-rendered' ],
|
2016-02-25 11:55:36 +00:00
|
|
|
$valueHtml
|
|
|
|
);
|
|
|
|
|
|
|
|
$html .= Html::rawElement( 'div',
|
2016-04-12 20:53:25 +00:00
|
|
|
[ 'class' => 'wb-details wb-math-details' ],
|
|
|
|
Html::element( 'code', [], $tex )
|
2016-02-25 11:55:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2015-12-14 20:26:29 +00:00
|
|
|
/**
|
2016-01-25 15:38:53 +00:00
|
|
|
* @return string One of the SnakFormatter::FORMAT_... constants.
|
2015-12-14 20:26:29 +00:00
|
|
|
*/
|
|
|
|
public function getFormat() {
|
|
|
|
return $this->format;
|
|
|
|
}
|
2016-01-25 15:38:53 +00:00
|
|
|
|
2015-12-14 20:26:29 +00:00
|
|
|
}
|