Class: Brief::Document::Section::Builder
- Inherits:
-
Object
- Object
- Brief::Document::Section::Builder
- Defined in:
- lib/brief/document/section/builder.rb
Instance Attribute Summary collapse
-
#high ⇒ Object
Returns the value of attribute high.
-
#low ⇒ Object
Returns the value of attribute low.
-
#nodes ⇒ Object
Returns the value of attribute nodes.
-
#source ⇒ Object
Returns the value of attribute source.
Class Method Summary collapse
Instance Method Summary collapse
- #even? ⇒ Boolean
-
#initialize(source, options = {}) ⇒ Builder
constructor
A new instance of Builder.
- #maxed_out? ⇒ Boolean
- #run ⇒ Object
- #to_fragment ⇒ Object
Constructor Details
#initialize(source, options = {}) ⇒ Builder
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/brief/document/section/builder.rb', line 11 def initialize(source, = {}) @source = source.map do |item| level, group = item [level, group.map { |f| f.is_a?(String) ? Nokogiri::HTML.fragment(f) : f }] end @low = .fetch(:low, 1) @high = .fetch(:high, 6) @nodes = [] @cycles = 0 begin run rescue raise BuilderError, $! end end |
Instance Attribute Details
#high ⇒ Object
Returns the value of attribute high.
9 10 11 |
# File 'lib/brief/document/section/builder.rb', line 9 def high @high end |
#low ⇒ Object
Returns the value of attribute low.
9 10 11 |
# File 'lib/brief/document/section/builder.rb', line 9 def low @low end |
#nodes ⇒ Object
Returns the value of attribute nodes.
9 10 11 |
# File 'lib/brief/document/section/builder.rb', line 9 def nodes @nodes end |
#source ⇒ Object
Returns the value of attribute source.
9 10 11 |
# File 'lib/brief/document/section/builder.rb', line 9 def source @source end |
Class Method Details
.run(source, options = {}) ⇒ Object
5 6 7 |
# File 'lib/brief/document/section/builder.rb', line 5 def self.run(source, = {}) new(source, ).to_fragment end |
Instance Method Details
#even? ⇒ Boolean
93 94 95 |
# File 'lib/brief/document/section/builder.rb', line 93 def even? source.map(&:first).uniq.length == 1 end |
#maxed_out? ⇒ Boolean
89 90 91 |
# File 'lib/brief/document/section/builder.rb', line 89 def maxed_out? @cycles > source.length end |
#run ⇒ 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 81 82 83 84 85 86 87 |
# File 'lib/brief/document/section/builder.rb', line 29 def run source.length.times do source.each_with_index do |item, index| n = index + 1 level, fragments = item next_level, next_fragments = source[n] if next_level && (next_level == level) && (level > low) new_fragment = (fragments + next_fragments).map(&:to_html).join('') source[index] = [level, [Nokogiri::HTML.fragment(new_fragment)]] source[n] = nil end end source.compact! end until even? || maxed_out? source.map! do |item| level, fragments = item [level, (fragments && fragments.first)] end if source.any? {|i| i[1].nil? } raise BuilderError, 'Fragments by level seems invalid' end source.each_with_index do |item, index| level, fragment = item n = index + 1 next_level, next_fragment = source[n] if fragment && next_level && (next_level > level) parent = fragment.css('section, article').first parent.add_child(next_fragment) source[index] = [level, fragment] source[n] = nil end end source.compact! @cycles += 1 end self.nodes = source.map(&:last).flatten nodes.each do |node| parent = node.css('section, article').first parents_first_el = parent && parent.children.first if parents_first_el && %w(h1 h2 h3 h4 h5 h6).include?(parent.children.first.name) parent['data-heading'] = parents_first_el.text end end nodes.map!(&:to_html) end |
#to_fragment ⇒ Object
97 98 99 100 |
# File 'lib/brief/document/section/builder.rb', line 97 def to_fragment @html = nodes.join('') unless nodes.empty? Nokogiri::HTML.fragment(@html || '<div/>') end |