chords2halftone.php
author Dan Fuhry <dan@enanocms.org>
Sat, 15 Sep 2012 13:18:13 -0400
changeset 6 07ceb3e35e25
parent 4 bb3789db954a
permissions -rw-r--r--
Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     1
<?php
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     2
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     3
if ( isset($_POST['text']) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     4
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     5
	$text = explode("\n", str_replace("\r\n", "\n", trim($_POST['text'])));
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     6
	$chordline = false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     7
	echo "<pre>";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     8
	foreach ( $text as $i => $line )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     9
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    10
		if ( $i == 0 )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    11
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    12
			echo "&lt;halftone title=\"" . htmlspecialchars(htmlspecialchars($line)) . "\"&gt;\n";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    13
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    14
		else if ( trim($line) == "" )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    15
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    16
			// do nothing
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    17
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    18
		else if ( preg_match('/^(.*?(?:Verse|Chorus|Bridge).*?):\s*$/i', $line, $match) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    19
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    20
			if ( $chordline )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    21
			{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    22
				$chordstack = preg_split("/([ \t]+)/", $chordline);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    23
				echo '(' . implode(") (", $chordstack) . ")\n";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    24
				$chordline = false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    25
			}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    26
			
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    27
			if ( $i > 3 )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    28
				echo "\n";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    29
			echo "= {$match[1]} =\n";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    30
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    31
		else if ( preg_match('/^\((.*?(?:Verse|Chorus|Bridge).*?)\)$/i', $line, $match) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    32
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    33
			if ( $chordline )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    34
			{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    35
				$chordstack = preg_split("/([ \t]+)/", $chordline);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    36
				echo '(' . implode(") (", $chordstack) . ")\n";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    37
				$chordline = false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    38
			}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    39
			echo "\n[{$match[1]}]\n";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    40
		}
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 0
diff changeset
    41
		else if ( preg_match('/^(\s*([A-G][#b]?(?:[Mm]?7?|2|5|6|add9|sus4|[Mm]aj[79]|dim|aug)?)(\/[A-G][#b]?)?\s*)*$/', $line) )
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    42
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    43
			if ( $chordline )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    44
			{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    45
				// we have two chord lines in a row... treat the last one as a transition
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    46
				$chordstack = preg_split("/([ \t]+)/", $chordline);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    47
				echo '(' . implode(") (", $chordstack) . ")\n";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    48
			}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    49
			
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    50
			// chord line
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    51
			$chordline = $line;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    52
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    53
		else if ( $chordline && trim($line) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    54
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    55
			// combine chord line with text line
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    56
			$chordline = preg_split('/([ \t]+)/', $chordline, -1, PREG_SPLIT_DELIM_CAPTURE);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    57
			
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    58
			if ( count($chordline) >= 2 && preg_match('/^\s*$/', $chordline[0]) && preg_match('/^\s*$/', $chordline[1]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    59
			{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    60
				$merger = array_shift($chordline);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    61
				$chordline[0] .= $merger;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    62
			}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    63
			
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    64
			$chordstack = array();
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    65
			for ( $j = 0; $j < count($chordline); $j++ )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    66
			{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    67
				if ( $j == 0 && !preg_match('/^\s*$/', $chordline[$j]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    68
				{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    69
					$chordstack[] = "({$chordline[$j]})";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    70
					if ( isset($chordline[$j+1]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    71
					{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    72
						$chordline[$j+1] .= str_repeat(' ', strlen($chordline[$j]));
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    73
					}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    74
					continue;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    75
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    76
				// insert line up until this chord
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    77
				$chordstack[] = substr($line, 0, strlen($chordline[$j]));
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    78
				// chomp off the front of the line
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    79
				$line = substr($line, strlen($chordline[$j]));
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    80
				// insert this chord
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    81
				if ( isset($chordline[++$j]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    82
				{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    83
					if ( !empty($chordline[$j]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    84
					{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    85
						$chordstack[] = "({$chordline[$j]})";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    86
						if ( isset($chordline[$j+1]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    87
						{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    88
							$chordline[$j+1] .= str_repeat(' ', strlen($chordline[$j]));
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    89
						}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    90
					}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    91
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    92
			}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    93
			$chordstack[] = $line;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    94
			echo implode("", $chordstack) . "\n";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    95
			$chordline = false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    96
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    97
		else
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    98
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    99
			// assume it's a lyric line without chords...?
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   100
			echo "$line\n";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   101
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   102
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   103
	
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   104
	echo "&lt;/halftone&gt;";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   105
	echo "</pre>";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   106
	exit;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   107
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   108
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   109
?>
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   110
<form method="post">
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   111
<p><textarea name="text" rows="20" cols="100"></textarea></p>
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   112
<p><input type="submit" value="Make Halftone" /></p>
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   113
</form>