relaton-cli 0.3.6 → 0.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: '080ef2cfe08edfba51c903c9ce1d11bdf888257f'
4
- data.tar.gz: 338339afba81aee2d161fa677708a0cff46abe68
3
+ metadata.gz: 29ee3d1187d6023ead4d9f5cc04bd4bddb7e2931
4
+ data.tar.gz: bef6cd7d3d4c586f5843386bf2d1ca146cdd013f
5
5
  SHA512:
6
- metadata.gz: aa6ff088313b823a22ee62c19c0906f718af15f0202ae9785c2b9eaa63d2303c113e0158abe208daa0bb59c68bcd785318bcb253ec2ed9ec28866a08dc915723
7
- data.tar.gz: 1b03e8509251c6893c778fb134effc0221bdd0f3c103a5bc06b6064fb9dfd9c8fa0749383657e4a0a088854b6cb89aff7bcc3de14aba542bad2092953efc13da
6
+ metadata.gz: 78fa395ed259c408f7368170a53459229a6c21efae0f392362eefdb44b9c44a93fa740845bdd7bb87c2787d0bbfbf2500fcc607c7501a2b5f9e61b493ba390a2
7
+ data.tar.gz: 2594375e739b8d943d16d40079d776807a2bf622bdb46edec7dccabf9f3bf2d699574fec19a5314ef2cc80a0fece75c752d25faf0d148fe9d8626a8a0700ed5b
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- relaton-cli (0.3.6)
4
+ relaton-cli (0.3.7)
5
5
  liquid
6
6
  relaton (~> 0.5)
7
7
  thor
@@ -12,7 +12,7 @@ GEM
12
12
  addressable (2.7.0)
13
13
  public_suffix (>= 2.0.2, < 5.0)
14
14
  byebug (10.0.2)
15
- cnccs (0.1.3)
15
+ cnccs (0.1.4)
16
16
  coderay (1.1.2)
17
17
  debase (0.2.3)
18
18
  debase-ruby_core_source (>= 0.10.2)
@@ -21,10 +21,10 @@ GEM
21
21
  docile (1.3.2)
22
22
  equivalent-xml (0.6.0)
23
23
  nokogiri (>= 1.4.3)
24
- faraday (0.15.4)
24
+ faraday (0.17.0)
25
25
  multipart-post (>= 1.2, < 3)
26
- gb-agencies (0.0.5)
27
- isoics (0.1.7)
26
+ gb-agencies (0.0.6)
27
+ isoics (0.1.8)
28
28
  json (2.2.0)
29
29
  liquid (4.0.3)
30
30
  method_source (0.9.2)
@@ -38,7 +38,7 @@ GEM
38
38
  method_source (~> 0.9.0)
39
39
  public_suffix (4.0.1)
40
40
  rake (12.3.3)
41
- relaton (0.5.5)
41
+ relaton (0.5.6)
42
42
  relaton-gb (~> 0.6.0)
43
43
  relaton-iec (~> 0.4.0)
44
44
  relaton-ietf (~> 0.6.0)
@@ -46,31 +46,31 @@ GEM
46
46
  relaton-itu (~> 0.3.0)
47
47
  relaton-nist (~> 0.3.0)
48
48
  relaton-ogc (~> 0.1.0)
49
- relaton-bib (0.3.8)
49
+ relaton-bib (0.3.9)
50
50
  addressable
51
51
  nokogiri
52
- relaton-gb (0.6.4)
52
+ relaton-gb (0.6.5)
53
53
  cnccs (~> 0.1.1)
54
54
  gb-agencies (~> 0.0.1)
55
55
  relaton-iso-bib (~> 0.3.0)
56
- relaton-iec (0.4.6)
56
+ relaton-iec (0.4.8)
57
57
  addressable
58
58
  relaton-iso-bib (~> 0.3.0)
59
- relaton-ietf (0.6.5)
59
+ relaton-ietf (0.6.6)
60
60
  relaton-bib (~> 0.3.0)
61
- relaton-iso (0.6.6)
61
+ relaton-iso (0.6.7)
62
62
  relaton-iec (~> 0.4.0)
63
63
  relaton-iso-bib (~> 0.3.0)
64
- relaton-iso-bib (0.3.7)
64
+ relaton-iso-bib (0.3.9)
65
65
  isoics (~> 0.1.6)
66
66
  relaton-bib (~> 0.3.0)
67
67
  ruby_deep_clone (~> 0.8.0)
68
- relaton-itu (0.3.4)
68
+ relaton-itu (0.3.5)
69
69
  relaton-iso-bib (~> 0.3.0)
70
- relaton-nist (0.3.5)
70
+ relaton-nist (0.3.6)
71
71
  relaton-bib (~> 0.3.0)
72
72
  rubyzip
73
- relaton-ogc (0.1.1)
73
+ relaton-ogc (0.1.2)
74
74
  faraday
75
75
  relaton-iso-bib (~> 0.3.0)
76
76
  rspec (3.8.0)
data/docs/README.adoc CHANGED
@@ -17,7 +17,7 @@ The following executables are included:
17
17
 
18
18
  [source,console]
19
19
  ----
20
- $ relaton concatenate Source-Directory Collection-File -t TITLE -g ORGANIZATION
20
+ $ relaton concatenate Source-Directory Collection-File -t TITLE -g ORGANIZATION -n
21
21
  ----
22
22
 
23
23
  Iterates through all the Relaton YAML files in `Source-Directory`, and converts them in-place into Relaton XML.
@@ -31,16 +31,20 @@ file), a link to that file is inserted.
31
31
  If the `TITLE` or `ORGANIZATION` options are given, they are added to the `Collection-File` output as the
32
32
  title and author of the `relaton-collection` document.
33
33
 
34
+ If the `-n` or `--new` option given then use the new Relaton YAML format https://github.com/relaton/relaton-bib/blob/master/docs/hash.adoc.
35
+
34
36
  === relaton split
35
37
 
36
38
  [source,console]
37
39
  ----
38
- $ relaton split Collection-File Relaton-File-Directory -x rxl
40
+ $ relaton split Collection-File Relaton-File-Directory -x rxl -n
39
41
  ----
40
42
 
41
43
  Splits a `Relaton-Collection-File` into multiple files in the `Relaton-File-Directory`, and it also
42
44
  suports an addional `extension` options to use different extension.
43
45
 
46
+ If the `-n` or `--new` option given then use the new Relaton YAML format https://github.com/relaton/relaton-bib/blob/master/docs/hash.adoc.
47
+
44
48
  === relaton fetch
45
49
 
46
50
  [source,console]
data/lib/relaton-cli.rb CHANGED
@@ -3,4 +3,3 @@ require_relative "relaton/bibcollectionnew"
3
3
  require_relative "relaton/bibdata"
4
4
  require_relative "relaton/element_finder"
5
5
  require_relative "relaton/xml_document"
6
-
@@ -19,9 +19,8 @@ module Relaton
19
19
  value = options[k] || options[k.to_s]
20
20
  self.send("#{k}=", value)
21
21
  end
22
- self.items = (self.items || []).inject([]) do |acc,item|
23
- acc << if item.is_a?(::Relaton::Bibcollection) ||
24
- item.is_a?(::Relaton::Bibdata)
22
+ self.items = (self.items || []).inject([]) do |acc, item|
23
+ acc << if item.is_a?(::Relaton::Bibcollection) || item.is_a?(::Relaton::Bibdata)
25
24
  item
26
25
  else
27
26
  new_bib_item_class(item)
@@ -50,9 +49,9 @@ module Relaton
50
49
 
51
50
  def new_bib_item_class(options)
52
51
  if options["items"]
53
- ::Relaton::Bibcollection.new(options)
52
+ options[:new] ? ::Relaton::BibcollectionNew(options) : ::Relaton::Bibcollection.new(options)
54
53
  else
55
- ::Relaton::Bibdata.new(options)
54
+ options[:new] ? ::Relaton::BibdataNew(options) : ::Relaton::Bibdata.new(options)
56
55
  end
57
56
  end
58
57
 
@@ -20,8 +20,7 @@ module Relaton
20
20
  send("#{k}=", value)
21
21
  end
22
22
  self.items = (items || []).reduce([]) do |acc, item|
23
- acc << if item.is_a?(::Relaton::BibcollectionNew) ||
24
- item.class >= ::RelatonBib::BibliographicItem
23
+ acc << if item.is_a?(::Relaton::BibcollectionNew) || item.is_a?(::Relaton::BibdataNew)
25
24
  item
26
25
  else new_bib_item_class(item)
27
26
  end
@@ -39,7 +38,7 @@ module Relaton
39
38
 
40
39
  items = find_xpath("./relaton-collection/relation", source)&.map do |item|
41
40
  bibdata = find("./bibdata", item)
42
- klass = bibdata ? Bibdata : BibcollectionNew
41
+ klass = bibdata ? BibdataNew : BibcollectionNew
43
42
  klass.from_xml(bibdata || item)
44
43
  end
45
44
 
@@ -47,10 +46,10 @@ module Relaton
47
46
  end
48
47
 
49
48
  def new_bib_item_class(options)
50
- if options["items"]
49
+ if options.is_a?(Hash) && options["items"]
51
50
  ::Relaton::BibcollectionNew.new(options)
52
51
  else
53
- ::RelatonBib::BibliographicItem.new(options)
52
+ ::Relaton::BibdataNew.new(options)
54
53
  end
55
54
  end
56
55
 
@@ -0,0 +1,62 @@
1
+ require "date"
2
+
3
+ module Relaton
4
+ class BibdataNew
5
+ def initialize(bibitem)
6
+ @bibitem = bibitem
7
+ end
8
+
9
+ def method_missing(method, *args)
10
+ %r{(?<m>\w+)=$} =~ method
11
+ return unless m && %w[xml pdf doc html rxl txt].include?(m)
12
+
13
+ uri = @bibitem.link.detect { |u| u.type == method }
14
+ if uri
15
+ uri.content = args[0]
16
+ else
17
+ @bibitem.link << RelatonBib::TypedUri.new(type: m, content: args[0])
18
+ end
19
+ end
20
+
21
+ def docidentifier
22
+ @bibitem.docidentifier.first&.id
23
+ end
24
+
25
+ # From http://gavinmiller.io/2016/creating-a-secure-sanitization-function/
26
+ # FILENAME_BAD_CHARS = [ '/', '\\', '?', '%', '*', ':', '|', '"', '<', '>', '.', ' ' ]
27
+
28
+ # def docidentifier_code
29
+ # return "" if docidentifier.nil?
30
+ # a = FILENAME_BAD_CHARS.inject(docidentifier.downcase) do |result, bad_char|
31
+ # result.gsub(bad_char, '-')
32
+ # end
33
+ # end
34
+
35
+ # DOC_NUMBER_REGEX = /([\w\/]+)\s+(\d+):?(\d*)/
36
+ # def doc_number
37
+ # docidentifier&.match(DOC_NUMBER_REGEX) ? $2.to_i : 999999
38
+ # end
39
+
40
+ def self.from_xml(source)
41
+ new(Relaton::Cli.parse_xml(source))
42
+ end
43
+
44
+ def to_xml(opts = {})
45
+ options = { bibdata: true, date_format: :full }.merge opts.select { |k,v| k.is_a? Symbol }
46
+ @bibitem.to_xml nil, **options
47
+
48
+ # #datetype = stage&.casecmp("published") == 0 ? "published" : "circulated"
49
+
50
+ # ret = ref ? "<bibitem id= '#{ref}' type='#{doctype}'>\n" : "<bibdata type='#{doctype}'>\n"
51
+ # ret += "<fetched>#{Date.today.to_s}</fetched>\n"
52
+ end
53
+
54
+ def to_h
55
+ @bibitem.to_hash
56
+ end
57
+
58
+ def to_yaml
59
+ to_h.to_yaml
60
+ end
61
+ end
62
+ end
data/lib/relaton/cli.rb CHANGED
@@ -19,5 +19,24 @@ module Relaton
19
19
  def self.relaton
20
20
  @relaton ||= Relaton::Db.new("#{Dir.home}/.relaton/cache", nil)
21
21
  end
22
+
23
+ # @param content [Nokogiri::XML::Document]
24
+ # @return [RelatonBib::BibliographicItem, RelatonIsoBib::IsoBibliongraphicItem]
25
+ def self.parse_xml(doc)
26
+ if (processor = Relaton::Registry.instance.by_type(Relaton::Cli.doctype(doc)))
27
+ processor.from_xml(doc.to_s)
28
+ else
29
+ RelatonBib::XMLParser.from_xml(doc.to_s)
30
+ end
31
+ end
32
+
33
+ # @param content [Nokogiri::XML::Document] Document
34
+ # @return [String] Type prefix
35
+ def self.doctype(doc)
36
+ docid = doc.at "docidentifier"
37
+ return docid[:type] if docid && docid[:type]
38
+
39
+ docid&.text&.match(/^\w+/)&.to_s
40
+ end
22
41
  end
23
42
  end
@@ -1,5 +1,6 @@
1
1
  require "fileutils"
2
2
  require "relaton/bibdata"
3
+ require "relaton/bibdatanew"
3
4
  require "relaton/bibcollection"
4
5
  require "relaton/bibcollectionnew"
5
6
  require "relaton/cli/xml_to_html_renderer"
@@ -107,9 +108,9 @@ module Relaton
107
108
  def extract_docid(item)
108
109
  @default_filelabel += 1
109
110
  item.docidentifier.nil? and return @default_filelabel.to_s
110
- item.docidentifier.is_a?(Array) or return @default_filelabel.to_s
111
+ # item.docidentifier.is_a?(Array) or return @default_filelabel.to_s
111
112
  item.docidentifier.empty? and return @default_filelabel.to_s
112
- docidentifier_code(item.docidentifier[0].id)
113
+ docidentifier_code(item.docidentifier)
113
114
  end
114
115
 
115
116
  # From http://gavinmiller.io/2016/creating-a-secure-sanitization-function/
@@ -28,6 +28,7 @@ module Relaton
28
28
  desc "concatenate SOURCE-DIR COLLECTION-FILE", "Concatenate entries in DIRECTORY (containing Relaton-XML or YAML) into a Relaton Collection"
29
29
  option :title, aliases: :t, desc: "Title of resulting Relaton collection"
30
30
  option :organization, aliases: :g, desc: "Organization owner of Relaton collection"
31
+ option :new, aliases: :n, type: :boolean, desc: "Use the new Relaton YAML format"
31
32
 
32
33
  def concatenate(source_dir, outfile)
33
34
  Relaton::Cli::RelatonFile.concatenate(source_dir, outfile, options)
@@ -35,6 +36,7 @@ module Relaton
35
36
 
36
37
  desc "split Relaton-Collection-File Relaton-XML-Directory", "Split a Relaton Collection into multiple files"
37
38
  option :extension, aliases: :x, desc: "File extension of Relaton XML files, defaults to 'rxl'"
39
+ option :new, aliases: :n, type: :boolean, desc: "Use the new Relaton YAML format"
38
40
 
39
41
  def split(source, outdir)
40
42
  Relaton::Cli::RelatonFile.split(source, outdir, options)
@@ -16,7 +16,7 @@ module Relaton
16
16
  end
17
17
 
18
18
  def concatenate
19
- write_to_file(bibcollection.to_xml)
19
+ write_to_file(bibcollection.to_xml(options))
20
20
  end
21
21
 
22
22
  def split
@@ -81,7 +81,7 @@ module Relaton
81
81
  attr_reader :source, :options, :outdir, :outfile
82
82
 
83
83
  def bibcollection
84
- ::Relaton::Bibcollection.new(
84
+ bib_klass.new(
85
85
  title: options[:title],
86
86
  items: concatenate_files,
87
87
  doctype: options[:doctype],
@@ -103,8 +103,7 @@ module Relaton
103
103
  end
104
104
 
105
105
  def relaton_collection
106
- @relaton_collection ||=
107
- Relaton::Bibcollection.from_xml(nokogiri_document(nil, source))
106
+ @relaton_collection ||= bib_klass.from_xml(nokogiri_document(nil, source))
108
107
  end
109
108
 
110
109
  def extract_and_write_to_files
@@ -172,16 +171,30 @@ module Relaton
172
171
 
173
172
  def bibdata_instance(document, file)
174
173
  document = clean_nokogiri_document(document)
175
- bibdata = Relaton::Bibdata.from_xml(document.root)
174
+ bibdata = if options[:new]
175
+ Relaton::BibdataNew.from_xml document.root
176
+ else
177
+ Relaton::Bibdata.from_xml(document.root)
178
+ end
176
179
  build_bibdata_relaton(bibdata, file)
177
180
 
178
181
  bibdata
179
182
  end
180
183
 
184
+ # @param content [Nokogiri::XML::Document]
185
+ # @return [Hash]
186
+ def parse_doc(doc)
187
+ if (processor = Relaton::Registry.instance.by_type(doctype(doc)))
188
+ processor.from_xml(doc.to_s).to_hash
189
+ else
190
+ RelatonBib::XMLParser.from_xml(doc.to_s).to_hash
191
+ end
192
+ end
193
+
181
194
  def build_bibdata_relaton(bibdata, file)
182
195
  ["xml", "pdf", "doc", "html", "rxl", "txt"].each do |type|
183
196
  file = Pathname.new(file).sub_ext(".#{type}")
184
- bibdata.send("#{type}=", file) if File.file?(file)
197
+ bibdata.send("#{type}=", file.to_s) if File.file?(file)
185
198
  end
186
199
  end
187
200
 
@@ -203,8 +216,9 @@ module Relaton
203
216
  end
204
217
 
205
218
  def convert_yamls_to_xml
219
+ klass = options[:new] ? YAMLConvertorNew : YAMLConvertor
206
220
  select_files_with("yaml").map do |file|
207
- { file: file, content: YAMLConvertor.to_xml(file, write: false) }
221
+ { file: file, content: klass.to_xml(file, write: false) }
208
222
  end
209
223
  end
210
224
 
@@ -233,7 +247,7 @@ module Relaton
233
247
  end
234
248
 
235
249
  def build_filename(file, identifier = nil, ext = "rxl")
236
- identifier ||= Pathname.new(File.basename(file, ".xml")).to_s
250
+ identifier ||= Pathname.new(File.basename(file.to_s, ".xml")).to_s
237
251
  [sanitize_string(identifier), options[:extension] || ext].join(".")
238
252
  end
239
253
 
@@ -246,6 +260,10 @@ module Relaton
246
260
  bad_chars = ["/", "\\", "?", "%", "*", ":", "|", '"', "<", ">", ".", " "]
247
261
  bad_chars.inject(string.downcase) { |res, char| res.gsub(char, "-") }
248
262
  end
263
+
264
+ def bib_klass
265
+ @bib_klass ||= options[:new] ? Relaton::BibcollectionNew : Relaton::Bibcollection
266
+ end
249
267
  end
250
268
  end
251
269
  end
@@ -1,5 +1,5 @@
1
1
  module Relaton
2
2
  module Cli
3
- VERSION = "0.3.6".freeze
3
+ VERSION = "0.3.7".freeze
4
4
  end
5
5
  end
@@ -34,7 +34,7 @@ module Relaton
34
34
  def convert_content(content)
35
35
  if content.root.name == "bibdata"
36
36
  # Bibdata.from_xml(content.to_s)
37
- convert_bibdata content
37
+ Relaton::Cli.parse_xml(content).to_hash
38
38
  else
39
39
  # Bibcollection.from_xml(content)
40
40
  title = content.at("relaton-collection/title").text
@@ -42,32 +42,13 @@ module Relaton
42
42
  collection = { "root" => { "title" => title, "author" => author } }
43
43
 
44
44
  collection["root"]["items"] = content.xpath("//bibdata").map do |bib|
45
- convert_bibdata bib
45
+ Relaton::Cli.parse_xml(bib).to_hash
46
46
  end
47
47
 
48
48
  collection
49
49
  end
50
50
  end
51
51
 
52
- # @param content [Nokogiri::XML::Document]
53
- # @return [Hash]
54
- def convert_bibdata(doc)
55
- if (processor = Relaton::Registry.instance.by_type(doctype(doc)))
56
- processor.from_xml(doc.to_s).to_hash
57
- else
58
- RelatonBib::XMLParser.from_xml(doc.to_s).to_hash
59
- end
60
- end
61
-
62
- # @param content [Nokogiri::XML::Document]
63
- # @return [String]
64
- def doctype(doc)
65
- docid = doc.at "//docidentifier"
66
- return docid[:type] if docid && docid[:type]
67
-
68
- docid.text.match(/^\w+/).to_s
69
- end
70
-
71
52
  def file_content
72
53
  Nokogiri::XML(File.read(file, encoding: "utf-8")).remove_namespaces!
73
54
  end
@@ -9,7 +9,7 @@ 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(nil, 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
 
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: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-27 00:00:00.000000000 Z
11
+ date: 2019-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -222,6 +222,7 @@ files:
222
222
  - lib/relaton/bibcollection.rb
223
223
  - lib/relaton/bibcollectionnew.rb
224
224
  - lib/relaton/bibdata.rb
225
+ - lib/relaton/bibdatanew.rb
225
226
  - lib/relaton/cli.rb
226
227
  - lib/relaton/cli/_document.liquid
227
228
  - lib/relaton/cli/_index.liquid