Sha256: 2f4a3130ef488789da151d844d0f3cf373b1c06ea7b657fb36a3bc99d31c5a08

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 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)
        path = ".//table/*/tr[1]/td//*[case_insensitive_equals(text(),'#{HEADER_LABEL}')]"
        section_tables = fragment.xpath(path, 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

3 entries across 3 versions & 1 rubygems

Version Path
lcms-engine-0.2.0 lib/doc_template/tables/section.rb
lcms-engine-0.1.2 lib/doc_template/tables/section.rb
lcms-engine-0.1.0 lib/doc_template/tables/section.rb