Class: Metanorma::Ietf::Cleanup

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

Constant Summary collapse

BCP_KEYWORDS =
["MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
"SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY",
"NOT RECOMMENDED", "OPTIONAL"].freeze

Instance Method Summary collapse

Instance Method Details

#abstract_cleanup(xmldoc) ⇒ Object



32
33
34
35
36
37
# File 'lib/metanorma/ietf/cleanup.rb', line 32

def abstract_cleanup(xmldoc)
  xmldoc.xpath("//abstract[not(text())]").each do |x|
    x.remove
    warn "Empty abstract section removed"
  end
end

#bcp14_cleanup(xmldoc) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/metanorma/ietf/cleanup.rb', line 56

def bcp14_cleanup(xmldoc)
  @bcp_bold or return
  xmldoc.xpath("//strong").each do |s|
    BCP_KEYWORDS.include?(s.text) or next
    s["class"] = "bcp14"
    s.name = "span"
  end
end

#boilerplate_isodoc(xmldoc) ⇒ Object



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

def boilerplate_isodoc(xmldoc)
  x = xmldoc.dup
  x.root.add_namespace(nil, @conv.xml_namespace)
  @isodoc ||= @conv.isodoc(@lang, @script, @locale)
  # initialise @isodoc.xrefs, for @isodoc.xrefs.info
  @isodoc
end

#cleanup(xmldoc) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/metanorma/ietf/cleanup.rb', line 8

def cleanup(xmldoc)
  bcp14_cleanup(xmldoc)
  abstract_cleanup(xmldoc)
  super
  cref_cleanup(xmldoc)
  dt_cleanup(xmldoc)
  xmldoc
end

#copied_instance_variablesObject



4
5
6
# File 'lib/metanorma/ietf/cleanup.rb', line 4

def copied_instance_variables
  super + %i[bcp_bold]
end

#cref_cleanup(xmldoc) ⇒ Object



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

def cref_cleanup(xmldoc)
  xmldoc.xpath("//crefref").each do |r|
    if c = xmldoc.at("//annotation[@anchor = '#{r.text}']")
      id = "_#{UUIDTools::UUID.random_create}"
      c["from"] = id
      c["to"] = id
      r.replace("<bookmark id='#{id}'/>")
    else
      @log.add("IETF_1", r, params: [r.text])
    end
  end
end

#dt_cleanup(xmldoc) ⇒ Object



25
26
27
28
29
30
# File 'lib/metanorma/ietf/cleanup.rb', line 25

def dt_cleanup(xmldoc)
  xmldoc.xpath("//dt").each do |dt|
    /:$/.match?(dt.text.strip) and next
    dt << ":"
  end
end

#norm_ref_preface(sect, isodoc) ⇒ Object



109
# File 'lib/metanorma/ietf/cleanup.rb', line 109

def norm_ref_preface(sect, isodoc); end

#note_cleanup(xmldoc) ⇒ Object



107
# File 'lib/metanorma/ietf/cleanup.rb', line 107

def note_cleanup(xmldoc); end

#quotesource_cleanup(xmldoc) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/metanorma/ietf/cleanup.rb', line 94

def quotesource_cleanup(xmldoc)
  xmldoc.xpath("//quote/source | //terms/source").each do |x|
    if x["target"]&.match?(URI::DEFAULT_PARSER.make_regexp)
      x["uri"] = x["target"]
      x.delete("target")
    else
      xref_to_eref(x, "source")
    end
  end
end

#section_names_refs_cleanup(xml) ⇒ Object



105
# File 'lib/metanorma/ietf/cleanup.rb', line 105

def section_names_refs_cleanup(xml); end

#smartquotes_cleanup(xmldoc) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/metanorma/ietf/cleanup.rb', line 65

def smartquotes_cleanup(xmldoc)
  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/, '"')
                .gsub(/[\u2010-\u2015]/, "-")
                .gsub(/\u2026/, "...")
                .gsub(/[\u200b-\u200c]/, "")
                .gsub(/[\u2000-\u200a]|\u202f|\u205f/, " "),
                :basic,
              ))
  end
  xmldoc
end

#xref_cleanup(xmldoc) ⇒ Object



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

def xref_cleanup(xmldoc)
  super
  xmldoc.xpath("//xref").each do |x|
    x.delete("displayFormat")
    x.delete("relative")
  end
end

#xref_to_eref(xref, name) ⇒ Object



81
82
83
84
# File 'lib/metanorma/ietf/cleanup.rb', line 81

def xref_to_eref(xref, name)
  super
  xref.delete("format")
end