2015-12-14 20:26:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use DataValues\StringValue;
|
|
|
|
use ValueFormatters\ValueFormatter;
|
2019-06-07 15:04:12 +00:00
|
|
|
use Wikibase\Lib\Formatters\SnakFormatter;
|
2015-12-14 20:26:29 +00:00
|
|
|
|
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
|
2017-07-10 09:04:39 +00:00
|
|
|
* Formats can look like this: "text/html; disposition=diff"
|
2017-06-20 07:11:57 +00:00
|
|
|
* 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
|
|
|
*/
|
|
|
|
public function __construct( $format ) {
|
2018-04-13 13:50:34 +00:00
|
|
|
$this->format = $format;
|
2015-12-14 20:26:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-25 15:38:53 +00:00
|
|
|
/**
|
2015-12-14 20:26:29 +00:00
|
|
|
* @param StringValue $value
|
|
|
|
*
|
2018-05-18 18:01:05 +00:00
|
|
|
* @throws InvalidArgumentException if not called with a StringValue
|
2015-12-14 20:26:29 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function format( $value ) {
|
|
|
|
if ( !( $value instanceof StringValue ) ) {
|
2018-05-18 18:01:05 +00:00
|
|
|
throw new InvalidArgumentException( '$value must be a StringValue' );
|
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 = $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>";
|
2018-04-13 13:50:34 +00:00
|
|
|
|
|
|
|
// Intentionally fall back to MathML output in all other, possibly unknown cases.
|
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
|
|
|
|
2017-07-10 09:40:12 +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
|
|
|
|
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.
|
|
|
|
*
|
2018-04-13 13:32:05 +00:00
|
|
|
* @param string $valueHtml
|
|
|
|
* @param string $tex
|
2016-02-25 11:55:36 +00:00
|
|
|
*
|
|
|
|
* @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
|
|
|
}
|