Class: IsoDoc::JIS::PresentationXMLConvert

Inherits:
Iso::PresentationXMLConvert
  • Object
show all
Includes:
Init
Defined in:
lib/isodoc/jis/presentation_xml_convert.rb

Constant Summary collapse

JPAN =
"\\p{Hiragana}\\p{Katakana}\\p{Han}".freeze
JPAN_BOLD =
"<span style='font-family:\"MS Gothic\"'>".freeze

Instance Method Summary collapse

Methods included from Init

#i18n_init, #metadata_init, #omit_docid_prefix, #std_docid_semantic, #xref_init

Instance Method Details

#admits(elem) ⇒ Object



42
43
44
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 42

def admits(elem)
  elem.children.first.previous = @i18n.l10n("#{@i18n.admitted}: ")
end

#annex(docxml) ⇒ Object



146
147
148
149
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 146

def annex(docxml)
  super
  move_commentaries_to_end(docxml)
end

#annex1(elem) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 137

def annex1(elem)
  elem["commentary"] == "true" and return
  lbl = @xrefs.anchor(elem["id"], :label)
  if t = elem.at(ns("./title"))
    t.children = "<strong>#{to_xml(t.children)}</strong>"
  end
  prefix_name(elem, "<br/>", lbl, "title")
end

#block(docxml) ⇒ Object



46
47
48
49
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 46

def block(docxml)
  super
  dl docxml
end

#display_order(docxml) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 160

def display_order(docxml)
  i = 0
  i = display_order_xpath(docxml, "//preface/*", i)
  i = display_order_at(docxml, "//sections/introduction", i)
  i = display_order_at(docxml, "//clause[@type = 'scope']", i)
  i = display_order_at(docxml, @xrefs.klass.norm_ref_xpath, i)
  i = display_order_at(docxml, "//sections/terms | " \
                               "//sections/clause[descendant::terms]", i)
  i = display_order_at(docxml, "//sections/definitions", i)
  i = display_order_xpath(docxml, @xrefs.klass.middle_clause(docxml), i)
  i = display_order_xpath(docxml, "//annex[not(@commentary = 'true')]", i)
  i = display_order_xpath(docxml, @xrefs.klass.bibliography_xpath, i)
  i = display_order_xpath(docxml, "//annex[@commentary = 'true']", i)
  i = display_order_xpath(docxml, "//indexsect", i)
  display_order_xpath(docxml, "//colophon/*", i)
end

#dl(docxml) ⇒ Object



51
52
53
54
55
56
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 51

def dl(docxml)
  docxml.xpath(ns("//table//dl | //figure//dl")).each do |l|
    l.at(ns("./dl")) || l.at("./ancestor::xmlns:dl") and next
    dl_to_para(l)
  end
end

#dl_id_insert(node, ret) ⇒ Object



71
72
73
74
75
76
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 71

def dl_id_insert(node, ret)
  a = node.replace(ret)
  p = a.at("./descendant-or-self::xmlns:p")
  node["id"] and p << "<bookmark id='#{node['id']}'/>"
  a.xpath("./descendant-or-self::*[@id = '']").each { |x| x.delete("id") }
end

#dl_to_para(node) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 62

def dl_to_para(node)
  ret = dl_to_para_name(node)
  ret += dl_to_para_terms(node)
  node.elements.reject { |n| %w(dt dd name).include?(n.name) }.each do |x|
    ret += x.to_xml
  end
  dl_id_insert(node, ret)
end

#dl_to_para_name(node) ⇒ Object



78
79
80
81
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 78

def dl_to_para_name(node)
  e = node.at(ns("./name"))
  "<p class='ListTitle'>#{e&.children&.to_xml || @i18n.key}</p>"
end

#dl_to_para_terms(node) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 83

def dl_to_para_terms(node)
  ret = ""
  node.elements.select { |n| dt_dd?(n) }.each_slice(2) do |dt, dd|
    term = strip_para(dt)
    defn = strip_para(dd)
    bkmk = dd["id"] ? "<bookmark id='#{dd['id']}'/>" : ""
    ret += "<p class='dl' id='#{dt['id']}'>#{term}: #{bkmk}#{defn}</p>"
  end
  ret
end

#dt_dd?(node) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 58

def dt_dd?(node)
  %w{dt dd}.include? node.name
end

#full_row(cols, elem) ⇒ Object



133
134
135
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 133

def full_row(cols, elem)
  "<tr><td border='0' colspan='#{cols}'>#{elem}</td></tr>"
end

#inline(docxml) ⇒ Object



7
8
9
10
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 7

def inline(docxml)
  super
  strong(docxml)
end

#move_commentaries_to_end(docxml) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 151

def move_commentaries_to_end(docxml)
  docxml.at(ns("//annex[@commentary = 'true']")) or return
  b = docxml.at(ns("//bibliography")) ||
    docxml.at(ns("//annex[last()]")).after(" ").next
  docxml.xpath(ns("//annex[@commentary = 'true']")).reverse.each do |x|
    b.next = x.remove
  end
end

#move_introduction(doc) ⇒ Object



197
198
199
200
201
202
203
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 197

def move_introduction(doc)
  source = doc.at(ns("//preface/introduction")) or return
  dest = doc.at(ns("//sections")) ||
    doc.at(ns("//preface")).after("<sections> </sections>").next_element
  dest.children.empty? and dest.children = " "
  dest.children.first.next = source
end

#ol_depth(node) ⇒ Object



36
37
38
39
40
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 36

def ol_depth(node)
  depth = node.ancestors("ol").size + 1
  depth == 1 and return :alphabetic
  :arabic
end

#preface_rearrange(doc) ⇒ Object



192
193
194
195
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 192

def preface_rearrange(doc)
  move_introduction(doc)
  super
end

#strip_para(node) ⇒ Object



94
95
96
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 94

def strip_para(node)
  node.children.to_xml.gsub(%r{</?p( [^>]*)?>}, "")
end

#strong(docxml) ⇒ Object



15
16
17
18
19
20
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 15

def strong(docxml)
  docxml.xpath(ns("//strong")).each do |x|
    (x.children.size == 1 && x.children.first.text?) or next # too hard
    x.replace(strong1(x.text))
  end
end

#strong1(text) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 22

def strong1(text)
  jpan = /^[#{JPAN}]/o.match?(text[0])
  ret = jpan ? JPAN_BOLD : "<strong>"
  text.chars.each do |n|
    new = /^[#{JPAN}]/o.match?(n)
    jpan && !new and ret += "</span><strong>"
    !jpan && new and ret += "</strong>#{JPAN_BOLD}"
    ret += n
    jpan = new
  end
  ret += /[#{JPAN}]/o.match?(text[-1]) ? "</span>" : "</strong>"
  ret
end

#table1(node) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 98

def table1(node)
  super
  cols = table_cols_count(node)
  name = node.at(ns("./name"))
  thead = table_thead_pt(node, name)
  table_unit_note(node, thead, cols)
  table_name(name, thead, cols)
end

#table_cols_count(node) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 113

def table_cols_count(node)
  cols = 0
  node.at(ns(".//tr")).xpath(ns("./td | ./th")).each do |x|
    cols += x["colspan"]&.to_i || 1
  end
  cols
end

#table_name(name, thead, cols) ⇒ Object



126
127
128
129
130
131
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 126

def table_name(name, thead, cols)
  name or return
  thead.children.first.previous =
    full_row(cols, "<p class='TableTitle' style='text-align:center;'> " \
                   "#{name.remove.children.to_xml}</p>")
end

#table_thead_pt(node, name) ⇒ Object



107
108
109
110
111
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 107

def table_thead_pt(node, name)
  node.at(ns("./thead")) ||
    name&.after("<thead> </thead>")&.next ||
    node.elements.first.before("<thead> </thead>").previous
end

#table_unit_note(node, thead, cols) ⇒ Object



121
122
123
124
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 121

def table_unit_note(node, thead, cols)
  unit_note = node.at(ns(".//note[@type = 'units']")) or return
  thead.children.first.previous = full_row(cols, unit_note.remove.to_xml)
end

#tablesource(elem) ⇒ Object



177
178
179
180
181
182
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 177

def tablesource(elem)
  while elem&.next_element&.name == "source"
    elem << "; #{to_xml(elem.next_element.remove.children)}"
  end
  elem.children = l10n("#{@i18n.source}: #{to_xml(elem.children).strip}")
end

#toc_title_insert_pt(docxml) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 184

def toc_title_insert_pt(docxml)
  ins = docxml.at(ns("//preface")) ||
    docxml.at(ns("//sections | //annex | //bibliography"))
      &.before("<preface> </preface>")
      &.previous_element or return nil
  ins.children.last.after(" ").next
end