Module: Metanorma::Standoc::Index

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

Constant Summary collapse

EMPTY_INDEX_XPATH =
"//index[not(.//primary[normalize-space(text())]) " \
"or .//secondary[. and not(normalize-space(text()))] " \
"or .//tertiary[. and not(normalize-space(text()))]]".freeze

Instance Method Summary collapse

Instance Method Details

#block_index_cleanup(xmldoc) ⇒ Object



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

def block_index_cleanup(xmldoc)
  xmldoc.xpath("//quote | //td | //th | //formula | //li | //dt | " \
               "//dd | //example | //note | //figure | //sourcecode | " \
               "//admonition | //termnote | //termexample | //form  | " \
               "//requirement | //recommendation | //permission | " \
               "//imagemap | //svgmap").each do |b|
    b.xpath("./p[indexterm]").each do |p|
      indexterm_para?(p) or next
      p.replace(p.children)
    end
  end
end

#empty_index_context(node) ⇒ Object



21
22
23
24
# File 'lib/metanorma/cleanup/index.rb', line 21

def empty_index_context(node)
  ctx = node.ancestors.find { |a| a["id"] || a["anchor"] }
  ctx ? (ctx["id"] || ctx["anchor"]) : "(unknown location)"
end

#include_indexterm?(elem) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/metanorma/cleanup/index.rb', line 45

def include_indexterm?(elem)
  elem.nil? and return false
  !%w(image literal sourcecode).include?(elem.name)
end

#index_cleanup(xmldoc) ⇒ Object



9
10
11
12
13
# File 'lib/metanorma/cleanup/index.rb', line 9

def index_cleanup(xmldoc)
  para_index_cleanup(xmldoc)
  block_index_cleanup(xmldoc)
  index_empty_check(xmldoc)
end

#index_cleanup1(term, fieldofappl) ⇒ Object



77
78
79
80
81
82
# File 'lib/metanorma/cleanup/index.rb', line 77

def index_cleanup1(term, fieldofappl)
  term or return
  idx = term.children.dup
  fieldofappl.empty? or idx << ", &#x3c;#{fieldofappl}&#x3e;"
  term << "<index><primary>#{idx.to_xml}</primary></index>"
end

#index_empty_check(xmldoc) ⇒ Object



15
16
17
18
19
# File 'lib/metanorma/cleanup/index.rb', line 15

def index_empty_check(xmldoc)
  xmldoc.xpath(EMPTY_INDEX_XPATH).each do |i|
    @log.add("STANDOC_64", i, params: [empty_index_context(i)])
  end
end

#indexterm_para?(para) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
# File 'lib/metanorma/cleanup/index.rb', line 39

def indexterm_para?(para)
  p = para.dup
  p.xpath("./index").each(&:remove)
  p.text.strip.empty?
end

#para_index_cleanup(xmldoc) ⇒ Object



50
51
52
53
54
55
# File 'lib/metanorma/cleanup/index.rb', line 50

def para_index_cleanup(xmldoc)
  xmldoc.xpath("//p[index]").select { |p| indexterm_para?(p) }
    .each do |p|
      para_index_cleanup1(p, p.previous_element, p.next_element)
    end
end

#para_index_cleanup1(para, prev, foll) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/metanorma/cleanup/index.rb', line 57

def para_index_cleanup1(para, prev, foll)
  if include_indexterm?(prev)
    prev << para.remove.children
  elsif include_indexterm?(foll) # && !foll.children.empty?
    foll.add_first_child para.remove.children
  end
end

#term_index_cleanup(xmldoc) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/metanorma/cleanup/index.rb', line 65

def term_index_cleanup(xmldoc)
  @index_terms or return
  xmldoc.xpath("//preferred").each do |p|
    index_cleanup1(p.at("./expression/name | ./letter-symbol/name"),
                   p.xpath("./field-of-application | ./usage-info")
      &.map(&:text)&.join(", "))
  end
  xmldoc.xpath("//definitions/dl/dt").each do |p|
    index_cleanup1(p, "")
  end
end