kramdown-rfc2629 1.7.12 → 1.7.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a02556065093c31f3e45dd93a7497bd94a08e304d8eb00a409d2ee15da28987c
4
- data.tar.gz: 8bb1dd4ea40f37625497b6742fa0c8822620c9a237e773ed1d2f6797e47269ba
3
+ metadata.gz: 37a6e52b1686d310bf22c8c2530ce893e3ea30b74675b6cdd17143c141a51321
4
+ data.tar.gz: f9cedeb1b71146d4be4affd7ba343c1a2a497b607e82565b772445d7fc686c06
5
5
  SHA512:
6
- metadata.gz: bd31dc90ac74e3f080491ef7df46814f90b7f60d801547598c8e4086215462280fa78e58c01b25b28c26753fb5021025828c768bbb8a792db67c494d398f05e7
7
- data.tar.gz: 917ebce9132621c9117160273cdab943cf1ec947cc632f320557c0cdb18dbcc01c1c9ead0e79c3a254671d34eb3c4cf91f4fdc6578b0bd6c7454348cb3999a48
6
+ metadata.gz: 92fc3fc628cbcc21b0368f692f078206caa01fa8d5eb8f712777f369632f1d472efa20c0af96cd71a0e0e2752449d1efbeb2c354973fca4f740722545811b213
7
+ data.tar.gz: 924dc4cd075193b1cc9310ddee259290e5bd0026a5ccbb7ff2da65ea897ec3a6560e2fccbaee5122cacaa262ac9d4b692bfb14417359b6f39dfbdb8aaea7b340
data/README.md CHANGED
@@ -12,6 +12,11 @@ Who would care? Anybody who is writing Internet-Drafts and RFCs in
12
12
  the [IETF][] and prefers (or has co-authors who prefer) to do part of
13
13
  their work in markdown.
14
14
 
15
+ kramdown-rfc is documented on this page, and also on
16
+ [the wiki][].
17
+
18
+ [the wiki]: https://github.com/cabo/kramdown-rfc/wiki
19
+
15
20
  # Usage
16
21
 
17
22
  Start by installing the kramdown-rfc gem (this automatically
data/bin/kramdown-rfc-lsr CHANGED
@@ -1,6 +1,13 @@
1
1
  #!/usr/bin/env ruby -KU
2
2
  # frozen_string_literal: true
3
- require 'rexml/document' # for SVG and bibxml acrobatics
3
+
4
+ # List Section References from a RFCXML document
5
+ #
6
+ # (PoC, in urgent need of refactoring)
7
+ # Requires xml2rfc and tidy commands in path
8
+ # Use without open-uri-cached is untested
9
+
10
+ require 'rexml/document'
4
11
  require 'yaml'
5
12
  require 'json'
6
13
  require 'shellwords'
@@ -91,13 +98,21 @@ def series_info_to_name(si)
91
98
  end
92
99
  end
93
100
 
94
- def section_number_to_pn(s)
101
+ def section_number_to_pn_candidates(s)
95
102
  if /^[0-9]/ =~ s
96
- "section-#{s}"
103
+ ["section-#{s}"]
97
104
  elsif /[.]/ =~ s
98
- "section-#{s.downcase}"
105
+ ["section-#{s.downcase}", "section-appendix.#{s.downcase}"]
99
106
  else
100
- "section-appendix.#{s.downcase}"
107
+ ["section-appendix.#{s.downcase}"]
108
+ end
109
+ end
110
+
111
+ def section_number_to_htmlid(s)
112
+ if /^[0-9]/ =~ s
113
+ "section-#{s}"
114
+ else
115
+ "appendix-#{s.upcase}"
101
116
  end
102
117
  end
103
118
 
@@ -109,7 +124,10 @@ references = Hash[REXML::XPath.match(doc.root, "//reference").map {|r|
109
124
  [r[:anchor], si ? [si, sn] : nil]
110
125
  }] # XXX duplicates?
111
126
 
112
- puts "# #{xmlsource}"
127
+ heading1 = "# #{xmlsource}"
128
+ title = REXML::XPath.first(doc.root, "//title")
129
+ heading1 << "\n(#{title.all_text})" if title
130
+ puts heading1
113
131
 
114
132
  per_reference = Hash.new { |h, k| h[k] = Set[]}
115
133
 
@@ -130,7 +148,44 @@ def error_out(s)
130
148
  $exit_code = 1
131
149
  end
132
150
 
133
- per_reference.keys.sort.each do |trg|
151
+ def num_expand(s)
152
+ s.gsub(/\d+/) {|n| "%09d" % n.to_i}
153
+ end
154
+
155
+ require 'open3'
156
+
157
+ module OpenURI
158
+ class << self
159
+ def prepped(uri, *rest)
160
+ newuri = uri.to_s.sub(/\.xml$/, ".prepped.xml")
161
+ response = Cache.get(newuri) || (
162
+ unprepped = open_uri(uri, *rest).read
163
+ fn = [OpenURI::Cache.cache_path, uri.sub(/.*\//, '')].join('/')
164
+ File.open(fn, 'wb'){|f| f.write unprepped }
165
+ _prep_out, s = Open3.capture2("xml2rfc", "--prep", fn)
166
+ fail s.inspect unless s.success?
167
+ new_fn = fn.sub(/\.xml$/, ".prepped.xml")
168
+ Cache.set(newuri, File.open(new_fn))
169
+ )
170
+ response
171
+ end
172
+ def tidied(uri, *rest)
173
+ newuri = uri.to_s.sub(/\.html$/, ".tidied.html")
174
+ response = Cache.get(newuri) || (
175
+ unprepped = open_uri(uri, *rest).read
176
+ fn = [OpenURI::Cache.cache_path, uri.sub(/.*\//, '')].join('/')
177
+ File.open(fn, 'wb'){|f| f.write unprepped }
178
+ _prep_out, s = Open3.capture2("tidy", "-mq", "-asxml", "-f", "/dev/null", fn)
179
+ fail s.inspect unless s.exited?
180
+ Cache.set(newuri, File.open(fn))
181
+ )
182
+ response
183
+ end
184
+ end
185
+ end
186
+
187
+ # go through section-referenced documents in sequence
188
+ per_reference.keys.sort_by {|x| num_expand(x)}.each do |trg|
134
189
  uri, sname = references[trg]
135
190
  add = +''
136
191
  if sname != trg
@@ -139,46 +194,75 @@ per_reference.keys.sort.each do |trg|
139
194
  begin
140
195
  ref = URI(uri).open.read
141
196
  refdoc = REXML::Document.new(ref)
197
+ if REXML::XPath.match(refdoc.root, "/rfc/front/abstract[@pn]").size == 0
198
+ ref = OpenURI.prepped(uri).read
199
+ refdoc = REXML::Document.new(ref)
200
+ add << " [+prep]"
201
+ end
142
202
  add << " (#{REXML::XPath.match(refdoc.root, "//title").first.all_text})"
143
203
  rescue OpenURI::HTTPError => e
144
204
  begin
145
- refjson = URI(uri.sub(/\.xml$/, ".json")).open.read
205
+ jsonuri = uri.sub(/\.xml$/, ".json")
206
+ refjson = URI(jsonuri).open.read
146
207
  refdata = JSON.load(refjson)
147
- add << " (#{refdata["title"]})"
148
- rescue Exception => e
208
+ add << " (#{refdata["title"].strip})"
209
+ rescue OpenURI::HTTPError => e
149
210
  add << " [No XML or JSON]"
211
+ rescue Exception => e
212
+ warn "*** error getting #{jsonuri.inspect}: #{e}"
150
213
  end
151
214
  rescue Exception => e
152
215
  warn "*** error getting #{uri.inspect}: #{e}"
153
216
  end
154
217
  puts "\n## #{trg}#{add}"
155
- per_reference[trg].to_a.sort.each do |s| # XXX numerically
156
- secterm = if /^[0-9]/ =~ s
157
- "Section"
158
- else
159
- "Appendix"
160
- end
218
+ unless refdoc
219
+ begin
220
+ htmluri = uri.sub(/\.xml$/, ".html")
221
+ refhtml = OpenURI.tidied(htmluri).read
222
+ refhtmldoc = REXML::Document.new(refhtml)
223
+ rescue Exception => e
224
+ warn "*** error tidying up HTML for #{htmluri.inspect}: #{e}"
225
+ end
226
+ end
227
+ # go through individual section references in sequence
228
+ per_reference[trg].to_a.sort_by {|x| num_expand(x)}.each do |s|
161
229
  add = +''
162
- if refdoc
163
- # find section name from anchor s
164
- # secname = "DUMMY" # REXML::XPath.match(refdoc.root, "//section").first.all_text
165
- secpn = section_number_to_pn(s)
166
- secs = REXML::XPath.match(refdoc.root, "//section[@pn=$pn]", {}, {"pn" => secpn})
167
- secname = nil
168
- case secs.size
169
- when 0
170
- error_out "*** cannot match #{secpn} in #{trg}"
171
- secname = "*** DOESN'T EXIST ***"
172
- when 1
173
- sec = secs.first
174
- secname = sec[:title] || sec.elements["name"].all_text
175
- else
176
- error_out "*** multiple matches for #{secpn} in #{trg}"
177
- secname = "*** MULTIPLE MATCHES ***"
178
- end
179
- add << " (#{secname})"
230
+ if refdoc # find section name in XML from anchor s
231
+ secpn = section_number_to_pn_candidates(s)
232
+ secs = secpn.flat_map{ |c|
233
+ REXML::XPath.match(refdoc.root, "//section[@pn=$pn]",
234
+ {}, {"pn" => c})}
235
+ what = "#{secpn.join(" or ")} in #{trg}"
236
+ add << " (#{case secs.size
237
+ when 0
238
+ error_out "*** cannot match #{what}"
239
+ "*** DOESN'T EXIST ***"
240
+ when 1
241
+ sec = secs.first
242
+ sec[:title] || sec.elements["name"].all_text
243
+ else
244
+ error_out "*** multiple matches for #{what}"
245
+ "*** MULTIPLE MATCHES ***"
246
+ end})"
247
+ elsif refhtmldoc # find section name in HTML from anchor s
248
+ secpn = section_number_to_htmlid(s)
249
+ secs = REXML::XPath.match(refhtmldoc.root,
250
+ "//xmlns:a[@id=$pn]/ancestor::xmlns:span",
251
+ {"xmlns" => "http://www.w3.org/1999/xhtml"},
252
+ {"pn" => secpn})
253
+ what = "#{secpn} in #{trg}"
254
+ add << " (#{case secs.size
255
+ when 0
256
+ error_out "*** cannot match #{what}"
257
+ "*** DOESN'T EXIST ***"
258
+ when 1
259
+ secs.first.text.sub(/^\.\s+/, '')
260
+ else
261
+ error_out "*** multiple matches for #{what}"
262
+ "*** MULTIPLE MATCHES ***"
263
+ end})"
180
264
  end
181
- puts "* #{secterm} #{s}#{add}"
265
+ puts "* #{/^[0-9]/ =~ s ? "Section" : "Appendix"} #{s}#{add}"
182
266
  end
183
267
  end
184
268
 
@@ -1,6 +1,6 @@
1
1
  spec = Gem::Specification.new do |s|
2
2
  s.name = 'kramdown-rfc2629'
3
- s.version = '1.7.12'
3
+ s.version = '1.7.13'
4
4
  s.summary = "Kramdown extension for generating RFCXML (RFC 799x)."
5
5
  s.description = %{An RFCXML (RFC 799x) generating backend for Thomas Leitner's
6
6
  "kramdown" markdown parser. Mostly useful for RFC writers.}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kramdown-rfc2629
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.12
4
+ version: 1.7.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carsten Bormann