Class: Brief::Document::Section

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

Defined Under Namespace

Classes: Builder, Mapping

Constant Summary collapse

Headings =
%w(h1 h2 h3 h4 h5 h6)
BuilderError =
Class.new(Exception)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, fragment, config) ⇒ Section

Returns a new instance of Section.



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

def initialize(title, fragment, config)
  @title = title
  @fragment = fragment
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/brief/document/section.rb', line 3

def config
  @config
end

#fragmentObject

Returns the value of attribute fragment.



2
3
4
# File 'lib/brief/document/section.rb', line 2

def fragment
  @fragment
end

#titleObject

Returns the value of attribute title.



2
3
4
# File 'lib/brief/document/section.rb', line 2

def title
  @title
end

Instance Method Details

#itemsObject



21
22
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
# File 'lib/brief/document/section.rb', line 21

def items
  return @items if @items

  data = []

  config.selectors.each do |selector|
    settings = config.selector_config[selector]

    if Headings.include?(selector)
      headings = fragment.css("article > #{selector}")

      articles = headings.map do |el|
        el.parent.set_attribute('data-parent-heading', el.text)
        el.parent
      end

      unless settings.empty?
        articles.compact.each do |article|
          data.push(settings.inject({}.to_mash) do |memo, pair|
            attribute, selector = pair
            result = article.css(selector)
            memo[attribute] = result.length > 1 ? result.map(&:text) : result.text
            memo
          end)
        end
      end
    end
  end

  @items = data
end

#textObject



17
18
19
# File 'lib/brief/document/section.rb', line 17

def text
  fragment.text
end

#to_htmlObject



13
14
15
# File 'lib/brief/document/section.rb', line 13

def to_html
  fragment.to_html
end