Class: Gly::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/gly/parser.rb

Overview

parses gly source

Instance Method Summary collapse

Constructor Details

#initialize(syllable_separator = nil) ⇒ Parser

Returns a new instance of Parser.



6
7
8
9
# File 'lib/gly/parser.rb', line 6

def initialize(syllable_separator=nil)
  @syllable_separator = syllable_separator || '--'
  @current_block = :score
end

Instance Method Details

#parse(source) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/gly/parser.rb', line 11

def parse(source)
  if source.is_a? String
    parse_fname source
  else
    parse_io source
  end
end

#parse_fname(str) ⇒ Object



19
20
21
22
23
# File 'lib/gly/parser.rb', line 19

def parse_fname(str)
  File.open(str) do |fr|
    parse_io fr
  end
end

#parse_io(io) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
# File 'lib/gly/parser.rb', line 29

def parse_io(io)
  @doc = Document.new
  @score = Score.new

  if io.respond_to? :path
    @doc.path = io.path
  end

  io.each do |line|
    line = strip_comments(line)

    if empty?(line) && @current_block != :markup
      next
    # keywords specifying line or block type
    elsif new_score? line
      push_score
      @score = Score.new
      @current_block = :score
    elsif header_start? line
      push_score
      @score = @doc.header
      @current_block = :header
    elsif markup_start? line
      push_score
      @doc.content << Markup.new
      @current_block = :markup
    elsif block_start? line
      @current_block = line.match(/\w+/)[0].to_sym
    elsif explicit_lyrics? line
      parse_lyrics line
    elsif explicit_music? line
      parse_music line
    elsif explicit_markup? line
      push_score
      parse_markup line
    # line in a typed block
    elsif @current_block != :score
      parse_default line
    # content type autodetection
    elsif header_line? line
      parse_header line
    elsif lyrics_line? line
      parse_lyrics line
    else
      parse_default line
    end
  end

  push_score

  return @doc
end

#parse_str(str) ⇒ Object



25
26
27
# File 'lib/gly/parser.rb', line 25

def parse_str(str)
  parse_io(StringIO.new(str))
end