Class: Relaton::Render::General
- Inherits:
-
Object
- Object
- Relaton::Render::General
show all
- Defined in:
- lib/relaton/render/general/render.rb,
lib/relaton/render/general/render_classes.rb
Direct Known Subclasses
Archival, Audiovisual, Book, Booklet, Conversation, Electronic_resource, Graphic_work, Hosted, Internal, Journal, Manual, Map, Misc, Music, Patent, Presentation, Proceedings, Standard, Techreport, Thesis, Unpublished
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(opt = {}) ⇒ General
Returns a new instance of General.
17
18
19
20
21
22
23
24
|
# File 'lib/relaton/render/general/render.rb', line 17
def initialize(opt = {})
options = read_config.merge(Utils::string_keys(opt))
@type = self.class.name.downcase.split("::").last
klass_initialize(options)
root_initalize(options)
render_initialize(options)
@parse ||= options["parse"]
end
|
Instance Attribute Details
#authorcitetemplate ⇒ Object
Returns the value of attribute authorcitetemplate.
12
13
14
|
# File 'lib/relaton/render/general/render.rb', line 12
def authorcitetemplate
@authorcitetemplate
end
|
#date ⇒ Object
Returns the value of attribute date.
12
13
14
|
# File 'lib/relaton/render/general/render.rb', line 12
def date
@date
end
|
#edition ⇒ Object
Returns the value of attribute edition.
12
13
14
|
# File 'lib/relaton/render/general/render.rb', line 12
def edition
@edition
end
|
#edition_ordinal ⇒ Object
Returns the value of attribute edition_ordinal.
12
13
14
|
# File 'lib/relaton/render/general/render.rb', line 12
def edition_ordinal
@edition_ordinal
end
|
#extenttemplate ⇒ Object
Returns the value of attribute extenttemplate.
12
13
14
|
# File 'lib/relaton/render/general/render.rb', line 12
def extenttemplate
@extenttemplate
end
|
#i18n ⇒ Object
Returns the value of attribute i18n.
12
13
14
|
# File 'lib/relaton/render/general/render.rb', line 12
def i18n
@i18n
end
|
#journaltemplate ⇒ Object
Returns the value of attribute journaltemplate.
12
13
14
|
# File 'lib/relaton/render/general/render.rb', line 12
def journaltemplate
@journaltemplate
end
|
#lang ⇒ Object
Returns the value of attribute lang.
12
13
14
|
# File 'lib/relaton/render/general/render.rb', line 12
def lang
@lang
end
|
#nametemplate ⇒ Object
Returns the value of attribute nametemplate.
12
13
14
|
# File 'lib/relaton/render/general/render.rb', line 12
def nametemplate
@nametemplate
end
|
#script ⇒ Object
Returns the value of attribute script.
12
13
14
|
# File 'lib/relaton/render/general/render.rb', line 12
def script
@script
end
|
#seriestemplate ⇒ Object
Returns the value of attribute seriestemplate.
12
13
14
|
# File 'lib/relaton/render/general/render.rb', line 12
def seriestemplate
@seriestemplate
end
|
#sizetemplate ⇒ Object
Returns the value of attribute sizetemplate.
12
13
14
|
# File 'lib/relaton/render/general/render.rb', line 12
def sizetemplate
@sizetemplate
end
|
#template ⇒ Object
Returns the value of attribute template.
12
13
14
|
# File 'lib/relaton/render/general/render.rb', line 12
def template
@template
end
|
Class Method Details
.inherited(subclass) ⇒ Object
rubocop:disable Lint/MissingSuper
14
15
16
17
|
# File 'lib/relaton/render/general/render_classes.rb', line 14
def self.inherited(subclass)
type = subclass.name.downcase.sub(/relaton::render::/, "")
General.descendants[type] = subclass
end
|
.subclass(type) ⇒ Object
19
20
21
|
# File 'lib/relaton/render/general/render_classes.rb', line 19
def self.subclass(type)
@descendants[type]
end
|
Instance Method Details
#citations1(bib) ⇒ Object
167
168
169
170
171
172
173
174
|
# File 'lib/relaton/render/general/render.rb', line 167
def citations1(bib)
bib.each_with_object([]).with_index do |(b, m), i|
data_liquid = @fieldsklass.new(renderer: self)
.compound_fields_format(@parse.(b))
m << { author: data_liquid[:authorcite], date: data_liquid[:date],
ord: i, id: b.id, data_liquid: data_liquid, type: b.type }
end
end
|
#default_template ⇒ Object
99
100
101
|
# File 'lib/relaton/render/general/render.rb', line 99
def default_template
"{{creatornames}}. {{title}}. {{date}}."
end
|
#extentrenderers(opt) ⇒ Object
89
90
91
92
|
# File 'lib/relaton/render/general/render.rb', line 89
def extentrenderers(opt)
@extenttemplateklass
.new(template: template_hash_fill(opt["extenttemplate"]), i18n: @i18n)
end
|
#i18n_initialize(opt) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/relaton/render/general/render.rb', line 56
def i18n_initialize(opt)
@lang = opt["language"]
@script = opt["script"]
@locale = opt["locale"]
@i18n = opt["i18n"] ||
i18n_klass(language: @lang, script: @script, locale: @locale,
i18nhash: opt["i18nhash"])
@edition_ordinal = opt["edition_ordinal"] || @i18n.edition_ordinal
@edition = opt["edition"] || @i18n.edition
@date = opt["date"] || @i18n.get["date_formats"] ||
{ "month_year" => "yMMMM", "day_month_year" => "to_long_s",
"date_time" => "to_long_s" }
end
|
#i18n_klass(language: "en", script: "Latn", locale: nil, i18nhash: nil) ⇒ Object
103
104
105
106
|
# File 'lib/relaton/render/general/render.rb', line 103
def i18n_klass(language: "en", script: "Latn", locale: nil, i18nhash: nil)
::IsoDoc::RelatonRenderI18n.new(language, script, locale: locale,
i18nhash: i18nhash)
end
|
#klass_initialize(_options) ⇒ Object
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/relaton/render/general/render.rb', line 30
def klass_initialize(_options)
@nametemplateklass = Relaton::Render::Template::Name
@authorcitetemplateklass = Relaton::Render::Template::AuthorCite
@seriestemplateklass = Relaton::Render::Template::Series
@extenttemplateklass = Relaton::Render::Template::Extent
@sizetemplateklass = Relaton::Render::Template::Size
@generaltemplateklass = Relaton::Render::Template::General
@fieldsklass = Relaton::Render::Fields
@parseklass = Relaton::Render::Parse
end
|
#parse(doc, embedded: false) ⇒ Object
115
116
117
118
119
120
121
122
|
# File 'lib/relaton/render/general/render.rb', line 115
def parse(doc, embedded: false)
f = doc.formattedref and
return embedded ? f.children.to_xml : doc.to_xml
ret = parse1(doc) or return nil
embedded and return ret
"<formattedref>#{ret}</formattedref>"
end
|
#parse1(doc) ⇒ Object
133
134
135
136
137
138
139
|
# File 'lib/relaton/render/general/render.rb', line 133
def parse1(doc)
r = renderer(doc.type || "misc")
data = @parse.(doc)
data_liquid = @fieldsklass.new(renderer: self)
.compound_fields_format(data)
valid_parse(@i18n.l10n(r.render(data_liquid)))
end
|
#read_config ⇒ Object
26
27
28
|
# File 'lib/relaton/render/general/render.rb', line 26
def read_config
YAML.load_file(File.join(File.dirname(__FILE__), "config.yml"))
end
|
#render(bib, embedded: false) ⇒ Object
108
109
110
111
112
113
|
# File 'lib/relaton/render/general/render.rb', line 108
def render(bib, embedded: false)
if bib.is_a?(String) && Nokogiri::XML(bib).errors.empty?
bib = RelatonBib::XMLParser.from_xml bib
end
parse(bib, embedded: embedded)
end
|
#render_all(bib, type: "author-date") ⇒ Object
expect array of Relaton objects, in sorted order
147
148
149
150
151
|
# File 'lib/relaton/render/general/render.rb', line 147
def render_all(bib, type: "author-date")
bib = sanitise_citations_input(bib) or return
Citations.new(type: type, renderer: self, i18n: @i18n)
.render(citations1(bib))
end
|
#render_initialize(opt) ⇒ Object
70
71
72
73
74
75
76
77
78
|
# File 'lib/relaton/render/general/render.rb', line 70
def render_initialize(opt)
case opt["template"]
when String
@template = @generaltemplateklass
.new(template: opt["template"], i18n: @i18n)
when Hash
@render = renderers(opt)
end
end
|
#renderer(type) ⇒ Object
124
125
126
127
128
129
130
131
|
# File 'lib/relaton/render/general/render.rb', line 124
def renderer(type)
ret = @template || @render[type]&.template or
raise "No renderer defined for #{type}"
@type == "general" || @type == type or
raise "No renderer defined for #{type}"
ret
end
|
#renderers(opt) ⇒ Object
80
81
82
83
84
85
86
87
|
# File 'lib/relaton/render/general/render.rb', line 80
def renderers(opt)
template_hash_fill(opt["template"]).each_with_object({}) do |(k, v), m|
@type == "general" || @type == k or next
m[k] = General.subclass(k)
.new(template: v, parse: @parse, i18n: @i18n,
language: @lang, script: @script)
end
end
|
#root_initalize(opt) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/relaton/render/general/render.rb', line 41
def root_initalize(opt)
i18n_initialize(opt)
@parse = @parseklass.new(lang: @lang, script: @script, i18n: @i18n)
@nametemplate = @nametemplateklass
.new(template: opt["nametemplate"], i18n: @i18n)
@authorcitetemplate = @authorcitetemplateklass
&.new(template: opt["authorcitetemplate"], i18n: @i18n)
@seriestemplate = @seriestemplateklass
.new(template: opt["seriestemplate"], i18n: @i18n)
@journaltemplate = @seriestemplateklass
.new(template: opt["journaltemplate"], i18n: @i18n)
@extenttemplate = extentrenderers(opt)
@sizetemplate = sizerenderers(opt)
end
|
153
154
155
156
|
# File 'lib/relaton/render/general/render.rb', line 153
def sanitise_citations_input(bib)
bib.is_a?(Array) and return bib
bib.is_a?(String) and return sanitise_citations_input_string(bib)
end
|
158
159
160
161
162
163
164
165
|
# File 'lib/relaton/render/general/render.rb', line 158
def sanitise_citations_input_string(bib)
p = Nokogiri::XML(bib) or return
(p.errors.empty? && p.root.at("./bibitem")) or return nil
p.root.xpath("./bibitem").each_with_object([]) do |b, m|
m << RelatonBib::XMLParser.from_xml(b.to_xml)
end
end
|
#sizerenderers(opt) ⇒ Object
94
95
96
97
|
# File 'lib/relaton/render/general/render.rb', line 94
def sizerenderers(opt)
@sizetemplateklass
.new(template: template_hash_fill(opt["sizetemplate"]), i18n: @i18n)
end
|
#valid_parse(ret) ⇒ Object
141
142
143
144
|
# File 'lib/relaton/render/general/render.rb', line 141
def valid_parse(ret)
@i18n.get["no_date"] == ret and return nil
ret
end
|