Module: RFC::V3::Base

Defined in:
lib/asciidoctor/rfc/v3/base.rb

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#cleanup(doc) ⇒ Object

clean up XML



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/asciidoctor/rfc/v3/base.rb', line 312

def cleanup(doc)
  xmldoc = Nokogiri::XML(doc)
  crefs = xmldoc.xpath("//cref")
  # any crefs that are direct children of section should become children of the preceding
  # paragraph, if it exists; otherwise, they need to be wrapped in a paragraph
  crefs.each do |cref|
    if cref.parent.name == "section"
      prev = cref.previous_element
      if !prev.nil? && prev.name == "t"
        cref.parent = prev
      else
        t = Nokogiri::XML::Element.new("t", xmldoc)
        cref.before(t)
        cref.parent = t
      end
    end
  end
  xmldoc = smart_quote_cleanup(xmldoc) unless $smart_quotes
  xmldoc.to_xml(encoding: "US-ASCII")
end

#document(node) ⇒ Object

Syntax:

=Title
Author
:ipr
:obsoletes
:updates
:submissionType
:indexInclude
:iprExtract
:sortRefs
:symRefs
:tocInclude
:tocDepth

ABSTRACT

NOTEs

==first title
CONTENT

[bibliography] # start of back matter
== Bibliography

[appendix] # start of back matter if not already started
== Appendix


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/asciidoctor/rfc/v3/base.rb', line 32

def document(node)
  $seen_back_matter = false
  # If this is present, then BCP14 keywords in boldface are not assumed to be <bcp14> tags. By default they are.
  $bcp_bold = !(node.attr? "no-rfc-bold-bcp14")
  $smart_quotes = (node.attr("smart-quotes") != "false")
  $xreftext = {}
  result = []
  result << '<?xml version="1.0" encoding="UTF-8"?>'

  t = Time.now.getutc
  preptime = sprintf(
    "%04d-%02d-%02dT%02d:%02d:%02dZ",
    t.year, t.month, t.day, t.hour, t.min, t.sec
  )

  rfc_attributes = {
    ipr:            node.attr("ipr"),
    obsoletes:      node.attr("obsoletes"),
    updates:        node.attr("updates"),
    indexInclude:   node.attr("index-include"),
    iprExtract:     node.attr("ipr-extract"),
    sortRefs:       node.attr("sort-refs"),
    symRefs:        node.attr("sym-refs"),
    tocInclude:     node.attr("toc-include"),
    tocDepth:       node.attr("toc-depth"),
    submissionType: node.attr("submission-type") || "IETF",
    'xml:lang':     node.attr("xml-lang"),
    prepTime:       preptime,
    version:        "3",
    'xmlns:xi':        "http://www.w3.org/2001/XInclude",
  }

  rfc_open = noko { |xml| xml.rfc **attr_code(rfc_attributes) }.join.gsub(/\/>$/, ">")
  result << rfc_open

  result << (link node)

  result << noko { |xml| front node, xml }
  result.last.last.gsub! /<\/front>$/, "" # FIXME: this is a hack!
  result << "</front><middle1>"

  result << node.content if node.blocks?
  result << ($seen_back_matter ? "</back>" : "</middle>")
  result << "</rfc>"

  # <middle> needs to move after preamble
  result = result.flatten
  result = if result.any? { |e| e =~ /<\/front><middle>/ } && result.any? { |e| e =~ /<\/front><middle1>/ }
             result.reject { |e| e =~ /<\/front><middle1>/ }
           else
             result.map { |e| e =~ /<\/front><middle1>/ ? "</front><middle>" : e }
           end
  ret = result * "\n"
  ret = cleanup(ret)
  ret1 = Nokogiri::XML(ret)
  ret1 = set_pis(node, ret1)
  ret1 = insert_biblio(node, ret1) unless node.attr("biblio-dir").nil? || node.attr("biblio-dir").empty?
  output_dtd()
  Validate::validate(ret1)
  ret1 = resolve_references(node, ret1)
  # Validate::validate(ret1)
  ret1.to_xml
end

#image(node) ⇒ Object

Note:

ignoring width, height attributes

Syntax:

[[id]]
.Name
[link=xxx,align=left|center|right,alt=alt_text,type]
image::filename[alt,width,height]


287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/asciidoctor/rfc/v3/base.rb', line 287

def image(node)
  uri = node.image_uri node.attr("target")
  artwork_attributes = {
    align: node.attr("align"),
    alt: node.alt,
    anchor: node.id,
    height: node.attr("height"),
    name: node.title,
    src: uri,
    type: (uri =~ /\.svg$/ ? "svg" : "binary-art"),
    width: node.attr("width"),
  }

  noko do |xml|
    if node.parent.context != :example
      xml.figure do |xml_figure|
        xml_figure.artwork **attr_code(artwork_attributes)
      end
    else
      xml.artwork **attr_code(artwork_attributes)
    end
  end
end

#inline_break(node) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/asciidoctor/rfc/v3/base.rb', line 124

def inline_break(node)
  # <br> is only defined within tables
  noko do |xml|
    xml << node.text
    xml.br if node.parent.context == :cell
  end.join
end

#inline_quoted(node) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/asciidoctor/rfc/v3/base.rb', line 138

def inline_quoted(node)
  noko do |xml|
    case node.type
    when :emphasis then xml.em node.text
    when :strong
      if $bcp_bold && BCP_KEYWORDS.include?(node.text)
        xml.bcp14 node.text
      else
        xml.strong node.text
      end
    when :monospaced then xml.tt node.text
    when :double
      xml << ($smart_quotes ? "#{node.text}" : "\"#{node.text}\"")
    when :single
      xml << ($smart_quotes ? "#{node.text}" : "'#{node.text}'")
    when :superscript then xml.sup node.text
    when :subscript then xml.sub node.text
    else
      # [bcp14]#MUST NOT#
      if node.role == "bcp14"
        xml.bcp14 node.text.upcase
      elsif node.role == "comment"
        xml.comment " " + node.text + " "
      else
        xml << node.text
      end
    end
  end.join
end

Syntax:

= Title
Author
:link href,href rel


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/asciidoctor/rfc/v3/base.rb', line 108

def link(node)
  result = []
  result << noko do |xml|
    links = (node.attr("link") || "").split(/,\s*/)
    links.each do |link|
      matched = /^(?<href>\S+)\s+(?<rel>\S+)$/.match link
      link_attributes = {
        href: matched.nil? ? link : matched[:href],
        rel: matched.nil? ? nil : matched[:rel],
      }
      xml.link **attr_code(link_attributes)
    end
  end
  result
end

#paragraph(node) ⇒ Object

Syntax:

[[id]]
[keepWithNext=true,keepWithPrevious=true] (optional)
Text


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/asciidoctor/rfc/v3/base.rb', line 172

def paragraph(node)
  if node.role == "comment"
    return noko do |xml|
      xml.comment " " + [flatten_rawtext(node)].flatten.join("\n") + " "
    end.join("\n")
  end

  t_attributes = {
    anchor: node.id,
    keepWithNext: node.attr("keep-with-next"),
    keepWithPrevious: node.attr("keep-with-previous"),
  }
  return noko do |xml|
    xml.t **attr_code(t_attributes) do |xml_t|
      xml_t << node.content
    end
  end.join("\n")
end

#ref_section(node) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/asciidoctor/rfc/v3/base.rb', line 191

def ref_section(node)
  result = []
  $processing_reflist = true
  references_attributes = {
    anchor: node.id,
  }

  if node.blocks.empty?
    result << noko do |xml|
      xml.references **references_attributes do |xml_references|
        xml_references.name node.title unless node.title.nil?
      end
    end
  end
  node.blocks.each do |block|
    if block.context == :section
      result << section(block)
    elsif block.context == :pass
      # we are assuming a single contiguous :pass block of XML
      result << noko do |xml|
        xml.references **references_attributes do |xml_references|
          xml_references.name node.title unless node.title.nil?
          # xml_references << reflist(block).join("\n")
          # NOTE: we're allowing the user to do more or less whathever
          #   in the passthrough since the xpath below just fishes out ALL
          #   <reference>s in an unrooted fragment, regardless of structure.
          Nokogiri::XML::DocumentFragment.
            parse(block.content).xpath(".//reference").
            each { |reference| xml_references << reference.to_xml }
        end
      end
    elsif block.context == :ulist
      block.items.each(&:text)
      # we only process the item for its displayreferences
    end
  end

  unless $xreftext.empty? || $seen_back_matter
    result = result.unshift($xreftext.keys.map { |k| %(<displayreference target="#{k}" to="#{$xreftext[k]}"/>) })
  end
  result = result.unshift("</middle><back>") unless $seen_back_matter
  $processing_reflist = false
  $seen_back_matter = true
  result
end

#resolve_references(node, doc) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/asciidoctor/rfc/v3/base.rb', line 96

def resolve_references(node, doc)
  extract_entities(node, doc).each do |entity|
    # TODO actual XML
    entity[:node].replace("<xi:include href='#{entity[:url]}' parse='text'/>")
  end
  doc
end

#section(node) ⇒ Object

Syntax:

:sectnums: (toggle)
:sectnums!: (toggle)
[[id]]
[removeInRFC=true,toc=include|exclude|default] (optional)
== title
Content

[[id]]
[bibliography]
== Normative|Informative References
* [[[ref1]]] Ref [must provide references as list]
* [[[ref2]]] Ref


250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/asciidoctor/rfc/v3/base.rb', line 250

def section(node)
  if node.attr("style") == "bibliography" ||
      node.parent.context == :section && node.parent.attr("style") == "bibliography"
    result = ref_section(node)
  else
    result = []
    if $seen_back_matter && node.attr("style") != "appendix"
      warn "Appendix not marked up with [appendix]!"
    end
    if node.attr("style") == "appendix"
      result << "</middle><back>" unless $seen_back_matter
      $seen_back_matter = true
    end

    section_attributes = {
      anchor: node.id,
      removeInRFC: node.attr("remove-in-rfc"),
      toc: node.attr("toc"),
      numbered: node.attr?("sectnums"),
    }

    result << noko do |xml|
      xml.section **attr_code(section_attributes) do |section_xml|
        section_xml.name { |name| name << node.title } unless node.title.nil?
        section_xml << node.content
      end
    end
  end
  result
end