Halftone.php
changeset 3 c3150cee8dd9
parent 2 2f3b76a405ce
child 4 bb3789db954a
equal deleted inserted replaced
2:2f3b76a405ce 3:c3150cee8dd9
    11   "Version list" : ['0.1']
    11   "Version list" : ['0.1']
    12 }
    12 }
    13 **!*/
    13 **!*/
    14 
    14 
    15 $plugins->attachHook('render_wikiformat_posttemplates', 'halftone_process_tags($text);');
    15 $plugins->attachHook('render_wikiformat_posttemplates', 'halftone_process_tags($text);');
    16 $plugins->attachHook('html_attribute_whitelist', '$whitelist["halftone"] = array("title", "key");');
    16 $plugins->attachHook('html_attribute_whitelist', '$whitelist["halftone"] = array("title", "transpose");');
    17 $plugins->attachHook('session_started', 'register_special_page(\'HalftoneRender\', \'Halftone AJAX render handler\', false);');
    17 $plugins->attachHook('session_started', 'register_special_page(\'HalftoneRender\', \'Halftone AJAX render handler\', false);');
    18 
    18 
    19 define('KEY_C', 0);
    19 define('KEY_C', 0);
    20 define('KEY_D', 2);
    20 define('KEY_D', 2);
    21 define('KEY_E', 4);
    21 define('KEY_E', 4);
   245 	return $result;
   245 	return $result;
   246 }
   246 }
   247 
   247 
   248 function halftone_process_tags(&$text)
   248 function halftone_process_tags(&$text)
   249 {
   249 {
       
   250 	global $circle_of_fifths;
       
   251 	
   250 	static $css_added = false;
   252 	static $css_added = false;
   251 	if ( !$css_added )
   253 	if ( !$css_added )
   252 	{
   254 	{
   253 		global $template;
   255 		global $template;
   254 		$template->preload_js(array('jquery', 'jquery-ui'));
   256 		$template->preload_js(array('jquery', 'jquery-ui'));
   305 			$attribs = decodeTagAttributes($matches[1][$i]);
   307 			$attribs = decodeTagAttributes($matches[1][$i]);
   306 			$song_title = isset($attribs['title']) ? $attribs['title'] : 'Untitled song';
   308 			$song_title = isset($attribs['title']) ? $attribs['title'] : 'Untitled song';
   307 			$chord_list = array();
   309 			$chord_list = array();
   308 			$inner = trim($matches[2][$i]);
   310 			$inner = trim($matches[2][$i]);
   309 			$song = halftone_render_body($inner, $chord_list);
   311 			$song = halftone_render_body($inner, $chord_list);
       
   312 			
   310 			$src = base64_encode($whole_match);
   313 			$src = base64_encode($whole_match);
   311 			$key = name_to_key(detect_key($chord_list));
   314 			$origkey = $key = name_to_key(detect_key($chord_list));
       
   315 			if ( isset($attribs['transpose']) && is_int($tokey = name_to_key($attribs['transpose'])) )
       
   316 			{
       
   317 				// re-render in new key
       
   318 				$transpose = $tokey - $key;
       
   319 				$song = halftone_render_body($inner, $chord_list, $tokey, $transpose);
       
   320 				$key = $tokey;
       
   321 			}
   312 			$select = '<select class="halftone-key">';
   322 			$select = '<select class="halftone-key">';
   313 			for ( $i = 0; $i < 12; $i++ )
   323 			for ( $i = 0; $i < 12; $i++ )
   314 			{
   324 			{
   315 				$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);
   325 				$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);
   316 				$label = prettify_accidentals($label);
   326 				$label = prettify_accidentals($label);
   317 				$sel = $key == $i ? ' selected="selected"' : '';
   327 				$sel = $key == $i ? ' selected="selected"' : '';
   318 				$select .= sprintf("<option%s value=\"%d\" halftone:abs=\"%d\">%s</option>", $sel, $i - $key, $i, $label);
   328 				$select .= sprintf("<option%s value=\"%d\" halftone:abs=\"%d\">%s</option>", $sel, $i - $origkey, $i, $label);
   319 			}
   329 			}
   320 			$select .= '</select>';
   330 			$select .= '</select>';
   321 			$headid = 'song:' . sanitize_page_id($song_title);
   331 			$headid = 'song:' . sanitize_page_id($song_title);
   322 			$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);
   332 			$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);
   323 		}
   333 		}
   324 	}
   334 	}
   325 }
   335 }
   326 
   336 
   327 function halftone_render_body($inner, &$chord_list, $inkey = false)
   337 function halftone_render_body($inner, &$chord_list, $inkey = false, $transpose = 0)
   328 {
   338 {
   329 	global $accidentals;
   339 	global $accidentals;
   330 	$song = '<div class="section">';
   340 	$song = '<div class="section">';
   331 	$chord_list = array();
   341 	$chord_list = array();
   332 	$transpose = isset($_GET['transpose']) ? intval($_GET['transpose']) : 0;
   342 	$transpose = isset($_GET['transpose']) ? intval($_GET['transpose']) : $transpose;
   333 	$transpose_accidental = $inkey ? $accidentals[$inkey] : false;
   343 	$transpose_accidental = $inkey ? $accidentals[$inkey] : false;
   334 	foreach ( explode("\n", $inner) as $line )
   344 	foreach ( explode("\n", $inner) as $line )
   335 	{
   345 	{
   336 		$chordline = false;
   346 		$chordline = false;
   337 		$chords_regex = '/(\((?:[A-G][#b]?(?:m?7?|2|add9|sus4|[Mm]aj[79])?(?:\/[A-G][#b]?)?)\))/';
   347 		$chords_regex = '/(\((?:[A-G][#b]?(?:m?7?|2|add9|sus4|[Mm]aj[79])?(?:\/[A-G][#b]?)?)\))/';