Module: IsoDoc::Function::Cleanup

Included in:
Common
Defined in:
lib/isodoc/function/cleanup.rb

Constant Summary collapse

TABLENOTE_CSS =
"div[@class = 'Note' or @class = 'BlockSource' " \
"or @class = 'TableFootnote' or @class = 'figdl']".freeze

Instance Method Summary collapse

Instance Method Details

#admonition_cleanup(docxml) ⇒ Object

todo PRESENTATION XML



29
30
31
32
33
34
35
36
37
# File 'lib/isodoc/function/cleanup.rb', line 29

def admonition_cleanup(docxml)
  docxml.xpath("//div[@class = 'Admonition'][title]").each do |d|
    title = d.at("./title")
    n = title.next_element
    n&.children&.first
      &.add_previous_sibling("#{title.remove.text}—")
  end
  docxml
end

#cleanup(docxml) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/isodoc/function/cleanup.rb', line 16

def cleanup(docxml)
  @i18n ||= i18n_init(@lang, @script, @locale)
  comment_cleanup(docxml)
  footnote_cleanup(docxml)
  inline_header_cleanup(docxml)
  figure_cleanup(docxml)
  table_cleanup(docxml)
  symbols_cleanup(docxml)
  example_cleanup(docxml)
  admonition_cleanup(docxml)
end

#example_cleanup(docxml) ⇒ Object



39
40
41
42
43
44
# File 'lib/isodoc/function/cleanup.rb', line 39

def example_cleanup(docxml)
  docxml.xpath("//table[@class = 'example']//p[not(@class)]").each do |p|
    p["class"] = "example"
  end
  docxml
end

#figure_cleanup(docxml) ⇒ Object



46
# File 'lib/isodoc/function/cleanup.rb', line 46

def figure_cleanup(docxml); end

#footnote_cleanup(docxml) ⇒ Object

todo PRESENTATION XML



62
63
64
65
66
67
68
# File 'lib/isodoc/function/cleanup.rb', line 62

def footnote_cleanup(docxml)
  docxml.xpath('//a[@class = "FootnoteRef"]/sup')
    .each_with_index do |x, i|
    x.content = (i + 1).to_s
  end
  docxml
end

#footnote_reference_format(link) ⇒ Object



150
151
152
# File 'lib/isodoc/function/cleanup.rb', line 150

def footnote_reference_format(link)
  link
end

#inline_header_cleanup(docxml) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/isodoc/function/cleanup.rb', line 48

def inline_header_cleanup(docxml)
  docxml.xpath('//span[@class="zzMoveToFollowing"]').each do |x|
    x.delete("class")
    n = x.next_element
    if n.nil?
      x.name = "p"
    else
      n.add_first_child(x.remove)
    end
  end
  docxml
end

#merge_fnref_into_fn_text(elem) ⇒ Object



70
71
72
73
74
# File 'lib/isodoc/function/cleanup.rb', line 70

def merge_fnref_into_fn_text(elem)
  fn = elem.at('.//span[@class="TableFootnoteRef"]/..')
  n = fn.next_element
  n&.children&.first&.add_previous_sibling(fn.remove)
end

#new_fullcolspan_row(table, tfoot) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/isodoc/function/cleanup.rb', line 112

def new_fullcolspan_row(table, tfoot)
  # how many columns in the table?
  cols = 0
  table.at(".//tr").xpath("./td | ./th").each do |td|
    cols += (td["colspan"] ? td["colspan"].to_i : 1)
  end
  style =
    %{border-top:0pt;border-bottom:#{IsoDoc::Function::Table::SW} 1.5pt;}
  tfoot.add_child("<tr><td colspan='#{cols}' style='#{style}'/></tr>")
  tfoot.xpath(".//td").last
end

#passthrough_cleanup(docxml) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/isodoc/function/cleanup.rb', line 8

def passthrough_cleanup(docxml)
  docxml.split(%r{(<passthrough>|</passthrough>)}).each_slice(4)
    .map do |a|
    a.size > 2 and a[2] = HTMLEntities.new.decode(a[2])
    [a[0], a[2]]
  end.join
end

#remove_bottom_border(cell) ⇒ Object



96
97
98
99
# File 'lib/isodoc/function/cleanup.rb', line 96

def remove_bottom_border(cell)
  cell["style"] =
    cell["style"].gsub(/border-bottom:[^;]+;/, "border-bottom:0pt;")
end

#symbols_cleanup(docxml) ⇒ Object



144
# File 'lib/isodoc/function/cleanup.rb', line 144

def symbols_cleanup(docxml); end

#table_cleanup(docxml) ⇒ Object



138
139
140
141
142
# File 'lib/isodoc/function/cleanup.rb', line 138

def table_cleanup(docxml)
  table_footnote_cleanup(docxml)
  table_note_cleanup(docxml)
  docxml
end

#table_footnote_cleanup(docxml) ⇒ Object

preempt html2doc putting MsoNormal under TableFootnote class



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/isodoc/function/cleanup.rb', line 77

def table_footnote_cleanup(docxml)
  docxml.xpath("//table[descendant::aside]").each do |t|
    t.xpath(".//aside").each do |a|
      merge_fnref_into_fn_text(a)
      a.name = "div"
      a["class"] = "TableFootnote"
      t << a.remove
    end
  end
  table_footnote_cleanup_propagate(docxml)
end

#table_footnote_cleanup_propagate(docxml) ⇒ Object



89
90
91
92
93
94
# File 'lib/isodoc/function/cleanup.rb', line 89

def table_footnote_cleanup_propagate(docxml)
  docxml.xpath("//p[not(self::*[@class])]" \
               "[ancestor::*[@class = 'TableFootnote']]").each do |p|
    p["class"] = "TableFootnote"
  end
end

#table_footnote_reference_format(link) ⇒ Object



146
147
148
# File 'lib/isodoc/function/cleanup.rb', line 146

def table_footnote_reference_format(link)
  link
end

#table_get_or_make_tfoot(table) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/isodoc/function/cleanup.rb', line 101

def table_get_or_make_tfoot(table)
  tfoot = table.at(".//tfoot")
  if tfoot.nil?
    table.add_child("<tfoot></tfoot>")
    tfoot = table.at(".//tfoot")
  else
    tfoot.xpath(".//td | .//th").each { |td| remove_bottom_border(td) }
  end
  tfoot
end

#table_note_cleanup(docxml) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/isodoc/function/cleanup.rb', line 127

def table_note_cleanup(docxml)
  docxml.xpath("//table[dl or #{TABLENOTE_CSS}]").each do |t|
    tfoot = table_get_or_make_tfoot(t)
    insert_here = new_fullcolspan_row(t, tfoot)
    t.xpath("dl | p[@class = 'ListTitle'] | #{TABLENOTE_CSS}")
      .each do |d|
      d.parent = insert_here
    end
  end
end

#textcleanup(docxml) ⇒ Object



4
5
6
# File 'lib/isodoc/function/cleanup.rb', line 4

def textcleanup(docxml)
  passthrough_cleanup(docxml)
end