Class: Chordpro::HTML

Inherits:
Object
  • Object
show all
Defined in:
lib/chordpro/html.rb

Instance Method Summary collapse

Constructor Details

#initialize(song) ⇒ HTML

Returns a new instance of HTML.



5
6
7
8
9
# File 'lib/chordpro/html.rb', line 5

def initialize(song)
  @song = song
  @html = Builder::XmlMarkup.new
  @song.accept(self)
end

Instance Method Details

#comment(text) ⇒ Object Also known as: c



68
69
70
# File 'lib/chordpro/html.rb', line 68

def comment(text)
  @html.span(text, class: "comment")
end

#line(line, parts) ⇒ Object



26
27
28
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
# File 'lib/chordpro/html.rb', line 26

def line(line, parts)
  chords = []
  lyrics = []

  line.each do |element|
    if element.is_a?(Lyric)
      lyrics << element
    elsif element.is_a?(Chord)
      if chords[lyrics.size]
        chords << element
        lyrics << nil
      else
        chords[lyrics.size] = element
      end
    end
  end

  # ensure chords has same number of cells as lyrics
  chords[lyrics.size - 1] ||= nil if lyrics.size > 0

  @html.table do |table|
    if chords.any?
      table.tr(class: "chords") do |tr|
        chords.each do |chord|
          tr.td { |td| td.text! chord.to_s }
        end
      end
    end
    if lyrics.any?
      table.tr do |tr|
        lyrics.each do |lyric|
          tr.td { |td| td.text! lyric.to_s }
        end
      end
    end
  end
end

#linebreak(_) ⇒ Object



64
65
66
# File 'lib/chordpro/html.rb', line 64

def linebreak(_)
  @html.br
end

#subtitle(subtitle) ⇒ Object Also known as: st, su



20
21
22
# File 'lib/chordpro/html.rb', line 20

def subtitle(subtitle)
  @html.h2(class: "subtitle") { |h2| h2.text! subtitle }
end

#title(title) ⇒ Object Also known as: t



15
16
17
# File 'lib/chordpro/html.rb', line 15

def title(title)
  @html.h1(class: "title") { |h1| h1.text! title }
end

#to_sObject



11
12
13
# File 'lib/chordpro/html.rb', line 11

def to_s
  @html.target!
end