Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
MathMLFormatter | |
0.00% |
0 / 14 |
|
0.00% |
0 / 3 |
56 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
format | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
getFormat | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | namespace MediaWiki\Extension\MathSearch\Wikidata\MathML; |
3 | |
4 | use DataValues\StringValue; |
5 | use MediaWiki\Html\Html; |
6 | use ValueFormatters\Exceptions\MismatchingDataValueTypeException; |
7 | use ValueFormatters\ValueFormatter; |
8 | use Wikibase\Lib\Formatters\SnakFormatter; |
9 | |
10 | /* |
11 | * Formats the tex string based on the known formats |
12 | * * text/plain: used in the value input field of Wikidata |
13 | * * text/x-wiki: wikitext |
14 | * * text/html: used in Wikidata to display the value of properties |
15 | * Formats can look like this: "text/html; disposition=diff" |
16 | * or just "text/plain" |
17 | */ |
18 | |
19 | class MathMLFormatter implements ValueFormatter { |
20 | |
21 | /** |
22 | * @var string One of the SnakFormatter::FORMAT_... constants. |
23 | */ |
24 | private $format; |
25 | |
26 | /** |
27 | * Loads format to distinguish the type of formatting |
28 | * |
29 | * @param string $format One of the SnakFormatter::FORMAT_... constants. |
30 | */ |
31 | public function __construct( $format ) { |
32 | $this->format = $format; |
33 | } |
34 | |
35 | /** |
36 | * @param StringValue $value |
37 | * |
38 | * @return string |
39 | * @throws MismatchingDataValueTypeException |
40 | */ |
41 | public function format( $value ) { |
42 | if ( !( $value instanceof StringValue ) ) { |
43 | throw new InvalidArgumentException( '$value must be a StringValue' ); |
44 | } |
45 | |
46 | $mml = $value->getValue(); |
47 | |
48 | switch ( $this->format ) { |
49 | case SnakFormatter::FORMAT_WIKI: |
50 | return "<math type='pmml'>$mml</math>"; |
51 | case SnakFormatter::FORMAT_HTML_DIFF: |
52 | return Html::rawElement( 'h4', |
53 | [ 'class' => 'wb-details wb-math-details wb-math-rendered' ], $mml ) . |
54 | Html::rawElement( 'div', [ 'class' => 'wb-details wb-math-details' ], |
55 | Html::element( 'code', [], $mml ) ); |
56 | default: |
57 | return $mml; |
58 | } |
59 | } |
60 | |
61 | /** |
62 | * @return string One of the SnakFormatter::FORMAT_... constants. |
63 | */ |
64 | public function getFormat() { |
65 | return $this->format; |
66 | } |
67 | |
68 | } |