relaton-cli 1.1.4 → 1.5.pre

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: f04dc5ef23a80c1aefda7338334d5b685d451c313080991179df87d85dc03e1f
4
- data.tar.gz: da8683ae592398c2d23ee47a7905d746c85d9ade95a2ce9158b9ff2863120537
3
+ metadata.gz: 06c38df62c297fd9442baf35d93d67c88ba4db3f5de065c9b60f6acd3ed21aa5
4
+ data.tar.gz: 6edcec27f8a15828986f1fbe7b15ed3974c64132b08f3ac0a8a8b6f88b32f794
5
5
  SHA512:
6
- metadata.gz: 84c4db993685042758436a34aef83026ecd56084acc299ff359993e3a28413ad8e1ca9f54f30f39ec7a41f4a0beba046cd702f18f2000093432ea0a5df6b35a0
7
- data.tar.gz: e0bd3dfcadd709c9e8e0627a4f27ac1ca475a896fe7efc62c8988d24b8ddeac25de1465d9dcd6f948de9f73f302d71a4376c24ce52af57b0ccaf6361d68a6de6
6
+ metadata.gz: 4f98489915c8424bc4240fe9ebeab0fd4cd9aaca450a42a44625b95a6ebc1dc33a1ab87e02963fbe2d6ed99e3bd11792aef54e994781e15ef63fbe963e8974a2
7
+ data.tar.gz: bfe7348c746b684a6091fab7b4669cb00392ebcc64bf120bf6cb72454bae151a816099f92fd6cb43605ed1e056b5d66ce5341813f359ced5691caad46c3d2be8
@@ -209,3 +209,11 @@ $ relaton yaml2html YAML [<stylesheet>] [<liquid-template-dir>]
209
209
 
210
210
  Render a Relaton YAML file (`filename.yaml`) as an HTML file. The `stylesheet` and `liquid-template-dir` directories are as for <<relaton-xml2html,relaton xml2html>>.
211
211
 
212
+ === relaton convert
213
+
214
+ [source,conxole]
215
+ ----
216
+ $ relaton convert XML -f FORMAT -o OUTPUT-FILE
217
+ ----
218
+
219
+ Convert a Relaton XML document into YAML, AsciiBib, or BibTex format. Allowed -f or --format options are yaml, asciibib, bibtex. If the option -o or --output is omitted then a new file will be created in the folder where the original file is, with the same name but another appropriated extension.
@@ -18,4 +18,3 @@ end
18
18
  require "relaton/cli"
19
19
 
20
20
  Relaton::Cli.start(ARGV)
21
-
@@ -42,9 +42,9 @@ module Relaton
42
42
 
43
43
  def to_xml(opts = {})
44
44
  options = { bibdata: true, date_format: :full }.merge(
45
- opts.select { |k, _v| k.is_a? Symbol },
45
+ opts.select { |k, _v| k.is_a? Symbol }
46
46
  )
47
- @bibitem.to_xml nil, **options
47
+ @bibitem.to_xml **options
48
48
  end
49
49
 
50
50
  def to_h
@@ -39,7 +39,7 @@ module Relaton
39
39
  file,
40
40
  style: style || File.join(File.dirname(__FILE__), "../../../templates/index-style.css"),
41
41
  template: template || File.join(File.dirname(__FILE__), "../../../templates/"),
42
- extension: "html",
42
+ extension: "html"
43
43
  ).to_html
44
44
  end
45
45
 
@@ -91,13 +91,6 @@ module Relaton
91
91
  end
92
92
 
93
93
  def write_to_file_collection(content, format)
94
- # if outdir && (content.is_a?(Relaton::Bibcollection))
95
- # FileUtils.mkdir_p(outdir)
96
- # content.items_flattened.each do |item|
97
- # collection = collection_filename(item.docidentifier_code)
98
- # write_to_a_file(item_output(item, format), collection)
99
- # end
100
- # end
101
94
  if outdir && content.is_a?(Relaton::Bibcollection)
102
95
  FileUtils.mkdir_p(outdir)
103
96
  content.items_flattened.each do |item|
@@ -45,7 +45,7 @@ module Relaton
45
45
  desc "yaml2xml YAML", "Convert Relaton YAML into Relaton Collection XML or separate files"
46
46
  option :extension, aliases: :x, default: "rxl", desc: "File extension of Relaton XML files, defaults to 'rxl'"
47
47
  option :prefix, aliases: :p, desc: "Filename prefix of individual Relaton XML files, defaults to empty"
48
- option :outdir, aliases: :o, desc: "Output to the specified directory with individual Relaton Bibdata XML files"
48
+ option :outdir, aliases: :o, desc: "Output to the specified directory with individual Relaton Bibdata XML files"
49
49
  option :require, aliases: :r, type: :array, desc: "Require LIBRARY prior to execution"
50
50
  option :overwrite, aliases: :f, type: :boolean, default: false, desc: "Overwrite the existing file"
51
51
 
@@ -53,7 +53,7 @@ module Relaton
53
53
  Relaton::Cli::YAMLConvertor.to_xml(filename, options)
54
54
  end
55
55
 
56
- desc "xml2yamlnew XML", "Convert Relaton XML into Relaton Bibdata / Bibcollection YAML (and separate files)"
56
+ desc "xml2yaml XML", "Convert Relaton XML into Relaton Bibdata / Bibcollection YAML (and separate files)"
57
57
  option :extension, aliases: :x, default: "yaml", desc: "File extension of Relaton YAML files, defaults to 'yaml'"
58
58
  option :prefix, aliases: :p, desc: "Filename prefix of Relaton XML files, defaults to empty"
59
59
  option :outdir, aliases: :o, desc: "Output to the specified directory with individual Relaton Bibdata YAML files"
@@ -82,6 +82,25 @@ module Relaton
82
82
  Relaton::Cli::YAMLConvertor.to_html(file, style, template)
83
83
  end
84
84
 
85
+ desc "convert XML", "Convert Relaton XML document"
86
+ option :format, aliases: :f, required: true, desc: "Output format (yaml, bibtex, asciibib)"
87
+ option :output, aliases: :o, desc: "Output to the specified file"
88
+
89
+ def convert(file)
90
+ item = Relaton::Cli.parse_xml Nokogiri::XML(File.read(file, encoding: "UTF-8"))
91
+ result = if /yaml|yml/.match?(options[:format])
92
+ item.to_hash.to_yaml
93
+ else item.send "to_#{options[:format]}"
94
+ end
95
+ ext = case options[:format]
96
+ when "bibtex" then "bib"
97
+ when "asciibib" then "adoc"
98
+ else options[:format]
99
+ end
100
+ output = options[:output] || file.sub(/(?<=\.)[^\.]+$/, ext)
101
+ File.write output, result, encoding: "UTF-8"
102
+ end
103
+
85
104
  private
86
105
 
87
106
  # @param code [String]
@@ -1,5 +1,5 @@
1
1
  module Relaton
2
2
  module Cli
3
- VERSION = "1.1.4".freeze
3
+ VERSION = "1.5.pre".freeze
4
4
  end
5
5
  end
@@ -9,23 +9,49 @@ 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
@@ -39,35 +65,23 @@ 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
81
  if content.has_key?("root")
68
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
86
  Relaton::Bibcollection.new(content["root"])
73
87
  end
@@ -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.5.pre"
38
38
  spec.add_runtime_dependency "thor"
39
39
  # spec.add_runtime_dependency 'byebug'
40
40
  end
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.4
4
+ version: 1.5.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-18 00:00:00.000000000 Z
11
+ date: 2020-10-07 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.5.pre
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.5.pre
181
181
  - !ruby/object:Gem::Dependency
182
182
  name: thor
183
183
  requirement: !ruby/object:Gem::Requirement
@@ -239,7 +239,7 @@ homepage: https://github.com/metanorma/relaton-cli
239
239
  licenses:
240
240
  - BSD-2-Clause
241
241
  metadata: {}
242
- post_install_message:
242
+ post_install_message:
243
243
  rdoc_options: []
244
244
  require_paths:
245
245
  - lib
@@ -250,12 +250,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
250
250
  version: 2.4.0
251
251
  required_rubygems_version: !ruby/object:Gem::Requirement
252
252
  requirements:
253
- - - ">="
253
+ - - ">"
254
254
  - !ruby/object:Gem::Version
255
- version: '0'
255
+ version: 1.3.1
256
256
  requirements: []
257
257
  rubygems_version: 3.0.6
258
- signing_key:
258
+ signing_key:
259
259
  specification_version: 4
260
260
  summary: Relaton Command-line Interface
261
261
  test_files: []