relaton-ietf 1.8.0 → 1.9.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,60 @@
1
+ module RelatonIetf
2
+ class RfcIndexEntry
3
+ #
4
+ # Document parser initalization
5
+ #
6
+ # @param [String] doc_id document id
7
+ # @param [Array<String>] is_also also document ids
8
+ #
9
+ def initialize(name, doc_id, is_also)
10
+ @name = name
11
+ @shortnum = doc_id[-4..-1].sub(/^0+/, "")
12
+ @doc_id = doc_id
13
+ @is_also = is_also
14
+ end
15
+
16
+ #
17
+ # Document id
18
+ #
19
+ # @return [Strinng] document id
20
+ #
21
+ def docnumber
22
+ @doc_id
23
+ end
24
+
25
+ #
26
+ # Initialize document parser and run it
27
+ #
28
+ # @param [Nokogiri::XML::Element] doc document
29
+ #
30
+ # @return [RelatonIetf:RfcIndexEntry, nil]
31
+ #
32
+ def self.parse(doc)
33
+ doc_id = doc.at("./xmlns:doc-id")
34
+ is_also = doc.xpath("./xmlns:is-also/xmlns:doc-id").map &:text
35
+ return unless doc_id && is_also.any?
36
+
37
+ name = doc.name.split("-").first
38
+ new(name, doc_id.text, is_also)
39
+ end
40
+
41
+ #
42
+ # Render document as XML
43
+ #
44
+ # @return [String] XML
45
+ #
46
+ def to_xml # rubocop:disable Metrics/MethodLength
47
+ Nokogiri::XML::Builder.new do |xml|
48
+ anchor = "#{@name.upcase}#{@shortnum}"
49
+ url = "https://www.rfc-editor.org/info/#{@name}#{@shortnum}"
50
+ xml.referencegroup("xmlns:xi" => "http://www.w3.org/2001/XInclude",
51
+ anchor: anchor, target: url) do
52
+ @is_also.each do |did|
53
+ num = did[-4..-1]
54
+ xml["xi"].send("include", href: "https://www.rfc-editor.org/refs/bibxml/reference.RFC.#{num}.xml")
55
+ end
56
+ end
57
+ end.doc.root.to_xml
58
+ end
59
+ end
60
+ end