Sha256: ed804fe3929e7f097d61cb7b7f58a4ce5c566511b1c183b949ef42e4cc7476a6

Contents?: true

Size: 1013 Bytes

Versions: 44

Compression:

Stored size: 1013 Bytes

Contents

class Brief::Document::Section
  attr_accessor :title, :fragment
  attr_reader :config

  Headings = %w(h1 h2 h3 h4 h5 h6)

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

  def to_html
    fragment.to_html
  end

  def text
    fragment.text
  end

  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 > h2')
        articles = headings.map(&:parent)

        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
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
brief-1.16.2 lib/brief/document/section.rb
brief-1.16.1 lib/brief/document/section.rb
brief-1.16.0 lib/brief/document/section.rb
brief-1.15.5 lib/brief/document/section.rb
brief-1.15.4 lib/brief/document/section.rb
brief-1.15.3 lib/brief/document/section.rb
brief-1.15.2 lib/brief/document/section.rb
brief-1.15.1 lib/brief/document/section.rb
brief-1.15.0 lib/brief/document/section.rb
brief-1.14.3 lib/brief/document/section.rb
brief-1.14.2 lib/brief/document/section.rb
brief-1.14.1 lib/brief/document/section.rb
brief-1.14.0 lib/brief/document/section.rb
brief-1.13.2 lib/brief/document/section.rb
brief-1.13.1 lib/brief/document/section.rb
brief-1.12.9 lib/brief/document/section.rb
brief-1.12.8 lib/brief/document/section.rb
brief-1.12.7 lib/brief/document/section.rb
brief-1.12.6 lib/brief/document/section.rb
brief-1.12.5 lib/brief/document/section.rb