author | Dan Fuhry <dan@enanocms.org> |
Tue, 08 May 2012 01:38:29 -0400 | |
changeset 4 | bb3789db954a |
parent 3 | c3150cee8dd9 |
child 5 | c36fbf04faac |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
||
3 |
/**!info** |
|
4 |
{ |
|
5 |
"Plugin Name" : "Halftone", |
|
6 |
"Plugin URI" : "http://enanocms.org/plugin/halftone", |
|
7 |
"Description" : "Allows semantic input and transposition of chord sheets.", |
|
8 |
"Author" : "Dan Fuhry", |
|
9 |
"Version" : "0.1", |
|
10 |
"Author URI" : "http://enanocms.org/", |
|
11 |
"Version list" : ['0.1'] |
|
12 |
} |
|
13 |
**!*/ |
|
14 |
||
15 |
$plugins->attachHook('render_wikiformat_posttemplates', 'halftone_process_tags($text);'); |
|
3
c3150cee8dd9
Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents:
2
diff
changeset
|
16 |
$plugins->attachHook('html_attribute_whitelist', '$whitelist["halftone"] = array("title", "transpose");'); |
0 | 17 |
$plugins->attachHook('session_started', 'register_special_page(\'HalftoneRender\', \'Halftone AJAX render handler\', false);'); |
18 |
||
19 |
define('KEY_C', 0); |
|
20 |
define('KEY_D', 2); |
|
21 |
define('KEY_E', 4); |
|
22 |
define('KEY_F', 5); |
|
23 |
define('KEY_G', 7); |
|
24 |
define('KEY_A', 9); |
|
25 |
define('KEY_B', 11); |
|
26 |
define('KEY_C_SHARP', 1); |
|
27 |
define('KEY_E_FLAT', 3); |
|
28 |
define('KEY_F_SHARP', 6); |
|
29 |
define('KEY_G_SHARP', 8); |
|
30 |
define('KEY_B_FLAT', 10); |
|
31 |
||
32 |
define('ACC_FLAT', -1); |
|
33 |
define('ACC_SHARP', 1); |
|
34 |
||
35 |
$circle_of_fifths = array(KEY_C, KEY_G, KEY_D, KEY_A, KEY_E, KEY_B, KEY_F_SHARP, KEY_C_SHARP, KEY_G_SHARP, KEY_E_FLAT, KEY_B_FLAT, KEY_F); |
|
36 |
$accidentals = array( |
|
37 |
KEY_C => ACC_FLAT, |
|
38 |
KEY_G => ACC_SHARP, |
|
39 |
KEY_D => ACC_SHARP, |
|
40 |
KEY_A => ACC_SHARP, |
|
41 |
KEY_E => ACC_SHARP, |
|
42 |
KEY_B => ACC_SHARP, |
|
43 |
KEY_F_SHARP => ACC_SHARP, |
|
44 |
KEY_C_SHARP => ACC_SHARP, |
|
45 |
KEY_G_SHARP => ACC_FLAT, |
|
46 |
KEY_E_FLAT => ACC_FLAT, |
|
47 |
KEY_B_FLAT => ACC_FLAT, |
|
48 |
KEY_F => ACC_FLAT |
|
49 |
); |
|
50 |
||
51 |
function get_consonants($root_key) |
|
52 |
{ |
|
53 |
global $circle_of_fifths; |
|
54 |
$first = $root_key; |
|
55 |
$key = array_search($root_key, $circle_of_fifths); |
|
56 |
$fourth = $circle_of_fifths[(($key - 1) + count($circle_of_fifths)) % count($circle_of_fifths)]; |
|
57 |
$fifth = $circle_of_fifths[($key + 1) % count($circle_of_fifths)]; |
|
58 |
||
59 |
$minor1 = $circle_of_fifths[($key + 2) % count($circle_of_fifths)]; |
|
60 |
$minor2 = $circle_of_fifths[($key + 3) % count($circle_of_fifths)]; |
|
61 |
$minor3 = $circle_of_fifths[($key + 4) % count($circle_of_fifths)]; |
|
62 |
||
63 |
$result = array( |
|
64 |
'first' => $first, |
|
65 |
'fourth' => $fourth, |
|
66 |
'fifth' => $fifth, |
|
67 |
'minors' => array($minor1, $minor2, $minor3) |
|
68 |
); |
|
69 |
return $result; |
|
70 |
} |
|
71 |
||
72 |
function get_sharp($chord) |
|
73 |
{ |
|
74 |
return key_to_name(name_to_key($chord), ACC_SHARP); |
|
75 |
} |
|
76 |
||
77 |
function detect_key($chord_list) |
|
78 |
{ |
|
79 |
$majors = array(); |
|
80 |
$minors = array(); |
|
81 |
$sharp_or_flat = ACC_SHARP; |
|
4
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
82 |
// sus4 chords are also a great indicator since they are almost always |
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
83 |
// used exclusively on the fifth |
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
84 |
$have_sus4 = false; |
0 | 85 |
// index which chords are used in the song |
86 |
foreach ( $chord_list as $chord ) |
|
87 |
{ |
|
88 |
// discard bass note |
|
89 |
list($chord) = explode('/', $chord); |
|
90 |
$match = array(); |
|
4
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
91 |
preg_match('/((?:[Mm]?7?|2|5|6|add9|sus4|[Mm]aj[79]|dim|aug)?)$/', $chord, $match); |
0 | 92 |
if ( !empty($match[1]) ) |
4
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
93 |
{ |
0 | 94 |
$chord = str_replace_once($match[1], '', $chord); |
4
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
95 |
if ( $match[1] === 'sus4' ) |
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
96 |
$have_sus4 = $chord; |
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
97 |
} |
0 | 98 |
$sharp_or_flat = get_sharp($chord) == $chord ? ACC_SHARP : ACC_FLAT; |
99 |
$chord = get_sharp($chord); |
|
100 |
if ( $match[1] == 'm' || $match[1] == 'm7' ) |
|
101 |
{ |
|
102 |
// minor chord |
|
103 |
if ( !isset($minors[$chord]) ) |
|
104 |
$minors[$chord] = 0; |
|
105 |
$minors[$chord]++; |
|
106 |
} |
|
107 |
else |
|
108 |
{ |
|
109 |
// major chord |
|
110 |
if ( !isset($majors[$chord]) ) |
|
111 |
$majors[$chord] = 0; |
|
112 |
$majors[$chord]++; |
|
113 |
} |
|
114 |
} |
|
4
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
115 |
/* |
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
116 |
// remove very low scorers |
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
117 |
foreach ( $majors as $key => $count ) |
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
118 |
{ |
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
119 |
if ( $count < 1 ) |
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
120 |
unset($majors[$key]); |
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
121 |
} |
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
122 |
*/ |
0 | 123 |
// now we go through each of the detected major chords, calculate its consonants, and determine how many of its consonants are present in the song. |
124 |
$scores = array(); |
|
125 |
foreach ( $majors as $key => $count ) |
|
126 |
{ |
|
127 |
$scores[$key] = 0; |
|
128 |
$consonants = get_consonants(name_to_key($key)); |
|
129 |
if ( isset($majors[key_to_name($consonants['fourth'])]) ) |
|
130 |
$scores[$key] += 2; |
|
131 |
if ( isset($majors[key_to_name($consonants['fifth'])]) ) |
|
4
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
132 |
$scores[$key] += $have_sus4 === key_to_name($consonants['fifth']) ? 4 : 2; |
0 | 133 |
if ( isset($majors[key_to_name($consonants['minors'][0])]) ) |
134 |
$scores[$key] += 1; |
|
135 |
if ( isset($majors[key_to_name($consonants['minors'][1])]) ) |
|
136 |
$scores[$key] += 2; |
|
137 |
if ( isset($majors[key_to_name($consonants['minors'][2])]) ) |
|
138 |
$scores[$key] += 1; |
|
139 |
} |
|
140 |
$winner_val = -1; |
|
141 |
$winner_key = ''; |
|
142 |
foreach ( $scores as $key => $score ) |
|
143 |
{ |
|
144 |
if ( $score > $winner_val ) |
|
145 |
{ |
|
146 |
$winner_val = $score; |
|
147 |
$winner_key = $key; |
|
148 |
} |
|
149 |
} |
|
150 |
$winner_key = key_to_name(name_to_key($winner_key), $sharp_or_flat); |
|
151 |
return $winner_key; |
|
152 |
} |
|
153 |
||
154 |
function key_to_name($root_key, $accidental = ACC_SHARP) |
|
155 |
{ |
|
156 |
switch($root_key) |
|
157 |
{ |
|
158 |
case KEY_C: |
|
159 |
return 'C'; |
|
160 |
case KEY_D: |
|
161 |
return 'D'; |
|
162 |
case KEY_E: |
|
163 |
return 'E'; |
|
164 |
case KEY_F: |
|
165 |
return 'F'; |
|
166 |
case KEY_G: |
|
167 |
return 'G'; |
|
168 |
case KEY_A: |
|
169 |
return 'A'; |
|
170 |
case KEY_B: |
|
171 |
return 'B'; |
|
172 |
case KEY_C_SHARP: |
|
173 |
return $accidental == ACC_FLAT ? 'Db' : 'C#'; |
|
174 |
case KEY_E_FLAT: |
|
175 |
return $accidental == ACC_FLAT ? 'Eb' : 'D#'; |
|
176 |
case KEY_F_SHARP: |
|
177 |
return $accidental == ACC_FLAT ? 'Gb' : 'F#'; |
|
178 |
case KEY_G_SHARP: |
|
179 |
return $accidental == ACC_FLAT ? 'Ab' : 'G#'; |
|
180 |
case KEY_B_FLAT: |
|
181 |
return $accidental == ACC_FLAT ? 'Bb' : 'A#'; |
|
182 |
default: |
|
183 |
return false; |
|
184 |
} |
|
185 |
} |
|
186 |
||
187 |
function name_to_key($name) |
|
188 |
{ |
|
189 |
switch($name) |
|
190 |
{ |
|
191 |
case 'C': return KEY_C; |
|
192 |
case 'D': return KEY_D; |
|
193 |
case 'E': return KEY_E; |
|
194 |
case 'F': return KEY_F; |
|
195 |
case 'G': return KEY_G; |
|
196 |
case 'A': return KEY_A; |
|
197 |
case 'B': return KEY_B; |
|
198 |
case 'C#': case 'Db': return KEY_C_SHARP; |
|
199 |
case 'D#': case 'Eb': return KEY_E_FLAT; |
|
200 |
case 'F#': case 'Gb': return KEY_F_SHARP; |
|
201 |
case 'G#': case 'Ab': return KEY_G_SHARP; |
|
202 |
case 'A#': case 'Bb': return KEY_B_FLAT; |
|
203 |
default: return false; |
|
204 |
} |
|
205 |
} |
|
206 |
||
207 |
function prettify_accidentals($chord) |
|
208 |
{ |
|
209 |
if ( count(explode('/', $chord)) > 1 ) |
|
210 |
{ |
|
211 |
list($upper, $lower) = explode('/', $chord); |
|
212 |
return prettify_accidentals($upper) . '/' . prettify_accidentals($lower); |
|
213 |
} |
|
214 |
||
215 |
if ( strlen($chord) < 2 ) |
|
216 |
return $chord; |
|
217 |
||
218 |
if ( $chord{1} == 'b' ) |
|
219 |
{ |
|
220 |
$chord = $chord{0} . '♭' . substr($chord, 2); |
|
221 |
} |
|
222 |
else if ( $chord{1} == '#' ) |
|
223 |
{ |
|
224 |
$chord = $chord{0} . '♯' . substr($chord, 2); |
|
225 |
} |
|
226 |
return $chord; |
|
227 |
} |
|
228 |
||
229 |
function transpose_chord($chord, $increment, $accidental = false) |
|
230 |
{ |
|
231 |
global $circle_of_fifths; |
|
232 |
||
233 |
if ( count(explode('/', $chord)) > 1 ) |
|
234 |
{ |
|
235 |
list($upper, $lower) = explode('/', $chord); |
|
236 |
return transpose_chord($upper, $increment, $accidental) . '/' . transpose_chord($lower, $increment, $accidental); |
|
237 |
} |
|
238 |
// shave off any wacky things we're doing to the chord (minor, seventh, etc.) |
|
4
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
239 |
preg_match('/((?:[Mm]?7?|2|5|6|add9|sus4|[Mm]aj[79]|dim|aug)?)$/', $chord, $match); |
0 | 240 |
// find base chord |
241 |
if ( !empty($match[1]) ) |
|
242 |
$chord = str_replace($match[1], '', $chord); |
|
243 |
// what's our accidental? allow it to be specified, and autodetect if it isn't |
|
244 |
if ( !$accidental ) |
|
245 |
$accidental = strstr($chord, '#') ? ACC_SHARP : ACC_FLAT; |
|
246 |
// convert to numeric value |
|
247 |
$key = name_to_key($chord); |
|
248 |
if ( $key === false ) |
|
249 |
// should never happen |
|
250 |
return "[TRANSPOSITION FAILED: " . $chord . $match[1] . "]"; |
|
251 |
// transpose |
|
252 |
$key = (($key + $increment) + count($circle_of_fifths)) % count($circle_of_fifths); |
|
253 |
// return result |
|
254 |
$kname = key_to_name($key, $accidental); |
|
255 |
if ( !$kname ) |
|
256 |
// again, should never happen |
|
257 |
return "[TRANSPOSITION FAILED: " . $chord . $match[1] . " + $increment (->$key)]"; |
|
258 |
$result = $kname . $match[1]; |
|
259 |
// echo "$chord{$match[1]} + $increment = $result<br />"; |
|
260 |
return $result; |
|
261 |
} |
|
262 |
||
263 |
function halftone_process_tags(&$text) |
|
264 |
{ |
|
3
c3150cee8dd9
Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents:
2
diff
changeset
|
265 |
global $circle_of_fifths; |
c3150cee8dd9
Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents:
2
diff
changeset
|
266 |
|
0 | 267 |
static $css_added = false; |
268 |
if ( !$css_added ) |
|
269 |
{ |
|
270 |
global $template; |
|
271 |
$template->preload_js(array('jquery', 'jquery-ui')); |
|
272 |
$template->add_header(' |
|
273 |
<style type="text/css"> |
|
1
87dfd1a261bd
Several minor CSS changes to allow better integration with a theme designed for stage use and printing
Dan Fuhry <dan@enanocms.org>
parents:
0
diff
changeset
|
274 |
div.halftone { |
0 | 275 |
page-break-before: always; |
276 |
} |
|
277 |
span.halftone-line { |
|
278 |
display: block; |
|
279 |
padding-top: 10pt; |
|
280 |
position: relative; /* allows the absolute positioning in chords to work */ |
|
281 |
} |
|
282 |
span.halftone-chord { |
|
283 |
position: absolute; |
|
284 |
top: 0pt; |
|
285 |
color: rgb(27, 104, 184); |
|
286 |
} |
|
4
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
287 |
span.halftone-line.labeled span.halftone-chord { |
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
288 |
position: static; |
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
289 |
} |
0 | 290 |
span.halftone-chord.sequential { |
291 |
padding-left: 20pt; |
|
292 |
} |
|
293 |
div.halftone-key-select { |
|
294 |
float: right; |
|
295 |
} |
|
296 |
</style> |
|
297 |
<script type="text/javascript"> |
|
298 |
addOnloadHook(function() |
|
299 |
{ |
|
2
2f3b76a405ce
Fixed issue where js would break on pages with no halftone blocks
Dan Fuhry <dan@enanocms.org>
parents:
1
diff
changeset
|
300 |
var first_ht = $("div.halftone").get(0); |
2f3b76a405ce
Fixed issue where js would break on pages with no halftone blocks
Dan Fuhry <dan@enanocms.org>
parents:
1
diff
changeset
|
301 |
if ( first_ht ) |
2f3b76a405ce
Fixed issue where js would break on pages with no halftone blocks
Dan Fuhry <dan@enanocms.org>
parents:
1
diff
changeset
|
302 |
first_ht.style.pageBreakBefore = "auto"; |
0 | 303 |
$("select.halftone-key").change(function() |
304 |
{ |
|
305 |
var me = this; |
|
306 |
var src = $(this.parentNode.parentNode).attr("halftone:src"); |
|
307 |
ajaxPost(makeUrlNS("Special", "HalftoneRender", "transpose=" + $(this).val()) + "&tokey=" + $("option:selected", this).attr("halftone:abs"), "src=" + encodeURIComponent(src), function(ajax) |
|
308 |
{ |
|
309 |
if ( ajax.readyState == 4 && ajax.status == 200 ) |
|
310 |
{ |
|
311 |
var $songbody = $("div.halftone-song", me.parentNode.parentNode); |
|
312 |
$songbody.html(ajax.responseText); |
|
313 |
} |
|
314 |
}); |
|
315 |
}); |
|
316 |
}); |
|
317 |
</script> |
|
318 |
'); |
|
319 |
$css_added = true; |
|
320 |
} |
|
321 |
if ( preg_match_all('/<halftone(.*?)>(.+?)<\/halftone>/s', $text, $matches) ) |
|
322 |
{ |
|
323 |
foreach ( $matches[0] as $i => $whole_match ) |
|
324 |
{ |
|
325 |
$attribs = decodeTagAttributes($matches[1][$i]); |
|
326 |
$song_title = isset($attribs['title']) ? $attribs['title'] : 'Untitled song'; |
|
327 |
$chord_list = array(); |
|
328 |
$inner = trim($matches[2][$i]); |
|
329 |
$song = halftone_render_body($inner, $chord_list); |
|
3
c3150cee8dd9
Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents:
2
diff
changeset
|
330 |
|
0 | 331 |
$src = base64_encode($whole_match); |
3
c3150cee8dd9
Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents:
2
diff
changeset
|
332 |
$origkey = $key = name_to_key(detect_key($chord_list)); |
c3150cee8dd9
Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents:
2
diff
changeset
|
333 |
if ( isset($attribs['transpose']) && is_int($tokey = name_to_key($attribs['transpose'])) ) |
c3150cee8dd9
Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents:
2
diff
changeset
|
334 |
{ |
c3150cee8dd9
Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents:
2
diff
changeset
|
335 |
// re-render in new key |
c3150cee8dd9
Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents:
2
diff
changeset
|
336 |
$transpose = $tokey - $key; |
c3150cee8dd9
Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents:
2
diff
changeset
|
337 |
$song = halftone_render_body($inner, $chord_list, $tokey, $transpose); |
c3150cee8dd9
Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents:
2
diff
changeset
|
338 |
$key = $tokey; |
c3150cee8dd9
Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents:
2
diff
changeset
|
339 |
} |
0 | 340 |
$select = '<select class="halftone-key">'; |
341 |
for ( $i = 0; $i < 12; $i++ ) |
|
342 |
{ |
|
343 |
$label = in_array($i, array(KEY_C_SHARP, KEY_E_FLAT, KEY_F_SHARP, KEY_G_SHARP, KEY_B_FLAT)) ? sprintf("%s/%s", key_to_name($i, ACC_SHARP), key_to_name($i, ACC_FLAT)) : key_to_name($i); |
|
344 |
$label = prettify_accidentals($label); |
|
345 |
$sel = $key == $i ? ' selected="selected"' : ''; |
|
3
c3150cee8dd9
Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents:
2
diff
changeset
|
346 |
$select .= sprintf("<option%s value=\"%d\" halftone:abs=\"%d\">%s</option>", $sel, $i - $origkey, $i, $label); |
0 | 347 |
} |
348 |
$select .= '</select>'; |
|
1
87dfd1a261bd
Several minor CSS changes to allow better integration with a theme designed for stage use and printing
Dan Fuhry <dan@enanocms.org>
parents:
0
diff
changeset
|
349 |
$headid = 'song:' . sanitize_page_id($song_title); |
87dfd1a261bd
Several minor CSS changes to allow better integration with a theme designed for stage use and printing
Dan Fuhry <dan@enanocms.org>
parents:
0
diff
changeset
|
350 |
$text = str_replace_once($whole_match, "<div id=\"$headid\" class=\"halftone\" halftone:src=\"$src\"><div class=\"halftone-key-select\">$select</div><h1 class=\"halftone-title\">$song_title</h1>\n\n<div class=\"halftone-song\">\n" . $song . "</div></div>", $text); |
0 | 351 |
} |
352 |
} |
|
353 |
} |
|
354 |
||
3
c3150cee8dd9
Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents:
2
diff
changeset
|
355 |
function halftone_render_body($inner, &$chord_list, $inkey = false, $transpose = 0) |
0 | 356 |
{ |
357 |
global $accidentals; |
|
1
87dfd1a261bd
Several minor CSS changes to allow better integration with a theme designed for stage use and printing
Dan Fuhry <dan@enanocms.org>
parents:
0
diff
changeset
|
358 |
$song = '<div class="section">'; |
0 | 359 |
$chord_list = array(); |
3
c3150cee8dd9
Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents:
2
diff
changeset
|
360 |
$transpose = isset($_GET['transpose']) ? intval($_GET['transpose']) : $transpose; |
0 | 361 |
$transpose_accidental = $inkey ? $accidentals[$inkey] : false; |
362 |
foreach ( explode("\n", $inner) as $line ) |
|
363 |
{ |
|
364 |
$chordline = false; |
|
4
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
365 |
$chords_regex = '/(\((?:[A-G][#b]?(?:[Mm]?7?|2|5|6|add9|sus4|[Mm]aj[79]|dim|aug)?(?:\/[A-G][#b]?)?)\))/'; |
0 | 366 |
$line_split = preg_split($chords_regex, $line, -1, PREG_SPLIT_DELIM_CAPTURE); |
4
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
367 |
$line_pattern = ''; |
0 | 368 |
if ( preg_match_all($chords_regex, $line, $chords) ) |
369 |
{ |
|
370 |
// this is a line with lyrics + chords |
|
371 |
// echo out the line, adding spans around chords. here is where we also do transposition |
|
372 |
// (if requested) and |
|
373 |
$line_final = array(); |
|
374 |
$last_was_chord = false; |
|
375 |
foreach ( $line_split as $entry ) |
|
376 |
{ |
|
377 |
if ( preg_match($chords_regex, $entry) ) |
|
378 |
{ |
|
379 |
if ( $last_was_chord ) |
|
380 |
{ |
|
381 |
while ( !($pop = array_pop($line_final)) ); |
|
382 |
$new_entry = preg_replace('#</span>$#', '', $pop); |
|
383 |
$new_entry .= str_repeat(' ', 4); |
|
384 |
$new_entry .= prettify_accidentals($chord_list[] = transpose_chord(trim($entry, '()'), $transpose, $transpose_accidental)) . '</span>'; |
|
385 |
$line_final[] = $new_entry; |
|
386 |
} |
|
387 |
else |
|
388 |
{ |
|
389 |
$line_final[] = '<span class="halftone-chord">' . prettify_accidentals($chord_list[] = transpose_chord(trim($entry, '()'), $transpose, $transpose_accidental)) . '</span>'; |
|
390 |
} |
|
391 |
$last_was_chord = true; |
|
4
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
392 |
$line_pattern .= 'c'; |
0 | 393 |
} |
394 |
else |
|
395 |
{ |
|
396 |
if ( trim($entry) != "" ) |
|
397 |
{ |
|
398 |
$last_was_chord = false; |
|
399 |
$line_final[] = $entry; |
|
4
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
400 |
$line_pattern .= 'w'; |
0 | 401 |
} |
402 |
} |
|
403 |
} |
|
4
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
404 |
$class_append = preg_match('/^w?c+$/', $line_pattern) ? ' labeled' : ''; |
bb3789db954a
Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents:
3
diff
changeset
|
405 |
$song .= '<span class="halftone-line' . $class_append . '">' . implode("", $line_final) . "</span>\n"; |
0 | 406 |
} |
407 |
else if ( preg_match('/^=\s*(.+?)\s*=$/', $line, $match) ) |
|
408 |
{ |
|
1
87dfd1a261bd
Several minor CSS changes to allow better integration with a theme designed for stage use and printing
Dan Fuhry <dan@enanocms.org>
parents:
0
diff
changeset
|
409 |
$song .= "</div>\n<div class=\"section\">\n== {$match[1]} ==\n\n"; |
0 | 410 |
} |
411 |
else if ( trim($line) == '' ) |
|
412 |
{ |
|
413 |
continue; |
|
414 |
} |
|
1
87dfd1a261bd
Several minor CSS changes to allow better integration with a theme designed for stage use and printing
Dan Fuhry <dan@enanocms.org>
parents:
0
diff
changeset
|
415 |
else if ( preg_match('/^\[(.+)\]$/', trim($line), $match) ) |
87dfd1a261bd
Several minor CSS changes to allow better integration with a theme designed for stage use and printing
Dan Fuhry <dan@enanocms.org>
parents:
0
diff
changeset
|
416 |
{ |
87dfd1a261bd
Several minor CSS changes to allow better integration with a theme designed for stage use and printing
Dan Fuhry <dan@enanocms.org>
parents:
0
diff
changeset
|
417 |
$song .= "<br /><strong>Jump to:</strong> {$match[1]}\n"; |
87dfd1a261bd
Several minor CSS changes to allow better integration with a theme designed for stage use and printing
Dan Fuhry <dan@enanocms.org>
parents:
0
diff
changeset
|
418 |
} |
0 | 419 |
else |
420 |
{ |
|
421 |
$song .= "$line<br />\n"; |
|
422 |
} |
|
423 |
} |
|
1
87dfd1a261bd
Several minor CSS changes to allow better integration with a theme designed for stage use and printing
Dan Fuhry <dan@enanocms.org>
parents:
0
diff
changeset
|
424 |
return $song . '</div>'; |
0 | 425 |
} |
426 |
||
427 |
function page_Special_HalftoneRender() |
|
428 |
{ |
|
429 |
global $accidentals; |
|
430 |
$text = isset($_POST['src']) ? base64_decode($_POST['src']) : ''; |
|
431 |
if ( preg_match('/<halftone(.*?)>(.+?)<\/halftone>/s', $text, $match) ) |
|
432 |
{ |
|
433 |
require_once(ENANO_ROOT . '/includes/wikiformat.php'); |
|
434 |
$carp = new Carpenter(); |
|
435 |
$carp->exclusive_rule('heading'); |
|
436 |
$tokey = isset($_GET['tokey']) ? intval($_GET['tokey']) : false; |
|
437 |
echo $carp->render(halftone_render_body($match[2], $chord_list, $tokey)); |
|
438 |
} |
|
439 |
} |