Class: Relaton::Render::General

Inherits:
Object
  • Object
show all
Defined in:
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.



15
16
17
18
19
20
21
22
# File 'lib/relaton/render/general/render.rb', line 15

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

#dateObject (readonly)

Returns the value of attribute date.



11
12
13
# File 'lib/relaton/render/general/render.rb', line 11

def date
  @date
end

#editionObject (readonly)

Returns the value of attribute edition.



11
12
13
# File 'lib/relaton/render/general/render.rb', line 11

def edition
  @edition
end

#edition_ordinalObject (readonly)

Returns the value of attribute edition_ordinal.



11
12
13
# File 'lib/relaton/render/general/render.rb', line 11

def edition_ordinal
  @edition_ordinal
end

#extenttemplateObject (readonly)

Returns the value of attribute extenttemplate.



11
12
13
# File 'lib/relaton/render/general/render.rb', line 11

def extenttemplate
  @extenttemplate
end

#i18nObject (readonly)

Returns the value of attribute i18n.



11
12
13
# File 'lib/relaton/render/general/render.rb', line 11

def i18n
  @i18n
end

#journaltemplateObject (readonly)

Returns the value of attribute journaltemplate.



11
12
13
# File 'lib/relaton/render/general/render.rb', line 11

def journaltemplate
  @journaltemplate
end

#langObject (readonly)

Returns the value of attribute lang.



11
12
13
# File 'lib/relaton/render/general/render.rb', line 11

def lang
  @lang
end

#nametemplateObject (readonly)

Returns the value of attribute nametemplate.



11
12
13
# File 'lib/relaton/render/general/render.rb', line 11

def nametemplate
  @nametemplate
end

#scriptObject (readonly)

Returns the value of attribute script.



11
12
13
# File 'lib/relaton/render/general/render.rb', line 11

def script
  @script
end

#seriestemplateObject (readonly)

Returns the value of attribute seriestemplate.



11
12
13
# File 'lib/relaton/render/general/render.rb', line 11

def seriestemplate
  @seriestemplate
end

#sizetemplateObject (readonly)

Returns the value of attribute sizetemplate.



11
12
13
# File 'lib/relaton/render/general/render.rb', line 11

def sizetemplate
  @sizetemplate
end

#templateObject (readonly)

Returns the value of attribute template.



11
12
13
# File 'lib/relaton/render/general/render.rb', line 11

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

#default_templateObject



93
94
95
# File 'lib/relaton/render/general/render.rb', line 93

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

#extentrenderers(opt) ⇒ Object



83
84
85
86
# File 'lib/relaton/render/general/render.rb', line 83

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

#i18n_initialize(opt) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/relaton/render/general/render.rb', line 51

def i18n_initialize(opt)
  @lang = opt["language"]
  @script = opt["script"]
  @i18n = opt["i18n"] ||
    i18n_klass(opt["language"], opt["script"], 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(lang = "en", script = "Latn", i18nhash = nil) ⇒ Object



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

def i18n_klass(lang = "en", script = "Latn", i18nhash = nil)
  ::IsoDoc::RelatonRenderI18n.new(lang, script, i18nhash: i18nhash)
end

#klass_initialize(_options) ⇒ Object



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

def klass_initialize(_options)
  @nametemplateklass = Relaton::Render::Template::Name
  @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



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

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



126
127
128
129
130
131
132
# File 'lib/relaton/render/general/render.rb', line 126

def parse1(doc)
  r = renderer(doc.type || "misc")
  data = @parse.extract(doc)
  data_liquid = @fieldsklass.new(renderer: self)
    .compound_fields_format(data)
  @i18n.l10n(r.render(data_liquid))
end

#read_configObject



24
25
26
# File 'lib/relaton/render/general/render.rb', line 24

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

#render(bib, embedded: false) ⇒ Object



101
102
103
104
105
106
# File 'lib/relaton/render/general/render.rb', line 101

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_initialize(opt) ⇒ Object



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

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



117
118
119
120
121
122
123
124
# File 'lib/relaton/render/general/render.rb', line 117

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



74
75
76
77
78
79
80
81
# File 'lib/relaton/render/general/render.rb', line 74

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



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

def root_initalize(opt)
  i18n_initialize(opt)
  @parse = @parseklass.new(lang: @lang, script: @script)
  @nametemplate = @nametemplateklass
    .new(template: opt["nametemplate"], 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

#sizerenderers(opt) ⇒ Object



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

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