Class: Brief::Document::Section::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/brief/document/section/builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, options = {}) ⇒ Builder

Returns a new instance of 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, options = {})
  @source = source.map do |item|
    level, group = item
    [level, group.map { |f| f.is_a?(String) ? Nokogiri::HTML.fragment(f) : f }]
  end

  @low = options.fetch(:low, 1)
  @high = options.fetch(:high, 6)
  @nodes = []
  @cycles = 0

  begin
    run
  rescue
    raise BuilderError, $!
  end
end

Instance Attribute Details

#highObject

Returns the value of attribute high.



9
10
11
# File 'lib/brief/document/section/builder.rb', line 9

def high
  @high
end

#lowObject

Returns the value of attribute low.



9
10
11
# File 'lib/brief/document/section/builder.rb', line 9

def low
  @low
end

#nodesObject

Returns the value of attribute nodes.



9
10
11
# File 'lib/brief/document/section/builder.rb', line 9

def nodes
  @nodes
end

#sourceObject

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, options = {})
  new(source, options).to_fragment
end

Instance Method Details

#even?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


89
90
91
# File 'lib/brief/document/section/builder.rb', line 89

def maxed_out?
  @cycles > source.length
end

#runObject



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_fragmentObject



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