relaton-cli 0.5.0 → 0.6.0
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 +4 -4
- data/.github/workflows/macos.yml +9 -4
- data/.github/workflows/ubuntu.yml +9 -4
- data/.github/workflows/windows.yml +9 -4
- data/.rspec +1 -0
- data/docs/README.adoc +10 -272
- data/lib/relaton/bibcollection.rb +11 -13
- data/lib/relaton/bibdata.rb +55 -97
- data/lib/relaton/cli/base_convertor.rb +13 -17
- data/lib/relaton/cli/command.rb +11 -35
- data/lib/relaton/cli/relaton_file.rb +6 -25
- data/lib/relaton/cli/version.rb +1 -1
- data/lib/relaton/cli/xml_convertor.rb +13 -1
- data/lib/relaton/cli/xml_to_html_renderer.rb +1 -1
- data/lib/relaton/cli/yaml_convertor.rb +30 -4
- data/lib/relaton/cli.rb +13 -2
- data/lib/relaton-cli.rb +0 -1
- data/relaton-cli.gemspec +1 -1
- data/templates/_document.liquid +2 -2
- metadata +5 -9
- data/lib/relaton/bibcollectionnew.rb +0 -116
- data/lib/relaton/bibdatanew.rb +0 -62
- data/lib/relaton/cli/xml_convertor_new.rb +0 -57
- data/lib/relaton/cli/yaml_convertor_new.rb +0 -85
- data/lib/relaton/xml_document.rb +0 -92
@@ -1,8 +1,6 @@
|
|
1
1
|
require "fileutils"
|
2
2
|
require "relaton/bibdata"
|
3
|
-
require "relaton/bibdatanew"
|
4
3
|
require "relaton/bibcollection"
|
5
|
-
require "relaton/bibcollectionnew"
|
6
4
|
require "relaton/cli/xml_to_html_renderer"
|
7
5
|
|
8
6
|
module Relaton
|
@@ -47,10 +45,6 @@ module Relaton
|
|
47
45
|
|
48
46
|
attr_reader :file, :outdir, :options, :writable, :overwrite
|
49
47
|
|
50
|
-
def default_ext
|
51
|
-
raise "Override this method"
|
52
|
-
end
|
53
|
-
|
54
48
|
def convert_to_html
|
55
49
|
Relaton::Cli::XmlToHtmlRenderer.render(
|
56
50
|
xml_content(file),
|
@@ -59,9 +53,9 @@ module Relaton
|
|
59
53
|
)
|
60
54
|
end
|
61
55
|
|
62
|
-
def xml_content(file)
|
63
|
-
|
64
|
-
end
|
56
|
+
# def xml_content(file)
|
57
|
+
# File.read(file, encoding: "utf-8")
|
58
|
+
# end
|
65
59
|
|
66
60
|
def install_dependencies(dependencies)
|
67
61
|
dependencies.each { |dependency| require(dependency) }
|
@@ -84,19 +78,21 @@ module Relaton
|
|
84
78
|
outfile ||= Pathname.new(file).sub_ext(extension).to_s
|
85
79
|
|
86
80
|
if !File.exists?(outfile) || overwrite
|
87
|
-
File.open(outfile, "w:utf-8")
|
81
|
+
File.open(outfile, "w:utf-8") do |file|
|
82
|
+
file.write(content)
|
83
|
+
end
|
88
84
|
end
|
89
85
|
end
|
90
86
|
|
91
87
|
def write_to_file_collection(content, format)
|
88
|
+
# if outdir && (content.is_a?(Relaton::Bibcollection))
|
89
|
+
# FileUtils.mkdir_p(outdir)
|
90
|
+
# content.items_flattened.each do |item|
|
91
|
+
# collection = collection_filename(item.docidentifier_code)
|
92
|
+
# write_to_a_file(item_output(item, format), collection)
|
93
|
+
# end
|
94
|
+
# end
|
92
95
|
if outdir && (content.is_a?(Relaton::Bibcollection))
|
93
|
-
FileUtils.mkdir_p(outdir)
|
94
|
-
content.items_flattened.each do |item|
|
95
|
-
collection = collection_filename(item.docidentifier_code)
|
96
|
-
write_to_a_file(item_output(item, format), collection)
|
97
|
-
end
|
98
|
-
end
|
99
|
-
if outdir && (content.is_a?(Relaton::BibcollectionNew))
|
100
96
|
FileUtils.mkdir_p(outdir)
|
101
97
|
content.items_flattened.each do |item|
|
102
98
|
collection = collection_filename(extract_docid(item))
|
data/lib/relaton/cli/command.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
require "relaton/cli/relaton_file"
|
2
2
|
require "relaton/cli/xml_convertor"
|
3
|
-
require "relaton/cli/xml_convertor_new"
|
4
3
|
require "relaton/cli/yaml_convertor"
|
5
|
-
require "relaton/cli/yaml_convertor_new"
|
6
4
|
require "fcntl"
|
7
5
|
|
8
6
|
module Relaton
|
@@ -13,7 +11,7 @@ module Relaton
|
|
13
11
|
option :year, aliases: :y, type: :numeric, desc: "Year the standard was published"
|
14
12
|
|
15
13
|
def fetch(code)
|
16
|
-
Relaton
|
14
|
+
Relaton.db
|
17
15
|
io = IO.new(STDOUT.fcntl(::Fcntl::F_DUPFD), mode: 'w:UTF-8')
|
18
16
|
io.puts(fetch_document(code, options) || supported_type_message)
|
19
17
|
end
|
@@ -54,18 +52,7 @@ module Relaton
|
|
54
52
|
Relaton::Cli::YAMLConvertor.to_xml(filename, options)
|
55
53
|
end
|
56
54
|
|
57
|
-
desc "
|
58
|
-
option :extension, aliases: :x, default: "rxl", desc: "File extension of Relaton XML files, defaults to 'rxl'"
|
59
|
-
option :prefix, aliases: :p, desc: "Filename prefix of individual Relaton XML files, defaults to empty"
|
60
|
-
option :outdir, aliases: :o, desc: "Output to the specified directory with individual Relaton Bibdata XML files"
|
61
|
-
option :require, aliases: :r, type: :array, desc: "Require LIBRARY prior to execution"
|
62
|
-
option :overwrite, aliases: :f, type: :boolean, default: false, desc: "Overwrite the existing file"
|
63
|
-
|
64
|
-
def yaml2xmlnew(filename)
|
65
|
-
Relaton::Cli::YAMLConvertorNew.to_xml(filename, options)
|
66
|
-
end
|
67
|
-
|
68
|
-
desc "xml2yaml XML", "Convert Relaton XML into Relaton Bibdata / Bibcollection YAML (and separate files)"
|
55
|
+
desc "xml2yamlnew XML", "Convert Relaton XML into Relaton Bibdata / Bibcollection YAML (and separate files)"
|
69
56
|
option :extension, aliases: :x, default: "yaml", desc: "File extension of Relaton YAML files, defaults to 'yaml'"
|
70
57
|
option :prefix, aliases: :p, desc: "Filename prefix of Relaton XML files, defaults to empty"
|
71
58
|
option :outdir, aliases: :o, desc: "Output to the specified directory with individual Relaton Bibdata YAML files"
|
@@ -76,17 +63,6 @@ module Relaton
|
|
76
63
|
Relaton::Cli::XMLConvertor.to_yaml(filename, options)
|
77
64
|
end
|
78
65
|
|
79
|
-
desc "xml2yamlnew XML", "Convert Relaton XML into Relaton Bibdata / Bibcollection YAML (and separate files)"
|
80
|
-
option :extension, aliases: :x, default: "yaml", desc: "File extension of Relaton YAML files, defaults to 'yaml'"
|
81
|
-
option :prefix, aliases: :p, desc: "Filename prefix of Relaton XML files, defaults to empty"
|
82
|
-
option :outdir, aliases: :o, desc: "Output to the specified directory with individual Relaton Bibdata YAML files"
|
83
|
-
option :require, aliases: :r, type: :array, desc: "Require LIBRARY prior to execution"
|
84
|
-
option :overwrite, aliases: :f, type: :boolean, default: false, desc: "Overwrite the existing file"
|
85
|
-
|
86
|
-
def xml2yamlnew(filename)
|
87
|
-
Relaton::Cli::XMLConvertorNew.to_yaml(filename, options)
|
88
|
-
end
|
89
|
-
|
90
66
|
desc "xml2html RELATON-INDEX-XML", "Convert Relaton Collection XML into HTML"
|
91
67
|
option :stylesheet, aliases: :s, desc: "Stylesheet file path for rendering HTML index"
|
92
68
|
option :templatedir, aliases: :t, desc: "Liquid template directory for rendering Relaton items and collection"
|
@@ -96,22 +72,22 @@ module Relaton
|
|
96
72
|
Relaton::Cli::XMLConvertor.to_html(file, style, template)
|
97
73
|
end
|
98
74
|
|
99
|
-
desc "yaml2html RELATON-INDEX-YAML", "Concatenate Relaton Collection YAML into HTML"
|
100
|
-
option :stylesheet, aliases: :s, desc: "Stylesheet file path for rendering HTML index"
|
101
|
-
option :templatedir, aliases: :t, desc: "Liquid template directory for rendering Relaton items and collection"
|
102
|
-
option :overwrite, aliases: :f, type: :boolean, default: false, desc: "Overwrite the existing file"
|
75
|
+
# desc "yaml2html RELATON-INDEX-YAML", "Concatenate Relaton Collection YAML into HTML"
|
76
|
+
# option :stylesheet, aliases: :s, desc: "Stylesheet file path for rendering HTML index"
|
77
|
+
# option :templatedir, aliases: :t, desc: "Liquid template directory for rendering Relaton items and collection"
|
78
|
+
# option :overwrite, aliases: :f, type: :boolean, default: false, desc: "Overwrite the existing file"
|
103
79
|
|
104
|
-
def yaml2html(file, style = nil, template = nil)
|
105
|
-
|
106
|
-
end
|
80
|
+
# def yaml2html(file, style = nil, template = nil)
|
81
|
+
# Relaton::Cli::YAMLConvertor.to_html(file, style, template)
|
82
|
+
# end
|
107
83
|
|
108
84
|
desc "yaml2htmlnew RELATON-INDEX-YAML", "Concatenate Relaton Collection YAML into HTML"
|
109
85
|
option :stylesheet, aliases: :s, desc: "Stylesheet file path for rendering HTML index"
|
110
86
|
option :templatedir, aliases: :t, desc: "Liquid template directory for rendering Relaton items and collection"
|
111
87
|
option :overwrite, aliases: :f, type: :boolean, default: false, desc: "Overwrite the existing file"
|
112
88
|
|
113
|
-
def
|
114
|
-
Relaton::Cli::
|
89
|
+
def yaml2html(file, style = nil, template = nil)
|
90
|
+
Relaton::Cli::YAMLConvertor.to_html(file, style, template)
|
115
91
|
end
|
116
92
|
|
117
93
|
private
|
@@ -81,7 +81,7 @@ module Relaton
|
|
81
81
|
attr_reader :source, :options, :outdir, :outfile
|
82
82
|
|
83
83
|
def bibcollection
|
84
|
-
|
84
|
+
Bibcollection.new(
|
85
85
|
title: options[:title],
|
86
86
|
items: concatenate_files,
|
87
87
|
doctype: options[:doctype],
|
@@ -103,7 +103,7 @@ module Relaton
|
|
103
103
|
end
|
104
104
|
|
105
105
|
def relaton_collection
|
106
|
-
@relaton_collection ||=
|
106
|
+
@relaton_collection ||= Bibcollection.from_xml(nokogiri_document(nil, source))
|
107
107
|
end
|
108
108
|
|
109
109
|
def extract_and_write_to_files
|
@@ -125,7 +125,7 @@ module Relaton
|
|
125
125
|
bib.remove_namespaces!
|
126
126
|
bib.root.add_namespace(nil, "xmlns")
|
127
127
|
|
128
|
-
bibdata = Relaton::
|
128
|
+
bibdata = Relaton::Bibdata.from_xml(bib.root)
|
129
129
|
build_bibdata_relaton(bibdata, file)
|
130
130
|
|
131
131
|
write_to_file(bibdata.send(output_type), outdir, build_filename(file))
|
@@ -175,33 +175,19 @@ module Relaton
|
|
175
175
|
when "yml", "yaml"
|
176
176
|
:to_yaml
|
177
177
|
else
|
178
|
-
puts "
|
178
|
+
puts "[relaton-cli] the given extension of '#{ext}' is not supported."
|
179
179
|
:to_xml
|
180
180
|
end
|
181
181
|
end
|
182
182
|
|
183
183
|
def bibdata_instance(document, file)
|
184
184
|
document = clean_nokogiri_document(document)
|
185
|
-
bibdata =
|
186
|
-
Relaton::BibdataNew.from_xml document.root
|
187
|
-
else
|
188
|
-
Relaton::Bibdata.from_xml(document.root)
|
189
|
-
end
|
185
|
+
bibdata = Relaton::Bibdata.from_xml document.root
|
190
186
|
build_bibdata_relaton(bibdata, file)
|
191
187
|
|
192
188
|
bibdata
|
193
189
|
end
|
194
190
|
|
195
|
-
# @param content [Nokogiri::XML::Document]
|
196
|
-
# @return [Hash]
|
197
|
-
def parse_doc(doc)
|
198
|
-
if (processor = Relaton::Registry.instance.by_type(doctype(doc)))
|
199
|
-
processor.from_xml(doc.to_s).to_hash
|
200
|
-
else
|
201
|
-
RelatonBib::XMLParser.from_xml(doc.to_s).to_hash
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
191
|
def build_bibdata_relaton(bibdata, file)
|
206
192
|
["xml", "pdf", "doc", "html", "rxl", "txt"].each do |type|
|
207
193
|
file = Pathname.new(file).sub_ext(".#{type}")
|
@@ -227,9 +213,8 @@ module Relaton
|
|
227
213
|
end
|
228
214
|
|
229
215
|
def convert_yamls_to_xml
|
230
|
-
klass = options[:new] ? YAMLConvertorNew : YAMLConvertor
|
231
216
|
select_files_with("yaml").map do |file|
|
232
|
-
{ file: file, content:
|
217
|
+
{ file: file, content: YAMLConvertor.to_xml(file, write: false) }
|
233
218
|
end
|
234
219
|
end
|
235
220
|
|
@@ -271,10 +256,6 @@ module Relaton
|
|
271
256
|
bad_chars = ["/", "\\", "?", "%", "*", ":", "|", '"', "<", ">", ".", " "]
|
272
257
|
bad_chars.inject(string.downcase) { |res, char| res.gsub(char, "-") }
|
273
258
|
end
|
274
|
-
|
275
|
-
def bib_klass
|
276
|
-
@bib_klass ||= options[:new] ? Relaton::BibcollectionNew : Relaton::Bibcollection
|
277
|
-
end
|
278
259
|
end
|
279
260
|
end
|
280
261
|
end
|
data/lib/relaton/cli/version.rb
CHANGED
@@ -29,11 +29,23 @@ module Relaton
|
|
29
29
|
"yaml"
|
30
30
|
end
|
31
31
|
|
32
|
+
# @param content [Nokogiri::XML::Document]
|
33
|
+
# @return [Hash]
|
32
34
|
def convert_content(content)
|
33
35
|
if content.root.name == "bibdata"
|
34
|
-
Bibdata.from_xml(content
|
36
|
+
Bibdata.from_xml(content)
|
37
|
+
# Relaton::Cli.parse_xml(content).to_hash
|
35
38
|
else
|
36
39
|
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
|
37
49
|
end
|
38
50
|
end
|
39
51
|
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require "yaml"
|
2
2
|
require "relaton/cli/base_convertor"
|
3
|
+
require "relaton_bib"
|
3
4
|
|
4
5
|
module Relaton
|
5
6
|
module Cli
|
@@ -8,7 +9,7 @@ module Relaton
|
|
8
9
|
if writable
|
9
10
|
convert_and_write(file_content, :to_xml)
|
10
11
|
else
|
11
|
-
convert_content(file_content).to_xml
|
12
|
+
convert_content(file_content).to_xml(date_format: :full, bibdata: true)
|
12
13
|
end
|
13
14
|
end
|
14
15
|
|
@@ -34,21 +35,46 @@ module Relaton
|
|
34
35
|
end
|
35
36
|
|
36
37
|
def file_content
|
37
|
-
YAML.load_file(file)
|
38
|
+
date_to_string(YAML.load_file(file))
|
39
|
+
end
|
40
|
+
|
41
|
+
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
|
38
47
|
end
|
39
48
|
|
40
49
|
def convert_single_file(content)
|
41
|
-
Relaton::
|
50
|
+
if (processor = Relaton::Registry.instance.by_type(doctype(content["docid"])))
|
51
|
+
processor.hash_to_bib content
|
52
|
+
else
|
53
|
+
RelatonBib::BibliographicItem.new(RelatonBib::HashConverter::hash_to_bib(content))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
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
|
42
64
|
end
|
43
65
|
|
44
66
|
def convert_collection(content)
|
45
67
|
if content.has_key?("root")
|
68
|
+
content["root"]["items"] = content["root"]["items"].map do |i|
|
69
|
+
# RelatonBib::HashConverter::hash_to_bib(i)
|
70
|
+
convert_single_file(i)
|
71
|
+
end
|
46
72
|
Relaton::Bibcollection.new(content["root"])
|
47
73
|
end
|
48
74
|
end
|
49
75
|
|
50
76
|
def xml_content(_raw_file)
|
51
|
-
convert_content(file_content).to_xml
|
77
|
+
convert_content(file_content).to_xml(date_format: :full, bibdata: true)
|
52
78
|
end
|
53
79
|
|
54
80
|
def convert_content(content)
|
data/lib/relaton/cli.rb
CHANGED
@@ -1,10 +1,21 @@
|
|
1
1
|
require "thor"
|
2
2
|
require "relaton"
|
3
|
-
require "relaton/xml_document"
|
4
3
|
require_relative "cli/command"
|
5
4
|
|
6
5
|
module Relaton
|
6
|
+
def self.db
|
7
|
+
Cli.relaton
|
8
|
+
end
|
9
|
+
|
7
10
|
module Cli
|
11
|
+
class RelatonDb
|
12
|
+
include Singleton
|
13
|
+
|
14
|
+
def db
|
15
|
+
@db ||= Relaton::Db.new("#{Dir.home}/.relaton/cache", nil)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
8
19
|
def self.start(arguments)
|
9
20
|
Relaton::Cli::Command.start(arguments)
|
10
21
|
end
|
@@ -17,7 +28,7 @@ module Relaton
|
|
17
28
|
# whenever necessary.
|
18
29
|
#
|
19
30
|
def self.relaton
|
20
|
-
|
31
|
+
RelatonDb.instance.db
|
21
32
|
end
|
22
33
|
|
23
34
|
# @param content [Nokogiri::XML::Document]
|
data/lib/relaton-cli.rb
CHANGED
data/relaton-cli.gemspec
CHANGED
@@ -34,7 +34,7 @@ Gem::Specification.new do |spec|
|
|
34
34
|
spec.add_development_dependency "debase"
|
35
35
|
spec.add_development_dependency "simplecov"
|
36
36
|
|
37
|
-
spec.add_runtime_dependency "relaton", "~> 0.
|
37
|
+
spec.add_runtime_dependency "relaton", "~> 0.8.0"
|
38
38
|
spec.add_runtime_dependency "thor"
|
39
39
|
spec.add_runtime_dependency "liquid"
|
40
40
|
# spec.add_runtime_dependency 'byebug'
|
data/templates/_document.liquid
CHANGED
@@ -8,9 +8,9 @@
|
|
8
8
|
<div class="doc-identifier">
|
9
9
|
<h{{ depth }}>
|
10
10
|
{% if document.html == "" %}
|
11
|
-
{{ document.
|
11
|
+
{{ document.docid.id }}
|
12
12
|
{% else %}
|
13
|
-
<a href="{{ document.html }}">{{ document.
|
13
|
+
<a href="{{ document.html }}">{{ document.docid.id }}</a>
|
14
14
|
{% endif %}
|
15
15
|
</h{{ depth }}>
|
16
16
|
</div>
|
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.
|
4
|
+
version: 0.6.0
|
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-
|
11
|
+
date: 2020-02-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -156,14 +156,14 @@ dependencies:
|
|
156
156
|
requirements:
|
157
157
|
- - "~>"
|
158
158
|
- !ruby/object:Gem::Version
|
159
|
-
version: 0.
|
159
|
+
version: 0.8.0
|
160
160
|
type: :runtime
|
161
161
|
prerelease: false
|
162
162
|
version_requirements: !ruby/object:Gem::Requirement
|
163
163
|
requirements:
|
164
164
|
- - "~>"
|
165
165
|
- !ruby/object:Gem::Version
|
166
|
-
version: 0.
|
166
|
+
version: 0.8.0
|
167
167
|
- !ruby/object:Gem::Dependency
|
168
168
|
name: thor
|
169
169
|
requirement: !ruby/object:Gem::Requirement
|
@@ -207,6 +207,7 @@ files:
|
|
207
207
|
- ".github/workflows/windows.yml"
|
208
208
|
- ".gitignore"
|
209
209
|
- ".hound.yml"
|
210
|
+
- ".rspec"
|
210
211
|
- ".rubocop.yml"
|
211
212
|
- CHANGELOG.md
|
212
213
|
- Gemfile
|
@@ -220,9 +221,7 @@ files:
|
|
220
221
|
- i18n.yaml
|
221
222
|
- lib/relaton-cli.rb
|
222
223
|
- lib/relaton/bibcollection.rb
|
223
|
-
- lib/relaton/bibcollectionnew.rb
|
224
224
|
- lib/relaton/bibdata.rb
|
225
|
-
- lib/relaton/bibdatanew.rb
|
226
225
|
- lib/relaton/cli.rb
|
227
226
|
- lib/relaton/cli/_document.liquid
|
228
227
|
- lib/relaton/cli/_index.liquid
|
@@ -231,12 +230,9 @@ files:
|
|
231
230
|
- lib/relaton/cli/relaton_file.rb
|
232
231
|
- lib/relaton/cli/version.rb
|
233
232
|
- lib/relaton/cli/xml_convertor.rb
|
234
|
-
- lib/relaton/cli/xml_convertor_new.rb
|
235
233
|
- lib/relaton/cli/xml_to_html_renderer.rb
|
236
234
|
- lib/relaton/cli/yaml_convertor.rb
|
237
|
-
- lib/relaton/cli/yaml_convertor_new.rb
|
238
235
|
- lib/relaton/element_finder.rb
|
239
|
-
- lib/relaton/xml_document.rb
|
240
236
|
- relaton-cli.gemspec
|
241
237
|
- templates/_document.liquid
|
242
238
|
- templates/_index.liquid
|
@@ -1,116 +0,0 @@
|
|
1
|
-
require "relaton/element_finder"
|
2
|
-
|
3
|
-
module Relaton
|
4
|
-
class BibcollectionNew
|
5
|
-
extend Relaton::ElementFinder
|
6
|
-
|
7
|
-
ATTRIBS = %i[
|
8
|
-
title
|
9
|
-
items
|
10
|
-
doctype
|
11
|
-
author
|
12
|
-
].freeze
|
13
|
-
|
14
|
-
attr_accessor *ATTRIBS
|
15
|
-
|
16
|
-
def initialize(options)
|
17
|
-
self.items = []
|
18
|
-
ATTRIBS.each do |k|
|
19
|
-
value = options[k] || options[k.to_s]
|
20
|
-
send("#{k}=", value)
|
21
|
-
end
|
22
|
-
self.items = (items || []).reduce([]) do |acc, item|
|
23
|
-
acc << if item.is_a?(::Relaton::BibcollectionNew) || item.is_a?(::Relaton::BibdataNew)
|
24
|
-
item
|
25
|
-
else new_bib_item_class(item)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
# arbitrary number, must sort after all bib items
|
31
|
-
def doc_number
|
32
|
-
9999999
|
33
|
-
end
|
34
|
-
|
35
|
-
def self.from_xml(source)
|
36
|
-
title = find_text("./relaton-collection/title", source)
|
37
|
-
author = find_text("./relaton-collection/contributor[role/@type = 'author']/organization/name", source)
|
38
|
-
|
39
|
-
items = find_xpath("./relaton-collection/relation", source)&.map do |item|
|
40
|
-
bibdata = find("./bibdata", item)
|
41
|
-
klass = bibdata ? BibdataNew : BibcollectionNew
|
42
|
-
klass.from_xml(bibdata || item)
|
43
|
-
end
|
44
|
-
|
45
|
-
new(title: title, author: author, items: items)
|
46
|
-
end
|
47
|
-
|
48
|
-
def new_bib_item_class(options)
|
49
|
-
if options.is_a?(Hash) && options["items"]
|
50
|
-
::Relaton::BibcollectionNew.new(options)
|
51
|
-
else
|
52
|
-
::Relaton::BibdataNew.new(options)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def items_flattened
|
57
|
-
items.sort_by! do |b|
|
58
|
-
b.docnumber
|
59
|
-
end
|
60
|
-
|
61
|
-
items.inject([]) do |acc,item|
|
62
|
-
if item.is_a? ::Relaton::BibcollectionNew
|
63
|
-
acc << item.items_flattened
|
64
|
-
else
|
65
|
-
acc << item
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def to_xml(opts)
|
71
|
-
items.sort_by! do |b|
|
72
|
-
b.docnumber
|
73
|
-
end
|
74
|
-
|
75
|
-
collection_type = if doctype
|
76
|
-
"type=\"#{doctype}\""
|
77
|
-
else
|
78
|
-
'xmlns="https://open.ribose.com/relaton-xml"'
|
79
|
-
end
|
80
|
-
|
81
|
-
ret = "<relaton-collection #{collection_type}>"
|
82
|
-
ret += "<title>#{title}</title>" if title
|
83
|
-
if author
|
84
|
-
ret += "<contributor><role type='author'/><organization><name>#{author}</name></organization></contributor>"
|
85
|
-
end
|
86
|
-
unless items.empty?
|
87
|
-
items.each do |item|
|
88
|
-
ret += "<relation type='partOf'>"
|
89
|
-
ret += item.to_xml(opts)
|
90
|
-
ret += "</relation>\n"
|
91
|
-
end
|
92
|
-
end
|
93
|
-
ret += "</relaton-collection>\n"
|
94
|
-
end
|
95
|
-
|
96
|
-
def to_yaml
|
97
|
-
to_h.to_yaml
|
98
|
-
end
|
99
|
-
|
100
|
-
def to_h
|
101
|
-
items.sort_by! do |b|
|
102
|
-
b.doc_number
|
103
|
-
end
|
104
|
-
|
105
|
-
a = ATTRIBS.inject({}) do |acc, k|
|
106
|
-
acc[k.to_s] = send(k)
|
107
|
-
acc
|
108
|
-
end
|
109
|
-
|
110
|
-
a["items"] = a["items"].map(&:to_h)
|
111
|
-
|
112
|
-
{ "root" => a }
|
113
|
-
end
|
114
|
-
|
115
|
-
end
|
116
|
-
end
|
data/lib/relaton/bibdatanew.rb
DELETED
@@ -1,62 +0,0 @@
|
|
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 == m }
|
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
|