InPlaceEditor removed from the view.
[lyrix.git] / lib / lyric_parser.rb
blob279bb0d7ac8c54b6b1864829f45aa073733a2188
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       end
32       
33       parsed
34     end
35     
36     def to_slides
37       parse[:slides]
38     end
39   end
40 end