-
Notifications
You must be signed in to change notification settings - Fork 0
/
musicparser.export.html.php
170 lines (137 loc) · 5.07 KB
/
musicparser.export.html.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
<?php
/* HTML EXPORT */
namespace MusicParser\Export\HTML{
abstract class SongObject implements \MusicParser\IExport{
public static function export(&$el){
if(!$el->children) return nl2br($el->text,false);
$o = "";
foreach($el->children as $child) $o .= $child->__toHTML();
return $o;
}
}
class Song extends SongObject{
public static function export(&$el){
return "<div class=\"song\">".parent::export($el)."</div>";
}
}
class Section extends SongObject{
public static function export(&$el){
return "<div class=\"section\">".parent::export($el)."</div>";
}
}
class Column extends SongObject{
public static function export(&$el){
$width = floor(100/count($el->getClosest("Section")->columns));
return "<div class=\"column\" style=\"width:$width%;\">".parent::export($el)."</div>";
}
}
abstract class SongPart extends SongObject{
protected static function mergeChildrenHTML(&$el){
$o = "";
$i = 0;
$only_chords = null;
$line_start = true;
$children = &$el->children;
while(@$children[$i]){
$class = end(explode("\\",get_class($children[$i])));
switch($class){
case "Chord":
case "Text":
$text = "";
$chord = "";
$separate = false;
if($only_chords === null){
$a = $i;
while(@$children[$a] instanceof \MusicParser\Structure\Chord) $a++;
$only_chords = (!isset($children[$a]) || ($children[$a] instanceof \MusicParser\Structure\EOL));
}
if($children[$i] instanceof \MusicParser\Structure\Chord){
$chord .= $children[$i]->__toHTML();
if(!$only_chords) $chord .= "<br>";
$separate = $children[$i]->separate;
$i++;
}
if(!$separate) while(@$children[$i] && $children[$i] instanceof \MusicParser\Structure\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 static function export(&$el){
$classes = array("part",strtolower(end(explode("\\",get_class($el)))));
if($el->float) $classes[] = "float";
if($el->clear) $classes[] = "clear";
$label = $el->label ? "<span class=\"label\"><span>".$el->label.$el->delimiter."</span> </span>" : "";
return "<div class=\"".join(" ",$classes)."\">".$label.self::mergeChildrenHTML($el)."</div>";
}
}
class Verse extends SongPart{}
class Chorus extends SongPart{}
class Part extends SongPart{}
class BlockTab extends SongPart{
public static function export(&$el){
return nl2br($el->precedingBlank."<span class=\"tab block\">".str_replace(" "," ",$el->contents)."</span>",false);
}
}
class TextBlock extends SongPart{
public static function export(&$el){
$classes = array("part","textblock");
if($el->float) $classes[] = "float";
if($el->clear) $classes[] = "clear";
return "<div class=\"".join(" ",$classes)."\">".self::mergeChildrenHTML($el)."</div>";
}
}
/* INLINE BLOCKS */
abstract class SongInline extends SongObject{}
class Text extends SongInline{
public static function export(&$el){
$text = $el->text;
/* stylisticke upravy */
$text = preg_replace("/(?<![\w])\"(?=\w)/","„",$text);
$text = preg_replace("/(?<=[\.,\w])\"(?!\w)/","”",$text);
$text = preg_replace("/\.{3}/","…",$text);
$text = preg_replace("/(^|[\W])(\d+)x(?=(\W|$))/","$1$2×",$text);
return $text;
}
}
class InlineTab extends SongInline{
public static function export(&$el){
return "<span class=\"tab inline\">".str_replace(" "," ",parent::export($el))."</span>";
}
}
class Diagram extends SongInline{
public static function export(&$el){
$chord = $el->chord;
$template = $el->template;
$size = 100;
ob_start();
include __DIR__."/diagram-svg.php";
return "<span class=\"diagram\">".ob_get_clean()."</span>";
}
}
class Chord extends SongInline{
public static function export(&$el){
$chords = "";
foreach($el->chords as $chord) $chords .= "<span>$chord</span> ";
return "<span class=\"chord\">$chords</span>";
}
}
class EOL extends SongInline{
public static function export(&$el){
return "<br>";
}
}
}
?>