-
Notifications
You must be signed in to change notification settings - Fork 0
/
musicparser.export.php
405 lines (299 loc) · 12.4 KB
/
musicparser.export.php
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
namespace{
class MusicParser{
const regSection = "/\n<-{2,}>\s*\n/";
const regColumn = "/\n-{2,}>\s*\n/";
const regVerse = "/^((?:-{2,}>)?)(\d+|#)([\.\)])( |$)/m";
const regChorus = "/^((?:-{2,}>)?)((?:R\d*|\(R\d*\)|®|Ref.?|Chorus)\:) /m";
const regPart = "/^((?:-{2,}>)?)([^\s\[]+)(\:)( |$)/m";
const regBlockTab = "/(^|^\s*\n|^\s*\n\s*\n|\n\s*\n\s*\n)(?=(?:[\w ]+\n)?[A-H]?\:?\|\-)/i";//(.+\n)?(?=$|\n\s*\n)
const regTextBlock = "/(?:^|\n\s*\n\s*\n)(-{2,}>)?/";
const regChord = "/(?<!\[)\[([A-H][^\]\|]*)(\|?)\]/i";
const regDiagram = "/\[\[([^\] ]+)( ([xX\d]{0,6}))?\]\]/";
const regInlineTab = "/(?<=\s)[A-H ]?\:?\|.+(?=\s)/i";
const regEOL = "/\n/";
const regLineStart = "/(^|\n)()([^\[]+)(?=\[)/i";
const regLinePart = "/()(\[[A-H][^\]]*\])([^\[\n]*)(?=\[|\n|$)/i";
public static function mergeSearch($search){
$parts = array();
foreach($search as $type => $matches){
foreach($matches as $item){
if(isset($parts[$item[0][1]])) continue;
$parts[$item[0][1]]= array("offset" => $item[0][1],"type" => $type);
foreach($item as $pattern) $parts[$item[0][1]]["matches"][] = $pattern[0];
}
}
usort($parts,function($a,$b){return $a["offset"]>$b["offset"] ? 1 : -1;});
return $parts;
}
public static function parse($data){
/* Normalizace koncu radku */
$data = str_replace("\r\n","\n",$data);
return new Musicparser\Song(null,$data);
}
public static function toHTML($data){
$data = htmlspecialchars($data);
/* Stylisticke upravy */
$data = preg_replace("/\"(.+)\"/s","„$1”",$data);
$data = preg_replace("/\.{3}/","…",$data);
$data = preg_replace("/(\d+)x(?=\W)/","$1×",$data);
/* parse into Song object */
$song = self::parse($data);
//\Lethe::dump($song->__toPlainStructure());
return $song->__toHTML();
}
}
}
/* SONG STRUCTURE */
namespace MusicParser{
abstract class SongObject{
protected $parent;
protected $children = array();
protected $text = "";
public function __construct($parent,$contents){
$this->parent = $parent;
if(method_exists($this,"parse")) call_user_func_array(array($this,"parse"),array_slice(func_get_args(),1));
else $this->text = $contents;
}
public function __toString(){return $this->text;}
public function __toPlainStructure($indent = ""){
$o = get_class($this);
if($this->children) for($i = 0; $i < count($this->children);$i++) $o .= "\n$indent'-".$this->children[$i]->__toPlainStructure($indent.($i === count($this->children) - 1 ? " " : "| "));
else $o .= " (".str_replace("\n","/",mb_substr($this->text,0,20000)).")";
return $o;
}
public function __toStructure(){
$o = array("type" => get_class($this));
if($this->children) foreach($this->children as $child) $o["children"][] = $child->__toStructure();
else $o["type"] .= " (".$this->__toString().")";
return $o;
}
public function __toHTML(){
if(!$this->children) return nl2br($this->text,false);
$o = "";
foreach($this->children as $child) $o .= $child->__toHTML();
return $o;
}
public function getParent($class){
if(get_class($this) === __NAMESPACE__."\\".$class) return $this;
if(!$this->parent) return null;
return $this->parent->getParent($class);
}
}
class Song extends SongObject{
public $sections;
public $verseNum = 0;
public function parse(&$contents){
foreach(preg_split(\MusicParser::regSection,$contents) as $childContent) $this->children[] = new Section($this,$childContent);
//\Lethe::dump($this->children);
$this->sections = &$this->children;
}
public function __toHTML(){
return "<div class=\"song\">".parent::__toHTML()."</div>";
}
}
class Section extends SongObject{
public $columns;
public function parse(&$contents){
$this->columns = &$this->children;
foreach(preg_split(\MusicParser::regColumn,$contents) as $childContent) $this->children[] = new Column($this,$childContent);
}
public function __toHTML(){
return "<div class=\"section\">".parent::__toHTML()."</div>";
}
}
class Column extends SongObject{
public $verses;
public function parse(&$contents){
preg_match_all(\MusicParser::regVerse,$contents,$search["verse"],PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
preg_match_all(\MusicParser::regChorus,$contents,$search["chorus"],PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
preg_match_all(\MusicParser::regPart,$contents,$search["part"],PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
preg_match_all(\MusicParser::regBlockTab,$contents,$search["blocktab"],PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
preg_match_all(\MusicParser::regTextBlock,$contents,$search["textblock"],PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
$parts = \MusicParser::mergeSearch($search);
for($i = 0;$i < count($parts);$i++){
$start = $parts[$i]["offset"] + strlen($parts[$i]["matches"][0]);
$length = @$parts[$i+1]["offset"] ? $parts[$i+1]["offset"] - $start : null;
$part_content = $length ? substr($contents,$start,$length) : substr($contents,$start);
$matches = @$parts[$i]["matches"];
switch($parts[$i]["type"]){
case "verse":
$this->children[] = new Verse($this,$part_content,$matches);
break;
case "chorus":
$this->children[] = new Chorus($this,$part_content,$matches);
break;
case "part":
$this->children[] = new Part($this,$part_content,$matches);
break;
case "blocktab":
$this->children[] = new BlockTab($this,$part_content,$matches);
break;
case "textblock":
$this->children[] = new TextBlock($this,$part_content,$matches);
break;
}
}
}
public function __toHTML(){
$width = floor(100/count($this->getParent("Section")->columns));
return "<div class=\"column\" style=\"width:$width%;\">".parent::__toHTML()."</div>";
}
}
abstract class SongPart extends SongObject{
public $float;
public $number;
public $delimiter;
public function parse($contents,$matches = array()){
$this->float = (bool) @$matches[1];
$this->label = @$matches[2];
$this->delimiter = @$matches[3];
preg_match_all(\MusicParser::regChord,$contents,$search["chord"],PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
preg_match_all(\MusicParser::regInlineTab,$contents,$search["inlinetab"],PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
preg_match_all(\MusicParser::regDiagram,$contents,$search["diagram"],PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
preg_match_all(\MusicParser::regEOL,$contents,$search["eol"],PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
$parts = \MusicParser::mergeSearch($search);
$offset = 0;
for($i = 0;$i < count($parts);$i++){
$text = substr($contents,$offset,$parts[$i]["offset"] - $offset);
if($text) $this->children[] = new Text($this,$text);
$matches = @$parts[$i]["matches"];
switch($parts[$i]["type"]){
case "chord":
$this->children[] = new Chord($this,$matches[0],$matches);
break;
case "inlinetab":
$this->children[] = new InlineTab($this,$matches[0],$matches);
break;
case "diagram":
$this->children[] = new Diagram($this,$matches[0],$matches);
break;
case "eol":
$this->children[] = new EOL($this,$matches[0]);
break;
}
$offset = $parts[$i]["offset"] + strlen($matches[0]);
}
$text = substr($contents,$offset);
if($text) $this->children[] = new Text($this,$text);
}
public function mergeChildrenHTML(){
$o = "";
$i = 0;
$only_chords = null;
$line_start = true;
$children = &$this->children;
while(@$children[$i]){
$class = str_replace(__NAMESPACE__."\\","",get_class($children[$i]));
switch($class){
case "Chord":
case "Text":
$text = "";
$chord = "";
$separate = false;
if($only_chords === null){
$a = $i;
while($children[$a] instanceof Chord) $a++;
$only_chords = ($children[$a] instanceof EOL);
}
if($children[$i] instanceof Chord){
$chord .= $children[$i]->__toHTML();
if(!$only_chords) $chord .= "<br>";
$separate = $children[$i]->separate;
$i++;
}
if(!$separate) while(@$children[$i] && $children[$i] instanceof Text){$text .= $children[$i]->__toHTML();$i++;}
if($line_start) $text = ltrim($text);
if(!$text && $chord && !$only_chords) $text = " ";
$text = preg_replace("/(^ | $)/"," ",$text); // preceding and trailing space does not render at the end of inline-block
$line_start = false;
$o .= "<span class=\"linepart\">{$chord}{$text}</span>";
break;
case "EOL":
$only_chords = null;
$line_start = true;
default:
$o .= $children[$i]->__toHTML();
$i++;
}
}
unset($text,$chord);
return $o;
}
public function __toHTML(){
$classes = array("part",strtolower(str_replace(__NAMESPACE__."\\","",get_class($this))));
if($this->float) $classes[] = "float";
$label = $this->label ? "<span class=\"label\"><span>".$this->label.$this->delimiter."</span> </span>" : "";
return "<div class=\"".join(" ",$classes)."\">".$label.$this->mergeChildrenHTML()."</div>";
}
}
class Verse extends SongPart{
public function parse($contents,$matches){
parent::parse($contents,$matches);
if($this->label === "#") $this->label = $this->getParent("Song")->verseNum + 1;
$this->getParent("Song")->verseNum = (int) $this->label;
}
}
class Chorus extends SongPart{}
class Part extends SongPart{}
class BlockTab extends SongPart{
private $precedingBlank;
private $contents;
public function parse(&$contents,$matches){
$this->precedingBlank = $matches[1];
$this->contents = $contents;
}
public function __toHTML(){
return nl2br($this->precedingBlank."<span class=\"tab block\">".str_replace(" "," ",$this->contents)."</span>",false);
}
}
class TextBlock extends SongPart{
public function __toHTML(){
$classes = array("part","textblock");
if($this->float) $classes[] = "float";
return "<div class=\"".join(" ",$classes)."\">".$this->mergeChildrenHTML()."</div>";
}
}
/* INLINE BLOCKS */
abstract class SongInline extends SongObject{
}
class Text extends SongInline{}
class InlineTab extends SongInline{
public function __toHTML(){
return "<span class=\"tab inline\">".str_replace(" "," ",parent::__toHTML())."</span>";
}
}
class Diagram extends SongInline{
public function parse($contents,$matches){
$this->chord = $matches[1];
$this->template = $matches[2];
}
public function __toHTML(){
$chord = $this->chord;
$template = $this->template;
$size = 100;
ob_start();
include __DIR__."/diagram-svg.php";
return ob_get_clean();
}
}
class Chord extends SongInline{
public $chords;
public $separate = false;
public function parse($contents,$matches){
$this->chords = array_map("trim",explode(",",$matches[1]));
$this->separate = ($matches[2] === "|");
}
public function __toHTML(){
$chords = "";
foreach($this->chords as $chord) $chords .= "<span>$chord</span> ";
return "<span class=\"chord\">$chords</span>";
}
}
class EOL extends SongInline{
public function __toHTML(){
return "<br>";
}
}
}
?>