Sha256: adea60f00cc25f9f4c099c9f53c902eba2c6877a8e7a203aed566839f8475921

Contents?: true

Size: 1.4 KB

Versions: 13

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module DocTemplate
  module Tables
    class Section < Base
      FAKE_SECTION_TITLE = 'lesson'
      HEADER_LABEL = 'section-metadata'
      HTML_VALUE_FIELDS = ['section-summary'].freeze
      MATERIALS_KEY = 'section-materials'

      def parse(fragment, *args)
        section_tables = fragment.xpath(xpath_meta_headers, XpathFunctions.new)

        # # Allows to handle ELA as Math:: inject fake section
        return fake_section(fragment) if section_tables.empty? && args.extract_options![:force_inject]

        [].tap do |result|
          section_tables.each do |el|
            table = el.ancestors('table').first
            data = fetch table

            value = data['section-title'].parameterize
            table.replace section_placeholder(value)

            data = fetch_materials data, MATERIALS_KEY

            result << data
          end
        end
      end

      private

      def fake_section(fragment)
        if fragment.children.empty?
          fragment.add_child Nokogiri::HTML.fragment(section_placeholder(FAKE_SECTION_TITLE))
        else
          fragment.children.first.before section_placeholder(FAKE_SECTION_TITLE)
        end
        [{ 'section-title' => FAKE_SECTION_TITLE }]
      end

      def section_placeholder(value)
        "<p><span>[#{::DocTemplate::Tags::ActivityMetadataSectionTag::TAG_NAME}: #{value}]</span></p>"
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
lcms-engine-0.5.5 lib/doc_template/tables/section.rb
lcms-engine-0.5.4 lib/doc_template/tables/section.rb
lcms-engine-0.5.3 lib/doc_template/tables/section.rb
lcms-engine-0.5.2 lib/doc_template/tables/section.rb
lcms-engine-0.5.1 lib/doc_template/tables/section.rb
lcms-engine-0.5.0 lib/doc_template/tables/section.rb
lcms-engine-0.4.2 lib/doc_template/tables/section.rb
lcms-engine-0.4.1 lib/doc_template/tables/section.rb
lcms-engine-0.4.0 lib/doc_template/tables/section.rb
lcms-engine-0.3.1 lib/doc_template/tables/section.rb
lcms-engine-0.1.4 lib/doc_template/tables/section.rb
lcms-engine-0.3.0 lib/doc_template/tables/section.rb
lcms-engine-0.1.3 lib/doc_template/tables/section.rb