Class: Slippery::Processors::HrToSections
- Inherits:
-
Object
- Object
- Slippery::Processors::HrToSections
- Defined in:
- lib/slippery/processors/hr_to_sections.rb
Overview
Take a flat list of elements, and wrap elements between <hr> lines into a sections.
Class Method Summary collapse
Instance Method Summary collapse
- #call(doc) ⇒ Object
- #hr_to_section(element) ⇒ Object
-
#initialize(wrapper = H[:section], selector = 'body', options = {}) ⇒ HrToSections
constructor
A new instance of HrToSections.
Constructor Details
#initialize(wrapper = H[:section], selector = 'body', options = {}) ⇒ HrToSections
Returns a new instance of HrToSections.
14 15 16 |
# File 'lib/slippery/processors/hr_to_sections.rb', line 14 def initialize(wrapper = H[:section], selector = 'body', = {}) @selector, @wrapper, @anchor = selector, wrapper, .fetch(:anchor, true) end |
Class Method Details
.call(doc) ⇒ Object
10 11 12 |
# File 'lib/slippery/processors/hr_to_sections.rb', line 10 def self.call(doc) self.new.call(doc) end |
Instance Method Details
#call(doc) ⇒ Object
18 19 20 |
# File 'lib/slippery/processors/hr_to_sections.rb', line 18 def call(doc) doc.replace(@selector) { |element| hr_to_section(element) } end |
#hr_to_section(element) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/slippery/processors/hr_to_sections.rb', line 22 def hr_to_section(element) sections = [@wrapper] page = 1 element.children.each do |child| if child.tag == :hr last_section = @wrapper.merge_attrs(child) if @anchor last_section = last_section.merge_attrs(name: "#{page}") page += 1 end sections << last_section else sections[-1] = sections.last << child end end element.set_children(sections) end |