forked from wikimedia/mediawiki-extensions-Math
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathFormatter.php
More file actions
111 lines (95 loc) · 2.64 KB
/
MathFormatter.php
File metadata and controls
111 lines (95 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
use DataValues\StringValue;
use ValueFormatters\Exceptions\MismatchingDataValueTypeException;
use ValueFormatters\ValueFormatter;
use Wikibase\Lib\SnakFormatter;
/*
* 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"
*/
class MathFormatter implements ValueFormatter {
/**
* @var string One of the SnakFormatter::FORMAT_... constants.
*/
private $format;
/**
* Loads format to distinguish the type of formatting
*
* @param string $format One of the SnakFormatter::FORMAT_... constants.
*
* @throws InvalidArgumentException
*/
public function __construct( $format ) {
switch ( $format ) {
case SnakFormatter::FORMAT_PLAIN:
case SnakFormatter::FORMAT_WIKI:
case SnakFormatter::FORMAT_HTML:
case SnakFormatter::FORMAT_HTML_DIFF:
case SnakFormatter::FORMAT_HTML_WIDGET:
$this->format = $format;
break;
default:
throw new InvalidArgumentException( 'Unsupported output format: ' . $format );
}
}
/**
* @param StringValue $value
*
* @throws MismatchingDataValueTypeException
* @return string
*/
public function format( $value ) {
if ( !( $value instanceof StringValue ) ) {
throw new MismatchingDataValueTypeException( 'StringValue', get_class( $value ) );
}
$tex = $value->getValue();
switch ( $this->format ) {
case SnakFormatter::FORMAT_PLAIN:
return $tex;
case SnakFormatter::FORMAT_WIKI:
return "<math>$tex</math>";
default:
$renderer = new MathMathML( $tex );
if ( $renderer->checkTex() && $renderer->render() ) {
$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 $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',
[ 'class' => 'wb-details wb-math-details wb-math-rendered' ],
$valueHtml
);
$html .= Html::rawElement( 'div',
[ 'class' => 'wb-details wb-math-details' ],
Html::element( 'code', [], $tex )
);
return $html;
}
/**
* @return string One of the SnakFormatter::FORMAT_... constants.
*/
public function getFormat() {
return $this->format;
}
}