relaton-cli 1.1.1 → 1.2.1

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: d8049656d1afb46e4ba336f812b0eb6261525e64b6d127b313c03a8c5ce4c72e
4
- data.tar.gz: bcfa7ebc31360fd27f2ccb2f21f1e864fc283433bdba045ff6c8e7393aabc25b
3
+ metadata.gz: 8a02348a5d77b33a2e249bc4aa1a22ed07d8fff2cfdb5323799abc9cc0d0a445
4
+ data.tar.gz: e4ba64c5d0d8e5db5ad4e7b3a7be42a13e7bfc22c2beb81336391decb96853b0
5
5
  SHA512:
6
- metadata.gz: 8a49edaeb34c75d64fcdb63ac642a3882086c0d994f169a0691f9f86fe27b7a1be4680f2295bbf80301b6089d76154bdfa734364ecbb2909ee0d630dd4cc616a
7
- data.tar.gz: 2ead71208ec783d66af83bfcc89901d3d9da70e441d9464cb6f5f6b57838c5e5cde8bec940d4856804abe0260e60c6e30cfda9c7becec50c60a0b31fc66edf63
6
+ metadata.gz: ee47c92805a92f72e5a23b554cc8a5737f38392b33a64f9a3d36f6732b61958052ae493a9e2bd0cbb02b2854bf9b453e6b68b82182c4907c9d90e64af7867331
7
+ data.tar.gz: 6bac8c0e2f42e093ca23592d7ac908f86389907125d9f39400c6a8c562b322809ca06f9134ffbdacc5b7cb50313438c447d0f7b142c4de400b498a1b07c1937a
data/.gitignore CHANGED
@@ -13,3 +13,4 @@
13
13
  .vscode/
14
14
  .byebug_history
15
15
  Gemfile.lock
16
+ .DS_Store
@@ -41,7 +41,7 @@ module Relaton
41
41
  )
42
42
 
43
43
  items = find_xpath("./relaton-collection/relation", source)&.map do |item|
44
- bibdata = find("./bibdata", item)
44
+ bibdata = find("./bibdata|./bibitem", item)
45
45
  klass = bibdata ? Bibdata : Bibcollection
46
46
  klass.from_xml(bibdata || item)
47
47
  end
@@ -10,18 +10,6 @@ module Relaton
10
10
  @bibitem = bibitem
11
11
  end
12
12
 
13
- # def method_missing(method, *args)
14
- # %r{(?<m>\w+)=$} =~ method
15
- # return unless m && %w[xml pdf doc html rxl txt].include?(m)
16
-
17
- # uri = @bibitem.link.detect { |u| u.type == m }
18
- # if uri
19
- # uri.content = args[0]
20
- # else
21
- # @bibitem.link << RelatonBib::TypedUri.new(type: m, content: args[0])
22
- # end
23
- # end
24
-
25
13
  def docidentifier
26
14
  @bibitem.docidentifier.first&.id
27
15
  end
@@ -57,15 +45,10 @@ module Relaton
57
45
  opts.select { |k, _v| k.is_a? Symbol },
58
46
  )
59
47
  @bibitem.to_xml nil, **options
60
-
61
- # #datetype = stage&.casecmp("published") == 0 ? "published" : "circulated"
62
-
63
- # ret = ref ? "<bibitem id= '#{ref}' type='#{doctype}'>\n" : "<bibdata type='#{doctype}'>\n"
64
- # ret += "<fetched>#{Date.today.to_s}</fetched>\n"
65
48
  end
66
49
 
67
50
  def to_h
68
- URL_TYPES.reduce({ items: [] }.merge(@bibitem.to_hash)) do |h, t|
51
+ URL_TYPES.reduce(@bibitem.to_hash) do |h, t|
69
52
  value = send t
70
53
  h[t.to_s] = value
71
54
  h
@@ -76,6 +59,9 @@ module Relaton
76
59
  to_h.to_yaml
77
60
  end
78
61
 
62
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize
63
+ # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity, Style/MissingRespondToMissing
64
+
79
65
  def method_missing(meth, *args)
80
66
  if @bibitem.respond_to?(meth)
81
67
  @bibitem.send meth, *args
@@ -98,5 +84,7 @@ module Relaton
98
84
  super
99
85
  end
100
86
  end
87
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/AbcSize
88
+ # rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity, Style/MissingRespondToMissing
101
89
  end
102
90
  end
@@ -34,20 +34,22 @@ module Relaton
34
34
  # @param content [Nokogiri::XML::Document]
35
35
  # @return [RelatonBib::BibliographicItem, RelatonIsoBib::IsoBibliongraphicItem]
36
36
  def self.parse_xml(doc)
37
- if (processor = Relaton::Registry.instance.by_type(Relaton::Cli.doctype(doc)))
38
- processor.from_xml(doc.to_s)
37
+ if (proc = Cli.processor(doc))
38
+ proc.from_xml(doc.to_s)
39
39
  else
40
40
  RelatonBib::XMLParser.from_xml(doc.to_s)
41
41
  end
42
42
  end
43
43
 
44
- # @param content [Nokogiri::XML::Document] Document
44
+ # @param doc [Nokogiri::XML::Element]
45
45
  # @return [String] Type prefix
46
- def self.doctype(doc)
46
+ def self.processor(doc)
47
47
  docid = doc.at "docidentifier"
48
- return docid[:type] if docid && docid[:type]
49
-
50
- docid&.text&.match(/^\w+/)&.to_s
48
+ if docid && docid[:type]
49
+ proc = Relaton::Registry.instance.by_type(docid[:type])
50
+ return proc if proc
51
+ end
52
+ Relaton::Registry.instance.by_type(docid&.text&.match(/^\w+/)&.to_s)
51
53
  end
52
54
  end
53
55
  end
@@ -28,7 +28,7 @@ module Relaton
28
28
  option :title, aliases: :t, desc: "Title of resulting Relaton collection"
29
29
  option :organization, aliases: :g, desc: "Organization owner of Relaton collection"
30
30
  option :new, aliases: :n, type: :boolean, desc: "Use the new Relaton YAML format"
31
- option :extension, aliases: :x, default: "rxl", desc: "File extension of destination Relaton file, defaults to 'rxl'"
31
+ option :extension, aliases: :x, desc: "File extension of destination Relaton file, defaults to 'rxl'"
32
32
 
33
33
  def concatenate(source_dir, outfile)
34
34
  Relaton::Cli::RelatonFile.concatenate(source_dir, outfile, options)
@@ -118,7 +118,6 @@ module Relaton
118
118
  bib = nokogiri_document(bib.to_xml)
119
119
  elsif (rfc = xml.at("//rfc"))
120
120
  require "relaton_ietf/scrapper"
121
- #ietf = RelatonIetf::Scrapper.bib_item rfc, "rfc"
122
121
  ietf = RelatonIetf::Scrapper.fetch_rfc rfc
123
122
  bib = nokogiri_document ietf.to_xml(bibdata: true)
124
123
  else
@@ -142,18 +141,16 @@ module Relaton
142
141
 
143
142
  xml_files.flatten.reduce([]) do |mem, xml|
144
143
  doc = nokogiri_document(xml[:content])
145
- if (refs = doc.xpath("//rfc//reference")).any?
144
+ if (rfc = doc.at("/rfc"))
146
145
  require "relaton_ietf/scrapper"
147
- mem + refs.map do |rfc|
148
- ietf = RelatonIetf::Scrapper.fetch_rfc rfc
149
- d = nokogiri_document ietf.to_xml(bibdata: true)
150
- bibdata_instance(d, xml[:file])
151
- end
152
- elsif doc&.root&.name == "bibdata"
146
+ ietf = RelatonIetf::Scrapper.fetch_rfc rfc
147
+ d = nokogiri_document ietf.to_xml(bibdata: true)
148
+ mem << bibdata_instance(d, xml[:file])
149
+ elsif %w[bibitem bibdata].include? doc&.root&.name
153
150
  mem << bibdata_instance(doc, xml[:file])
154
151
  else mem
155
152
  end
156
- end.compact
153
+ end.uniq &:id
157
154
  end
158
155
 
159
156
  def concatenate_and_write_to_files
@@ -170,21 +167,23 @@ module Relaton
170
167
  end
171
168
  end
172
169
 
173
- def find_available_bibrxl_file(name, ouputdir, content)
170
+ def find_available_bibrxl_file(name, _ouputdir, content)
174
171
  if options[:extension] == "yaml" || options[:extension] == "yml"
175
172
  bib_rxl = Pathname.new([outdir, name].join("/")).sub_ext(".rxl")
176
173
  content.bib_rxl = bib_rxl.to_s if File.file?(bib_rxl)
177
174
  end
178
175
  end
179
176
 
180
- def output_type(ext=options[:extension])
177
+ def output_type(ext = options[:extension])
178
+ ext ||= File.extname(outfile)[1..-1] if outfile
181
179
  case ext
182
180
  when "rxl", "xml"
183
181
  :to_xml
184
182
  when "yml", "yaml"
185
183
  :to_yaml
186
184
  else
187
- puts "[relaton-cli] the given extension of '#{ext}' is not supported."
185
+ puts "[relaton-cli] the given extension of '#{ext}' is "\
186
+ "not supported. Use 'rxl'."
188
187
  :to_xml
189
188
  end
190
189
  end
@@ -270,7 +269,7 @@ module Relaton
270
269
 
271
270
  def replace_bad_characters(string)
272
271
  bad_chars = ["/", "\\", "?", "%", "*", ":", "|", '"', "<", ">", ".", " "]
273
- bad_chars.inject(string.downcase) { |res, char| res.gsub(char, "-") }
272
+ bad_chars.reduce(string.downcase) { |res, char| res.gsub(char, "-") }
274
273
  end
275
274
  end
276
275
  end
@@ -1,5 +1,5 @@
1
1
  module Relaton
2
2
  module Cli
3
- VERSION = "1.1.1".freeze
3
+ VERSION = "1.2.1".freeze
4
4
  end
5
5
  end
@@ -34,18 +34,8 @@ module Relaton
34
34
  def convert_content(content)
35
35
  if %w[bibitem bibdata].include? content.root.name
36
36
  Bibdata.from_xml(content)
37
- # Relaton::Cli.parse_xml(content).to_hash
38
37
  else
39
38
  Bibcollection.from_xml(content)
40
- # title = content.at("relaton-collection/title").text
41
- # author = content.at("relaton-collection/contributor/organization/name").text
42
- # collection = { "root" => { "title" => title, "author" => author } }
43
-
44
- # collection["root"]["items"] = content.xpath("//bibdata").map do |bib|
45
- # Relaton::Cli.parse_xml(bib).to_hash
46
- # end
47
-
48
- # collection
49
39
  end
50
40
  end
51
41
 
@@ -1,6 +1,6 @@
1
- require "yaml"
2
- require "relaton/cli/base_convertor"
3
- require "relaton_bib"
1
+ require 'yaml'
2
+ require 'relaton/cli/base_convertor'
3
+ require 'relaton_bib'
4
4
 
5
5
  module Relaton
6
6
  module Cli
@@ -9,29 +9,55 @@ module Relaton
9
9
  if writable
10
10
  convert_and_write(file_content, :to_xml)
11
11
  else
12
- convert_content(file_content).to_xml(date_format: :full, bibdata: true)
12
+ convert_content(file_content).to_xml date_format: :full, bibdata: true
13
13
  end
14
14
  end
15
15
 
16
- # Convert to XML
17
- #
18
- # This interface allow us to convert any YAML file to XML.
19
- # It only require us to provide a valid YAML file and it can
20
- # do converstion using default attributes, but it also allow
21
- # us to provide custom options to customize this converstion
22
- # process.
23
- #
24
- # @param file [File] The complete path to a YAML file
25
- # @param options [Hash] Options as hash key, value pairs.
26
- #
27
- def self.to_xml(file, options = {})
28
- new(file, options).to_xml
16
+ class << self
17
+ # Convert to XML
18
+ #
19
+ # This interface allow us to convert any YAML file to XML.
20
+ # It only require us to provide a valid YAML file and it can
21
+ # do converstion using default attributes, but it also allow
22
+ # us to provide custom options to customize this converstion
23
+ # process.
24
+ #
25
+ # @param file [File] The complete path to a YAML file
26
+ # @param options [Hash] Options as hash key, value pairs.
27
+ #
28
+ def to_xml(file, options = {})
29
+ new(file, options).to_xml
30
+ end
31
+
32
+ # @param content [Hash] document in YAML format
33
+ # @return [RelatonBib::BibliographicItem,
34
+ # RelatonIso::IsoBiblioraphicItem]
35
+ def convert_single_file(content)
36
+ if (processor = Registry.instance.by_type(doctype(content['docid'])))
37
+ processor.hash_to_bib content
38
+ else
39
+ RelatonBib::BibliographicItem.new(
40
+ RelatonBib::HashConverter::hash_to_bib(content)
41
+ )
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ # @param content [Hash]
48
+ # @return [String]
49
+ def doctype(docid)
50
+ did = docid.is_a?(Array) ? docid.fetch(0) : docid
51
+ return unless did
52
+
53
+ did['type'] || did.fetch('id')&.match(/^\w+/)&.to_s
54
+ end
29
55
  end
30
56
 
31
57
  private
32
58
 
33
59
  def default_ext
34
- "rxl"
60
+ 'rxl'
35
61
  end
36
62
 
37
63
  def file_content
@@ -39,37 +65,25 @@ module Relaton
39
65
  end
40
66
 
41
67
  def date_to_string(obj)
42
- obj.is_a? Hash and
43
- return obj.inject({}){|memo,(k,v)| memo[k] = date_to_string(v); memo}
44
- obj.is_a? Array and
45
- return obj.inject([]){|memo,v | memo << date_to_string(v); memo}
46
- return obj.is_a?(Date) ? obj.to_s : obj
47
- end
48
-
49
- def convert_single_file(content)
50
- if (processor = Relaton::Registry.instance.by_type(doctype(content["docid"])))
51
- processor.hash_to_bib content
68
+ if obj.is_a? Hash
69
+ obj.reduce({}) do |memo, (k, v)|
70
+ memo[k] = date_to_string(v)
71
+ memo
72
+ end
73
+ elsif obj.is_a? Array
74
+ obj.reduce([]) { |memo, v| memo << date_to_string(v) }
52
75
  else
53
- RelatonBib::BibliographicItem.new(RelatonBib::HashConverter::hash_to_bib(content))
76
+ obj.is_a?(Date) ? obj.to_s : obj
54
77
  end
55
78
  end
56
79
 
57
- # @param content [Hash]
58
- # @return [String]
59
- def doctype(docid)
60
- did = docid.is_a?(Array) ? docid.fetch(0) : docid
61
- return did["type"] if did && did["type"]
62
-
63
- did&.fetch("id")&.match(/^\w+/)&.to_s
64
- end
65
-
66
80
  def convert_collection(content)
67
- if content.has_key?("root")
68
- content["root"]["items"] = content["root"]["items"].map do |i|
81
+ if content.has_key?('root')
82
+ content['root']['items'] = content['root']['items'].map do |i|
69
83
  # RelatonBib::HashConverter::hash_to_bib(i)
70
- convert_single_file(i)
84
+ self.class.convert_single_file(i)
71
85
  end
72
- Relaton::Bibcollection.new(content["root"])
86
+ Relaton::Bibcollection.new(content['root'])
73
87
  end
74
88
  end
75
89
 
@@ -78,7 +92,7 @@ module Relaton
78
92
  end
79
93
 
80
94
  def convert_content(content)
81
- convert_collection(content) || convert_single_file(content)
95
+ convert_collection(content) || self.class.convert_single_file(content)
82
96
  end
83
97
  end
84
98
  end
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.require_paths = ["lib"]
23
23
  spec.required_ruby_version = ">= 2.4.0"
24
24
 
25
- spec.add_development_dependency "byebug", "~> 10.0"
25
+ spec.add_development_dependency "byebug", "~> 11.0"
26
26
  spec.add_development_dependency "debase"
27
27
  spec.add_development_dependency "equivalent-xml", "~> 0.6"
28
28
  spec.add_development_dependency "pry"
@@ -34,7 +34,7 @@ Gem::Specification.new do |spec|
34
34
  spec.add_development_dependency "simplecov"
35
35
 
36
36
  spec.add_runtime_dependency "liquid"
37
- spec.add_runtime_dependency "relaton", "~> 1.1.0"
37
+ spec.add_runtime_dependency "relaton", "~> 1.2.0"
38
38
  spec.add_runtime_dependency "thor"
39
39
  # spec.add_runtime_dependency 'byebug'
40
40
  end
@@ -54,7 +54,7 @@
54
54
 
55
55
  <div class="doc-bib">
56
56
  <div class="doc-bib-relaton">
57
- <a href="{{ document.relaton }}">Relaton XML</a>
57
+ <a href="{{ document.rxl }}">Relaton XML</a>
58
58
  </div>
59
59
  </div>
60
60
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relaton-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-10 00:00:00.000000000 Z
11
+ date: 2020-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '10.0'
19
+ version: '11.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '10.0'
26
+ version: '11.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: debase
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -170,14 +170,14 @@ dependencies:
170
170
  requirements:
171
171
  - - "~>"
172
172
  - !ruby/object:Gem::Version
173
- version: 1.1.0
173
+ version: 1.2.0
174
174
  type: :runtime
175
175
  prerelease: false
176
176
  version_requirements: !ruby/object:Gem::Requirement
177
177
  requirements:
178
178
  - - "~>"
179
179
  - !ruby/object:Gem::Version
180
- version: 1.1.0
180
+ version: 1.2.0
181
181
  - !ruby/object:Gem::Dependency
182
182
  name: thor
183
183
  requirement: !ruby/object:Gem::Requirement