Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
ScoreFormatter | |
0.00% |
0 / 33 |
|
0.00% |
0 / 5 |
132 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
format | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
formatAsHtml | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
formatDetails | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
getFormat | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /* |
3 | Score, a MediaWiki extension for rendering musical scores with LilyPond. |
4 | Copyright © 2011 Alexander Klauer |
5 | |
6 | This program is free software: you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by |
8 | the Free Software Foundation, either version 3 of the License, or |
9 | (at your option) any later version. |
10 | |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | GNU General Public License for more details. |
15 | |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | |
19 | */ |
20 | |
21 | namespace MediaWiki\Extension\Score; |
22 | |
23 | use DataValues\StringValue; |
24 | use InvalidArgumentException; |
25 | use MediaWiki\Html\Html; |
26 | use ValueFormatters\ValueFormatter; |
27 | use Wikibase\Lib\Formatters\SnakFormatter; |
28 | |
29 | /** |
30 | * Formats Lilypond string based on the known formats |
31 | * - text/plain: used in the value input field of Wikidata |
32 | * - text/x-wiki: wikitext |
33 | * - text/html: used in Wikidata to display the value of properties |
34 | * Formats can look like this: "text/html; disposition=diff" |
35 | * or just "text/plain" |
36 | */ |
37 | class ScoreFormatter implements ValueFormatter { |
38 | |
39 | /** |
40 | * @var string One of the SnakFormatter::FORMAT_... constants. |
41 | */ |
42 | private $format; |
43 | |
44 | /** |
45 | * Loads format to distinguish the type of formatting |
46 | * |
47 | * @param string $format One of the SnakFormatter::FORMAT_... constants. |
48 | */ |
49 | public function __construct( $format ) { |
50 | $this->format = $format; |
51 | } |
52 | |
53 | /** |
54 | * @param StringValue $value |
55 | * |
56 | * @throws InvalidArgumentException if not called with a StringValue |
57 | * @return string |
58 | */ |
59 | public function format( $value ) { |
60 | if ( !( $value instanceof StringValue ) ) { |
61 | throw new InvalidArgumentException( '$value must be a StringValue' ); |
62 | } |
63 | |
64 | $valueString = $value->getValue(); |
65 | |
66 | switch ( $this->format ) { |
67 | case SnakFormatter::FORMAT_PLAIN: |
68 | return $valueString; |
69 | case SnakFormatter::FORMAT_WIKI: |
70 | return "<score>$valueString</score>"; |
71 | default: |
72 | return $this->formatAsHtml( $valueString ); |
73 | } |
74 | } |
75 | |
76 | /** |
77 | * @param string $valueString |
78 | * @return string |
79 | */ |
80 | private function formatAsHtml( $valueString ) { |
81 | $args = []; |
82 | |
83 | global $wgWikibaseMusicalNotationLineWidthInches; |
84 | $args['line_width_inches'] = $wgWikibaseMusicalNotationLineWidthInches; |
85 | |
86 | try { |
87 | $valueHtml = Score::renderScore( |
88 | $valueString, |
89 | $args |
90 | ); |
91 | } catch ( ScoreException $exception ) { |
92 | return (string)$exception; |
93 | } |
94 | |
95 | if ( $this->format === SnakFormatter::FORMAT_HTML_DIFF ) { |
96 | $valueHtml = $this->formatDetails( $valueHtml, $valueString ); |
97 | } |
98 | |
99 | return $valueHtml; |
100 | } |
101 | |
102 | /** |
103 | * Constructs a detailed HTML rendering for use in diff views. |
104 | * |
105 | * @param string $valueHtml |
106 | * @param string $valueString |
107 | * |
108 | * @return string HTML |
109 | */ |
110 | private function formatDetails( $valueHtml, $valueString ) { |
111 | $detailsHtml = ''; |
112 | $detailsHtml .= Html::rawElement( 'h4', |
113 | [ 'class' => 'wb-details wb-musical-notation-details wb-musical-notation-rendered' ], |
114 | $valueHtml |
115 | ); |
116 | |
117 | $detailsHtml .= Html::rawElement( 'div', |
118 | [ 'class' => 'wb-details wb-musical-notation-details' ], |
119 | Html::element( 'code', [], $valueString ) |
120 | ); |
121 | |
122 | return $detailsHtml; |
123 | } |
124 | |
125 | /** |
126 | * @return string One of the SnakFormatter::FORMAT_... constants. |
127 | */ |
128 | public function getFormat() { |
129 | return $this->format; |
130 | } |
131 | |
132 | } |