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
Returns a new instance of Builder.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/brief/document/section/builder.rb', line 9 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 run end |
Instance Attribute Details
#high ⇒ Object
Returns the value of attribute high.
7 8 9 |
# File 'lib/brief/document/section/builder.rb', line 7 def high @high end |
#low ⇒ Object
Returns the value of attribute low.
7 8 9 |
# File 'lib/brief/document/section/builder.rb', line 7 def low @low end |
#nodes ⇒ Object
Returns the value of attribute nodes.
7 8 9 |
# File 'lib/brief/document/section/builder.rb', line 7 def nodes @nodes end |
#source ⇒ Object
Returns the value of attribute source.
7 8 9 |
# File 'lib/brief/document/section/builder.rb', line 7 def source @source end |
Class Method Details
.run(source, options = {}) ⇒ Object
3 4 5 |
# File 'lib/brief/document/section/builder.rb', line 3 def self.run(source, = {}) new(source, ).to_fragment end |
Instance Method Details
#even? ⇒ Boolean
80 81 82 |
# File 'lib/brief/document/section/builder.rb', line 80 def even? source.map(&:first).uniq.length == 1 end |
#maxed_out? ⇒ Boolean
76 77 78 |
# File 'lib/brief/document/section/builder.rb', line 76 def maxed_out? @cycles > source.length end |
#run ⇒ Object
23 24 25 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 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/brief/document/section/builder.rb', line 23 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.first] 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) nodes.each do |node| parent = node.css('section, article').first if %w(h1 h2 h3 h4 h5 h6).include?(parent.children.first.name) parent['data-heading'] = parent.children.first.text end end nodes.map!(&:to_html) end |
#to_fragment ⇒ Object
84 85 86 87 |
# File 'lib/brief/document/section/builder.rb', line 84 def to_fragment @html = nodes.join('') unless nodes.empty? Nokogiri::HTML.fragment(@html || '<div/>') end |