Class: DocTemplate::Tables::Agenda

Inherits:
Base
  • Object
show all
Defined in:
lib/doc_template/tables/agenda.rb

Constant Summary collapse

HEADER_LABEL =
'[agenda]'
ICONS_KEY =
'icon'
MATERIALS_KEY =
'materials'
METADATA_HEADER_LABEL =
'metadata'
GENERAL_TAG =
'general'

Constants inherited from Base

Base::SPLIT_REGEX

Instance Attribute Summary

Attributes inherited from Base

#errors

Instance Method Summary collapse

Methods inherited from Base

#collect_and_render_tags, #fetch_materials, #initialize, parse, #parse_in_context, #table_exist?

Constructor Details

This class inherits a constructor from DocTemplate::Tables::Base

Instance Method Details

#dataObject



55
56
57
# File 'lib/doc_template/tables/agenda.rb', line 55

def data
  @data || []
end

#parse(fragment, *_args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/doc_template/tables/agenda.rb', line 12

def parse(fragment, *_args)
  xpath = "table[.//*[case_insensitive_contains(text(), '#{HEADER_LABEL}')]]"
  table = fragment.at_xpath(xpath, XpathFunctions.new)
  return self unless table

  # retain new lines
  table.search('br').each { |br| br.replace("\n") }
  @data = []

  # skip the header
  table.xpath('./*/tr[position() > 1]').each_with_index do |tr, index|
    # take the only two fields
    , metacognition = tr.xpath('./td')

    # identify the referencing tag
    next unless .content.present?

    tag_name, tag_value = DocTemplate::FULL_TAG.match(.content).try(:captures)

    element = {
      id: tag_value.parameterize,
      title: tag_value.gsub(/^\p{Space}*/, ''),
      metadata: (),
      metacognition: parse_metacognition(metacognition),
      children: []
    }

    # the group tags are parents and the following
    # sections after each group are children of that group
    if tag_name.downcase.include?('group')
      @data << element
    elsif index.zero?
      @data << { id: GENERAL_TAG, title: GENERAL_TAG.humanize, metadata: {}, metacognition: {}, children: [] }
      @data.last[:children] << element
    else
      @data.last[:children] << element
    end
  end

  table.remove
  self
end