Class: Pslm::PslmReader
- Inherits:
-
Object
- Object
- Pslm::PslmReader
- Defined in:
- lib/pslm/pslmreader.rb
Overview
reads pslm files, provides their parsed data
Defined Under Namespace
Classes: PslmSyntaxError
Constant Summary collapse
- VERSE_PARTS =
verse parts: order, names, distinguishing regexes
[ { :name => :flex, :method => :flex=, :regex => /\+\s*$/ }, { :name => :first, :method => :first=, :regex => /\*\s*$/ }, { :name => :second, :method => :second=, :regex => /[^\s\*\+]\s*$/ } ]
Instance Attribute Summary collapse
-
#header ⇒ Object
readonly
Returns the value of attribute header.
Instance Method Summary collapse
-
#load_psalm(istream, has_title = true, autodetect = true) ⇒ Object
has_title - does the psalm text begin with a title line and an empty line? autodetect - check if there really is a title to prevent wrong interpretation.
- #load_verse(istream) ⇒ Object
- #read_file(fname, has_title = true, autodetect = true) ⇒ Object
-
#read_str(str, has_title = true, autodetect = true) ⇒ Object
public interface.
Instance Attribute Details
#header ⇒ Object (readonly)
Returns the value of attribute header.
18 19 20 |
# File 'lib/pslm/pslmreader.rb', line 18 def header @header end |
Instance Method Details
#load_psalm(istream, has_title = true, autodetect = true) ⇒ Object
has_title - does the psalm text begin with a title line and an empty line? autodetect - check if there really is a title to prevent wrong interpretation
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/pslm/pslmreader.rb', line 24 def load_psalm(istream, has_title=true, autodetect=true) ps = Pslm::Psalm.new ps.header.title = load_title(istream, has_title, autodetect) while v = load_verse(istream) do if v == '' then ps.add_strophe else ps.add_verse v end end return ps end |
#load_verse(istream) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/pslm/pslmreader.rb', line 47 def load_verse(istream) v = Psalm::Verse.new do |verse| part_loaded = gets_drop_comments istream VERSE_PARTS.each_with_index do |part, i| if part_loaded == nil then return part_loaded # eof end # empty line is strophe delimiter - return empty string to signal new strophe while part_loaded =~ /^\s*$/ do return '' end unless part_loaded =~ part[:regex] if part[:name] == :flex then next # probably a verse without flex - try to read the loaded line as a first half-verse end if istream.respond_to? :lineno then raise PslmSyntaxError.new("Unexpected verse part on line #{istream.lineno}: \"#{part_loaded}\" Expecting #{part[:name]}") else raise PslmSyntaxError.new("Unexpected verse part: \"#{part_loaded}\" Expecting #{part[:name]}") end end part_src = part_loaded.dup if [:flex, :first].include? part[:name] then part_loaded.sub!(/[\+\*]\s*$/, '') # there should be only one of these chars, at the very end of the line part_loaded.strip! end # split words, be aware of accents (space between accent parenthesis doesn't break word) words_raw = [] in_accent = false word_start = 0 part_loaded.chars.each_with_index do |c, ci| if c == '[' then in_accent = true elsif c == ']' then in_accent = false elsif in_accent == false && c == ' ' then words_raw << part_loaded[word_start .. ci-1] word_start = ci+1 end end words_raw << part_loaded[word_start .. -1] # last word words = words_raw.collect do |w| sylls = [] while w.size > 0 do i = w.index(/[\/\[\]]/) if i == nil then # last syllable sylls << Psalm::Syllable.new(syll_strip_special_chars(w)) break end s = w.slice!(0..i) accent = (s[-1] == ']') s = s[0..-2] # discard the delimiter next if s == '' # delimiter at the beginning/end of the string sylls << Psalm::Syllable.new(syll_strip_special_chars(s), accent) end Psalm::Word.new(sylls) end versepart = Psalm::VersePart.new(words, part_src, part[:name]) verse.send(part[:method], versepart) unless part[:name] == :second part_loaded = gets_drop_comments istream end end end return v end |
#read_file(fname, has_title = true, autodetect = true) ⇒ Object
14 15 16 |
# File 'lib/pslm/pslmreader.rb', line 14 def read_file(fname, has_title=true, autodetect=true) return load_psalm(CountingIStream.new(File.open(str)), has_title, autodetect) end |
#read_str(str, has_title = true, autodetect = true) ⇒ Object
public interface
10 11 12 |
# File 'lib/pslm/pslmreader.rb', line 10 def read_str(str, has_title=true, autodetect=true) return load_psalm(CountingIStream.new(StringIO.new(str)), has_title, autodetect) end |