Backend for songs.zachdecook.com
Diffstat (limited to 'laravel/app/Http/Controllers/SongController.php')
| -rw-r--r-- | laravel/app/Http/Controllers/SongController.php | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/laravel/app/Http/Controllers/SongController.php b/laravel/app/Http/Controllers/SongController.php index 44c766d..d1d4741 100644 --- a/laravel/app/Http/Controllers/SongController.php +++ b/laravel/app/Http/Controllers/SongController.php @@ -11,6 +11,50 @@ class SongController extends Controller public function show( $songNumber ) { $song = Song::where('number', $songNumber )->first(); - return view('song', ['song' => $song ] ); + $lines = explode( "\n", $song['text'] ); + $newText = ''; + foreach( $lines as $line ){ + $line = htmlspecialchars( $line ); + if ( $this->chordline( $line ) ) { + $transp = 0; + $class = "tabs chord$transp"; + $line = str_replace( + array('{','}'), + array('</b>{', "}<b class='$class'>" ), + $line ); + $newText .= "<b class='$class'>" . $line . "</b>\n"; + } else { + $newText .= "$line\n"; + } + } + $song['escapedText'] = $newText; + return view('song', ['song' => $song, 'transp' => 0 ] ); } + + /** + * @brief Determine whether or not this line contains chords. + */ + private function chordline($line) + { + $chords = + array( "C","C#","D","D#","E","F","F#","G","G#","A#","B",//"A", + "Db", "Eb", "Gb", "Bb",//"Ab", + "Cm", "Dm", "Fm", "Gm", "Bm", //"Em", "Am", + ); + $ambiguous = array( "Ab", "Em", "Am", "A" ); + $line = str_replace(array('/','7', "\n", '2', '4'), ' ', $line); + $tokens = array_filter(explode(' ', $line)); + + $badtokens = 0; + $ambtokens = 0; + $goodtokens = 0; + foreach ($tokens as $token) { + if( in_array( substr($token, 0,2), $chords ) ) $goodtokens++; + else if ( in_array( substr( $token, 0,2), $ambiguous) ) $ambtokens++; + else if( $badtokens > 10 ) return FALSE; + else $badtokens++; + } + return ($goodtokens *2)+ $ambtokens >= $badtokens; + } + } |