Module: Metanorma::Standoc::Toc

Included in:
Cleanup
Defined in:
lib/metanorma/cleanup/toc.rb

Instance Method Summary collapse

Instance Method Details

#toc_cleanup(xmldoc) ⇒ Object



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

def toc_cleanup(xmldoc)
  toc_cleanup_para(xmldoc)
  xmldoc.xpath("//toc").each { |t| toc_cleanup1(t, xmldoc) }
  toc_cleanup_clause(xmldoc)
  (xmldoc)
end

#toc_cleanup1(toc, xmldoc) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/metanorma/cleanup/toc.rb', line 43

def toc_cleanup1(toc, xmldoc)
  depth = 1
  ret = ""
  toc_index(toc, xmldoc).each do |x|
    ret = toc_cleanup1_entry(x, depth, ret)
    depth = x[:depth]
  end
  toc.children = "<ul>#{ret}</ul>"
end

#toc_cleanup1_entry(entry, depth, ret) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/metanorma/cleanup/toc.rb', line 53

def toc_cleanup1_entry(entry, depth, ret)
  if depth > entry[:depth]
    ret += "</ul></li>" * (depth - entry[:depth])
  elsif depth < entry[:depth]
    ret += "<li><ul>" * (entry[:depth] - depth)
  end
  ret + "<li><xref target='#{entry[:target]}'><display-text>#{entry[:text]}</display-text></xref></li>"
end

#toc_cleanup_clause(xmldoc) ⇒ Object



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

def toc_cleanup_clause(xmldoc)
  xmldoc
    .xpath("//clause[@type = 'toc'] | //annex[@type = 'toc']").each do |c|
    c.xpath(".//ul[not(ancestor::ul)]").each do |ul|
      toc_cleanup_clause_entry(xmldoc, ul)
      ul.replace("<toc>#{ul.to_xml}</toc>")
    end
  end
end

#toc_cleanup_clause_entry(xmldoc, list) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/metanorma/cleanup/toc.rb', line 72

def toc_cleanup_clause_entry(xmldoc, list)
  list.xpath(".//xref[not(text())][not(display-text)]").each do |x|
    c1 = xmldoc.at("//*[@anchor = '#{x['target']}']")
    t = c1.at("./variant-title[@type = 'toc']") || c1.at("./title")
    x << "<display-text>#{to_xml(t.dup.children)}</display-text>"
  end
end

#toc_cleanup_para(xmldoc) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/metanorma/cleanup/toc.rb', line 11

def toc_cleanup_para(xmldoc)
  xmldoc.xpath("//p[toc]").each do |x|
    x.xpath("./toc").reverse_each do |t|
      x.next = t
    end
    x.remove if x.text.strip.empty?
  end
end

#toc_index(toc, xmldoc) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/metanorma/cleanup/toc.rb', line 20

def toc_index(toc, xmldoc)
  depths = toc_index_depths(toc)
  depths.keys.each_with_object([]) do |key, arr|
    xmldoc.xpath(key).each do |x|
      arr << toc_index1(key, x, depths)
    end
  end.sort_by { |a| a[:line] }
end

#toc_index1(key, entry, depths) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/metanorma/cleanup/toc.rb', line 29

def toc_index1(key, entry, depths)
  t = entry.at("./following-sibling::variant-title[@type = 'toc']") and
    entry = t
  { text: entry.children.to_xml, depth: depths[key].to_i,
    target: entry.xpath("(./ancestor-or-self::*/@id)[last()]")[0].text,
    line: entry.line }
end

#toc_index_depths(toc) ⇒ Object



37
38
39
40
41
# File 'lib/metanorma/cleanup/toc.rb', line 37

def toc_index_depths(toc)
  toc.xpath("./toc-xpath").each_with_object({}) do |x, m|
    m[x.text] = x["depth"]
  end
end

#toc_metadata(xmldoc) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/metanorma/cleanup/toc.rb', line 80

def (xmldoc)
  @htmltoclevels || @doctoclevels || @pdftoclevels || @toclevels or return
  # Check if TOC metadata already exists to avoid duplication
  xmldoc.at("//presentation-metadata/toc-heading-levels") and return
  ins = add_misc_container(xmldoc)
  toc_metadata1(ins)
end

#toc_metadata1(ins) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/metanorma/cleanup/toc.rb', line 88

def toc_metadata1(ins)
  content = [[@toclevels, "toc-heading-levels"],
             [@htmltoclevels, "html-toc-heading-levels"],
             [@doctoclevels, "doc-toc-heading-levels"],
             [@pdftoclevels, "pdf-toc-heading-levels"]].map do |n|
    n[0] ? "<#{n[1]}>#{n[0]}</#{n[1]}>" : nil
  end.compact.join
  content.empty? or ins << "<presentation-metadata>#{content}</presentation-metadata>"
end