-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsyntax.php
More file actions
377 lines (335 loc) · 11.7 KB
/
Copy pathsyntax.php
File metadata and controls
377 lines (335 loc) · 11.7 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
/**
* DokuWiki Plugin ABC2 (Syntax Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Anika Henke <anika@selfthinker.org>
*/
// must be run within Dokuwiki
if (!defined('DOKU_INC')) {
die();
}
class syntax_plugin_abc2 extends DokuWiki_Syntax_Plugin
{
/**
* @return string Syntax mode type
*/
public function getType()
{
return 'protected';
}
/**
* @return string Paragraph type
*/
public function getPType()
{
return 'block';
}
/**
* @return int Sort order - Low numbers go before high numbers
*/
public function getSort()
{
return 190;
}
/**
* Connect lookup pattern to lexer.
*
* @param string $mode Parser mode
*/
public function connectTo($mode)
{
$this->Lexer->addEntryPattern('<abc(?=.*\x3C/abc\x3E)',$mode,'plugin_abc2');
}
public function postConnect()
{
$this->Lexer->addExitPattern('</abc>','plugin_abc2');
}
/**
* Handle matches of the abc2 syntax
*
* @param string $match The match of the syntax
* @param int $state The state of the handler
* @param int $pos The position in the document
* @param Doku_Handler $handler The handler
*
* @return array Data for the renderer
*/
public function handle($match, $state, $pos, Doku_Handler $handler)
{
if ( $state == DOKU_LEXER_UNMATCHED ) {
$matches = preg_split('/>/u',$match,2);
$matches[0] = trim($matches[0]);
return array($matches[1],$matches[0]);
}
return true;
}
/**
* Render xhtml output or metadata
*
* @param string $mode Renderer mode (supported modes: xhtml)
* @param Doku_Renderer $renderer The renderer
* @param array $data The data from the handler() function
*
* @return bool If rendering was successful.
*/
public function render($mode, Doku_Renderer $renderer, $data)
{
if ($mode !== 'xhtml') {
return false;
}
if(strlen($data[0] ?? null) > 1){
$src = $data[0];
$transStr = $data[1];
// display just the code if 'abcok' is switched off
if (!$this->getConf('abcok')) {
$renderer->doc .= $renderer->file($src);
return true;
}
// render the main ABC block
$src = $this->_fixLibraryBugs($src);
$this->_renderAbcBlock($renderer, $src, true);
// transposition
// via adding `shift=xy` to key information field
// doesn't work with abcjs this way
if ($transStr && ($this->getConf('library') !== 'abcjs')) {
$transArray = $this->_transStringToArray($transStr);
foreach($transArray as &$trans) {
$transShiftStr = $this->_transposeToShift($trans);
$keyLine = $this->_getAbcLine($src, 'K');
$titleLine = $this->_getAbcLine($src, 'T');
// checking for already existing shift|score|sound not necessary
// a first 'shift' parameter is ignored
// 'score' or 'sound' will cause the score to be transposed further
if ($keyLine && $titleLine) {
$transSrc = $src;
// add shift parameter into key information field
$keyLineNew = $keyLine.' shift='.$transShiftStr;
$transSrc = $this->_replace_first($transSrc, $keyLine, $keyLineNew);
// add transposition semitone after title
$titleLineNew = $titleLine.' ['.$trans.']';
$transSrc = $this->_replace_first($transSrc, $titleLine, $titleLineNew);
// render another ABC block per transposition
$this->_renderAbcBlock($renderer, $transSrc, false);
}
}
}
}
return true;
}
/**
* Get transposition parameters into reasonable array
*
* @param string $str ABC parameter, string of transposition numbers
*
* @return array Array with transposition numbers
*/
function _transStringToArray($str) {
$arr = explode(" ", $str);
// the semitones to transpose have to be integers
$arr = array_map("intval", $arr);
// do not transpose by the same amount of semitones more than once
$arr = array_unique($arr);
// do not transpose higher or lower than 12 semitones
$arr = array_filter($arr, function($t){ return($t<12 && $t >-12); });
// do not allow transposition into more than 8 keys
array_splice($arr, 8);
return $arr;
}
/**
* Turn transposition number into 'shift' voice modifier
*
* ABC 2.1 had 'transpose' which worked with semitones
* ABC 2.2 has 'shift' which works with an interval of two notes
*
* @param int $num transpose, number of semitones
*
* @return string shift, string of two notes
*/
function _transposeToShift($num) {
$arr = array(
0 => 'CC',
1 => 'Bc',
2 => 'CD',
3 => 'Bd',
4 => 'CE',
5 => 'CF',
6 => 'BF',
7 => 'CG',
8 => 'Bg',
9 => 'CA',
10 => 'Ba',
11 => 'CB',
12 => 'Cc',
-1 => 'cB',
-2 => 'DC',
-3 => 'dB',
-4 => 'EC',
-5 => 'FC',
-6 => 'FB',
-7 => 'GC',
-8 => 'gB',
-9 => 'AC',
-10 => 'aB',
-11 => 'BC',
-12 => 'cC',
);
return $arr[$num];
}
/**
* Calculate default unit length
* according to http://abcnotation.com/wiki/abc:standard:v2.1#lunit_note_length
*
* @param string $meterLine line of meter (M) information field
*
* @return string string with default length
*/
function _getDefaultLength($meterLine) {
$meter = preg_replace('/\s?M\s?:/', '', $meterLine);
// default to 1/8 if meter is empty or "none"
if (!$meter || $meter == 'none') return "1/8";
// replace meter symbols with standard meters
$meter = str_replace('C|', '2/4', $meterLine);
$meter = str_replace('C', '4/4', $meterLine);
// meter is usually in the form <number>/<number>
preg_match("/(\d)\/(\d)/", $meter, $matches);
// default to 1/8 if meter isn't in that form
if (count($matches) != 3) return "1/8";
// default unit length calculation
$ratio = (int) $matches[1] / (int) $matches [2];
if ($ratio < 0.75) {
$length = "1/16";
} else {
$length = "1/8";
}
return $length;
}
/**
* Build classes for abc container depending on chosen abc library
*
* @param bool $orig original source (not a transposition)
*
* @return array CSS classes
*/
function _getClasses($orig) {
switch($this->getConf('library')) {
case 'abcjs':
// makes the midi player bigger
$libClasses = 'abcjs-large';
break;
case 'abc2svg':
$libClasses = 'abc';
break;
case 'abc-ui':
// 'abc-source' is mandatory and needs to be first
$libClasses = 'abc-source '.$this->getConf('abcuiConfig');
break;
}
// generic class plus class identifying the chosen library
$containerClasses = ' abc2-plugin lib-'.$this->getConf('library');
if ($orig && $this->getConf('showSource')) {
$containerClasses .= ' show-source';
} else {
$containerClasses .= ' hide-source';
}
return array(
'lib-classes' => $libClasses,
'container-classes' => $containerClasses,
);
}
/**
* Fix ABC library bugs:
*
* * abc2svg doesn't render anything if there is a space after the X:
* * $ABC_UI messes with note lengths if L isn't set
*
* @param string $src ABC code source
*
* @return string adjusted ABC code
*/
function _fixLibraryBugs($src) {
// remove spaces after 'X:'
// fixes a bug in abc2svg which won't render anything with a space after X:
// fixed upstream, see https://chiselapp.com/user/moinejf/repository/abc2svg/tktview?name=25d793e76f
$xLine = $this->_getAbcLine($src, 'X');
$xLineNoSpaces = str_replace(' ', '', $xLine);
$src = $this->_replace_first($src, $xLine, $xLineNoSpaces);
// add L: line if there isn't one
// fixes bug in $ABC_UI which has a wrong default unit length
$lLine = $this->_getAbcLine($src, 'L');
if (!$lLine) {
$mLine = $this->_getAbcLine($src, 'M');
if ($mLine) {
$lValue = $this->_getDefaultLength($mLine);
$mLineAndLline = $mLine.NL.'L:'.$lValue;
$src = $this->_replace_first($src, $mLine, $mLineAndLline);
}
}
return $src;
}
/**
* Render block of ABC
*
* @param Doku_Renderer $renderer The renderer
* @param string $src ABC code source
* @param bool $orig original source (not a transposition)
*
* @return void
*/
function _renderAbcBlock($renderer, $src, $orig) {
$classes = $this->_getClasses($orig);
// needs an extra parent div because abc2svg will otherwise break any broken rhythm
// see https://chiselapp.com/user/moinejf/repository/abc2svg/tktview/f632b51e4da81e3bd8292a30d078a5810488b878
// cannot be used for all libs because otherwise abc-ui will break
if ($this->getConf('library') == 'abc2svg') {
$renderer->doc .= '<div class="'.$classes['container-classes'].'">'.NL;
$renderer->doc .= '<div class="'.$classes['lib-classes'].'">';
} else {
// needs to be a div, otherwise abc-ui won't work
$renderer->doc .= '<div class="'.$classes['lib-classes'].$classes['container-classes'].'">';
}
if ($this->getConf('library') == 'abc-ui') {
$renderer->doc .= '%%player_top'.NL;
}
$renderer->doc .= hsc($src);
if ($this->getConf('library') == 'abc2svg') {
$renderer->doc .= '</div>';
}
$renderer->doc .= '</div>'.NL;
}
/**
* Get line of ABC with specific information field
*
* @param string $src ABC code source
* @param string $field ABC information field identifier
*
* @return string information field, whole line
*/
function _getAbcLine($src, $field) {
if (preg_match("/^\s?".$field."\s?:(.*?)$/m", $src, $result)) {
return $result[0];
} else {
return false;
}
}
/**
* Replace first string
*
* @author Zombat [https://stackoverflow.com/users/81205/zombat]
* @source https://stackoverflow.com/a/1252710/340300
* @license CC BY-SA 3.0 [https://creativecommons.org/licenses/by-sa/3.0/]
*
* @param string $haystack
* @param string $needle
* @param string $replace
*
* @return string
*/
function _replace_first($haystack, $needle, $replace) {
$pos = strpos($haystack, $needle);
if ($pos !== false) {
$newstring = substr_replace($haystack, $replace, $pos, strlen($needle));
}
return $newstring;
}
}