Module: Metanorma::Standoc::Table

Included in:
Cleanup, Converter
Defined in:
lib/metanorma/cleanup/table.rb,
lib/metanorma/converter/table.rb

Instance Method Summary collapse

Instance Method Details

#dl1_table_cleanup(xmldoc) ⇒ Object



4
5
6
7
8
9
# File 'lib/metanorma/cleanup/table.rb', line 4

def dl1_table_cleanup(xmldoc)
  q = "//table/following-sibling::*[1][self::key]"
  xmldoc.xpath(q).each do |s|
    s.previous_element << s.remove
  end
end

#dl2_table_cleanup(xmldoc) ⇒ Object

move Key dl after table footer



12
13
14
# File 'lib/metanorma/cleanup/table.rb', line 12

def dl2_table_cleanup(xmldoc)
  text_key_extract(xmldoc, "table", "key")
end

#header_rows_cleanup(xmldoc) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/metanorma/cleanup/table.rb', line 26

def header_rows_cleanup(xmldoc)
  xmldoc.xpath("//table[@headerrows]").each do |s|
    thead = insert_thead(s)
    (thead.xpath("./tr").size...s["headerrows"].to_i).each do
      s.at("./tbody/tr").parent = thead
    end
    thead.xpath(".//td").each { |n| n.name = "th" }
    s.delete("headerrows")
  end
end

#insert_thead(table) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/metanorma/cleanup/table.rb', line 16

def insert_thead(table)
  thead = table.at("./thead")
  thead.nil? or return thead
  if tname = table.at("./name")
    thead = tname.add_next_sibling("<thead/>").first
    return thead
  end
  table.children.first.add_previous_sibling("<thead/>").first
end

#notes_table_cleanup(xmldoc) ⇒ Object

move notes into table



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/metanorma/cleanup/table.rb', line 60

def notes_table_cleanup(xmldoc)
  nomatches = false
  until nomatches
    nomatches = true
    xmldoc.xpath("//table/following-sibling::*[1]" \
                 "[self::note[not(@keep-separate = 'true')]]").each do |n|
      n.delete("keep-separate")
      n.previous_element << n.remove
      nomatches = false
    end
  end
end

#sources_table_cleanup(xmldoc) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/metanorma/cleanup/table.rb', line 47

def sources_table_cleanup(xmldoc)
  nomatches = false
  until nomatches
    nomatches = true
    xmldoc.xpath("//table/following-sibling::*[1]" \
                 "[self::source]").each do |n|
      n.previous_element << n.remove
      nomatches = false
    end
  end
end

#table(node) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/metanorma/converter/table.rb', line 15

def table(node)
  @table_fn_number = "a"
  noko do |xml|
    xml.table **attr_code(table_attrs(node)) do |xml_table|
      colgroup(node, xml_table)
      table_name(node, xml_table)
      %i(head body foot).reject { |tblsec| node.rows[tblsec].empty? }
      table_head_body_and_foot node, xml_table
    end
  end
end

#table_attrs(node) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/metanorma/converter/table.rb', line 4

def table_attrs(node)
  keep_attrs(node)
    .merge(id_unnum_attrs(node))
    .merge(headerrows: node.attr("headerrows"),
           alt: node.attr("alt"),
           plain: node.option?("plain") ? "true" : nil,
           style: node.attr("css-style"),
           summary: node.attr("summary"),
           width: node.attr("width"))
end

#table_cleanup(xmldoc) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/metanorma/cleanup/table.rb', line 37

def table_cleanup(xmldoc)
  dl1_table_cleanup(xmldoc)
  dl2_table_cleanup(xmldoc)
  sources_table_cleanup(xmldoc)
  notes_table_cleanup(xmldoc)
  header_rows_cleanup(xmldoc)
  tr_style_cleanup(xmldoc)
  td_style_cleanup(xmldoc)
end

#td_style_cleanup(xmldoc) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/metanorma/cleanup/table.rb', line 83

def td_style_cleanup(xmldoc)
  xmldoc.xpath("//td[.//td-style] | //th[.//td-style]").each do |tr|
    ret = tr.xpath(".//td-style").each_with_object([]) do |s, m|
      m << s.text
    end
    tr["style"] = ret.join(";")
  end
  xmldoc.xpath(".//td-style").each(&:remove)
end

#tr_style_cleanup(xmldoc) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/metanorma/cleanup/table.rb', line 73

def tr_style_cleanup(xmldoc)
  xmldoc.xpath("//tr[.//tr-style]").each do |tr|
    ret = tr.xpath(".//tr-style").each_with_object([]) do |s, m|
      m << s.text
    end
    tr["style"] = ret.join(";")
  end
  xmldoc.xpath(".//tr-style").each(&:remove)
end