Added repeated lyrics to the parser
[lyrix.git] / lib / lyric_parser.rb
blobd579585b8a4328dc8ac82e4d90672753e6082940
1 module Lyrix
2   class LyricParser
3     DEFAULT_OPTIONS = {}
4   
5     def initialize(lyrics, options = {})
6       @options = DEFAULT_OPTIONS.merge(options)
7       @lyrics = lyrics
8     end
9     
10     def parse
11       @parsed_lyrics ||= parse!
12       @parsed_lyrics
13     end
14     
15     def parse!
16       parsed = {}
17       
18       parsed[:slides] = @lyrics.strip.gsub("\r\n", "\n").split(/\n[ \t]*\n/)
19       parsed[:slides].collect! { |s| s.strip }
20       parsed[:slides].reject! { |s| s.empty? }
21       
22       parsed[:slides].each_with_index do |s,idx|
23         [:chorus, :bridge].each do |sub|
24           if s =~ Regexp.new("^\\[#{sub}\\]") and !parsed.has_key?(sub)
25             parsed[sub] = s.split("\n")[1..-1].join("\n")
26             parsed[:slides][idx] = parsed[sub]
27           end
28         
29           parsed[:slides][idx].gsub!("[#{sub}]", parsed[sub]) if parsed[sub]
30         end
31         
32         if s =~ /^\[([2-9])x\]/
33           parsed[:slides][idx] = (1..($1.to_i)).collect { s.split("\n")[1..-1].join("\n") }
34         end
35       end
36       
37       parsed[:slides].flatten!
38       
39       parsed[:slides].each_with_index do |s,idx|
40         slide = parsed[:slides][idx].split("\n")
41         slide.each_with_index do |line,i|
42           puts line.inspect
43           if line.strip =~ /\[([2-9])x\]$/
44             num = $1.to_i
45             line = line.gsub("[#{num}x]", "").strip
46             slide[i] = (1..num).collect { line }
47           end
48         end
49         parsed[:slides][idx] = slide.flatten.join("\n")
50       end
51       
52       parsed
53     end
54     
55     def to_slides
56       parse[:slides]
57     end
58   end
59 end