relaton-render 0.1.0 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,213 @@
1
+ require_relative "date"
2
+
3
+ module Relaton
4
+ module Render
5
+ class Fields
6
+ def initialize(options)
7
+ @r = options[:renderer]
8
+ end
9
+
10
+ def compound_fields_format(hash)
11
+ name_fields_format(hash)
12
+ role_fields_format(hash)
13
+ date_fields_format(hash)
14
+ edition_fields_format(hash)
15
+ misc_fields_format(hash)
16
+ end
17
+
18
+ def name_fields_format(hash)
19
+ hash[:place] = nameformat(hash[:place_raw]&.map do |x|
20
+ { nonpersonal: x }
21
+ end)
22
+ [%i(creatornames creators), %i(host_creatornames host_creators),
23
+ %i(publisher publisher_raw), %i(distributor distributor_raw)]
24
+ .each do |k|
25
+ hash[k[0]] = nameformat(hash[k[1]])
26
+ end
27
+ hash[:publisher_abbrev] = hash[:publisher_abbrev_raw]&.join(", ")
28
+ end
29
+
30
+ def role_fields_format(hash)
31
+ hash[:role] = role_inflect(hash[:creators], hash[:role_raw])
32
+ hash[:host_role] =
33
+ role_inflect(hash[:host_creators], hash[:host_role_raw])
34
+ end
35
+
36
+ def edition_fields_format(hash)
37
+ hash[:edition] = editionformat(hash[:edition_raw])
38
+ hash[:draft] = draftformat(hash[:draft_raw], hash)
39
+ end
40
+
41
+ def misc_fields_format(hash)
42
+ hash[:series] = seriesformat(hash)
43
+ hash[:medium] = mediumformat(hash[:medium_raw])
44
+ hash[:extent] = extentformat(hash[:extent_raw], hash)
45
+ hash[:size] = sizeformat(hash[:size_raw], hash)
46
+ hash[:uri] = uriformat(hash[:uri_raw])
47
+ hash
48
+ end
49
+
50
+ def date_fields_format(hash)
51
+ [%i(date date), %i(date_updated date_updated),
52
+ %i(date_accessed date_accessed)].each do |k|
53
+ hash[k[0]] = dateformat(hash[k[1]], hash)
54
+ end
55
+ end
56
+
57
+ # TODO is not being i18n-alised
58
+ def mediumformat(hash)
59
+ return nil if hash.nil?
60
+
61
+ %w(content genre form carrier size scale).each_with_object([]) do |i, m|
62
+ m << hash[i] if hash[i]
63
+ m
64
+ end.compact.join(", ")
65
+ end
66
+
67
+ def seriesformat(hash)
68
+ parts = %i(series_title series_abbr series_num series_partnumber
69
+ series_run series_formatted)
70
+ series_out = parts.each_with_object({}) do |i, m|
71
+ m[i] = hash[i]
72
+ end
73
+ t = hash[:type] == "article" ? @r.journaltemplate : @r.seriestemplate
74
+ t.render(series_out)
75
+ end
76
+
77
+ def nameformat(names)
78
+ return names if names.nil?
79
+
80
+ parts = %i(surname initials given middle nonpersonal)
81
+ names_out = names.each_with_object({}) do |n, m|
82
+ parts.each do |i|
83
+ m[i] ||= []
84
+ m[i] << n[i]
85
+ end
86
+ end
87
+ @r.nametemplate.render(names_out)
88
+ end
89
+
90
+ def role_inflect(contribs, role)
91
+ return nil if role.nil? || contribs.size.zero? ||
92
+ %w(author publisher).include?(role)
93
+
94
+ number = contribs.size > 1 ? "pl" : "sg"
95
+ @r.i18n.get[role][number] || role
96
+ end
97
+
98
+ def editionformat(edn)
99
+ return edn unless /^\d+$/.match?(edn)
100
+
101
+ num = edn
102
+ @r.edition_number and num = edn.to_i.localize(tw_cldr_lang)
103
+ .to_rbnf_s(*@r.edition_number)
104
+ @r.edition.sub(/%/, num)
105
+ end
106
+
107
+ def draftformat(num, _hash)
108
+ return nil if num.nil?
109
+ return nil if num.is_a?(Hash) && num[:status].nil? && num[:iteration].nil?
110
+
111
+ @r.i18n.draft.sub(/%/, num)
112
+ end
113
+
114
+ def extentformat(extent, hash)
115
+ extent.map do |e|
116
+ e1 = e.transform_values { |v| v.is_a?(Hash) ? range(v) : v }
117
+ ret = e.each_with_object({}) do |(k, v), m|
118
+ extentformat1(k, v, m, e1)
119
+ m
120
+ end
121
+ @r.extenttemplate.render(ret.merge(type: hash[:type]))
122
+ end.join("; ")
123
+ end
124
+
125
+ def extentformat1(key, val, hash, norm_hash)
126
+ if %i(volume page).include?(key)
127
+ hash["#{key}_raw".to_sym] = norm_hash[key]
128
+ hash[key] = pagevolformat(norm_hash[key], val, key.to_s, false)
129
+ end
130
+ end
131
+
132
+ def range(hash)
133
+ if hash[:on] then hash[:on]
134
+ elsif hash.has_key?(:from) && hash[:from].nil? then nil
135
+ elsif hash[:from]
136
+ hash[:to] ? "#{hash[:from]}&#x2013;#{hash[:to]}" : hash[:from]
137
+ else hash
138
+ end
139
+ end
140
+
141
+ def sizeformat(size, hash)
142
+ return nil unless size
143
+
144
+ ret = size.transform_values { |v| @r.i18n.l10n(v.join(" + ")) }
145
+ .each_with_object({}) do |(k, v), m|
146
+ sizeformat1(k, v, m)
147
+ m
148
+ end
149
+ @r.sizetemplate.render(ret.merge(type: hash[:type]))
150
+ end
151
+
152
+ def sizeformat1(key, val, hash)
153
+ case key
154
+ when "volume"
155
+ hash[:volume_raw] = val
156
+ hash[:volume] = pagevolformat(val, nil, "volume", true)
157
+ when "page"
158
+ hash[:page_raw] = val
159
+ hash[:page] = pagevolformat(val, nil, "page", true)
160
+ when "data" then hash[:data] = val
161
+ when "duration" then hash[:duration] = val
162
+ end
163
+ end
164
+
165
+ def pagevolformat(value, value_raw, type, is_size)
166
+ return nil if value.nil?
167
+
168
+ num = "pl"
169
+ if is_size
170
+ value == "1" and num = "sg"
171
+ else
172
+ value_raw[:to] or num = "sg"
173
+ end
174
+ @r.i18n.l10n(@r.i18n.get[is_size ? "size" : "extent"][type][num]
175
+ .sub(/%/, value))
176
+ end
177
+
178
+ def date_range(hash)
179
+ if hash[:from]
180
+ "#{hash[:from]}&#x2013;#{hash[:to]}"
181
+ else range(hash)
182
+ end
183
+ end
184
+
185
+ def dateformat(date, _hash)
186
+ return nil if date.nil?
187
+
188
+ %i(from to on).each do |k|
189
+ date[k] = ::Relaton::Render::Date.new(date[k], renderer: @r).render
190
+ end
191
+ date_range(date)
192
+ end
193
+
194
+ def uriformat(uri)
195
+ return nil if uri.nil? || uri.empty?
196
+
197
+ "<link target='#{uri}'>#{uri}</link>"
198
+ end
199
+
200
+ private
201
+
202
+ def tw_cldr_lang
203
+ if @r.lang != "zh"
204
+ @r.lang.to_sym
205
+ elsif @r.script == "Hant"
206
+ :"zh-tw"
207
+ else
208
+ :"zh-cn"
209
+ end
210
+ end
211
+ end
212
+ end
213
+ end
@@ -1,45 +1,49 @@
1
1
  nametemplate:
2
- one: "{{surname[0] | upcase }}, {{given[0]}} {{middle[0}}"
3
- two: "{{surname[0] | upcase}}, {{given[0]}} {{middle[0]}} {{ labels['and'] }} {{given[1]}} {{middle[1]}} {{surname[1] | upcase}}"
4
- more: "{{surname[0] | upcase}}, {{given[0]}} {{middle[0}} , {{given[1]}} {{middle[1]}} {{surname[1] | upcase}} {{ labels['and'] }} {{given[2]}} {{middle[2]}} {{surname[2] | upcase}}"
2
+ one: "{% if nonpersonal[0] %}{{ nonpersonal[0] }}{% else %}{{surname[0] | upcase }} ,_{%if given[0]%}{{given[0]}} {{middle[0]}}{%else%}{{initials[0] | join: '. '}}.{%endif%}{% endif %}"
3
+ two: "{% if nonpersonal[0] %}{{ nonpersonal[0] }}{% else %}{{surname[0] | upcase}} ,_{%if given[0]%}{{given[0]}} {{middle[0]}}{%else%}{{initials[0] | join: '. '}}.{%endif%}{% endif %} {{ labels['and'] }} {% if nonpersonal[1] %}{{ nonpersonal[1] }}{% else %}{%if given[1]%}{{given[1]}} {{middle[1]}}{%else%}{{initials[1] | join: '. '}}.{%endif%} {{surname[1] | upcase}}{% endif %}"
4
+ more: "{% if nonpersonal[0] %}{{ nonpersonal[0] }}{% else %}{{surname[0] | upcase}} ,_{%if given[0]%}{{given[0]}} {{middle[0]}}{%else%}{{initials[0] | join: '. '}}.{%endif%}{% endif %} , {% if nonpersonal[1] %}{{ nonpersonal[1] }}{% else %}{%if given[1]%}{{given[1]}} {{middle[1]}}{%else%}{{initials[1] | join: '. '}}.{%endif%} {{surname[1] | upcase}}{% endif %} {{ labels['and'] }} {% if nonpersonal[2] %}{{ nonpersonal[2] }}{% else %}{%if given[2]%}{{given[2]}} {{middle[2]}}{%else%}{{initials[2] | join: '. '}}.{%endif%} {{surname[2] | upcase}}{% endif %}"
5
5
  # disabled the following: they should be provided in inheriting calls
6
- # etal: "{{surname[0] | upcase}}, {{given[0]}} {{middle[0]}}, {{given[1]}} {{middle[1]}} {{surname[1] | upcase}} <em>et al.</em>"
6
+ # etal: "{% if nonpersonal[0] %}{{ nonpersonal[0] }}{% else %}{{surname[0] | upcase}} ,_{%if given[0]%}{{given[0]}} {{middle[0]}}{%else%}{{initials[0] | join: '. '}}.{%endif%}{% endif %}, {% if nonpersonal[1] %}{{ nonpersonal[1] }}{% else %}{%if given[1]%}{{given[1]}} {{middle[1]}}{%else%}{{initials[1] | join: '. '}}.{%endif%} {{surname[1] | upcase}}{% endif %} <em>et al.</em>"
7
7
  # etal_count: 5
8
- seriestemplate: "{% if series_abbr %}{{series_abbr}}{% else %}{{series_title}}{% endif %} ({{series_run}}) {{series_num}}|({{series_partnumber}})"
8
+ seriestemplate: "{% if series_formatted %}{{ series_formatted }}{%else%}{% if series_abbr %}{{series_abbr}}{% else %}{{series_title}}{% endif %} ({{series_run}}) {{series_num}}|({{series_partnumber}}){%endif%}"
9
9
  journaltemplate: "<em>{% if series_abbr %}{{series_abbr}}{% else %}{{series_title}}{% endif %}</em> ({{series_run}}) {{ labels['volume'] }}_{{series_num}} {{ labels['part'] }}_{{series_partnumber}}"
10
10
  extenttemplate:
11
- book: "{{ volume }}_{{ labels['extent']['volume'] }}, {{ page }}_{{ labels['extent']['page'] }}"
11
+ book: "{{ volume }}, {{ page }}"
12
12
  booklet: book
13
13
  proceedings: book
14
14
  journal: book
15
15
  standard: book
16
16
  techreport: book
17
17
  inbook: "{{ volume }}: {{ page }}"
18
- misc: "{{ labels['extent']['volume'] }}_{{ volume }}, {{ labels['extent']['page'] }}_{{ page }}"
19
- # TODO: software extent
18
+ misc: "{{ volume }}, {{ page }}, {{ duration }}"
19
+ sizetemplate:
20
+ dataset: "{{ data }}"
21
+ electronic resource: dataset
22
+ webresource: dataset
23
+ misc: "{{ volume }}, {{ page }}, {{ data }}, {{ duration }}"
20
24
  language: en
21
25
  script: Latn
22
26
  template:
23
- book: "{{ creatornames }} ({{role}}) . <em>{{ title }}</em> [{{medium}}] . {{ edition | capitalize }}. ({{ series }}.) {{place}}: {{publisher}}. {{date}}. {{ labels['updated'] | capitalize }}:_{{date_updated}}. {{ standardidentifier | first }}. {{ uri }}. {{ labels['at'] | capitalize}}:_{{ access_location }}. [{{ labels['viewed'] }}:_{{date_accessed}}]. {{extent}}."
27
+ book: "{{ creatornames }} ({{role}}) . <em>{{ title }}</em> [{{medium}}] . {{ edition | capitalize_first }}. ({{ series }}.) {{place}}: {{publisher}}. {{date}}. {{ labels['updated'] | capitalize }}:_{{date_updated}}. {{ standardidentifier | first }}. {{ uri }}. {{ labels['at'] | capitalize}}:_{{ access_location }}. [{{ labels['viewed'] }}:_{{date_accessed}}]. {{size}}. {{extent}}."
24
28
  # TODO: omitted: author ids, subsidiary titles, subsidiary creators, rights metadata, distributor, item attributes, relationships
25
29
  booklet: book
26
30
  manual: book
27
31
  proceedings: book
28
- inbook: "{{ creatornames }} ({{role}}) . {{ title }} . {{ labels['in'] | capitalize }}: {{ host_creatornames}} ({{ host_role}}) : <em>{{host_title}}</em> [{{medium}}] . {{ edition | capitalize }}. ({{ series }}.) {{place}}: {{publisher}}. {{date}}. {{extent}}. {{ labels['updated'] | capitalize }}:_{{date_updated}}. {{ standardidentifier | first }}. {{ uri }}. {{ labels['at'] | capitalize}}:_{{ access_location }}. [{{ labels['viewed'] }}:_{{date_accessed}}]."
32
+ inbook: "{{ creatornames }} ({{role}}) . {{ title }} . {{ labels['in'] | capitalize }}: {{ host_creatornames}} ({{ host_role}}) : <em>{{host_title}}</em> [{{medium}}] . {{ edition | capitalize_first }}. ({{ series }}.) {{place}}: {{publisher}}. {{date}}. {{size}}. {{extent}}. {{ labels['updated'] | capitalize }}:_{{date_updated}}. {{ standardidentifier | first }}. {{ uri }}. {{ labels['at'] | capitalize}}:_{{ access_location }}. [{{ labels['viewed'] }}:_{{date_accessed}}]."
29
33
  inproceedings: inbook
30
34
  incollection: inbook
31
35
  # TODO: omitted: author ids, additional info for component part, subsidiary titles of host resource, rights metadata, distributor, relationships
32
- journal: "<em>{{ title}}</em> [{{medium}}] . {{ edition | capitalize }}. {{place}}: {{publisher}}. {{date}}. {{extent}}. {{ standardidentifier | first }}. {{ uri }}. {{ labels['at'] | capitalize}}:_{{ access_location }}. [{{ labels['viewed'] }}:_{{date_accessed}}]."
36
+ journal: "<em>{{ title}}</em> [{{medium}}] . {{ edition | capitalize_first }}. {{place}}: {{publisher}}. {{date}}. {{size}}. {{extent}}. {{ standardidentifier | first }}. {{ uri }}. {{ labels['at'] | capitalize}}:_{{ access_location }}. [{{ labels['viewed'] }}:_{{date_accessed}}]."
33
37
  # TODO subsidiary titles, rights metadata, item attributes, relationships
34
- article: "{{ creatornames }} ({{role}}) . {{ title }}. {{ series }} [{{medium}}] . {{ edition | capitalize }}. {{ extent }}. {{place}}: {{publisher}}. {{date}}. {{ labels['updated'] | capitalize }}:_{{date_updated}}. {{ standardidentifier | first }}. {{ uri }}. {{ labels['at'] | capitalize}}:_{{ access_location }}. [{{ labels['viewed'] }}:_{{date_accessed}}]."
38
+ article: "{{ creatornames }} ({{role}}) . {{ title }}. {{ series }} [{{medium}}] . {{ edition | capitalize_first }}. {{size}}. {{ extent }}. {{place}}: {{publisher}}. {{date}}. {{ labels['updated'] | capitalize }}:_{{date_updated}}. {{ standardidentifier | first }}. {{ uri }}. {{ labels['at'] | capitalize}}:_{{ access_location }}. [{{ labels['viewed'] }}:_{{date_accessed}}]."
35
39
  # TODO: omitted: author ids, subsidiary titles, rights metadata, distributor, item attributes, relationships; newspapers
36
- software: "{{ creatornames }} ({{ role}}) . <em>{{ title }}</em> . {{ labels['version'] | capitalize }}_{{ edition_raw }}. {{medium | capitalize}}. {{place}}: {{publisher}}. {{date}}. {{ labels['updated'] | capitalize }}:_{{date_updated}}. {{ standardidentifier | first }}. {{ uri }}. {{ extent}}. [{{ labels['viewed'] }}:_{{date_accessed}}]. "
40
+ software: "{{ creatornames }} ({{ role}}) . <em>{{ title }}</em> . {{ labels['version'] | capitalize }}_{{ edition_raw }}. {{medium | capitalize}}. {{place}}: {{publisher}}. {{date}}. {{ labels['updated'] | capitalize }}:_{{date_updated}}. {{ standardidentifier | first }}. {{ uri }}. {{size}}. {{ extent}}. [{{ labels['viewed'] }}:_{{date_accessed}}]. "
37
41
  # TODO: omitted: author ids, subsidiary titles, subsidiary creators, copyright, license, distributor, system requirements, relationships
38
42
  electronic resource: software
39
- standard: "{{ creatornames }} ({{ role}}) . <em>{{ title }}</em> . {{ labels['in'] | capitalize }}:_{{ series_title }}. {{ medium | capitalize }}. {{ edition | capitalize }}. {{place}}: {{publisher}}. {{date}}. {{ extent }}. {{ standardidentifier | first }}. {{ uri }}. {{ labels['at'] | capitalize}}:_{{ access_location }}. [{{ labels['viewed'] }}:_{{date_accessed}}]."
43
+ standard: "{{ creatornames }} ({{ role}}) . <em>{{ title }}</em> . {{ labels['in'] | capitalize }}:_{{ series_title }}. {{ medium | capitalize }}. {{ edition | capitalize_first }}. {{place}}: {{publisher}}. {{date}}. {{size}}. {{ extent }}. {{ standardidentifier | first }}. {{ uri }}. {{ labels['at'] | capitalize}}:_{{ access_location }}. [{{ labels['viewed'] }}:_{{date_accessed}}]."
40
44
  # TODO: omitted: author ids, subsidiary titles, rights metadata, distributor, persistent identifier, item attributes, relationships
41
45
  techreport: standard
42
- dataset: "{{ creatornames }} ({{ role }}) . <em>{{ title }}</em> . {{ labels['version'] | capitalize }}_{{ edition_raw }}. {{medium | capitalize }}. {{ labels['in'] | capitalize }}:_{{series}}. {{date}}. {{ labels['updated'] | capitalize }}:_{{date_updated}}. {{ standardidentifier | first }}. {{ uri }}. {{ extent}}. [{{ labels['viewed'] }}:_{{date_accessed}}]. "
46
+ dataset: "{{ creatornames }} ({{ role }}) . <em>{{ title }}</em> . {{ labels['version'] | capitalize }}_{{ edition_raw }}. {{medium | capitalize }}. {{ labels['in'] | capitalize }}:_{{series}}. {{date}}. {{ labels['updated'] | capitalize }}:_{{date_updated}}. {{ standardidentifier | first }}. {{ uri }}. {{ size }}. {{ extent}}. [{{ labels['viewed'] }}:_{{date_accessed}}]. "
43
47
  # TODO: omitted: author ids, system requirements, host archive, copyright, license, data source, distributor, usage instructions, relationships, provenance
44
48
  website: "{{ creatornames }} ({{ role }}) . <em>{{ title }}</em> . {{ labels['version'] | capitalize }}_{{ edition_raw }}. {{medium | capitalize }}. {{ place }}: {{ publisher }}. {{date}}. {{ labels['updated'] | capitalize }}:_{{date_updated}}. {{ standardidentifier | first }}. {{ uri }}. [{{ labels['viewed'] }}:_{{date_accessed}}]. "
45
49
  # TODO: omitted: author ids, page title, system requirements, subsidiary creator, rights metadata, distributor, persistent identifier, archive location, archive date and time, relationships
@@ -49,19 +53,20 @@ template:
49
53
  # TODO: omitted: author ids, subsidiary titles, subsidiary creators, host archive, item attributes, relationships
50
54
  presentation: unpublished
51
55
  thesis: "{{ creatornames }} ({{ role }}) . <em>{{ title }}</em> . {{ medium | capitalize }}. {{ place }}: {{ publisher }}. {{ date }}. {{ labels['at'] | capitalize}}:_{{ access_location }}. {{ standardidentifier | first }}. {{ uri }}. [{{ labels['viewed'] }}:_{{date_accessed}}]."
52
- # unsupported types:
53
- # map: ""
54
- # audiovisual: ""
55
- # film: ""
56
- # video: ""
57
- # broadcast: ""
58
- # graphic_work: ""
59
- # music: ""
60
- # performance: "" (not in Relaton)
61
- # patent: ""
62
- # archival: ""
63
- # social_media: ""
64
- # alert: ""
65
- # message: ""
66
- # conversation: ""
67
- # misc: ""
56
+ misc: "{{ creatornames }} ({{ role }}) . <em>{{ title }}</em> . {{ date }}."
57
+ # following are # unsupported types:
58
+ map: misc
59
+ audiovisual: misc
60
+ film: misc
61
+ video: misc
62
+ broadcast: misc
63
+ graphic_work: misc
64
+ music: misc
65
+ performance: misc
66
+ patent: misc
67
+ archival: misc
68
+ social_media: misc
69
+ alert: misc
70
+ message: misc
71
+ conversation: misc
72
+ internal: misc
@@ -1,44 +1,58 @@
1
1
  require_relative "render_classes"
2
- require_relative "render_fields"
3
2
  require "yaml"
4
3
  require "liquid"
5
- require_relative "../isodoc/i18n"
6
- require_relative "../utils/utils"
4
+ require "relaton_bib"
5
+ require_relative "../template/template"
6
+ require_relative "../../../isodoc/i18n"
7
7
 
8
8
  module Relaton
9
9
  module Render
10
10
  class General
11
11
  attr_reader :template, :journaltemplate, :seriestemplate, :nametemplate,
12
- :extenttemplate, :lang, :script, :edition, :edition_number,
13
- :date
12
+ :extenttemplate, :sizetemplate, :lang, :script, :i18n,
13
+ :edition, :edition_number, :date
14
14
 
15
15
  def initialize(opt = {})
16
- options = YAML.load_file(File.join(File.dirname(__FILE__),
17
- "config.yml"))
18
- .merge(Utils::string_keys(opt))
19
- @type = self.class.name.downcase.sub(/relaton::render::/, "")
16
+ options = read_config.merge(Utils::string_keys(opt))
17
+ @type = self.class.name.downcase.split("::").last
18
+ klass_initialize(options)
20
19
  root_initalize(options)
21
20
  render_initialize(options)
22
21
  @parse ||= options["parse"]
23
22
  @i18n ||= options["i18n"]
24
23
  end
25
24
 
25
+ def read_config
26
+ YAML.load_file(File.join(File.dirname(__FILE__), "config.yml"))
27
+ end
28
+
29
+ def klass_initialize(_options)
30
+ @nametemplateklass = Relaton::Render::Template::Name
31
+ @seriestemplateklass = Relaton::Render::Template::Series
32
+ @extenttemplateklass = Relaton::Render::Template::Extent
33
+ @sizetemplateklass = Relaton::Render::Template::Size
34
+ @generaltemplateklass = Relaton::Render::Template::General
35
+ @fieldsklass = Relaton::Render::Fields
36
+ @parseklass = Relaton::Render::Parse
37
+ end
38
+
26
39
  def root_initalize(opt)
27
40
  i18n_initialize(opt)
28
- @parse = Iso690Parse.new
29
- @nametemplate = Iso690NameTemplate
41
+ @parse = @parseklass.new(lang: @lang, script: @script)
42
+ @nametemplate = @nametemplateklass
30
43
  .new(template: opt["nametemplate"], i18n: @i18n)
31
- @seriestemplate = Iso690SeriesTemplate
44
+ @seriestemplate = @seriestemplateklass
32
45
  .new(template: opt["seriestemplate"], i18n: @i18n)
33
- @journaltemplate = Iso690SeriesTemplate
46
+ @journaltemplate = @seriestemplateklass
34
47
  .new(template: opt["journaltemplate"], i18n: @i18n)
35
48
  @extenttemplate = extentrenderers(opt)
49
+ @sizetemplate = sizerenderers(opt)
36
50
  end
37
51
 
38
52
  def i18n_initialize(opt)
39
53
  @lang = opt["language"]
40
54
  @script = opt["script"]
41
- @i18n = i18n(opt["language"], opt["script"])
55
+ @i18n = i18n_klass(opt["language"], opt["script"])
42
56
  @edition_number = opt["edition_number"] || @i18n.edition_number
43
57
  @edition = opt["edition"] || @i18n.edition
44
58
  @date = opt["date"] || @i18n.date
@@ -47,8 +61,8 @@ module Relaton
47
61
  def render_initialize(opt)
48
62
  case opt["template"]
49
63
  when String
50
- @template = Iso690Template.new(template: opt["template"],
51
- i18n: @i18n)
64
+ @template = @generaltemplateklass
65
+ .new(template: opt["template"], i18n: @i18n)
52
66
  when Hash
53
67
  @render = renderers(opt)
54
68
  end
@@ -64,48 +78,53 @@ module Relaton
64
78
  end
65
79
 
66
80
  def extentrenderers(opt)
67
- Iso690ExtentTemplate
81
+ @extenttemplateklass
68
82
  .new(template: template_hash_fill(opt["extenttemplate"]), i18n: @i18n)
69
83
  end
70
84
 
85
+ def sizerenderers(opt)
86
+ @sizetemplateklass
87
+ .new(template: template_hash_fill(opt["sizetemplate"]), i18n: @i18n)
88
+ end
89
+
71
90
  def default_template
72
91
  "{{creatornames}}. {{title}}. {{date}}."
73
92
  end
74
93
 
75
- def i18n(lang = "en", script = "Latn")
76
- ::IsoDoc::I18n.new(lang, script)
94
+ def i18n_klass(lang = "en", script = "Latn")
95
+ ::IsoDoc::RelatonRenderI18n.new(lang, script)
77
96
  end
78
97
 
79
98
  def render(bib, embedded: false)
80
- docxml = Nokogiri::XML(bib)
81
- docxml.remove_namespaces!
82
- parse(docxml.root, embedded: embedded)
99
+ if bib.is_a?(String) && Nokogiri::XML(bib).errors.empty?
100
+ bib = RelatonBib::XMLParser.from_xml bib
101
+ end
102
+ parse(bib, embedded: embedded)
83
103
  end
84
104
 
85
105
  def parse(doc, embedded: false)
86
- f = doc.at("./formattedref") and
106
+ f = doc.formattedref and
87
107
  return embedded ? f.children.to_xml : doc.to_xml
88
108
 
89
- ret = parse1(doc)
109
+ ret = parse1(doc) or return nil
90
110
  embedded and return ret
91
111
  "<formattedref>#{ret}</formattedref>"
92
112
  end
93
113
 
94
- def renderer(doc)
95
- unless ret = @template || @render[doc["type"]]&.template
96
- raise "No renderer defined for #{doc['type']}"
97
- end
98
- unless @type == "general" || @type == doc["type"]
99
- raise "No renderer defined for #{doc['type']}"
100
- end
114
+ def renderer(type)
115
+ ret = @template || @render[type]&.template or
116
+ raise "No renderer defined for #{type}"
117
+ @type == "general" || @type == type or
118
+ raise "No renderer defined for #{type}"
101
119
 
102
120
  ret
103
121
  end
104
122
 
105
123
  def parse1(doc)
106
- r = renderer(doc)
124
+ r = renderer(doc.type || "misc")
107
125
  data = @parse.extract(doc)
108
- data_liquid = Fields.new(renderer: self).compound_fields_format(data)
126
+ data_liquid = @fieldsklass.new(renderer: self)
127
+ .compound_fields_format(data)
109
128
  @i18n.l10n(r.render(data_liquid))
110
129
  end
111
130
 
@@ -5,7 +5,7 @@ module Relaton
5
5
  standard unpublished map electronic_resource audiovisual film video
6
6
  broadcast software graphic_work music patent inbook incollection
7
7
  inproceedings journal website webresource dataset archival social_media
8
- alert message conversation misc).freeze
8
+ alert message conversation misc internal).freeze
9
9
 
10
10
  class General
11
11
  singleton_class.send(:attr_reader, :descendants)
@@ -122,5 +122,8 @@ module Relaton
122
122
 
123
123
  class Misc < General
124
124
  end
125
+
126
+ class Internal < General
127
+ end
125
128
  end
126
129
  end
@@ -0,0 +1,63 @@
1
+ require "nokogiri"
2
+ require "twitter_cldr"
3
+ require_relative "parse_contributors"
4
+ require_relative "parse_extract"
5
+
6
+ module Relaton
7
+ module Render
8
+ class Parse
9
+ def initialize(options)
10
+ @lang = options[:lang] || "en"
11
+ @script = options[:script] || "Latn"
12
+ end
13
+
14
+ def extract(doc)
15
+ host = host(doc)
16
+ simple_xml2hash(doc).merge(simple_or_host_xml2hash(doc, host))
17
+ .merge(host_xml2hash(host))
18
+ .merge(series_xml2hash(doc, host))
19
+ end
20
+
21
+ def simple_xml2hash(doc)
22
+ creators, role = creatornames(doc)
23
+ { type: type(doc), title: title(doc), extent_raw: extent(doc),
24
+ size_raw: size(doc),
25
+ standardidentifier: standardidentifier(doc), uri_raw: uri(doc),
26
+ status: status(doc), creators: creators, role_raw: role }
27
+ end
28
+
29
+ def simple_or_host_xml2hash(doc, host)
30
+ { edition_raw: edition(doc, host), medium_raw: medium(doc, host),
31
+ place_raw: place(doc, host), publisher_raw: publisher(doc, host),
32
+ publisher_abbrev_raw: publisher_abbrev(doc, host),
33
+ distributor_raw: distributor(doc, host), draft_raw: draft(doc, host),
34
+ access_location: access_location(doc, host),
35
+ date: date(doc, host), date_updated: date_updated(doc, host),
36
+ date_accessed: date_accessed(doc, host) }
37
+ end
38
+
39
+ def host_xml2hash(host)
40
+ creators, role = creatornames(host)
41
+ { host_creators: creators, host_role_raw: role,
42
+ host_title: title(host) }
43
+ end
44
+
45
+ def series_xml2hash(doc, host)
46
+ series = series(doc)
47
+ host and series ||= series(host)
48
+ series_xml2hash1(series, doc)
49
+ end
50
+
51
+ def series_xml2hash1(series, doc)
52
+ return {} unless series
53
+
54
+ { series_formatted: series_formatted(series, doc),
55
+ series_title: series_title(series, doc),
56
+ series_abbr: series_abbr(series, doc),
57
+ series_run: series_run(series, doc),
58
+ series_num: series_num(series, doc),
59
+ series_partnumber: series_partnumber(series, doc) }
60
+ end
61
+ end
62
+ end
63
+ end