Class: Relaton::Render::Parse
- Inherits:
-
Object
- Object
- Relaton::Render::Parse
- Defined in:
- lib/relaton/render/parse/parse.rb,
lib/relaton/render/parse/parse_extract.rb,
lib/relaton/render/parse/parse_contributors.rb
Instance Method Summary collapse
- #access_location(doc, host) ⇒ Object
- #contributor_role(contributors) ⇒ Object
- #creatornames(doc) ⇒ Object
- #creatornames1(doc) ⇒ Object
- #creatornames_roles_allowed ⇒ Object
- #date(doc, host) ⇒ Object
- #date1(date) ⇒ Object
- #date_accessed(doc, host) ⇒ Object
- #date_updated(doc, host) ⇒ Object
- #datepick(date) ⇒ Object
- #distributor(doc, host) ⇒ Object
- #draft(doc, host) ⇒ Object
- #edition(doc, host) ⇒ Object
- #edition_num(doc, host) ⇒ Object
- #extent(doc) ⇒ Object
- #extent1(localities) ⇒ Object
- #extract(doc) ⇒ Object
- #extract_orgname(org) ⇒ Object
- #extract_personname(person) ⇒ Object
- #extractname(contributor) ⇒ Object
- #given_and_middle_name(person) ⇒ Object
- #host(doc) ⇒ Object
- #host_xml2hash(host) ⇒ Object
- #included(type) ⇒ Object
-
#initialize(options) ⇒ Parse
constructor
A new instance of Parse.
- #iter_ordinal(doc) ⇒ Object
- #medium(doc, host) ⇒ Object
- #place(doc, host) ⇒ Object
- #publisher(doc, host) ⇒ Object
- #publisher_abbrev(doc, host) ⇒ Object
- #series(doc) ⇒ Object
- #series_abbr(series, _doc) ⇒ Object
- #series_formatted(series, _doc) ⇒ Object
- #series_num(series, _doc) ⇒ Object
- #series_partnumber(series, _doc) ⇒ Object
- #series_run(series, _doc) ⇒ Object
- #series_title(series, _doc) ⇒ Object
- #series_xml2hash(doc, host) ⇒ Object
- #series_xml2hash1(series, doc) ⇒ Object
- #simple_or_host_xml2hash(doc, host) ⇒ Object
- #simple_xml2hash(doc) ⇒ Object
- #size(doc) ⇒ Object
- #standardidentifier(doc) ⇒ Object
- #standardidentifier_exclude ⇒ Object
- #status(doc) ⇒ Object
-
#title(doc) ⇒ Object
TODO : first is naive choice.
- #type(doc) ⇒ Object
- #uri(doc) ⇒ Object
Constructor Details
#initialize(options) ⇒ Parse
Returns a new instance of Parse.
9 10 11 12 |
# File 'lib/relaton/render/parse/parse.rb', line 9 def initialize() @lang = [:lang] || "en" @script = [:script] || "Latn" end |
Instance Method Details
#access_location(doc, host) ⇒ Object
105 106 107 108 109 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 105 def access_location(doc, host) x = doc.accesslocation || host&.accesslocation or return nil x.first end |
#contributor_role(contributors) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/relaton/render/parse/parse_contributors.rb', line 37 def contributor_role(contributors) return nil unless contributors.length.positive? desc = contributors[0].role.first.description.join("\n") type = contributors[0].role.first.type desc.empty? ? type : desc end |
#creatornames(doc) ⇒ Object
45 46 47 48 49 |
# File 'lib/relaton/render/parse/parse_contributors.rb', line 45 def creatornames(doc) cr = creatornames1(doc) cr.empty? and return [nil, nil] [cr.map { |x| extractname(x) }, contributor_role(cr)] end |
#creatornames1(doc) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/relaton/render/parse/parse_contributors.rb', line 55 def creatornames1(doc) cr = [] return [] if doc.nil? creatornames_roles_allowed.each do |r| add = pick_contributor(doc, r) next if add.nil? cr = add and break end cr.nil? and cr = doc.contributor cr end |
#creatornames_roles_allowed ⇒ Object
51 52 53 |
# File 'lib/relaton/render/parse/parse_contributors.rb', line 51 def creatornames_roles_allowed %w(author performer adapter translator editor publisher distributor) end |
#date(doc, host) ⇒ Object
89 90 91 92 93 |
# File 'lib/relaton/render/parse/parse_contributors.rb', line 89 def date(doc, host) ret = date1(doc.date) host and ret ||= date1(host.date) datepick(ret) end |
#date1(date) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/relaton/render/parse/parse_contributors.rb', line 81 def date1(date) %w(published issued circulated).each do |t| ret = date.detect { |x| x.type == t } and return ret end date.first end |
#date_accessed(doc, host) ⇒ Object
101 102 103 104 105 |
# File 'lib/relaton/render/parse/parse_contributors.rb', line 101 def date_accessed(doc, host) ret = doc.date.detect { |x| x.type == "accessed" } host and ret ||= host.date.detect { |x| x.type == "accessed" } datepick(ret) end |
#date_updated(doc, host) ⇒ Object
95 96 97 98 99 |
# File 'lib/relaton/render/parse/parse_contributors.rb', line 95 def date_updated(doc, host) ret = doc.date.detect { |x| x.type == "updated" } host and ret ||= host.date.detect { |x| x.type == "updated" } datepick(ret) end |
#datepick(date) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/relaton/render/parse/parse_contributors.rb', line 69 def datepick(date) return nil if date.nil? on = date.on from = date.from to = date.to return { on: on } if on return { from: from, to: to } if from nil end |
#distributor(doc, host) ⇒ Object
124 125 126 127 128 129 |
# File 'lib/relaton/render/parse/parse_contributors.rb', line 124 def distributor(doc, host) x = pick_contributor(doc, "distributor") host and x ||= pick_contributor(host, "distributor") x.nil? and return nil x.map { |c| extractname(c) } end |
#draft(doc, host) ⇒ Object
141 142 143 144 145 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 141 def draft(doc, host) dr = doc.status&.stage&.value || host&.status&.stage&.value { iteration: iter_ordinal(doc) || iter_ordinal(host), status: dr } end |
#edition(doc, host) ⇒ Object
35 36 37 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 35 def edition(doc, host) doc.edition&.content || host&.edition&.content end |
#edition_num(doc, host) ⇒ Object
39 40 41 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 39 def edition_num(doc, host) doc.edition&.number || host&.edition&.number end |
#extent(doc) ⇒ Object
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 130 def extent(doc) doc.extent.each_with_object([]) do |e, acc| case e when RelatonBib::LocalityStack acc << extent1(e.locality) when RelatonBib::Locality acc << extent1(Array(e)) end end end |
#extent1(localities) ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 121 def extent1(localities) localities.each_with_object({}) do |l, ret| ret[(l.type || "page").to_sym] = { from: localized_string_or_text(l.reference_from), to: localized_string_or_text(l.reference_to), } end end |
#extract(doc) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/relaton/render/parse/parse.rb', line 14 def extract(doc) host = host(doc) simple_xml2hash(doc).merge(simple_or_host_xml2hash(doc, host)) .merge(host_xml2hash(host)) .merge(series_xml2hash(doc, host)) end |
#extract_orgname(org) ⇒ Object
4 5 6 |
# File 'lib/relaton/render/parse/parse_contributors.rb', line 4 def extract_orgname(org) org.name&.first&.content end |
#extract_personname(person) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/relaton/render/parse/parse_contributors.rb', line 8 def extract_personname(person) surname = person.name.surname || person.name.completename given, middle, initials = given_and_middle_name(person) { surname: surname.content, given: given, middle: middle, initials: initials } end |
#extractname(contributor) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/relaton/render/parse/parse_contributors.rb', line 26 def extractname(contributor) org = contributor.entity if contributor.entity .is_a?(RelatonBib::Organization) person = contributor.entity if contributor.entity .is_a?(RelatonBib::Person) return { nonpersonal: extract_orgname(org) } if org return extract_personname(person) if person nil end |
#given_and_middle_name(person) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/relaton/render/parse/parse_contributors.rb', line 17 def given_and_middle_name(person) forenames = person.name.forename.map(&:content) initials = person.name.initial.map(&:content) .map { |x| x.sub(/\.$/, "") } forenames.empty? and initials.empty? and return [nil, nil, nil] initials.empty? and initials = forenames.map { |x| x[0] } [forenames.first, forenames[1..-1], initials] end |
#host(doc) ⇒ Object
4 5 6 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 4 def host(doc) doc.relation.detect { |r| r.type == "includedIn" }&.bibitem end |
#host_xml2hash(host) ⇒ Object
40 41 42 43 44 |
# File 'lib/relaton/render/parse/parse.rb', line 40 def host_xml2hash(host) creators, role = creatornames(host) { host_creators: creators, host_role_raw: role, host_title: title(host) } end |
#included(type) ⇒ Object
111 112 113 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 111 def included(type) ["article", "inbook", "incollection", "inproceedings"].include? type end |
#iter_ordinal(doc) ⇒ Object
147 148 149 150 151 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 147 def iter_ordinal(doc) return nil unless iter = doc&.status&.iteration iter end |
#medium(doc, host) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 19 def medium(doc, host) x = doc.medium || host&.medium or return nil %w(content genre form carrier size scale).each_with_object({}) do |i, m| m[i] = x.send i end.compact end |
#place(doc, host) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 43 def place(doc, host) x = doc.place x.empty? && host and x = host.place x.empty? and return x x.map(&:name) end |
#publisher(doc, host) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/relaton/render/parse/parse_contributors.rb', line 107 def publisher(doc, host) x = pick_contributor(doc, "publisher") host and x ||= pick_contributor(host, "publisher") x.nil? and return nil x.map { |c| extractname(c) } end |
#publisher_abbrev(doc, host) ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/relaton/render/parse/parse_contributors.rb', line 114 def publisher_abbrev(doc, host) x = pick_contributor(doc, "publisher") host and x ||= pick_contributor(host, "publisher") x.nil? and return nil x.map do |c| c.entity.abbreviation&.content || c.entity.name.first&.content end end |
#series(doc) ⇒ Object
50 51 52 53 54 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 50 def series(doc) doc.series.detect { |s| s.type == "main" } || doc.series.detect { |s| s.type.nil? } || doc.series.first end |
#series_abbr(series, _doc) ⇒ Object
68 69 70 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 68 def series_abbr(series, _doc) series.abbreviation&.content end |
#series_formatted(series, _doc) ⇒ Object
64 65 66 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 64 def series_formatted(series, _doc) series.formattedref&.content end |
#series_num(series, _doc) ⇒ Object
72 73 74 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 72 def series_num(series, _doc) series.number end |
#series_partnumber(series, _doc) ⇒ Object
76 77 78 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 76 def series_partnumber(series, _doc) series.partnumber end |
#series_run(series, _doc) ⇒ Object
80 81 82 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 80 def series_run(series, _doc) series.run end |
#series_title(series, _doc) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 56 def series_title(series, _doc) return nil if series.nil? series.title.respond_to?(:titles) and return series.title.titles.first.title.content series.title&.title&.content || series.formattedref&.content end |
#series_xml2hash(doc, host) ⇒ Object
46 47 48 49 50 |
# File 'lib/relaton/render/parse/parse.rb', line 46 def series_xml2hash(doc, host) series = series(doc) host and series ||= series(host) series_xml2hash1(series, doc) end |
#series_xml2hash1(series, doc) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/relaton/render/parse/parse.rb', line 52 def series_xml2hash1(series, doc) return {} unless series { series_formatted: series_formatted(series, doc), series_title: series_title(series, doc), series_abbr: series_abbr(series, doc), series_run: series_run(series, doc), series_num: series_num(series, doc), series_partnumber: series_partnumber(series, doc) } end |
#simple_or_host_xml2hash(doc, host) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/relaton/render/parse/parse.rb', line 29 def simple_or_host_xml2hash(doc, host) { edition_raw: edition(doc, host), edition_num: edition_num(doc, host), medium_raw: medium(doc, host), place_raw: place(doc, host), publisher_raw: publisher(doc, host), publisher_abbrev_raw: publisher_abbrev(doc, host), distributor_raw: distributor(doc, host), draft_raw: draft(doc, host), access_location: access_location(doc, host), date: date(doc, host), date_updated: date_updated(doc, host), date_accessed: date_accessed(doc, host) } end |
#simple_xml2hash(doc) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/relaton/render/parse/parse.rb', line 21 def simple_xml2hash(doc) creators, role = creatornames(doc) { type: type(doc), title: title(doc), extent_raw: extent(doc), size_raw: size(doc), standardidentifier: standardidentifier(doc), uri_raw: uri(doc), status: status(doc), creators: creators, role_raw: role } end |
#size(doc) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 27 def size(doc) x = doc.size or return nil x.size.each_with_object({}) do |v, m| m[v.type] ||= [] m[v.type] << v.value end end |
#standardidentifier(doc) ⇒ Object
84 85 86 87 88 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 84 def standardidentifier(doc) doc.docidentifier.each_with_object([]) do |id, ret| ret << id.id unless standardidentifier_exclude.include? id.type end end |
#standardidentifier_exclude ⇒ Object
90 91 92 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 90 def standardidentifier_exclude %w(metanorma metanorma-ordinal) end |
#status(doc) ⇒ Object
153 154 155 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 153 def status(doc) doc.status&.stage&.value end |
#title(doc) ⇒ Object
TODO : first is naive choice
9 10 11 12 13 14 15 16 17 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 9 def title(doc) return nil if doc.nil? || doc.title.empty? t = doc.title.select { |x| x.title.language&.include? @lang } t.empty? and t = doc.title t1 = t.select { |x| x.type == "main" } t1.empty? and t1 = t t1.first&.title&.content end |
#type(doc) ⇒ Object
115 116 117 118 119 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 115 def type(doc) type = doc.type and return type doc.relation.any? { |r| r.type == "includedIn" } and return "inbook" "book" end |
#uri(doc) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/relaton/render/parse/parse_extract.rb', line 94 def uri(doc) uri = nil %w(doi uri src).each do |t| uri = doc.link.detect { |u| u.type == t } and break end uri ||= doc.link.first return nil unless uri uri.content.to_s end |