Sha256: 3a84ec37cefd691626e5b75c0c90830c215cd210483bb7d87519807b9cf0874a

Contents?: true

Size: 1.09 KB

Versions: 14

Compression:

Stored size: 1.09 KB

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 > #{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
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
brief-1.17.14 lib/brief/document/section.rb
brief-1.17.13 lib/brief/document/section.rb
brief-1.17.12 lib/brief/document/section.rb
brief-1.17.11 lib/brief/document/section.rb
brief-1.17.10 lib/brief/document/section.rb
brief-1.17.9 lib/brief/document/section.rb
brief-1.17.8 lib/brief/document/section.rb
brief-1.17.7 lib/brief/document/section.rb
brief-1.17.5 lib/brief/document/section.rb
brief-1.17.4 lib/brief/document/section.rb
brief-1.17.3 lib/brief/document/section.rb
brief-1.17.2 lib/brief/document/section.rb
brief-1.17.1 lib/brief/document/section.rb
brief-1.17.0 lib/brief/document/section.rb