Class: Asciidoctor::ITU::Converter

Inherits:
Standoc::Converter
  • Object
show all
Defined in:
lib/asciidoctor/itu/converter.rb,
lib/asciidoctor/itu/front.rb,
lib/asciidoctor/itu/cleanup.rb,
lib/asciidoctor/itu/validate.rb

Overview

A Converter implementation that generates RSD output, and a document schema encapsulation of the document for validation

Constant Summary collapse

PUBLISHER =
"./contributor[role/@type = 'publisher']/organization".freeze
XML_ROOT_TAG =
"itu-standard".freeze
XML_NAMESPACE =
"https://www.metanorma.org/ns/itu".freeze

Instance Method Summary collapse

Instance Method Details

#approval_validate(xmldoc) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/asciidoctor/itu/validate.rb', line 88

def approval_validate(xmldoc)
  s = xmldoc.at("//bibdata/ext/recommendationstatus/approvalstage") || return
  process = s["process"]
  if process == "aap" and %w(determined in-force).include? s.text
    @log.add("Document Attributes", nil, "Recommendation Status #{s.text} inconsistent with AAP")
  end
  if process == "tap" and !%w(determined in-force).include? s.text
    @log.add("Document Attributes", nil, "Recommendation Status #{s.text} inconsistent with TAP")
  end
end

#bibdata_validate(doc) ⇒ Object



4
5
6
7
# File 'lib/asciidoctor/itu/validate.rb', line 4

def bibdata_validate(doc)
  doctype_validate(doc)
  stage_validate(doc)
end

#biblio_reorder(xmldoc) ⇒ Object



158
159
160
161
162
# File 'lib/asciidoctor/itu/cleanup.rb', line 158

def biblio_reorder(xmldoc)
  xmldoc.xpath("//references").each do |r|
    biblio_reorder1(r)
  end
end

#cleanup(xmldoc) ⇒ Object



79
80
81
82
83
84
# File 'lib/asciidoctor/itu/cleanup.rb', line 79

def cleanup(xmldoc)
  symbols_cleanup(xmldoc)
  super
  obligations_cleanup(xmldoc)
  xmldoc
end

#content_validate(doc) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/asciidoctor/itu/validate.rb', line 24

def content_validate(doc)
  super
  approval_validate(doc)
  itu_identifier_validate(doc)
  bibdata_validate(doc.root)
  termdef_style(doc.root)
  title_validate1(doc.root)
  requirement_validate(doc.root)
  numbers_validate(doc.root)
end

#doc_converter(node) ⇒ Object



158
159
160
# File 'lib/asciidoctor/itu/converter.rb', line 158

def doc_converter(node)
  IsoDoc::ITU::WordConvert.new(doc_extract_attributes(node))
end

#doc_extract_attributes(node) ⇒ Object



142
143
144
# File 'lib/asciidoctor/itu/converter.rb', line 142

def doc_extract_attributes(node)
  super.merge(hierarchical_assets: node.attr("hierarchical-object-numbering"))
end

#doctype(node) ⇒ Object



41
42
43
44
45
# File 'lib/asciidoctor/itu/converter.rb', line 41

def doctype(node)
  ret = node.attr("doctype") || "recommendation"
  ret = "recommendation" if ret == "article"
  ret
end

#doctype_validate(xmldoc) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/asciidoctor/itu/validate.rb', line 9

def doctype_validate(xmldoc)
  doctype = xmldoc&.at("//bibdata/ext/doctype")&.text
  %w(recommendation recommendation-supplement recommendation-amendment 
  recommendation-corrigendum recommendation-errata recommendation-annex 
  focus-group implementers-guide technical-paper technical-report 
  joint-itu-iso-iec).include? doctype or
  @log.add("Document Attributes", nil, "#{doctype} is not a recognised document type")
end

#extract_text(node) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/asciidoctor/itu/validate.rb', line 45

def extract_text(node)
  return "" if node.nil?
  node1 = Nokogiri::XML.fragment(node.to_s)
  node1.xpath("//link | //locality | //localityStack").each(&:remove)
  ret = ""
  node1.traverse { |x| ret += x.text if x.text? }
  ret
end

#html_converter(node) ⇒ Object



150
151
152
# File 'lib/asciidoctor/itu/converter.rb', line 150

def html_converter(node)
  IsoDoc::ITU::HtmlConvert.new(html_extract_attributes(node))
end

#html_extract_attributes(node) ⇒ Object



138
139
140
# File 'lib/asciidoctor/itu/converter.rb', line 138

def html_extract_attributes(node)
  super.merge(hierarchical_assets: node.attr("hierarchical-object-numbering"))
end

#i18n_init(lang, script) ⇒ Object



127
128
129
# File 'lib/asciidoctor/itu/converter.rb', line 127

def i18n_init(lang, script)
  super
end

#init(node) ⇒ Object



29
30
31
32
33
34
# File 'lib/asciidoctor/itu/converter.rb', line 29

def init(node)
  super
  @smartquotes = node.attr("smartquotes") == "true"
  @no_insert_missing_sections = doctype(node) != "recommendation" ||
    node.attr("legacy-do-not-insert-missing-sections")
end

#insert_conventions(x) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/asciidoctor/itu/cleanup.rb', line 69

def insert_conventions(x)
  ins =  x.at("//sections//definitions") ||
    x.at("//sections/clause[descendant::definitions]")
  unless x.at("//sections/clause/title[text() = 'Conventions']")
    ins.next = "<clause id='_#{UUIDTools::UUID.random_create}'>"\
      "<title>Conventions</title><p>"\
      "#{@labels['clause_empty']}</p></clause>"
  end
end

#insert_missing_sections(x) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/asciidoctor/itu/cleanup.rb', line 17

def insert_missing_sections(x)
  insert_scope(x)
  insert_norm_ref(x)
  insert_terms(x)
  insert_symbols(x)
  insert_conventions(x)
end

#insert_norm_ref(x) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/asciidoctor/itu/cleanup.rb', line 38

def insert_norm_ref(x)
  x.at("//bibliography") or
    x.at("./*/annex[last()] | ./*/sections").next =
    "<bibliography><sentinel/></bibliography>"
  ins = x.at("//bibliography").elements.first
  unless x.at("//bibliography/references[@normative = 'true']")
    #ins.previous = "<references normative='true'><title>References</title><p>"     #  "#{@labels['clause_empty']}</p></references>"
    ins.previous = "<references normative='true'><title>References</title>"\
      "</references>"
  end
  x&.at("//sentinel")&.remove
end

#insert_scope(x) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/asciidoctor/itu/cleanup.rb', line 25

def insert_scope(x)
  x.at("./*/sections") or
    x.at("./*/preface | ./*/boilerplate | ./*/bibdata").next =
    "<sections><sentinel/></sections>"
  x.at("./*/sections/*") or x.at("./*/sections") << "<sentinel/>"
  ins = x.at("//sections").elements.first
  unless x.at("//sections/clause/title[text() = 'Scope']")
    ins.previous = "<clause><title>Scope</title><p>"\
      "#{@labels['clause_empty']}</p></clause>"
  end
  x&.at("//sentinel")&.remove
end

#insert_symbols(x) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/asciidoctor/itu/cleanup.rb', line 60

def insert_symbols(x)
  ins =  x.at("//sections/terms") ||
    x.at("//sections/clause[descendant::terms]")
  unless x.at("//sections//definitions")
    ins.next = "<definitions><title>Abbreviations and acronyms</title><p>"\
      "#{@labels['clause_empty']}</p></definitions>"
  end
end

#insert_terms(x) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/asciidoctor/itu/cleanup.rb', line 52

def insert_terms(x)
  ins =  x.at("//sections/clause/title[text() = 'Scope']/..")
  unless x.at("//sections//terms")
    ins.next = "<terms><title>Definitions</title><p>"\
      "#{@labels['clause_empty']}</p></terms>"
  end
end

#itu_id(node, xml) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/asciidoctor/itu/front.rb', line 142

def itu_id(node, xml)
  bureau = node.attr("bureau") || "T"
  return unless node.attr("docnumber")
  xml.docidentifier **{type: "ITU"} do |i|
    i << "ITU-#{bureau} "\
      "#{node.attr("docnumber")}"
  end
  xml.docnumber { |i| i << node.attr("docnumber") }
end

#itu_identifier_validate(xmldoc) ⇒ Object



99
100
101
102
103
104
# File 'lib/asciidoctor/itu/validate.rb', line 99

def itu_identifier_validate(xmldoc)
  s = xmldoc.xpath("//bibdata/docidentifier[@type = 'ITU']").each do |x|
    /^ITU-[RTF] [AD-VX-Z]\.[0-9]+$/.match(x.text) or
      @log.add("Style", nil, "#{x.text} does not match ITU document identifier conventions")
  end
end

#load_yaml(lang, script) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/asciidoctor/itu/converter.rb', line 116

def load_yaml(lang, script)
  y = if @i18nyaml then YAML.load_file(@i18nyaml)
      elsif lang == "en"
        YAML.load_file(File.join(File.dirname(__FILE__), "i18n-en.yaml"))
      else
        YAML.load_file(File.join(File.dirname(__FILE__), "i18n-en.yaml"))
      end
  @symbols_boilerplate = y["symbols_boilerplate"] || ""
  super.merge(y)
end

#makexml(node) ⇒ Object



36
37
38
39
# File 'lib/asciidoctor/itu/converter.rb', line 36

def makexml(node)
  @draft = node.attributes.has_key?("draft")
  super
end

#metadata_author(node, xml) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/asciidoctor/itu/front.rb', line 67

def (node, xml)
  xml.contributor do |c|
    c.role **{ type: "author" }
    c.organization do |a|
      a.name "International Telecommunication Union"
      a.abbreviation "ITU"
    end
  end
end

#metadata_committee(node, xml) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/asciidoctor/itu/front.rb', line 87

def (node, xml)
  (node, xml, "")
  suffix = 2
  while node.attr("bureau_#{suffix}")
    (node, xml, "_#{suffix}")
    suffix += 1
  end
end

#metadata_committee1(node, xml, suffix) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/asciidoctor/itu/front.rb', line 96

def (node, xml, suffix)
  xml.editorialgroup do |a|
    a.bureau ( node.attr("bureau#{suffix}") || "T" )
    if node.attr("group#{suffix}")
      a.group **attr_code(type: node.attr("grouptype#{suffix}")) do |g|
        (node, g, suffix, "")
      end
    end
    if node.attr("subgroup#{suffix}")
      a.subgroup **attr_code(type: node.attr("subgrouptype#{suffix}")) do |g|
        (node, g, suffix, "sub")
      end
    end
    if node.attr("workgroup#{suffix}")
      a.workgroup **attr_code(type: node.attr("workgrouptype#{suffix}")) do |g|
        (node, g, suffix, "work")
      end
    end
  end
end

#metadata_committee2(node, g, suffix, prefix) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/asciidoctor/itu/front.rb', line 117

def (node, g, suffix, prefix)
  g.name node.attr("#{prefix}group#{suffix}")
  node.attr("#{prefix}groupacronym#{suffix}") and
    g.acronym node.attr("#{prefix}groupacronym#{suffix}")
  if node.attr("#{prefix}groupyearstart#{suffix}")
    g.period do |p|
      p.start node.attr("#{prefix}groupyearstart#{suffix}")
      node.attr("#{prefix}groupacronym#{suffix}") and
        p.end node.attr("#{prefix}groupyearend#{suffix}")
    end
  end
end


152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/asciidoctor/itu/front.rb', line 152

def (node, xml)
  from = node.attr("copyright-year") || Date.today.year
  xml.copyright do |c|
    c.from from
    c.owner do |owner|
      owner.organization do |o|
        o.name "International Telecommunication Union"
        o.abbreviation "ITU"
      end
    end
  end
end

#metadata_ext(node, xml) ⇒ Object



206
207
208
209
210
211
212
213
# File 'lib/asciidoctor/itu/front.rb', line 206

def (node, xml)
  (node, xml)
  (node, xml)
  (node, xml)
  (node, xml)
  (node, xml)
  structured_id(node, xml)
end

#metadata_id(node, xml) ⇒ Object



130
131
132
133
# File 'lib/asciidoctor/itu/front.rb', line 130

def (node, xml)
  provisional_id(node, xml)
  itu_id(node, xml)
end

#metadata_ip_notice(node, xml) ⇒ Object



193
194
195
# File 'lib/asciidoctor/itu/front.rb', line 193

def (node, xml)
  xml.ip_notice_received (node.attr("ip-notice-received") || "false")
end

#metadata_keywords(node, xml) ⇒ Object



131
132
133
134
135
136
# File 'lib/asciidoctor/itu/converter.rb', line 131

def (node, xml)
  return unless node.attr("keywords")
  node.attr("keywords").split(/,[ ]*/).sort.each_with_index do |kw, i|
    xml.keyword (i == 0 ? kw.capitalize : kw)
  end
end

#metadata_publisher(node, xml) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/asciidoctor/itu/front.rb', line 77

def (node, xml)
  xml.contributor do |c|
    c.role **{ type: "publisher" }
    c.organization do |a|
      a.name "International Telecommunication Union"
      a.abbreviation "ITU"
    end
  end
end

#metadata_recommendationstatus(node, xml) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/asciidoctor/itu/front.rb', line 180

def (node, xml)
  return unless node.attr("recommendation-from")
  xml.recommendationstatus do |s|
    s.from node.attr("recommendation-from")
    s.to node.attr("recommendation-to") if node.attr("recommendation-to")
    if node.attr("approval-process")
      s.approvalstage **{process: node.attr("approval-process")} do |a|
        a << node.attr("approval-status")
      end
    end
  end
end

#metadata_series(node, xml) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/asciidoctor/itu/front.rb', line 165

def (node, xml)
  node.attr("series") and
    xml.series **{ type: "main" } do |s|
    s.title node.attr("series")
  end
  node.attr("series1") and
    xml.series **{ type: "secondary" } do |s|
    s.title node.attr("series1")
  end
  node.attr("series2") and
    xml.series **{ type: "tertiary" } do |s|
    s.title node.attr("series2")
  end
end

#metadata_status(node, xml) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/asciidoctor/itu/front.rb', line 8

def (node, xml)
  xml.status do |s|
    s.stage (node.attributes.has_key?("draft") ? "draft" :
             (node.attr("status") || node.attr("docstage") ||
              "published" ))
  end
end

#normref_cleanup(xmldoc) ⇒ Object



164
165
166
167
168
169
# File 'lib/asciidoctor/itu/cleanup.rb', line 164

def normref_cleanup(xmldoc)
  super
  r = xmldoc.at(NORM_REF) || return
  title = r.at("./title") and
    title.content = "References"
end

#numbers_validate(xmldoc) ⇒ Object

Editing Guidelines 9.4.3



65
66
67
68
69
70
71
72
73
74
# File 'lib/asciidoctor/itu/validate.rb', line 65

def numbers_validate(xmldoc)
  xmldoc.xpath("//clause | //preface/* | //annex").each do |x|
    xx = x.dup
    xx.xpath("./clause").each { |c| c.remove }
    style_two_regex_not_prev(x, extract_text(xx),
                             /^(?<num>-?[0-9][0-9,. ]{3,})$/,
                             %r{(\bISO|\bIEC|\bIEEE/)$},
                             "number not broken up in threes by apostrophe")
  end
end

#olist(node) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/asciidoctor/itu/converter.rb', line 47

def olist(node)
  id = Asciidoctor::Standoc::Utils::anchor_or_uuid(node)
  noko do |xml|
    xml.ol **attr_code(id: id, class: node.attr("class")) do |xml_ol|
      node.items.each { |item| li(xml_ol, item) }
    end
  end.join("\n")
end

#outputs(node, ret) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/asciidoctor/itu/converter.rb', line 56

def outputs(node, ret)
  File.open(@filename + ".xml", "w:UTF-8") { |f| f.write(ret) }
  presentation_xml_converter(node).convert(@filename + ".xml")
  html_converter(node).convert(@filename + ".presentation.xml", nil, false, "#{@filename}.html")
  doc_converter(node).convert(@filename + ".presentation.xml", nil, false, "#{@filename}.doc")
  pdf_converter(node)&.convert(@filename + ".presentation.xml", nil, false, "#{@filename}.pdf")
end

#pdf_converter(node) ⇒ Object



154
155
156
# File 'lib/asciidoctor/itu/converter.rb', line 154

def pdf_converter(node)
  IsoDoc::ITU::PdfConvert.new(html_extract_attributes(node))
end

#presentation_xml_converter(node) ⇒ Object



146
147
148
# File 'lib/asciidoctor/itu/converter.rb', line 146

def presentation_xml_converter(node)
  IsoDoc::ITU::PresentationXMLConvert.new(html_extract_attributes(node))
end

#provisional_id(node, xml) ⇒ Object



135
136
137
138
139
140
# File 'lib/asciidoctor/itu/front.rb', line 135

def provisional_id(node, xml)
  return unless node.attr("provisional-name")
  xml.docidentifier **{type: "ITU-provisional"} do |i|
    i << node.attr("provisional-name")
  end
end

#pub_class(bib) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/asciidoctor/itu/cleanup.rb', line 119

def pub_class(bib)
  return 1 if bib.at("#{PUBLISHER}[abbreviation = 'ITU']")
  return 1 if bib.at("#{PUBLISHER}[name = 'International "\
                     "Telecommunication Union']")
  return 2 if bib.at("#{PUBLISHER}[abbreviation = 'ISO']")
  return 2 if bib.at("#{PUBLISHER}[name = 'International Organization "\
                     "for Standardization']")
  return 3 if bib.at("#{PUBLISHER}[abbreviation = 'IEC']")
  return 3 if bib.at("#{PUBLISHER}[name = 'International "\
                     "Electrotechnical Commission']")
  return 4 if bib.at("./docidentifier[@type][not(@type = 'DOI' or "\
                     "@type = 'metanorma' or @type = 'ISSN' or @type = "\
                     "'ISBN')]")
  5
end

#requirement_validate(xmldoc) ⇒ Object

Editing Guidelines 7



55
56
57
58
59
60
61
62
# File 'lib/asciidoctor/itu/validate.rb', line 55

def requirement_validate(xmldoc)
  xmldoc.xpath("//preface/*").each do |c|
    extract_text(c).split(/\.\s+/).each do |t|
      /\b(shall|must)\b/i.match(t) and
        @log.add("Style", c, "Requirement possibly in preface: #{t.strip}")
    end
  end
end

#section_check(xmldoc) ⇒ Object

Editing Guidelines 7.2, 7.3



112
113
114
115
116
117
# File 'lib/asciidoctor/itu/validate.rb', line 112

def section_check(xmldoc)
  xmldoc.at("//bibdata/abstract") or
    @log.add("Style", nil, "No Summary has been provided")
  xmldoc.at("//bibdata/keywords") or
    @log.add("Style", nil, "No Keywords have been provided")
end

#section_validate(doc) ⇒ Object



106
107
108
109
# File 'lib/asciidoctor/itu/validate.rb', line 106

def section_validate(doc)
  super
  section_check(doc.root)
end

#sections_cleanup(x) ⇒ Object



4
5
6
7
# File 'lib/asciidoctor/itu/cleanup.rb', line 4

def sections_cleanup(x)
  super
  insert_missing_sections(x) unless @no_insert_missing_sections
end

#sectiontype_streamline(ret) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/asciidoctor/itu/converter.rb', line 74

def sectiontype_streamline(ret)
  case ret
  when "definitions" then "terms and definitions"
  when "abbreviations and acronyms" then "symbols and abbreviated terms"
  when "references" then "normative references"
  else
    super
  end
end

#smartquotes_cleanup(xmldoc) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/asciidoctor/itu/cleanup.rb', line 86

def smartquotes_cleanup(xmldoc)
  return super if @smartquotes
  xmldoc.traverse do |n|
    next unless n.text?
    n.replace(HTMLEntities.new.encode(
      n.text.gsub(/\u2019|\u2018|\u201a|\u201b/, "'").
      gsub(/\u201c|\u201d|\u201e|\u201f/, '"'), :basic))
  end
  xmldoc
end

#sort_biblio(bib) ⇒ Object



135
136
137
138
139
# File 'lib/asciidoctor/itu/cleanup.rb', line 135

def sort_biblio(bib)
  bib.sort do |a, b|
    sort_biblio_key(a) <=> sort_biblio_key(b)
  end
end

#sort_biblio_key(bib) ⇒ Object

sort by: doc class (ITU, ISO, IEC, other standard (not DOI &c), other then standard class (docid class other than DOI &c) then alphanumeric doc id (not DOI &c) then title



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/asciidoctor/itu/cleanup.rb', line 145

def sort_biblio_key(bib)
  pubclass = pub_class(bib)
  num = bib&.at("./docnumber")&.text
  id = bib&.at("./docidentifier[not(@type = 'DOI' or @type = "\
               "'metanorma' or @type = 'ISSN' or @type = 'ISBN')]")
  metaid = bib&.at("./docidentifier[@type = 'metanorma']")&.text
  abbrid = metaid unless /^\[\d+\]$/.match(metaid)
  type = id['type'] if id
  title = bib&.at("./title[@type = 'main']")&.text ||
    bib&.at("./title")&.text || bib&.at("./formattedref")&.text
  "#{pubclass} :: #{type} :: #{id&.text || metaid} :: #{title}"
end

#stage_validate(xmldoc) ⇒ Object



18
19
20
21
22
# File 'lib/asciidoctor/itu/validate.rb', line 18

def stage_validate(xmldoc)
  stage = xmldoc&.at("//bibdata/status/stage")&.text
  %w(in-force superseded in-force-prepublished withdrawn draft).include? stage or
    @log.add("Document Attributes", nil, "#{stage} is not a recognised status")
end

#structured_id(node, xml) ⇒ Object



197
198
199
200
201
202
203
204
# File 'lib/asciidoctor/itu/front.rb', line 197

def structured_id(node, xml)
  return unless node.attr("docnumber")
  xml.structuredidentifier do |i|
    i.bureau node.attr("bureau") || "T"
    i.docnumber node.attr("docnumber")
    a = node.attr("annexid") and i.annexid a
  end
end

#style(n, t) ⇒ Object



70
71
72
# File 'lib/asciidoctor/itu/converter.rb', line 70

def style(n, t)
  return
end

#style_two_regex_not_prev(n, text, re, re_prev, warning) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/asciidoctor/itu/validate.rb', line 76

def style_two_regex_not_prev(n, text, re, re_prev, warning)
  return if text.nil?
  arr = text.split(/\W+/)
  arr.each_index do |i|
    m = re.match arr[i]
    m_prev = i.zero? ? nil : re_prev.match(arr[i - 1])
    if !m.nil? && m_prev.nil?
      @log.add("Style", n, "#{warning}: #{m[:num]}")
    end
  end
end

#subtitle_english(node, xml) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/asciidoctor/itu/front.rb', line 48

def subtitle_english(node, xml)
  at = { language: "en", format: "text/plain", type: "subtitle" }
  a = node.attr("subtitle") || node.attr("subtitle-en")
  xml.title **attr_code(at) do |t|
    t << Asciidoctor::Standoc::Utils::asciidoc_sub(a)
  end
end

#subtitle_otherlangs(node, xml) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/asciidoctor/itu/front.rb', line 56

def subtitle_otherlangs(node, xml)
  node.attributes.each do |k, v|
    next unless /^subtitle-(?<lang>.+)$/ =~ k
    next if lang == "en"
    xml.title **attr_code(language: lang, format: "text/plain",
                          type: "subtitle") do |t|
      t << v
    end
  end
end

#symbols_cleanup(xmldoc) ⇒ Object



111
112
113
114
115
# File 'lib/asciidoctor/itu/cleanup.rb', line 111

def symbols_cleanup(xmldoc)
  sym = xmldoc.at("//definitions/title")
  sym and sym&.next_element&.name == "dl" and
    sym.next = "<p>#{@symbols_boilerplate}</p>"
end

#table_cleanup(xmldoc) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/asciidoctor/itu/cleanup.rb', line 9

def table_cleanup(xmldoc)
  super
  xmldoc.xpath("//thead/tr[1]/th | //thead/tr[1]/td").each do |t|
    text = t.at("./descendant::text()") or next
    text.replace(text.text.titlecase)
  end
end

#term_def_title(toplevel, node) ⇒ Object



84
85
86
87
# File 'lib/asciidoctor/itu/converter.rb', line 84

def term_def_title(toplevel, node)
  return node.title unless toplevel
  "Definitions"
end

#term_defs_boilerplate(div, source, term, preface, isodoc) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/asciidoctor/itu/converter.rb', line 101

def term_defs_boilerplate(div, source, term, preface, isodoc)
  internal, external = terms_extract(div)
  internal&.next_element&.name == "term" and
    internal.next = "<p>#{@internal_terms_boilerplate}</p>"
  internal and internal&.next_element == nil and
    internal.next = "<p>#{@no_terms_boilerplate}</p>"
  external&.next_element&.name == "term" and
    external.next = "<p>#{@external_terms_boilerplate}</p>"
  external and external&.next_element == nil and
    external.next = "<p>#{@no_terms_boilerplate}</p>"
  !internal and !external and
    %w(term terms).include? div&.next_element&.name and
    div.next = "<p>#{@term_def_boilerplate}</p>"
end

#termdef_boilerplate_cleanup(xmldoc) ⇒ Object



108
109
# File 'lib/asciidoctor/itu/cleanup.rb', line 108

def termdef_boilerplate_cleanup(xmldoc)
end

#termdef_cleanup(xmldoc) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/asciidoctor/itu/cleanup.rb', line 97

def termdef_cleanup(xmldoc)
  xmldoc.xpath("//term/preferred").each do |p|
    if ["terms defined elsewhere",
        "terms defined in this recommendation"].include? p.text.downcase
      p.name = "title"
      p.parent.name = "terms"
    end
  end
  super
end

#termdef_style(xmldoc) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/asciidoctor/itu/validate.rb', line 119

def termdef_style(xmldoc)
  xmldoc.xpath("//term").each do |t|
    para = t.at("./definition") || return
    term = t.at("./preferred").text
    termdef_warn(term, /^[A-Z][a-z]+/, t, term, "term is not lowercase")
    termdef_warn(para.text, /^[a-z]/, t, term, "term definition does not start with capital")
    termdef_warn(para.text, /[^.]$/, t, term, "term definition does not end with period")
  end
end

#termdef_warn(text, re, t, term, msg) ⇒ Object



129
130
131
# File 'lib/asciidoctor/itu/validate.rb', line 129

def termdef_warn(text, re, t, term, msg)
  re.match(text) && @log.add("Style", t, "#{term}: #{msg}")
end

#terms_extract(div) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/asciidoctor/itu/converter.rb', line 89

def terms_extract(div)
  internal = nil
  external = nil
  div.parent.xpath("./terms/title").each do |t|
    case t&.text&.downcase
    when "terms defined elsewhere" then external = t
    when "terms defined in this recommendation" then internal = t
    end
  end
  [internal, external]
end

#title(node, xml) ⇒ Object



42
43
44
45
46
# File 'lib/asciidoctor/itu/front.rb', line 42

def title(node, xml)
  super
  subtitle_english(node, xml)
  subtitle_otherlangs(node, xml)
end

#title_english(node, xml) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/asciidoctor/itu/front.rb', line 16

def title_english(node, xml)
  at = { language: "en", format: "text/plain", type: "main" }
  a = node.attr("title") || node.attr("title-en")
  xml.title **attr_code(at) do |t|
    t << (Asciidoctor::Standoc::Utils::asciidoc_sub(a) || node.title)
  end
  if a = node.attr("annextitle") || node.attr("annextitle-en")
    at[:type] = "annex"
    xml.title **attr_code(at) do |t|
      t << Asciidoctor::Standoc::Utils::asciidoc_sub(a)
    end
  end
end

#title_otherlangs(node, xml) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/asciidoctor/itu/front.rb', line 30

def title_otherlangs(node, xml)
  node.attributes.each do |k, v|
    next unless /^(annex)?title-(?<lang>.+)$/ =~ k
    next if lang == "en"
    type = /^annex/.match(k) ? "annex" : "main"
    xml.title **attr_code(language: lang, format: "text/plain",
                          type: type) do |t|
      t << v
    end
  end
end

#title_validate(root) ⇒ Object



25
26
27
# File 'lib/asciidoctor/itu/converter.rb', line 25

def title_validate(root)
  nil
end

#title_validate1(xmldoc) ⇒ Object

Editing Guidelines 6.3



36
37
38
39
40
41
42
43
# File 'lib/asciidoctor/itu/validate.rb', line 36

def title_validate1(xmldoc)
  t = xmldoc.at("//bibdata/title")&.text
  xmldoc.xpath("//bibdata/series/title").each do |s|
    series = s.text.sub(/^[A-Z]: /, "")
    t.downcase.include?(series.downcase) and
      @log.add("Document Attributes", nil, "Title includes series name #{series}")
  end
end

#validate(doc) ⇒ Object



64
65
66
67
68
# File 'lib/asciidoctor/itu/converter.rb', line 64

def validate(doc)
  content_validate(doc)
  schema_validate(formattedstr_strip(doc.dup),
                  File.join(File.dirname(__FILE__), "itu.rng"))
end