Class: Relaton::Render::General

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton/render/general/uri.rb,
lib/relaton/render/general/render.rb,
lib/relaton/render/general/render_classes.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ General

Returns a new instance of General.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/relaton/render/general/render.rb', line 19

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"]
  @semaphore = Mutex.new
  @urlcache = {}
  @url_warned = {}
end

Instance Attribute Details

#authorcitetemplateObject (readonly)

Returns the value of attribute authorcitetemplate.



14
15
16
# File 'lib/relaton/render/general/render.rb', line 14

def authorcitetemplate
  @authorcitetemplate
end

#dateObject (readonly)

Returns the value of attribute date.



14
15
16
# File 'lib/relaton/render/general/render.rb', line 14

def date
  @date
end

#dateklassObject (readonly)

Returns the value of attribute dateklass.



14
15
16
# File 'lib/relaton/render/general/render.rb', line 14

def dateklass
  @dateklass
end

#editionObject (readonly)

Returns the value of attribute edition.



14
15
16
# File 'lib/relaton/render/general/render.rb', line 14

def edition
  @edition
end

#edition_ordinalObject (readonly)

Returns the value of attribute edition_ordinal.



14
15
16
# File 'lib/relaton/render/general/render.rb', line 14

def edition_ordinal
  @edition_ordinal
end

#extenttemplateObject (readonly)

Returns the value of attribute extenttemplate.



14
15
16
# File 'lib/relaton/render/general/render.rb', line 14

def extenttemplate
  @extenttemplate
end

#fieldsklassObject (readonly)

Returns the value of attribute fieldsklass.



14
15
16
# File 'lib/relaton/render/general/render.rb', line 14

def fieldsklass
  @fieldsklass
end

#i18nObject (readonly)

Returns the value of attribute i18n.



14
15
16
# File 'lib/relaton/render/general/render.rb', line 14

def i18n
  @i18n
end

#journaltemplateObject (readonly)

Returns the value of attribute journaltemplate.



14
15
16
# File 'lib/relaton/render/general/render.rb', line 14

def journaltemplate
  @journaltemplate
end

#langObject (readonly)

Returns the value of attribute lang.



14
15
16
# File 'lib/relaton/render/general/render.rb', line 14

def lang
  @lang
end

#nametemplateObject (readonly)

Returns the value of attribute nametemplate.



14
15
16
# File 'lib/relaton/render/general/render.rb', line 14

def nametemplate
  @nametemplate
end

#scriptObject (readonly)

Returns the value of attribute script.



14
15
16
# File 'lib/relaton/render/general/render.rb', line 14

def script
  @script
end

#seriestemplateObject (readonly)

Returns the value of attribute seriestemplate.



14
15
16
# File 'lib/relaton/render/general/render.rb', line 14

def seriestemplate
  @seriestemplate
end

#sizetemplateObject (readonly)

Returns the value of attribute sizetemplate.



14
15
16
# File 'lib/relaton/render/general/render.rb', line 14

def sizetemplate
  @sizetemplate
end

#templateObject (readonly)

Returns the value of attribute template.



14
15
16
# File 'lib/relaton/render/general/render.rb', line 14

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) # rubocop:disable Lint/MissingSuper
  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

#access_url(url) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/relaton/render/general/uri.rb', line 19

def access_url(url)
  path = url.path or return false
  path.empty? and path = "/"
  url_head(url, path)
rescue StandardError => e
  warn e
  false
end

#add_date_accessed(data, template) ⇒ Object



209
210
211
212
213
214
215
216
217
# File 'lib/relaton/render/general/render.rb', line 209

def add_date_accessed(data, template)
  (/\{\{\s*date_accessed\s*\}\}/.match?(template) &&
    /\{\{\s*uri\s*\}\}/.match?(template) &&
    data[:uri_raw] && !data[:date_accessed]) or return
  if url_exist?(data[:uri_raw])
    data[:date_accessed] = { on: ::Date.today.to_s }
  else url_warn(data[:uri_raw])
  end
end

#citations1(bib) ⇒ Object



194
195
196
197
198
199
200
201
# File 'lib/relaton/render/general/render.rb', line 194

def citations1(bib)
  bib.each_with_object([]).with_index do |(b, m), i|
    data_liquid = @fieldsklass.new(renderer: self)
      .compound_fields_format(@parse.extract(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_templateObject



105
106
107
# File 'lib/relaton/render/general/render.rb', line 105

def default_template
  "{{creatornames}}. {{title}}. {{date}}."
end

#enhance_data(data, template) ⇒ Object

add to liquid data based on template



204
205
206
207
# File 'lib/relaton/render/general/render.rb', line 204

def enhance_data(data, template)
  template.is_a?(String) or return
  add_date_accessed(data, template)
end

#extentrenderers(opt) ⇒ Object



95
96
97
98
# File 'lib/relaton/render/general/render.rb', line 95

def extentrenderers(opt)
  @extenttemplateklass
    .new(template: template_hash_fill(opt["extenttemplate"]), i18n: @i18n)
end

#fmtref(doc) ⇒ Object



130
131
132
# File 'lib/relaton/render/general/render.rb', line 130

def fmtref(doc)
  "<formattedref>#{doc}</formattedref>"
end

#i18n_initialize(opt) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/relaton/render/general/render.rb', line 62

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



109
110
111
112
# File 'lib/relaton/render/general/render.rb', line 109

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



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/relaton/render/general/render.rb', line 35

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
  @dateklass = Relaton::Render::Date
  @parseklass = Relaton::Render::Parse
end

#liquid(data_liquid, renderer) ⇒ Object



154
155
156
# File 'lib/relaton/render/general/render.rb', line 154

def liquid(data_liquid, renderer)
  valid_parse(@i18n.l10n(renderer.render(data_liquid)))
end

#parse(doc) ⇒ Object



158
159
160
161
162
163
164
165
166
# File 'lib/relaton/render/general/render.rb', line 158

def parse(doc)
  doc = xml2relaton(doc)
  r = renderer(doc.type || "misc")
  data = @parse.extract(doc)
  enhance_data(data, r.template_raw)
  data_liquid = @fieldsklass.new(renderer: self)
    .compound_fields_format(data)
  [data_liquid, r]
end

#read_configObject



31
32
33
# File 'lib/relaton/render/general/render.rb', line 31

def read_config
  YAML.load_file(File.join(File.dirname(__FILE__), "config.yml"))
end

#render(bib, embedded: false) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/relaton/render/general/render.rb', line 114

def render(bib, embedded: false)
  bib = xml2relaton(bib)
  f = bib.formattedref and
    return embedded ? f.content : fmtref(f.content)
  ret = render1(bib) or return nil
  embedded and return ret
  fmtref(ret)
end

#render1(doc) ⇒ Object



142
143
144
145
146
147
# File 'lib/relaton/render/general/render.rb', line 142

def render1(doc)
  r = doc.relation.select { |x| x.type == "hasRepresentation" }
    .map { |x| @i18n.also_pub_as + render_single_bibitem(x.bibitem) }
  out = [render_single_bibitem(doc)] + r
  @i18n.l10n(out.join(". ").gsub(".. ", ". "))
end

#render_all(bib, type: "author-date") ⇒ Object

expect array of Relaton objects, in sorted order enhance_data is skipped here, and is done in batch inside Citations



175
176
177
178
179
# File 'lib/relaton/render/general/render.rb', line 175

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



76
77
78
79
80
81
82
83
84
# File 'lib/relaton/render/general/render.rb', line 76

def render_initialize(opt)
  case opt["template"]
  when String
    @template = @generaltemplateklass
      .new(template: opt["template"], i18n: @i18n)
  when Hash
    @render = renderers(opt)
  end
end

#render_single_bibitem(doc) ⇒ Object



149
150
151
152
# File 'lib/relaton/render/general/render.rb', line 149

def render_single_bibitem(doc)
  data_liquid, r = parse(doc)
  liquid(data_liquid, r)
end

#renderer(type) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/relaton/render/general/render.rb', line 134

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



86
87
88
89
90
91
92
93
# File 'lib/relaton/render/general/render.rb', line 86

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/relaton/render/general/render.rb', line 47

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

#sanitise_citations_input(bib) ⇒ Object



181
182
183
184
# File 'lib/relaton/render/general/render.rb', line 181

def sanitise_citations_input(bib)
  bib.is_a?(Array) and return bib
  bib.is_a?(String) and return sanitise_citations_input_string(bib)
end

#sanitise_citations_input_string(bib) ⇒ Object



186
187
188
189
190
191
192
# File 'lib/relaton/render/general/render.rb', line 186

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



100
101
102
103
# File 'lib/relaton/render/general/render.rb', line 100

def sizerenderers(opt)
  @sizetemplateklass
    .new(template: template_hash_fill(opt["sizetemplate"]), i18n: @i18n)
end

#url_exist?(url_string) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
# File 'lib/relaton/render/general/uri.rb', line 8

def url_exist?(url_string)
  return true # temporarily disabling validation of URIs
  url = URI.parse(url_string)
  url.host or return true # allow file URLs
  res = access_url(url) or return false
  res.is_a?(Net::HTTPRedirection) and return url_exist?(res["location"])
  res.code[0] != "4"
rescue Errno::ENOENT, SocketError
  false # false if can't find the server
end

#url_exist_async?(url_string) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/relaton/render/general/uri.rb', line 52

def url_exist_async?(url_string)
  { url: url_string, status: url_exist?(url_string) }
end

#url_head(url, path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/relaton/render/general/uri.rb', line 28

def url_head(url, path)
  ret = nil
  @semaphore.synchronize { ret = @urlcache[url.to_s] }
  ret and return ret
  ret = Net::HTTP.start(url.host, url.port,
                        read_timeout: 2, open_timeout: 2,
                        use_ssl: url.scheme == "https") do |http|
    http.request_head(path)
  end
  @semaphore.synchronize { @urlcache[url.to_s] = ret }
  ret
end

#url_warn(uri) ⇒ Object



219
220
221
222
223
# File 'lib/relaton/render/general/render.rb', line 219

def url_warn(uri)
  @url_warned[uri] and return
  warn "BIBLIOGRAPHY WARNING: cannot access #{uri}"
  @url_warned[uri] = true
end

#urls_exist_concurrent(urls) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/relaton/render/general/uri.rb', line 41

def urls_exist_concurrent(urls)
  responses = Concurrent::Array.new
  thread_pool = Concurrent::FixedThreadPool.new(5)
  urls.each do |u|
    thread_pool.post { responses << url_exist_async?(u) }
  end
  thread_pool.shutdown
  thread_pool.wait_for_termination
  responses.each_with_object({}) { |n, m| m[n[:url]] = n[:status] }
end

#valid_parse(ret) ⇒ Object



168
169
170
171
# File 'lib/relaton/render/general/render.rb', line 168

def valid_parse(ret)
  @i18n.get["no_date"] == ret and return nil
  ret
end

#xml2relaton(bib) ⇒ Object



123
124
125
126
127
128
# File 'lib/relaton/render/general/render.rb', line 123

def xml2relaton(bib)
  bib.is_a?(Nokogiri::XML::Element) and
    bib = bib.to_xml
  bib.is_a?(String) && Nokogiri::XML(bib).errors.empty? and
    bib = RelatonBib::XMLParser.from_xml(bib) or bib
end