Class: Metanorma::Itu::Cleanup

Inherits:
Standoc::Cleanup
  • Object
show all
Defined in:
lib/metanorma/itu/cleanup.rb,
lib/metanorma/itu/cleanup_section.rb

Constant Summary collapse

PUBLISHER =
"./contributor[role/@type = 'publisher']/organization".freeze

Instance Method Summary collapse

Instance Method Details

#biblio_reorder(xmldoc) ⇒ Object



88
89
90
91
92
# File 'lib/metanorma/itu/cleanup.rb', line 88

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

#boilerplate_file(_xmldoc) ⇒ Object



10
11
12
# File 'lib/metanorma/itu/cleanup.rb', line 10

def boilerplate_file(_xmldoc)
  File.join(@libdir, "boilerplate.adoc")
end

#cleanup(xmldoc) ⇒ Object



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

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

#copied_instance_variablesObject



6
7
8
# File 'lib/metanorma/itu/cleanup.rb', line 6

def copied_instance_variables
  super + %i[no_insert_missing_sections]
end

#header_rows_cleanup(xmldoc) ⇒ Object



22
23
24
25
26
27
# File 'lib/metanorma/itu/cleanup.rb', line 22

def header_rows_cleanup(xmldoc)
  super
  xmldoc.xpath("//table/thead/tr/th").each do |x|
    x["align"] = "center"
  end
end

#insert_conventions(xml) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/metanorma/itu/cleanup_section.rb', line 75

def insert_conventions(xml)
  ins =  xml.at("//sections//definitions") ||
    xml.at("//sections/clause[descendant::definitions]")
  unless xml.at("//sections/clause[@type = 'conventions']")
    ins.next = <<~XML
      <clause #{add_id_text} type='conventions'>
      <title #{add_id_text}>#{@i18n.conventions}</title>
      <p>#{@i18n.clause_empty}</p></clause>
    XML
  end
end

#insert_empty_clauses(xml) ⇒ Object



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

def insert_empty_clauses(xml)
  xml.xpath("//terms[not(./term)][not(.//terms)]").each do |c|
    insert_empty_clauses1(c, @i18n.clause_empty)
  end
  xml.xpath("//definitions[not(./dl)]").each do |c|
    insert_empty_clauses1(c, @i18n.clause_empty)
  end
end

#insert_empty_clauses1(clause, text) ⇒ Object



96
97
98
99
100
# File 'lib/metanorma/itu/cleanup_section.rb', line 96

def insert_empty_clauses1(clause, text)
  clause.at("./p") and return
  ins = clause.at("./title") or return
  ins.next = "<p>#{text}</p>"
end

#insert_missing_sections(xml) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/metanorma/itu/cleanup_section.rb', line 19

def insert_missing_sections(xml)
  xml.at("//metanorma-extension/semantic-metadata/" \
        "headless[text() = 'true']") and return nil
  insert_scope(xml)
  insert_norm_ref(xml)
  insert_terms(xml)
  insert_symbols(xml)
  insert_conventions(xml)
end

#insert_norm_ref(xml) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/metanorma/itu/cleanup_section.rb', line 43

def insert_norm_ref(xml)
  xml.at("//bibliography") or
    xml.at("./*/annex[last()] | ./*/sections").next =
      "<bibliography><sentinel/></bibliography>"
  ins = xml.at("//bibliography").elements.first
  xml.at("//bibliography/references[@normative = 'true']") or
    ins.previous = <<~XML
      <references #{add_id_text} normative='true'>
      <title #{add_id_text}>#{@i18n.normref}</title></references>
    XML
  xml.at("//sentinel")&.remove
end

#insert_scope(xml) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/metanorma/itu/cleanup_section.rb', line 29

def insert_scope(xml)
  xml.at("./*/sections") or
    xml.at("./*/preface | ./*/boilerplate | ./*/bibdata").next =
      "<sections><sentinel/></sections>"
  xml.at("./*/sections/*") or xml.at("./*/sections") << "<sentinel/>"
  ins = xml.at("//sections").elements.first
  xml.at("//sections/clause[@type = 'scope']") or
    ins.previous = <<~XML
      <clause type='scope' #{add_id_text}><title #{add_id_text}>#{@i18n.scope}</title>
      <p>#{@i18n.clause_empty}</p></clause>
    XML
  xml.at("//sentinel")&.remove
end

#insert_symbols(xml) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/metanorma/itu/cleanup_section.rb', line 64

def insert_symbols(xml)
  ins =  xml.at("//sections/terms") ||
    xml.at("//sections/clause[descendant::terms]")
  unless xml.at("//sections//definitions")
    ins.next = <<~XML
      <definitions #{add_id_text}>
      <title #{add_id_text}>#{@i18n.symbolsabbrev}</title></definitions>
    XML
  end
end

#insert_terms(xml) ⇒ Object



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

def insert_terms(xml)
  ins = xml.at("//sections/clause[@type = 'scope']")
  xml.at("//sections//terms") or
    ins.next = <<~XML
      <terms #{add_id_text}><title #{add_id_text}>#{@i18n.termsdef}</title></terms>
    XML
end

#pub_class(bib) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/metanorma/itu/cleanup.rb', line 50

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(#{@conv.skip_docid} or " \
                     "@type = 'metanorma')]")

  5
end

#published?(status, _xmldoc) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/metanorma/itu/cleanup.rb', line 94

def published?(status, _xmldoc)
  !%w(in-force-prepublished draft).include?(status.downcase)
end

#resolution_inline_header(xml) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/metanorma/itu/cleanup_section.rb', line 11

def resolution_inline_header(xml)
  @doctype == "resolution" or return
  xml.xpath("//clause//clause").each do |c|
    (title = c.at("./title")) && !title.text&.empty? and next
    c["inline-header"] = true
  end
end

#section_names_terms_cleanup(xml) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/metanorma/itu/cleanup_section.rb', line 125

def section_names_terms_cleanup(xml)
  super
  replace_title(
    xml, "//terms[@type = 'internal'] | " \
         "//clause[./terms[@type = 'internal']]" \
         "[not(./terms[@type = 'external'])]",
    @i18n&.internal_termsdef
  )
  replace_title(
    xml, "//terms[@type = 'external'] | " \
         "//clause[./terms[@type = 'external']]" \
         "[not(./terms[@type = 'internal'])]",
    @i18n&.external_termsdef
  )
end

#sections_cleanup(xml) ⇒ Object



4
5
6
7
8
9
# File 'lib/metanorma/itu/cleanup_section.rb', line 4

def sections_cleanup(xml)
  super
  insert_missing_sections(xml) unless @no_insert_missing_sections
  insert_empty_clauses(xml)
  resolution_inline_header(xml)
end

#sections_names_pref_cleanup(xml) ⇒ Object



147
148
149
150
151
152
# File 'lib/metanorma/itu/cleanup_section.rb', line 147

def sections_names_pref_cleanup(xml)
  t = xml.at("//preface//abstract/title")
  t&.text&.downcase == "summary" and summary = true
  super
  summary and replace_title(xml, "//preface//abstract", @i18n&.summary)
end

#smartquotes_cleanup(xmldoc) ⇒ Object



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

def smartquotes_cleanup(xmldoc)
  @smartquotes and return super
  xmldoc.traverse do |n|
    n.text? or next
    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



66
67
68
69
70
# File 'lib/metanorma/itu/cleanup.rb', line 66

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



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

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

#symbols_cleanup(xmldoc) ⇒ Object



141
142
143
144
145
# File 'lib/metanorma/itu/cleanup_section.rb', line 141

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

#table_cleanup(xmldoc) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/metanorma/itu/cleanup.rb', line 14

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(::Metanorma::Utils.strict_capitalize_first(text.text))
  end
end

#term_defs_boilerplate(div, _source, _term, _preface, _isodoc) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/metanorma/itu/cleanup_section.rb', line 110

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

#termdef_boilerplate_cleanup(xmldoc) ⇒ Object



102
# File 'lib/metanorma/itu/cleanup_section.rb', line 102

def termdef_boilerplate_cleanup(xmldoc); end

#terms_extract(div) ⇒ Object



104
105
106
107
108
# File 'lib/metanorma/itu/cleanup_section.rb', line 104

def terms_extract(div)
  internal = div.at("./terms[@type = 'internal']/title")
  external = div.at("./terms[@type = 'external']/title")
  [internal, external]
end