Module: Metanorma::Standoc::Footnotes

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

Constant Summary collapse

FIGURE_FN_XPATH =
"//figure/following-sibling::*[1][self::p and *[1][self::fn]]".freeze

Instance Method Summary collapse

Instance Method Details

#duplicate_footnote(fnote, idx, seen) ⇒ Object



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

def duplicate_footnote(fnote, idx, seen)
  content = footnote_content(fnote)
  if seen[content]
    outnum = seen[content]
    fnote.xpath(".//index | .//bookmark").each(&:remove)
  else
    idx += 1
    outnum = idx
    seen[content] = outnum
  end
  [fnote, idx, seen, outnum]
end

#figure_footnote_cleanup(xmldoc) ⇒ Object

include footnotes inside figure if they are the only content of the paras following



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/metanorma/cleanup/footnotes.rb', line 19

def figure_footnote_cleanup(xmldoc)
  nomatches = false
  until nomatches
    nomatches = true
    xmldoc.xpath(FIGURE_FN_XPATH).each do |s|
      next if s.children.map do |c|
                c.text? && /[[:alpha:]]/.match(c.text)
              end.any?

      s.previous_element << s.first_element_child.remove
      s.remove
      nomatches = false
    end
  end
end

#footnote_block_cleanup(xmldoc) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/metanorma/cleanup/footnotes.rb', line 94

def footnote_block_cleanup(xmldoc)
  ids = xmldoc.xpath("//footnoteblock").each_with_object([]) do |f, m|
    f.name = "fn"
    add_id(f)
    m << f.text
    if id = xmldoc.at("//*[@anchor = '#{f.text}']")
      f.children = id.dup.children
    else footnote_block_error(f)
    end
  end
  footnote_block_remove(xmldoc, ids)
end

#footnote_block_error(fnote) ⇒ Object



114
115
116
117
# File 'lib/metanorma/cleanup/footnotes.rb', line 114

def footnote_block_error(fnote)
  @log.add("STANDOC_15", fnote, params: [fnote.text])
  fnote.children = "[ERROR]"
end

#footnote_block_remove(xmldoc, ids) ⇒ Object



107
108
109
110
111
112
# File 'lib/metanorma/cleanup/footnotes.rb', line 107

def footnote_block_remove(xmldoc, ids)
  ids.each do |id|
    n = xmldoc.at("//*[@anchor = '#{id}']") and
      n.remove
  end
end

#footnote_cleanup(xmldoc) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/metanorma/cleanup/footnotes.rb', line 130

def footnote_cleanup(xmldoc)
  footnote_block_cleanup(xmldoc)
  title_footnote_move(xmldoc)
  process_hidden_footnotes(xmldoc)
  table_footnote_renumber(xmldoc)
  other_footnote_renumber(xmldoc)
  xmldoc.xpath("//fn").each do |fn|
    fn.delete("table")
  end
end

#footnote_content(fnote) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/metanorma/cleanup/footnotes.rb', line 6

def footnote_content(fnote)
  c = if fnote.children.respond_to?(:to_xml)
        fnote.children.to_xml
      else fn.children
      end
  c.gsub(/ id="[^"]+"/, "")
end

#other_footnote_renumber(xmldoc) ⇒ Object



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

def other_footnote_renumber(xmldoc)
  seen = {}
  i = 0
  xmldoc.xpath("//fn").each do |fn|
    i, seen = other_footnote_renumber1(fn, i, seen)
  end
end

#other_footnote_renumber1(fnote, idx, seen) ⇒ Object



69
70
71
72
73
74
# File 'lib/metanorma/cleanup/footnotes.rb', line 69

def other_footnote_renumber1(fnote, idx, seen)
  fnote["table"] and return [idx, seen]
  fnote, idx, seen, outnum = duplicate_footnote(fnote, idx, seen)
  fnote["reference"] = outnum.to_s
  [idx, seen]
end

#process_hidden_footnotes(xmldoc) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/metanorma/cleanup/footnotes.rb', line 119

def process_hidden_footnotes(xmldoc)
  xmldoc.xpath("//fn").each do |fn|
    first_text = fn.xpath(".//text()")
      .find { |node| !node.text.strip.empty? } or return
    if first_text.text.strip.start_with?("hiddenref%")
      first_text.content = first_text.text.sub(/^hiddenref%/, "")
      fn["hiddenref"] = true
    end
  end
end

#table_footnote_number(outnum) ⇒ Object



55
56
57
# File 'lib/metanorma/cleanup/footnotes.rb', line 55

def table_footnote_number(outnum)
  (outnum - 1 + "a".ord).chr
end

#table_footnote_renumber(xmldoc) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/metanorma/cleanup/footnotes.rb', line 59

def table_footnote_renumber(xmldoc)
  xmldoc.xpath("//table | //figure").each do |t|
    seen = {}
    i = 0
    t.xpath(".//fn[not(ancestor::name)]").each do |fn|
      i, seen = table_footnote_renumber1(fn, i, seen)
    end
  end
end

#table_footnote_renumber1(fnote, idx, seen) ⇒ Object



48
49
50
51
52
53
# File 'lib/metanorma/cleanup/footnotes.rb', line 48

def table_footnote_renumber1(fnote, idx, seen)
  fnote, idx, seen, outnum = duplicate_footnote(fnote, idx, seen)
  fnote["reference"] = table_footnote_number(outnum)
  fnote["table"] = true
  [idx, seen]
end

#title_footnote_move(xmldoc) ⇒ Object



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

def title_footnote_move(xmldoc)
  ins = xmldoc.at("//bibdata/language")
  xmldoc.xpath("//bibdata/title//fn").each do |f|
    f.name = "note"
    f["type"] = "title-footnote"
    f.delete("reference")
    ins.previous = f.remove
  end
end