Class: Slyde::Presentation::Parser
- Inherits:
-
Object
- Object
- Slyde::Presentation::Parser
- Defined in:
- lib/slyde/presentation/parser.rb
Constant Summary collapse
- SLIDE_TAG =
"article"
Instance Attribute Summary collapse
-
#html ⇒ Object
readonly
Returns the value of attribute html.
Class Method Summary collapse
Instance Method Summary collapse
- #document ⇒ Object
-
#initialize(html) ⇒ Parser
constructor
A new instance of Parser.
- #slide_headers ⇒ Object
- #slides ⇒ Object
- #title ⇒ Object
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
#html ⇒ Object (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
#document ⇒ Object
57 58 59 |
# File 'lib/slyde/presentation/parser.rb', line 57 def document @document ||= Nokogiri::HTML::DocumentFragment.parse(html) end |
#slide_headers ⇒ Object
53 54 55 |
# File 'lib/slyde/presentation/parser.rb', line 53 def @slide_headers ||= document.css("h1, h2, h3") end |
#slides ⇒ Object
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 ||= .each_with_index.inject([]) do |, (header, i)| next_header = [i + 1] nodes = next_header ? self.class.collect_until(header, next_header) : self.class.collect_rest(header) content = nodes.map(&:to_html).join .push({ title: header.content, body: "<#{SLIDE_TAG}>#{content}</#{SLIDE_TAG}>" }) end end |
#title ⇒ Object
49 50 51 |
# File 'lib/slyde/presentation/parser.rb', line 49 def title @title ||= document.at_css("h1").content end |