Sha256: a9f9b82970ca47f9e35d81d79ce9cd1efe36ed5ecdd8bce01493564bb8e7be8f

Contents?: true

Size: 933 Bytes

Versions: 1

Compression:

Stored size: 933 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 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)

        if !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

1 entries across 1 versions & 1 rubygems

Version Path
brief-1.2.0 lib/brief/document/section.rb