Class: IsoDoc::Jis::PresentationXMLConvert

Inherits:
Iso::PresentationXMLConvert
  • Object
show all
Includes:
Init
Defined in:
lib/isodoc/jis/presentation_section.rb,
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

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

Instance Method Details

#admits(elem) ⇒ Object



44
45
46
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 44

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

#annex(docxml) ⇒ Object



16
17
18
19
# File 'lib/isodoc/jis/presentation_section.rb', line 16

def annex(docxml)
  super
  move_commentaries_to_end(docxml)
end

#annex1(elem) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/isodoc/jis/presentation_section.rb', line 7

def annex1(elem)
  elem["commentary"] == "true" and return commentary(elem)
  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

#bibdata_i18n(bibdata) ⇒ Object



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

def bibdata_i18n(bibdata)
  super
  @lang == "ja" and date_translate(bibdata)
end

#block(docxml) ⇒ Object



48
49
50
51
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 48

def block(docxml)
  super
  dl docxml
end

#commentary(elem) ⇒ Object



30
31
32
33
34
# File 'lib/isodoc/jis/presentation_section.rb', line 30

def commentary(elem)
  t = elem.elements.first
  commentary_title_hdr(t)
  middle_title_main(t, "CommentaryStandardName")
end

#commentary_title_hdr(elem) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/isodoc/jis/presentation_section.rb', line 36

def commentary_title_hdr(elem)
  ret = <<~COMMENTARY
    <p class="CommentaryStandardNumber">JIS #{@meta.get[:docnumber_undated]}
  COMMENTARY
  yr = @meta.get[:docyear] and
    ret += ": <span class='CommentaryEffectiveYear'>#{yr}</span>"
  elem.previous = ret
end

#date_translate(bibdata) ⇒ Object



143
144
145
146
147
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 143

def date_translate(bibdata)
  bibdata.xpath(ns("./date")).each do |d|
    d.children = @i18n.japanese_date(d.text.strip)
  end
end

#dl(docxml) ⇒ Object



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

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



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

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



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

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



80
81
82
83
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 80

def dl_to_para_name(node)
  e = node.at(ns("./name")) or return ""
  "<p class='ListTitle'>#{e.children.to_xml}</p>"
end

#dl_to_para_terms(node) ⇒ Object



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

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)


60
61
62
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 60

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

#full_row(cols, elem) ⇒ Object



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

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

#inline(docxml) ⇒ Object



9
10
11
12
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 9

def inline(docxml)
  super
  strong(docxml)
end

#make_preface(docxml) ⇒ Object



45
46
47
48
49
50
# File 'lib/isodoc/jis/presentation_section.rb', line 45

def make_preface(docxml)
  docxml.at(ns("//preface")) ||
    docxml.at(ns("//sections | //annex | //bibliography"))
      &.before("<preface> </preface>")
      &.previous_element
end

#middle_subtitle_main(out, style) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/isodoc/jis/presentation_section.rb', line 145

def middle_subtitle_main(out, style)
  t = @meta.get[:docsubtitlemain]
  (t && !t.empty?) or return
  ret = middle_title_para(style, :docsubtitleintro, :docsubtitlemain,
                          :docsubtitlepart)
  if a = @meta.get[:docsubtitlepart]
    ret += "<p class='zzSTDTitle2'>"
    b = @meta.get[:docsubtitlepartlabel] and ret += "#{b}: "
    ret += "<br/><strong>#{a}</strong></p>"
  end
  out.previous = ret
end

#middle_title(docxml) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/isodoc/jis/presentation_section.rb', line 110

def middle_title(docxml)
  s = docxml.at(ns("//sections")) or return
  elem = s.children.first
  middle_title_hdr(elem)
  middle_title_main(elem, "zzSTDTitle1")
  middle_subtitle_main(elem, "zzSTDTitle2")
  # middle_title_amd(s.children.first)
end

#middle_title_hdr(out) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/isodoc/jis/presentation_section.rb', line 119

def middle_title_hdr(out)
  ret = "<p class='JapaneseIndustrialStandard'>#{@i18n.jis}"
  @meta.get[:unpublished] and ret += @i18n.l10n("(#{@i18n.draft_label})")
  ret += ("<tab/>" * 7)
  ret += "<span class='JIS'>JIS</span></p>"
  ret += "<p class='StandardNumber'><tab/>#{@meta.get[:docnumber_undated]}"
  if yr = @meta.get[:docyear]
    ret += ": <span class='EffectiveYear'>#{yr}</span>"
  end
  ret += "</p><p class='IDT'/>"
  out.previous = ret
end

#middle_title_main(out, style) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/isodoc/jis/presentation_section.rb', line 132

def middle_title_main(out, style)
  t = @meta.get[:doctitlemain]
  (t && !t.empty?) or return
  ret =
    middle_title_para(style, :doctitleintro, :doctitlemain, :doctitlepart)
  if a = @meta.get[:doctitlepart]
    ret += "<p class='zzSTDTitle1'>"
    b = @meta.get[:doctitlepartlabel] and ret += "#{b}: "
    ret += "<br/><strong>#{a}</strong></p>"
  end
  out.previous = ret
end

#middle_title_para(style, intro, main, part) ⇒ Object



158
159
160
161
162
163
164
165
# File 'lib/isodoc/jis/presentation_section.rb', line 158

def middle_title_para(style, intro, main, part)
  ret = "<p class='#{style}'>#{@meta.get[intro]}"
  ret += " &#x2014; " if @meta.get[intro] && @meta.get[main]
  ret += @meta.get[main]
  ret += " &#x2014; " if @meta.get[main] && @meta.get[part]
  ret += "</p>"
  ret
end

#move_commentaries_to_end(docxml) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/isodoc/jis/presentation_section.rb', line 21

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



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

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

#move_participants(doc) ⇒ Object



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

def move_participants(doc)
  p = doc.at(ns("//clause[@type = 'participants']")) or return
  t = participant_table(p) or return
  p.remove
  ins = make_preface(doc) or return nil
  ins.children.first.previous = t
end

#ol_depth(node) ⇒ Object



38
39
40
41
42
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 38

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

#participant_rows(yaml) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/isodoc/jis/presentation_section.rb', line 97

def participant_rows(yaml)
  yaml.map do |y|
    r = y["role"] ? @i18n.l10n("(#{y['role']})") : ""
    n = y["name"]
    if n.is_a?(Hash)
      n =
        if @lang == "ja" then "#{n['surname']} #{n['givenname']}"
        else "#{n['givenname']} #{n['surname']}" end
    end
    "<tr><td>#{r}</rd><td>#{n}</td><td>#{y['affiliation']}</td></tr>"
  end.join("\n")
end

#participant_table(clause) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/isodoc/jis/presentation_section.rb', line 79

def participant_table(clause)
  s = clause.at(ns("./sourcecode")) or return nil
  y = YAML.safe_load(s.children.to_xml(encoding: "UTF-8")) or return nil
  y.is_a?(Array) or return nil
  out1 = <<~OUTPUT
    <clause id='_#{UUIDTools::UUID.random_create}'><title>#{@meta.get[:"investigative-committee"]} #{@i18n.membership_table}</title>
    <table unnumbered='true'>
    <thead>
    <tr><th/><th>#{@i18n.full_name}</th><th>#{@i18n.affiliation}</th></tr>
    </thead>
    <tbody>
  OUTPUT
  out2 = <<~OUTPUT
    </tbody></table></clause>
  OUTPUT
  "#{out1}#{participant_rows(y)}#{out2}"
end

#preface_rearrange(doc) ⇒ Object



57
58
59
60
61
# File 'lib/isodoc/jis/presentation_section.rb', line 57

def preface_rearrange(doc)
  move_introduction(doc)
  super
  move_participants(doc)
end

#strip_para(node) ⇒ Object



96
97
98
# File 'lib/isodoc/jis/presentation_xml_convert.rb', line 96

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

#strong(docxml) ⇒ Object



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

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



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

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



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

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)
end

#table_cols_count(node) ⇒ Object



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

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_thead_pt(node, name) ⇒ Object



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

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



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

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



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

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



52
53
54
55
# File 'lib/isodoc/jis/presentation_section.rb', line 52

def toc_title_insert_pt(docxml)
  ins = make_preface(docxml) or return nil
  ins.children.last.after(" ").next
end