Class: Asciidoctor::Ietf::Converter

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

Constant Summary collapse

XML_ROOT_TAG =
"ietf-standard".freeze
XML_NAMESPACE =
"https://www.metanorma.org/ns/ietf".freeze
BCP_KEYWORDS =
["MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
"SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", "OPTIONAL"].freeze

Instance Method Summary collapse

Constructor Details

#initialize(backend, opts) ⇒ Converter

Returns a new instance of Converter.



16
17
18
19
# File 'lib/asciidoctor/ietf/converter.rb', line 16

def initialize(backend, opts)
  super
  @libdir = File.dirname(__FILE__)
end

Instance Method Details

#bcp14_cleanup(xmldoc) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/asciidoctor/ietf/converter.rb', line 134

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

#cache_workgroup(node) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/asciidoctor/ietf/validate.rb', line 71

def cache_workgroup(node)
  wg, wgcache_name = open_wg_cache(node)
  if wg.empty?
    File.open(wgcache_name, "w") do |b|
      wg = cache_workgroup_ietf(wg, b)
      wg = cache_workgroup_irtf(wg, b)
      b << wg.to_json
    end
  end
  wg
end

#cache_workgroup_ietf(wg, b) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/asciidoctor/ietf/validate.rb', line 46

def cache_workgroup_ietf(wg, b)
  STDERR.puts "Reading workgroups from https://tools.ietf.org/wg/..."
  Kernel.open("https://tools.ietf.org/wg/") do |f|
    f.each_line do |line|
      line.scan(%r{<td width="50%" style='padding: 0 1ex'>([^<]+)</td>}) do |w|
        wg << w[0].gsub(/\s+$/, "").gsub(/ Working Group$/, "")
      end
    end
  end
  wg
end

#cache_workgroup_irtf(wg, b) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/asciidoctor/ietf/validate.rb', line 58

def cache_workgroup_irtf(wg, b)
  STDERR.puts "Reading workgroups from https://irtf.org/groups..."
  Kernel.open("https://irtf.org/groups", ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE) do |f|
    f.each_line do |line|
      line.scan(%r{<a title="([^"]+) Research Group"[^>]+>([^<]+)<}) do |w|
        wg << w[0].gsub(/\s+$/, "")
        wg << w[1].gsub(/\s+$/, "") # abbrev
      end
    end
  end
  wg
end

#clause_parse(attrs, xml, node) ⇒ Object



180
181
182
183
184
185
# File 'lib/asciidoctor/ietf/converter.rb', line 180

def clause_parse(attrs, xml, node)
  attrs[:numbered] = node.attr("numbered")
  attrs[:removeInRFC] = node.attr("removeInRFC")
  attrs[:toc] = node.attr("toc")
  super
end

#cleanup(xmldoc) ⇒ Object



125
126
127
128
129
# File 'lib/asciidoctor/ietf/converter.rb', line 125

def cleanup(xmldoc)
  bcp14_cleanup(xmldoc)
  rfc_anchor_cleanup(xmldoc)
  super
end

#content_validate(doc) ⇒ Object



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

def content_validate(doc)
  super
  image_validate(doc)
  workgroup_validate(doc)
end

#dl_attrs(node) ⇒ Object



26
27
28
29
30
31
# File 'lib/asciidoctor/ietf/blocks.rb', line 26

def dl_attrs(node)
  attr_code(id: ::Asciidoctor::Standoc::Utils::anchor_or_uuid(node),
            newline: node.attr("newline"),
            indent: node.attr("indent"),
            spacing: node.attr("spacing"))
end

#doctype(node) ⇒ Object



44
45
46
47
48
# File 'lib/asciidoctor/ietf/converter.rb', line 44

def doctype(node)
  ret = node.attr("doctype")
  ret = "Internet-Draft" if ret == "article"
  ret
end

#document(node) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/asciidoctor/ietf/converter.rb', line 29

def document(node)
  init(node)
  ret1 = makexml(node)
  ret = ret1.to_xml(indent: 2)
  unless node.attr("nodoc") || !node.attr("docfile")
    filename = node.attr("docfile").gsub(/\.adoc/, ".xml").
      gsub(%r{^.*/}, "")
    File.open(filename, "w") { |f| f.write(ret) }
    rfc_converter(node).convert filename unless node.attr("nodoc")
  end
  @log.write(@localdir + @filename + ".err") unless @novalid
  @files_to_delete.each { |f| FileUtils.rm f }
  ret
end

#eref_text(node) ⇒ Object



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

def eref_text(node)
  matched = /^(of|comma|parens|bare),(.*+)$/.match node.text
  if matched.nil?
    f = nil
    c = node&.text&.sub(/^fn: /, "")
  else
    f = matched[1]
    c = matched[2]
  end
  [f, c]
end

#html_extract_attributes(node) ⇒ Object



202
203
204
# File 'lib/asciidoctor/ietf/converter.rb', line 202

def html_extract_attributes(node)
  super.merge(use_xinclude: node.attr("use-xinclude"))
end

#image_attributes(node) ⇒ Object



69
70
71
# File 'lib/asciidoctor/ietf/blocks.rb', line 69

def image_attributes(node)
  super.merge(attr_code(align: node.attr("align")))
end

#image_validate(doc) ⇒ Object



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

def image_validate(doc)
  doc.xpath("//image").each do |i|
    next if i["mimetype"] == "image/svg+xml"
    @log.add("MIME", i, "image #{i['src'][0, 40]} is not SVG!")
  end
end

#inline_anchor_xref(node) ⇒ Object



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

def inline_anchor_xref(node)
  f, c = xref_text(node)
  f1, c = eref_text(node) if f.nil?
  t, rel = xref_rel(node)
  noko do |xml|
    xml.xref **attr_code(target: t, type: "inline",
                         displayFormat: f1, format: f,
                         relative: rel ) do |x|
                           x << c
                         end
  end.join
end

#inline_quoted(node) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/asciidoctor/ietf/converter.rb', line 50

def inline_quoted(node)
  noko do |xml|
    case node.type
    when :emphasis then xml.em { |s| s << node.text }
    when :strong then xml.strong { |s| s << node.text }
    when :monospaced then xml.tt { |s| s << node.text }
    when :double then xml << "\"#{node.text}\""
    when :single then xml << "'#{node.text}'"
    when :superscript then xml.sup { |s| s << node.text }
    when :subscript then xml.sub { |s| s << node.text }
    when :asciimath then stem_parse(node.text, xml, :asciimath)
    when :latexmath then stem_parse(node.text, xml, :latexmath)
    else
      case node.role
      when "bcp14" then xml.bcp14 { |s| s << node.text.upcase }
      else
        xml << node.text
      end
    end
  end.join
end

#introduction_parse(attrs, xml, node) ⇒ Object



187
188
189
# File 'lib/asciidoctor/ietf/converter.rb', line 187

def introduction_parse(attrs, xml, node)
  clause_parse(attrs, xml, node)
end

#listing_attrs(node) ⇒ Object



73
74
75
# File 'lib/asciidoctor/ietf/blocks.rb', line 73

def listing_attrs(node)
  super.merge(attr_code(markers: node.attr("markers"), src: node.attr("src")))
end

#literal(node) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/asciidoctor/ietf/blocks.rb', line 57

def literal(node)
  noko do |xml|
    xml.figure **literal_attrs(node) do |f|
      figure_title(node, f)
      f.pre node.lines.join("\n"),
        **attr_code(align: node.attr("align"),
                    id: ::Asciidoctor::Standoc::Utils::anchor_or_uuid(nil),
                    alt: node.attr("alt"))
    end
  end
end

#makexml(node) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/asciidoctor/ietf/converter.rb', line 21

def makexml(node)
  @draft = node.attributes.has_key?("draft")
  @workgroups = cache_workgroup(node)
  @bcp_bold = !node.attr?("no-rfc-bold-bcp14")
  @xinclude = node.attr?("use-xinclude") 
  super
end

#metadata_author(node, xml) ⇒ Object



8
9
10
# File 'lib/asciidoctor/ietf/front.rb', line 8

def (node, xml)
  personal_author(node, xml)
end

#metadata_committee(node, xml) ⇒ Object



71
72
73
74
75
76
# File 'lib/asciidoctor/ietf/front.rb', line 71

def (node, xml)
  return unless node.attr("workgroup")
  xml.editorialgroup do |a|
    committee_component("workgroup", node, a)
  end
end


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/asciidoctor/ietf/front.rb', line 22

def (node, xml)
  publishers = node.attr("publisher") || "IETF"
  publishers.split(/,[ ]?/).each do |p|
    xml.copyright do |c|
      c.from (node.attr("copyright-year") || Date.today.year)
      c.owner do |owner|
        owner.organization { |o| organization(o, p) }
      end
    end
  end
end

#metadata_ext(node, xml) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/asciidoctor/ietf/front.rb', line 78

def (node, xml)
  super
  x = node.attr("area") and x.split(/,\s*/).each do |a|
    xml.area a
  end
  x = node.attr("ipr") and xml.ipr x
  x = node.attr("consensus") and xml.consensus x
  x = node.attr("index-include") and xml.indexInclude x
  x = node.attr("ipr-extract") and xml.iprExtract x
  x = node.attr("sort-refs") and xml.sortRefs x
  x = node.attr("sym-refs") and xml.symRefs x
  x = node.attr("toc-include") and xml.tocInclude x
  x = node.attr("toc-depth") and xml.tocDepth x
  x = node.attr("show-on-front-page") and xml.showOnFrontPage x
  xml.pi { |pi| set_pi(node, pi) }
end

#metadata_publisher(node, xml) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/asciidoctor/ietf/front.rb', line 12

def (node, xml)
  publishers = node.attr("publisher") || "IETF"
  publishers.split(/,[ ]?/).each do |p|
    xml.contributor do |c|
      c.role **{ type: "publisher" }
      c.organization { |a| organization(a, p) }
    end
  end
end

#metadata_series(node, xml) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/asciidoctor/ietf/front.rb', line 44

def (node, xml)
  xml.series **{ type: "stream" } do |s|
    s.title (node.attr("submission-type") || "IETF")
  end
  a = node.attr("intended-series") and
    xml.series **{ type: "intended" } do |s|
    parts = a.split(/ /)
    s.title parts[0]
    s.number parts[1..-1].join(" ") if parts.size > 1
  end
end

#norm_ref_preface(f) ⇒ Object



177
178
# File 'lib/asciidoctor/ietf/converter.rb', line 177

def norm_ref_preface(f)
end

#note(n) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/asciidoctor/ietf/blocks.rb', line 47

def note(n)
  noko do |xml|
    xml.note **attr_code(id: ::Asciidoctor::Standoc::Utils::anchor_or_uuid(n),
                         removeInRFC: n.attr("remove-in-rfc")) do |c|
      n.title.nil? or c.name { |name| name << n.title }
      wrap_in_para(n, c)
    end
  end.join("\n")
end

#ol_attrs(node) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/asciidoctor/ietf/blocks.rb', line 18

def ol_attrs(node)
  attr_code(id: ::Asciidoctor::Standoc::Utils::anchor_or_uuid(node),
            type: node.attr("format") || olist_style(node.style),
            group: node.attr("group"),
            spacing: node.attr("spacing"),
            start: node.attr("start"))
end

#open_wg_cache(node) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/asciidoctor/ietf/validate.rb', line 32

def open_wg_cache(node)
  wgcache_name = "#{Dir.home}/.metanorma-ietf-workgroup-cache.json"
  node.attr("flush-caches") == "true" and FileUtils.rm wgcache_name, :force => true
  wg = []
  if Pathname.new(wgcache_name).file?
    begin
      File.open(wgcache_name, "r") { |f| wg = JSON.parse(f.read) }
    rescue Exception => e
      STDERR.puts "Cache #{wgcache_name} is invalid, drop it"
    end
  end
  [wg, wgcache_name]
end

#organization(org, orgname) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/asciidoctor/ietf/front.rb', line 34

def organization(org, orgname)
  if ["IETF",
      "Internet Engineering Task Force"].include? orgname
    org.name "Internet Engineering Task Force"
    org.abbreviation "IETF"
  else
    org.name orgname
  end
end

#para_attrs(node) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/asciidoctor/ietf/blocks.rb', line 4

def para_attrs(node)
  attr_code( "keep-with-next": node.attr("keepWithNext") ||
            node.attr("keep-with-next"),
            "keep-with-previous": node.attr("keepWithPrevious") ||
            node.attr("keep-with-previous"),
            id: ::Asciidoctor::Standoc::Utils::anchor_or_uuid(node))
end

#pi_code(rfc_pis, pi) ⇒ Object



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

def pi_code(rfc_pis, pi)
  rfc_pis.each_pair do |k, v|
    next if v.nil?
    pi.send k.to_s, v
  end
end

#quotesource_cleanup(xmldoc) ⇒ Object



191
192
193
194
195
196
197
198
199
200
# File 'lib/asciidoctor/ietf/converter.rb', line 191

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

#relaton_relationsObject



4
5
6
# File 'lib/asciidoctor/ietf/front.rb', line 4

def relaton_relations
  %w(included-in described-by derived-from instance obsoletes updates)
end

#rfc_anchor_cleanup(xmldoc) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/asciidoctor/ietf/converter.rb', line 142

def rfc_anchor_cleanup(xmldoc)
  map = {}
  xmldoc.xpath("//bibitem[docidentifier/@type = 'rfc-anchor']").each do |b|
    map[b["id"]] = b.at("./docidentifier[@type = 'rfc-anchor']").text
    b["id"] = b.at("./docidentifier[@type = 'rfc-anchor']").text
  end
  xmldoc.xpath("//eref | //origin").each do |x|
    map[x["bibitemid"]] and x["bibitemid"] = map[x["bibitemid"]]
  end
  xmldoc
end

#rfc_converter(node) ⇒ Object



206
207
208
# File 'lib/asciidoctor/ietf/converter.rb', line 206

def rfc_converter(node)
  IsoDoc::Ietf::RfcConvert.new(html_extract_attributes(node))
end

#set_pi(node, pi) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/asciidoctor/ietf/front.rb', line 95

def set_pi(node, pi)
  rfc_pis = {
    artworkdelimiter: node.attr("artworkdelimiter"),
    artworklines: node.attr("artworklines"),
    authorship: node.attr("authorship"),
    autobreaks: node.attr("autobreaks"),
    background: node.attr("background"),
    colonspace: node.attr("colonspace"),
    comments: node.attr("comments"),
    docmapping: node.attr("docmapping"),
    editing: node.attr("editing"),
    emoticonic: node.attr("emoticonic"),
    footer: node.attr("footer"),
    header: node.attr("header"),
    inline: node.attr("inline"),
    iprnotified: node.attr("iprnotified"),
    linkmailto: node.attr("linkmailto"),
    linefile: node.attr("linefile"),
    notedraftinprogress: node.attr("notedraftinprogress"),
    private: node.attr("private"),
    refparent: node.attr("refparent"),
    rfcedstyle: node.attr("rfcedstyle"),
    slides: node.attr("slides"),
    "text-list-symbols": node.attr("text-list-symbols"),
    tocappendix: node.attr("tocappendix"),
    tocindent: node.attr("tocindent"),
    tocnarrow: node.attr("tocnarrow"),
    tocompact: node.attr("tocompact"),
    topblock: node.attr("topblock"),
    useobject: node.attr("useobject"),
    strict: node.attr("strict"),
    compact: node.attr("compact"),
    subcompact: node.attr("subcompact"),
    toc: node.attr("toc-include") == "false" ? "no" : "yes",
    tocdepth: node.attr("toc-depth"),
    symrefs: node.attr("sym-refs"),
    sortrefs: node.attr("sort-refs"),
  }
  pi_code(rfc_pis, pi)
end


37
38
39
40
41
42
43
44
45
# File 'lib/asciidoctor/ietf/blocks.rb', line 37

def sidebar(node)
  return unless draft?
  noko do |xml|
    xml.review **(sidebar_attrs(node)) do |r|
      node.title.nil? or r.name { |name| name << node.title }
      wrap_in_para(node, r)
    end
  end
end

#smartquotes_cleanup(xmldoc) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/asciidoctor/ietf/converter.rb', line 154

def smartquotes_cleanup(xmldoc)
  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

#table_attrs(node) ⇒ Object



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

def table_attrs(node)
  super.merge(align: node.attr("align"))
end

#title(node, xml) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/asciidoctor/ietf/front.rb', line 56

def title(node, xml)
  ["en"].each do |lang|
    at = { language: lang, format: "text/plain" }
    xml.title **attr_code(at.merge(type: "main")) do |t|
      t << (::Asciidoctor::Standoc::Utils::asciidoc_sub(node.attr("title")) ||
        ::Asciidoctor::Standoc::Utils::asciidoc_sub(node.attr("title-en")) ||
        node.title)
    end
    a = node.attr("abbrev") and
      xml.title a, **attr_code(at.merge(type: "abbrev"))
    a = node.attr("asciititle") and
      xml.title a, **attr_code(at.merge(type: "ascii"))
  end
end

#todo_attrs(node) ⇒ Object



33
34
35
# File 'lib/asciidoctor/ietf/blocks.rb', line 33

def todo_attrs(node)
  super.merge(attr_code(display: node.attr("display")))
end

#ul_attrs(node) ⇒ Object



12
13
14
15
16
# File 'lib/asciidoctor/ietf/blocks.rb', line 12

def ul_attrs(node)
  attr_code(id: ::Asciidoctor::Standoc::Utils::anchor_or_uuid(node),
            nobullet: node.attr("nobullet"),
            spacing: node.attr("spacing"))
end

#validate(doc) ⇒ Object



26
27
28
29
30
# File 'lib/asciidoctor/ietf/validate.rb', line 26

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

#workgroup_validate(doc) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/asciidoctor/ietf/validate.rb', line 17

def workgroup_validate(doc)
  return if @workgroups.empty?
  doc.xpath("//bibdata/ext/editorialgroup/workgroup").each do |wg|
    wg_norm = wg.text.sub(/ (Working|Research) Group$/, "")
    next if @workgroups.include?(wg_norm)
    @log.add("Document Attributes", nil, "IETF: unrecognised working group #{wg.text}")
  end
end

#xref_cleanup(xmldoc) ⇒ Object



169
170
171
172
173
174
175
# File 'lib/asciidoctor/ietf/converter.rb', line 169

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

#xref_rel(node) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/asciidoctor/ietf/converter.rb', line 113

def xref_rel(node)
  matched = /^([^#]+)#(.+)$/.match node.target
  if matched.nil?
    t = node.target.sub(/^#/, "")
    rel = nil
  else
    t = matched[1].sub(/\.(xml|adoc)$/, "")
    rel = matched[2]
  end
  [t, rel]
end

#xref_text(node) ⇒ Object



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

def xref_text(node)
  matched = /^format=(counter|title|none|default)(:.*+)?$/.match node.text
  if matched.nil?
    f = nil
    c = node&.text&.sub(/^fn: /, "")
  else
    f = matched[1]
    c = matched[2]&.sub(/^:/, "")
  end
  [f, c]
end

#xref_to_eref(x) ⇒ Object



164
165
166
167
# File 'lib/asciidoctor/ietf/converter.rb', line 164

def xref_to_eref(x)
  super
  x.delete("format")
end