Class: Slyde::Presentation::Parser

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

Constant Summary collapse

SLIDE_TAG =
"article"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html) ⇒ Parser

Returns a new instance of Parser.



26
27
28
# File 'lib/slyde/presentation/parser.rb', line 26

def initialize(html)
  @html = html
end

Instance Attribute Details

#htmlObject (readonly)

Returns the value of attribute html.



7
8
9
# File 'lib/slyde/presentation/parser.rb', line 7

def html
  @html
end

Class Method Details

.collect_rest(pointer) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/slyde/presentation/parser.rb', line 18

def self.collect_rest(pointer)
  [pointer].tap do |results|
    while pointer = pointer.next
      results << pointer
    end
  end
end

.collect_until(pointer, stop) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/slyde/presentation/parser.rb', line 9

def self.collect_until(pointer, stop)
  [].tap do |results|
    until pointer == stop
      results << pointer
      pointer = pointer.next
    end
  end
end

Instance Method Details

#documentObject



57
58
59
# File 'lib/slyde/presentation/parser.rb', line 57

def document
  @document ||= Nokogiri::HTML::DocumentFragment.parse(html)
end

#slide_headersObject



53
54
55
# File 'lib/slyde/presentation/parser.rb', line 53

def slide_headers
  @slide_headers ||= document.css("h1, h2, h3")
end

#slidesObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/slyde/presentation/parser.rb', line 30

def slides
  @slides ||= slide_headers.each_with_index.inject([]) do |slides, (header, i)|
    next_header = slide_headers[i + 1]

    nodes = next_header ?
      self.class.collect_until(header, next_header) :
      self.class.collect_rest(header)

    content = nodes.map(&:to_html).join

    slides.push({
      title: header.content,
      body: "<#{SLIDE_TAG}>#{content}</#{SLIDE_TAG}>"
    })

    slides
  end
end

#titleObject



49
50
51
# File 'lib/slyde/presentation/parser.rb', line 49

def title
  @title ||= document.at_css("h1").content
end