Class: Relaton::Render::Template::Name

Inherits:
General
  • Object
show all
Defined in:
lib/relaton/render/template/subclasses.rb

Direct Known Subclasses

AuthorCite

Instance Attribute Summary

Attributes inherited from General

#authorcitetemplate, #citeshorttemplate, #citetemplate, #config, #date, #dateklass, #edition, #extenttemplate, #fieldsklass, #i18n, #journaltemplate, #lang, #nametemplate, #script, #seriestemplate, #sizetemplate, #template

Instance Method Summary collapse

Methods inherited from General

#access_url, #add_date_accessed, #also_pub_as, #citation_renderers, #citations1, #citerenderers, #config_override, #default_template, #enhance_data, #esc_cleanup, #extentrenderers, #fmtref, #i18n_default_strs, #i18n_initialize, #i18n_klass, inherited, #init_misc, #init_options, #klass_initialize, #liquid, #parse, #read_config, #render, #render1, #render_all, #render_initialize, #render_single_bibitem, #renderer, #renderers, #root_initalize, #sanitise_citations_input, #sanitise_citations_input_string, #sizerenderers, subclass, #url_exist?, #url_exist_async?, #url_head, #url_warn, #urls_exist_concurrent, #valid_parse, #xml2relaton

Constructor Details

#initialize(opt = {}) ⇒ Name

Returns a new instance of Name.



20
21
22
23
24
25
26
# File 'lib/relaton/render/template/subclasses.rb', line 20

def initialize(opt = {})
  @etal_count = opt[:template]["etal_count"]
  @etal_display = opt[:template]["etal_display"] || @etal_count
  opt[:template].delete("etal_count")
  opt[:template].delete("etal_display")
  super
end

Instance Method Details

#combine_nametemplate(parts, size) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/relaton/render/template/subclasses.rb', line 60

def combine_nametemplate(parts, size)
  case size
  when 1 then parts[0] + parts[3]
  when 2 then parts[0] + parts[2] + parts[3]
  else parts.join
  end
end

#expand_nametemplate(template, size) ⇒ Object

assumes that template contains, consecutively and not interleaved, …[0], …[1], …[2]



49
50
51
52
53
54
55
56
57
58
# File 'lib/relaton/render/template/subclasses.rb', line 49

def expand_nametemplate(template, size)
  t = nametemplate_split(template)

  mid = (1..size - 2).each_with_object([]) do |i, m|
    m << t[1].gsub("[1]", "[#{i}]")
  end
  t[1] = mid.join
  t[2].gsub!(/\[\d+\]/, "[#{size - 1}]")
  template_process(combine_nametemplate(t, size))
end

#nametemplate_split(template) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/relaton/render/template/subclasses.rb', line 68

def nametemplate_split(template)
  curr = 0
  prec = ""
  t = template.split(/(\{[{%][^{]+?[}%]\})/)
    .each_with_object([""]) do |n, m|
    m, curr, prec = nametemplate_split1(n, m, curr, prec)

    m
  end
  [t[0], t[1], t[-1], prec]
end

#nametemplate_split1(elem, acc, curr, prec) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/relaton/render/template/subclasses.rb', line 80

def nametemplate_split1(elem, acc, curr, prec)
  if match = /\{[{%].+?\[(\d)\]/.match(elem)
    if match[1].to_i > curr
      curr += 1
      acc[curr] ||= ""
    end
    acc[curr] += prec
    prec = ""
    acc[curr] += elem
  elsif /\{%\s*endif/.match?(elem)
    acc[curr] += prec
    prec = ""
    acc[curr] += elem
  else prec += elem
  end
  [acc, curr, prec]
end

#template_select(names) ⇒ Object



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

def template_select(names)
  return nil if names.nil? || names.empty?

  case names[:surname].size
  when 1 then @template[:one]
  when 2 then @template[:two]
  when 3 then @template[:more]
  else template_select_etal(names)
  end
end

#template_select_etal(names) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/relaton/render/template/subclasses.rb', line 39

def template_select_etal(names)
  if @etal_count && names[:surname].size > @etal_count
    expand_nametemplate(@template_raw[:etal], @etal_display)
  else
    expand_nametemplate(@template_raw[:more], names[:surname].size)
  end
end