Add support for song titles.
[Akkordarbeit.git] / lib / akkordarbeit / text_formatter.rb
blobf1cff2a6ecb23b9c58ca67ee2760ca8f44a59493
1 # vim: fileencoding=UTF-8 ft=ruby syn=ruby ts=2 sw=2 ai eol et si
3 # Copyright (c) 2009 Marc Rummel <mailto:Marc.Rummel+Akkordarbeit@GoogleMail.Com>
4 # This code is licensed under the terms of the MIT License (see LICENSE.rdoc)
6 libdir = File.expand_path(File.dirname __FILE__).gsub(/(.*lib).*?/, '\1')
7 $LOAD_PATH.unshift libdir unless $LOAD_PATH.include? libdir
9 module Akkordarbeit
10   class TextFormatter
11     def format(parsetree, title = nil)
12       output = ''
13       output << '=' * (title.length + 2) << "\n" << ' ' << title << "\n" << '=' * (title.length + 2) << "\n" if title
14       parsetree.each do |section|
15         section.each do |line|
16           chords, lyrics = '', ''
17           line.each do |token|
18             regex = /(\[.*?\])/
19             if regex.match(token)
20               chords << token
21             else
22               lyrics << token
23               chords << ' ' * (lyrics.length - chords.length)
24             end
25           end
26           output << chords.rstrip << "\n" << lyrics.rstrip << "\n"
27         end
28         output << "\n"
29       end
30       return output.chomp
31     end
32   end
33 end