Class: Relaton::Render::Template::General

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

Direct Known Subclasses

Extent, Name, Series, Size

Constant Summary collapse

FIELD_DELIM =

denote start and end of field, so that we can detect empty fields in postprocessing FIELD_DELIM = “u0018”.freeze

"%%".freeze
LT_DELIM =

escape < >

"\u0019".freeze
GT_DELIM =
"\u001a".freeze
NON_SPACING_DELIM =

use tab internally for non-spacing delimiter

"\t".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ General

Returns a new instance of General.



34
35
36
37
38
39
# File 'lib/relaton/render/template/template.rb', line 34

def initialize(opt = {})
  @htmlentities = HTMLEntities.new
  @templatecache = CacheManager.instance
  customise_liquid
  parse_options(opt)
end

Instance Attribute Details

#template_rawObject (readonly)

Returns the value of attribute template_raw.



32
33
34
# File 'lib/relaton/render/template/template.rb', line 32

def template_raw
  @template_raw
end

Instance Method Details

#add_field_delim_to_template(template) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/relaton/render/template/template.rb', line 91

def add_field_delim_to_template(template)
  t = template.split(/(\{\{|\}\})/).each_slice(4).map do |a|
    unless !a[2] || punct_field?(a[2]&.strip)
      a[1] = "#{FIELD_DELIM}{{"
      a[3] = "}}#{FIELD_DELIM}"
    end
    a.join
  end.join.tr("\t", " ")
  t.gsub(/\}\}#{FIELD_DELIM}\|/o, "}}#{FIELD_DELIM}\t")
    .gsub(/\|#{FIELD_DELIM}\{\{/o, "\t#{FIELD_DELIM}{{")
end

#customise_liquidObject



54
55
56
57
# File 'lib/relaton/render/template/template.rb', line 54

def customise_liquid
  ::Liquid::Template
    .register_filter(::Relaton::Render::Template::CapitalizeFirst)
end

#liquid_hash(hash) ⇒ Object

need non-breaking spaces in fields: “Updated:_nil” — we want the “Updated:” deleted, even if it’s multiple words, as in French Mise_à_jour.



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/relaton/render/template/template.rb', line 141

def liquid_hash(hash)
  case hash
  when Hash
    hash.map { |k, v| [k.to_s, liquid_hash(v)] }.to_h
  when Array
    hash.map { |v| liquid_hash(v) }
  when String
    hash.empty? ? nil : hash.gsub("_", "\\_").tr(" ", "_")
  else hash
  end
end

#parse_options(opt) ⇒ Object



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

def parse_options(opt)
  opt = Utils::sym_keys(opt)
  @i18n = opt[:i18n]
  @template_raw = opt[:template].dup
  @template =
    case opt[:template]
    when Hash
      opt[:template].transform_values { |x| template_process(x) }
    when Array then opt[:template].map { |x| template_process(x) }
    else { default: template_process(opt[:template]) }
    end
end

#punct_field?(name) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
# File 'lib/relaton/render/template/template.rb', line 71

def punct_field?(name)
  name or return false
  name = name.tr("'", '"')
  %w(labels["qq-open"] labels["qq-close"] labels["q-open"]
     labels["q-close"]).include?(name)
end

#render(hash) ⇒ Object



103
104
105
106
107
# File 'lib/relaton/render/template/template.rb', line 103

def render(hash)
  t = template_select(hash) or return nil

  template_clean(t.render(liquid_hash(hash.merge("labels" => @i18n.get))))
end

#template_clean(str) ⇒ Object



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

def template_clean(str)
  str = str.gsub(/&#x3c;/i, LT_DELIM).gsub(/&#x3e;/i, GT_DELIM)
  str = template_clean1(@htmlentities.decode(str))
  /[[:alnum:]]/.match?(str) or return nil
  str.strip.gsub(/#{LT_DELIM}/o, "&#x3c;")
    .gsub(/#{GT_DELIM}/o, "&#x3e;")
    .gsub(/&(?!#\S+?;)/, "&#x26;")
end

#template_clean1(str) ⇒ Object

use tab internally for non-spacing delimiter



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/relaton/render/template/template.rb', line 123

def template_clean1(str)
  str.gsub(/\S*#{FIELD_DELIM}#{FIELD_DELIM}\S*/o, "")
    .gsub(/#{FIELD_DELIM}/o, "")
    .gsub(/([,:;]\s*)+([,:;](\s|_|$))/, "\\2")
    .gsub(/([,.:;]\s*)+([.](\s|_|$))/, "\\2")
    .gsub(/([,:;]\s*)+(,(\s|_|$))/, "\\2")
    .gsub(/(:\s+)(&\s)/, "\\2")
    .gsub(/\s+([,.:;)])/, "\\1")
    .sub(/^\s*[,.:;]\s*/, "")
    .sub(/[,:;]\s*$/, "")
    .gsub(/(?<!\\)_/, " ")
    .gsub("\\_", "_")
    .gsub(/#{NON_SPACING_DELIM}/o, "").gsub(/\s+/, " ")
end

#template_process(template) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/relaton/render/template/template.rb', line 78

def template_process(template)
  template.is_a?(String) or return template
  t = nil
  @templatecache.mutex.synchronize do
    unless t = @templatecache.retrieve(template)
      t = ::Liquid::Template
        .parse(add_field_delim_to_template(template))
      @templatecache.store(template, t)
    end
  end
  t
end

#template_select(_hash) ⇒ Object



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

def template_select(_hash)
  @template[:default]
end