relaton 3.1.4 → 3.1.6

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.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/README.adoc +1 -0
  3. data/lib/relaton/arxiv/README.adoc +59 -0
  4. data/lib/relaton/arxiv/api.rb +42 -0
  5. data/lib/relaton/arxiv/bibliography.rb +45 -0
  6. data/lib/relaton/arxiv/parser.rb +167 -0
  7. data/lib/relaton/arxiv/processor.rb +66 -0
  8. data/lib/relaton/arxiv/util.rb +8 -0
  9. data/lib/relaton/arxiv.rb +23 -0
  10. data/lib/relaton/bib/converter/endnote/from_endnote.rb +118 -0
  11. data/lib/relaton/bib/converter/endnote/to_endnote.rb +107 -0
  12. data/lib/relaton/bib/converter/endnote.rb +30 -0
  13. data/lib/relaton/bib/converter/ris/from_ris.rb +144 -0
  14. data/lib/relaton/bib/converter/ris/to_ris.rb +92 -0
  15. data/lib/relaton/bib/converter/ris.rb +7 -81
  16. data/lib/relaton/bib/item_data.rb +4 -0
  17. data/lib/relaton/bib.rb +1 -0
  18. data/lib/relaton/bibcollection.rb +45 -83
  19. data/lib/relaton/cie/data_fetcher.rb +142 -13
  20. data/lib/relaton/cli/base_convertor.rb +19 -3
  21. data/lib/relaton/cli/command.rb +19 -5
  22. data/lib/relaton/cli/relaton_file.rb +101 -64
  23. data/lib/relaton/cli/subcommand_collection.rb +6 -24
  24. data/lib/relaton/cli/xml_convertor.rb +6 -5
  25. data/lib/relaton/cli/xml_to_html_renderer.rb +15 -9
  26. data/lib/relaton/cli.rb +79 -20
  27. data/lib/relaton/collection_xml.rb +145 -0
  28. data/lib/relaton/db/registry.rb +6 -2
  29. data/lib/relaton/doi/README.adoc +14 -3
  30. data/lib/relaton/doi/crossref.rb +2 -12
  31. data/lib/relaton/doi/datacite.rb +40 -0
  32. data/lib/relaton/doi/datacite_parser.rb +301 -0
  33. data/lib/relaton/doi/http.rb +86 -0
  34. data/lib/relaton/doi/processor.rb +1 -1
  35. data/lib/relaton/doi/registration_agency.rb +30 -0
  36. data/lib/relaton/doi.rb +45 -0
  37. data/lib/relaton/iho/README.adoc +14 -0
  38. data/lib/relaton/iho/bibliography.rb +43 -1
  39. data/lib/relaton/iho/processor.rb +11 -0
  40. data/lib/relaton/iso/bibliography.rb +49 -2
  41. data/lib/relaton/iso/processor.rb +12 -0
  42. data/lib/relaton/render/iso690/styles/jis-en.yml +21 -5
  43. data/lib/relaton/render/iso690/styles/jis-ja.yml +11 -4
  44. data/lib/relaton/version.rb +1 -1
  45. data/lib/relaton-cli.rb +0 -1
  46. data/lib/relaton.rb +1 -0
  47. metadata +23 -7
  48. data/lib/relaton/element_finder.rb +0 -29
@@ -0,0 +1,144 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Relaton
4
+ module Bib
5
+ module Converter
6
+ module Ris
7
+ # One TY..ER record per entry; tags are `XX - value` lines.
8
+ class FromRis
9
+ # ELEC backs two entry types; the reverse pairs it with the
10
+ # webresource face the renderer's default pairs with.
11
+ REVERSE_ENTRY_TYPES = {
12
+ "STD" => "standard", "BOOK" => "book", "JOUR" => "article",
13
+ "CHAP" => "inbook", "CONF" => "inproceedings", "RPRT" => "report",
14
+ "THES" => "thesis", "ELEC" => "webresource",
15
+ }.freeze
16
+
17
+ TAG_LINE = /\A([A-Z0-9]{2}) - ?(.*)\z/
18
+
19
+ # @param ris_str [String]
20
+ # @return [Hash{String=>Relaton::Bib::ItemData}] keyed by the ID
21
+ # tag (falling back to the docidentifier, then the record
22
+ # position)
23
+ def self.parse(ris_str)
24
+ records = []
25
+ current = nil
26
+ ris_str.each_line do |line|
27
+ line = line.chomp
28
+ if line.start_with?("TY - ")
29
+ current = [line]
30
+ elsif current
31
+ current << line
32
+ # The renderer writes "ER - " with a trailing space; real
33
+ # files commonly stop at the dash.
34
+ if line.start_with?("ER -")
35
+ records << current
36
+ current = nil
37
+ end
38
+ end
39
+ end
40
+ records.each_with_object({}).with_index do |(record, hash), i|
41
+ item = new(record).item
42
+ hash[record_key(item) || "record-#{i + 1}"] = item
43
+ end
44
+ end
45
+
46
+ # @param record_lines [Array<String>]
47
+ def initialize(record_lines)
48
+ @tags = Hash.new { |h, k| h[k] = [] }
49
+ record_lines.each do |line|
50
+ next unless (m = line.chomp.match(TAG_LINE))
51
+
52
+ @tags[m[1]] << m[2]
53
+ end
54
+ end
55
+
56
+ # @return [Relaton::Bib::ItemData]
57
+ def item
58
+ ItemData.new(
59
+ type: entry_type,
60
+ docidentifier: fetch_docid,
61
+ title: fetch_title,
62
+ date: fetch_date,
63
+ contributor: fetch_contributor,
64
+ edition: fetch_edition,
65
+ language: @tags["LA"].map(&:itself),
66
+ keyword: fetch_keyword,
67
+ source: fetch_source,
68
+ )
69
+ end
70
+
71
+ # @return [String, nil] the primary docidentifier's content
72
+ def self.record_key(item)
73
+ ids = Array(item.docidentifier)
74
+ primary = (ids.find(&:primary) || ids.first)&.content.to_s
75
+ primary unless primary.empty?
76
+ end
77
+
78
+ private
79
+
80
+ def entry_type
81
+ REVERSE_ENTRY_TYPES.fetch(@tags["TY"].first, "standard")
82
+ end
83
+
84
+ def fetch_docid
85
+ @tags["ID"].map { |v| Docidentifier.new(content: v, primary: true) }
86
+ end
87
+
88
+ def fetch_title
89
+ @tags["TI"].map { |v| Title.new(type: "title-main", content: v) }
90
+ end
91
+
92
+ # RIS dates are "YYYY" or "YYYY/MM/DD".
93
+ def fetch_date
94
+ @tags["PY"].map do |v|
95
+ on = v.match?(/\A\d{4}\z/) ? v : (::Date.parse(v.tr("/", "-")).to_s rescue v)
96
+ Bib::Date.new(type: "published", at: on)
97
+ end
98
+ end
99
+
100
+ # The renderer emits "Surname, Forename" for persons and a bare
101
+ # name for organizations under the same AU tag; a comma splits a
102
+ # person, a bare token is an organization name.
103
+ def fetch_contributor # rubocop:disable Metrics/AbcSize
104
+ contribs = []
105
+ @tags["AU"].each do |name|
106
+ contribs << if name.include?(",")
107
+ person_contributor(name)
108
+ else
109
+ org_contributor(name, "author")
110
+ end
111
+ end
112
+ @tags["PB"].each { |name| contribs << org_contributor(name, "publisher") }
113
+ contribs
114
+ end
115
+
116
+ def person_contributor(name)
117
+ parts = name.split(", ")
118
+ surname = LocalizedString.new(content: parts.first)
119
+ forename = (parts[1] || "").split.map { |fn| FullNameType::Forename.new(content: fn) }
120
+ person = Person.new(name: FullName.new(surname: surname, forename: forename))
121
+ Contributor.new(person: person, role: [Contributor::Role.new(type: "author")])
122
+ end
123
+
124
+ def org_contributor(name, role)
125
+ org = Organization.new(name: [TypedLocalizedString.new(content: name)])
126
+ Contributor.new(organization: org, role: [Contributor::Role.new(type: role)])
127
+ end
128
+
129
+ def fetch_edition
130
+ Edition.new(content: @tags["ET"].first.to_s) if @tags["ET"].any?
131
+ end
132
+
133
+ def fetch_keyword
134
+ @tags["KW"].map { |v| Keyword.new(vocab: LocalizedString.new(content: v)) }
135
+ end
136
+
137
+ def fetch_source
138
+ @tags["UR"].map { |v| Uri.new(type: "src", content: v) }
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Relaton
4
+ module Bib
5
+ module Converter
6
+ module Ris
7
+ class ToRis
8
+ def initialize(item)
9
+ @item = item
10
+ end
11
+
12
+ # @return [String]
13
+ def to_s
14
+ lines = ["TY - #{ENTRY_TYPES[@item.type.to_s] || 'STD'}"]
15
+ add_authors(lines)
16
+ add_field(lines, "TI", primary_title)
17
+ add_field(lines, "ID", primary_docid)
18
+ add_field(lines, "PY", published_year)
19
+ add_field(lines, "ET", @item.edition&.to_s)
20
+ lang = Array(@item.language).first
21
+ add_field(lines, "LA", lang.to_s)
22
+ Array(@item.keyword).each { |k| add_field(lines, "KW", k.content.to_s) }
23
+ add_field(lines, "UR", source_uri)
24
+ lines << "ER - "
25
+ lines.join("\r\n") + "\r\n"
26
+ end
27
+
28
+ private
29
+
30
+ def add_field(lines, tag, value)
31
+ lines << "#{tag} - #{value}" if value && !value.empty?
32
+ end
33
+
34
+ def add_authors(lines)
35
+ Array(@item.contributor).each do |c|
36
+ roles = Array(c.role).map(&:type).compact
37
+ authorish = (roles & %w[author performer editor]).any?
38
+ publisher = roles.include?("publisher")
39
+ if authorish && c.person
40
+ out = person_display_name(c.person.name)
41
+ lines << "AU - #{out}" unless out.empty?
42
+ elsif authorish && c.organization
43
+ out = org_name(c.organization)
44
+ lines << "AU - #{out}" unless out.empty?
45
+ elsif publisher && c.organization
46
+ out = org_name(c.organization)
47
+ lines << "PB - #{out}" unless out.empty? || lines.any? { |l| l.start_with?("PB - ") }
48
+ end
49
+ end
50
+ end
51
+
52
+ def person_display_name(name)
53
+ return "" unless name
54
+
55
+ fore = Array(name.forename).map { |f| f.respond_to?(:content) ? f.content.to_s : f.to_s }.join(" ")
56
+ sur = name.surname.respond_to?(:content) ? name.surname.content.to_s : name.surname.to_s
57
+ complete = name.completename.respond_to?(:content) ? name.completename.content.to_s : name.completename.to_s
58
+ return "#{sur}, #{fore}" unless fore.empty? || sur.empty?
59
+ return sur unless sur.empty?
60
+
61
+ complete
62
+ end
63
+
64
+ def org_name(org)
65
+ name = Array(org.name).map { |n| n.respond_to?(:content) ? n.content.to_s : n.to_s }.find(&:itself)
66
+ name || org.abbreviation.to_s
67
+ end
68
+
69
+ def primary_title
70
+ Titles.of(@item)
71
+ end
72
+
73
+ def primary_docid
74
+ ids = Array(@item.docidentifier)
75
+ (ids.find(&:primary) || ids.first)&.content.to_s
76
+ end
77
+
78
+ def published_year
79
+ date = Array(@item.date).find { |d| %w[published issued].include?(d.type.to_s) } || @item.date.first
80
+ value = date && (date.at || date.from || date.to)
81
+ value.to_s[/\d{4}/]
82
+ end
83
+
84
+ def source_uri
85
+ Array(@item.source).map { |s| s.respond_to?(:content) ? s.content.to_s : s.to_s }
86
+ .find { |u| u.start_with?("http") }
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "ris/from_ris"
4
+ require_relative "ris/to_ris"
3
5
 
4
6
  module Relaton
5
7
  module Bib
@@ -13,90 +15,14 @@ module Relaton
13
15
  "thesis" => "THES", "website" => "ELEC", "webresource" => "ELEC",
14
16
  }.freeze
15
17
 
18
+ # ItemData -> RIS string
16
19
  def self.from_item(item)
17
- Renderer.new(item).to_s
20
+ ToRis.new(item).to_s
18
21
  end
19
22
 
20
- class Renderer
21
- def initialize(item)
22
- @item = item
23
- end
24
-
25
- def to_s
26
- lines = ["TY - #{ENTRY_TYPES[@item.type.to_s] || 'STD'}"]
27
- add_authors(lines)
28
- add_field(lines, "TI", primary_title)
29
- add_field(lines, "ID", primary_docid)
30
- add_field(lines, "PY", published_year)
31
- add_field(lines, "ET", @item.edition&.to_s)
32
- lang = Array(@item.language).first
33
- add_field(lines, "LA", lang.to_s)
34
- Array(@item.keyword).each { |k| add_field(lines, "KW", k.content.to_s) }
35
- add_field(lines, "UR", source_uri)
36
- lines << "ER - "
37
- lines.join("\r\n") + "\r\n"
38
- end
39
-
40
- private
41
-
42
- def add_field(lines, tag, value)
43
- lines << "#{tag} - #{value}" if value && !value.empty?
44
- end
45
-
46
- def add_authors(lines)
47
- Array(@item.contributor).each do |c|
48
- roles = Array(c.role).map(&:type).compact
49
- authorish = (roles & %w[author performer editor]).any?
50
- publisher = roles.include?("publisher")
51
- if authorish && c.person
52
- out = person_display_name(c.person.name)
53
- lines << "AU - #{out}" unless out.empty?
54
- elsif authorish && c.organization
55
- out = org_name(c.organization)
56
- lines << "AU - #{out}" unless out.empty?
57
- elsif publisher && c.organization
58
- out = org_name(c.organization)
59
- lines << "PB - #{out}" unless out.empty? || lines.any? { |l| l.start_with?("PB - ") }
60
- end
61
- end
62
- end
63
-
64
- def person_display_name(name)
65
- return "" unless name
66
-
67
- fore = Array(name.forename).map { |f| f.respond_to?(:content) ? f.content.to_s : f.to_s }.join(" ")
68
- sur = name.surname.respond_to?(:content) ? name.surname.content.to_s : name.surname.to_s
69
- complete = name.completename.respond_to?(:content) ? name.completename.content.to_s : name.completename.to_s
70
- return "#{sur}, #{fore}" unless fore.empty? || sur.empty?
71
- return sur unless sur.empty?
72
-
73
- complete
74
- end
75
-
76
- def org_name(org)
77
- name = Array(org.name).map { |n| n.respond_to?(:content) ? n.content.to_s : n.to_s }.find(&:itself)
78
- name || org.abbreviation.to_s
79
- end
80
-
81
- def primary_title
82
- Titles.of(@item)
83
- end
84
-
85
- def primary_docid
86
- ids = Array(@item.docidentifier)
87
- (ids.find(&:primary) || ids.first)&.content.to_s
88
- end
89
-
90
- def published_year
91
- date = Array(@item.date).find { |d| %w[published issued].include?(d.type.to_s) } || @item.date.first
92
- value = date && (date.at || date.from || date.to)
93
- value.to_s[/\d{4}/]
94
- end
95
-
96
- def source_uri
97
- Array(@item.source).map { |s| s.respond_to?(:content) ? s.content.to_s : s.to_s }
98
- .find { |u| u.start_with?("http") }
99
- end
23
+ # RIS string -> Hash{String=>ItemData}
24
+ def self.to_item(ris_str)
25
+ FromRis.parse(ris_str)
100
26
  end
101
27
  end
102
28
  end
@@ -166,6 +166,10 @@ module Relaton
166
166
  Converter::Ris.from_item(self)
167
167
  end
168
168
 
169
+ def to_endnote
170
+ Converter::Endnote.from_item(self)
171
+ end
172
+
169
173
  def to_csl_json
170
174
  Converter::Csl.from_item(self)
171
175
  end
data/lib/relaton/bib.rb CHANGED
@@ -25,6 +25,7 @@ module Relaton
25
25
  module Bib
26
26
  module Converter
27
27
  autoload :Ris, "relaton/bib/converter/ris"
28
+ autoload :Endnote, "relaton/bib/converter/endnote"
28
29
  autoload :Csl, "relaton/bib/converter/csl"
29
30
  autoload :Titles, "relaton/bib/converter/titles"
30
31
  end
@@ -1,20 +1,45 @@
1
- require_relative "element_finder"
2
-
3
1
  module Relaton
2
+ # A Relaton collection: a title, an author, a doctype and a list of items,
3
+ # where an item is a bibliographic item (`Relaton::Bibdata`) or a nested
4
+ # collection.
5
+ #
6
+ # The XML is read and written by the lutaml-model mapper
7
+ # `Relaton::CollectionXml` (relaton#459), as `Relaton::Bib::Item` maps
8
+ # `Relaton::Bib::ItemData`. The YAML (`#to_h`, `{"root" => …}`) is the
9
+ # format `Relaton::Cli::CollectionStore` keeps on disk, unchanged.
4
10
  class Bibcollection
5
- extend Relaton::ElementFinder
6
-
7
11
  ATTRIBS = %i[title items doctype author].freeze
8
12
 
9
- attr_accessor(*ATTRIBS)
13
+ attr_accessor :title, :doctype, :author
10
14
 
11
- # @param options [Hash]
12
- def initialize(options)
15
+ # Options of `#to_xml` that `CollectionXml` hands to the items.
16
+ attr_accessor :item_options
17
+
18
+ # @param options [Hash] the collection's attributes, with String or
19
+ # Symbol keys; an item is a Hash, a bibliographic item, a
20
+ # `Relaton::Bibdata` or a `Relaton::Bibcollection`
21
+ def initialize(options = {})
22
+ options ||= {}
13
23
  ATTRIBS.each do |k|
14
- value = options[k] || options[k.to_s]
15
- send("#{k}=", value)
24
+ send "#{k}=", options.key?(k) ? options[k] : options[k.to_s]
16
25
  end
17
- reduce_items
26
+ end
27
+
28
+ # @param source [String, #to_xml] Relaton collection XML, or a parsed
29
+ # document that answers `to_xml`
30
+ # @return [Relaton::Bibcollection]
31
+ def self.from_xml(source)
32
+ source = source.to_xml unless source.is_a?(String)
33
+ CollectionXml.from_xml source
34
+ end
35
+
36
+ def items
37
+ @items ||= []
38
+ end
39
+
40
+ # @param value [Array<Hash, Relaton::Bibdata, Relaton::Bibcollection>, nil]
41
+ def items=(value)
42
+ @items = (value || []).map { |item| new_bib_item_class item }
18
43
  end
19
44
 
20
45
  # arbitrary number, must sort after all bib items
@@ -28,59 +53,18 @@ module Relaton
28
53
  items << new_bib_item_class(item)
29
54
  end
30
55
 
31
- # rubocop:disable Metrics/MethodLength
32
-
33
- # @param source [Nokogiri::XML::Element]
34
- def self.from_xml(source)
35
- title = find_html("./relaton-collection/title", source)
36
- author = find_html(
37
- "./relaton-collection/contributor[role/@type='author']/organization/"\
38
- "name", source
39
- )
40
-
41
- items = find_xpath("./relaton-collection/relation", source)&.map do |item|
42
- bibdata = find("./bibdata|./bibitem", item)
43
- klass = bibdata ? Bibdata : Bibcollection
44
- klass.from_xml(bibdata || item)
45
- end
46
-
47
- new(title: title, author: author, items: items)
48
- end
49
-
50
- # rubocop:disable Metrics/AbcSize
51
-
52
- # @param opts [Hash]
56
+ # @param opts [Hash] options for the items' `to_xml`
53
57
  # @return [String] XML
54
58
  def to_xml(opts = {})
55
- items.sort_by! &:doc_number
56
-
57
- collection_type = if doctype
58
- "type=\"#{doctype}\""
59
- else
60
- 'xmlns="https://open.ribose.com/relaton-xml"'
61
- end
62
-
63
- ret = "<relaton-collection #{collection_type}>"
64
- ret += "<title>#{xml_escape(title)}</title>" if title
65
- if author
66
- ret += "<contributor><role type='author'/><organization><name>"\
67
- "#{xml_escape(author)}</name></organization></contributor>"
68
- end
69
- unless items.empty?
70
- items.each do |item|
71
- ret += "<relation type='partOf'>"
72
- ret += item.to_xml(opts)
73
- ret += "</relation>\n"
74
- end
75
- end
76
- ret += "</relaton-collection>\n"
59
+ items.sort_by!(&:doc_number)
60
+ self.item_options = opts.select { |k, _| k.is_a? Symbol }
61
+ CollectionXml.to_xml self
77
62
  end
78
- # rubocop:enable Metrics/AbcSize
79
63
 
80
64
  # @param item [Hash, RelatonBib::BibliographicItem, Relatin::Bibdata,
81
65
  # Relaton::Bibcollection]
82
66
  # @return [Relaton::Bibdata, Relaton::Bibcollection]
83
- def new_bib_item_class(item)
67
+ def new_bib_item_class(item) # rubocop:disable Metrics/MethodLength
84
68
  if item.is_a?(Hash)
85
69
  # `#to_h` writes a nested collection as `{"root" => …}`.
86
70
  if item["root"].is_a?(Hash)
@@ -96,10 +80,9 @@ module Relaton
96
80
  else ::Relaton::Bibdata.new(item)
97
81
  end
98
82
  end
99
- # rubocop:enable Metrics/MethodLength
100
83
 
101
84
  def items_flattened
102
- items.sort_by! &:doc_number
85
+ items.sort_by!(&:doc_number)
103
86
 
104
87
  items.reduce([]) do |acc, item|
105
88
  acc << if item.is_a? ::Relaton::Bibcollection
@@ -115,35 +98,14 @@ module Relaton
115
98
  end
116
99
 
117
100
  def to_h
118
- items.sort_by! &:doc_number
119
-
120
- a = ATTRIBS.reduce({}) do |acc, k|
121
- acc[k.to_s] = send(k)
122
- acc
123
- end
101
+ items.sort_by!(&:doc_number)
124
102
 
103
+ a = ATTRIBS.each_with_object({}) { |k, acc| acc[k.to_s] = send(k) }
125
104
  a["items"] = a["items"].map(&:to_h)
126
105
 
127
106
  { "root" => a }
128
107
  end
129
-
130
- private
131
-
132
- def reduce_items
133
- self.items = (items || []).reduce([]) do |acc, item|
134
- acc << if item.is_a?(Bibcollection) || item.is_a?(Bibdata)
135
- item
136
- else new_bib_item_class(item)
137
- end
138
- end
139
- end
140
-
141
- # Escape bare & in content for XML serialization, leaving already-encoded
142
- # entity references (&amp;, &#123;, &#x1f;) and inline markup (<em> etc.)
143
- # untouched. This prevents invalid XML when plain-text values (e.g. from
144
- # YAML) contain literal ampersands.
145
- def xml_escape(str)
146
- str.gsub(/&(?![a-zA-Z][a-zA-Z0-9]*;|#[0-9]+;|#x[0-9a-fA-F]+;)/, "&amp;")
147
- end
148
108
  end
149
109
  end
110
+
111
+ require_relative "collection_xml"