Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 28 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
ContentMathFormatter | |
0.00% |
0 / 28 |
|
0.00% |
0 / 4 |
132 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
format | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
72 | |||
formatDetails | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
getFormat | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\MathSearch\Wikidata\Content; |
4 | |
5 | use DataValues\StringValue; |
6 | use InvalidArgumentException; |
7 | use MediaWiki\Extension\Math\MathLaTeXML; |
8 | use MediaWiki\Html\Html; |
9 | use ValueFormatters\Exceptions\MismatchingDataValueTypeException; |
10 | use ValueFormatters\ValueFormatter; |
11 | use Wikibase\Lib\Formatters\SnakFormatter; |
12 | |
13 | /* |
14 | * Formats the tex string based on the known formats |
15 | * * text/plain: used in the value input field of Wikidata |
16 | * * text/x-wiki: wikitext |
17 | * * text/html: used in Wikidata to display the value of properties |
18 | * Formats can look like this: "text/html; disposition=diff" |
19 | * or just "text/plain" |
20 | */ |
21 | |
22 | class ContentMathFormatter implements ValueFormatter { |
23 | |
24 | /** |
25 | * @var string One of the SnakFormatter::FORMAT_... constants. |
26 | */ |
27 | private $format; |
28 | |
29 | /** |
30 | * Loads format to distinguish the type of formatting |
31 | * |
32 | * @param string $format One of the SnakFormatter::FORMAT_... constants. |
33 | */ |
34 | public function __construct( $format ) { |
35 | $this->format = $format; |
36 | } |
37 | |
38 | /** |
39 | * @param StringValue $value |
40 | * |
41 | * @return string |
42 | * @throws MismatchingDataValueTypeException |
43 | */ |
44 | public function format( $value ): string { |
45 | if ( !( $value instanceof StringValue ) ) { |
46 | throw new InvalidArgumentException( '$value must be a StringValue' ); |
47 | } |
48 | |
49 | $tex = $value->getValue(); |
50 | |
51 | switch ( $this->format ) { |
52 | case SnakFormatter::FORMAT_PLAIN: |
53 | return $tex; |
54 | case SnakFormatter::FORMAT_WIKI: |
55 | return "<math>$tex</math>"; |
56 | default: |
57 | $renderer = new MathLaTeXML( $tex ); |
58 | |
59 | if ( $renderer->checkTeX() && $renderer->render() ) { |
60 | $html = $renderer->getHtmlOutput(); |
61 | $renderer->writeCache(); |
62 | } else { |
63 | $html = $renderer->getLastError(); |
64 | } |
65 | |
66 | if ( $this->format === SnakFormatter::FORMAT_HTML_DIFF ) { |
67 | $html = $this->formatDetails( $html, $tex ); |
68 | } |
69 | |
70 | // TeX string is not valid or rendering failed |
71 | return $html; |
72 | } |
73 | } |
74 | |
75 | /** |
76 | * Constructs a detailed HTML rendering for use in diff views. |
77 | * |
78 | * @param string $valueHtml |
79 | * @param string $tex |
80 | * |
81 | * @return string HTML |
82 | */ |
83 | private function formatDetails( $valueHtml, $tex ) { |
84 | $html = ''; |
85 | $html .= Html::rawElement( 'h4', |
86 | [ 'class' => 'wb-details wb-math-details wb-math-rendered' ], |
87 | $valueHtml |
88 | ); |
89 | |
90 | $html .= Html::rawElement( 'div', |
91 | [ 'class' => 'wb-details wb-math-details' ], |
92 | Html::element( 'code', [], $tex ) |
93 | ); |
94 | |
95 | return $html; |
96 | } |
97 | |
98 | /** |
99 | * @return string One of the SnakFormatter::FORMAT_... constants. |
100 | */ |
101 | public function getFormat() { |
102 | return $this->format; |
103 | } |
104 | |
105 | } |