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.



10
11
12
13
14
# File 'lib/relaton/render/template/template.rb', line 10

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

Instance Attribute Details

#template_rawObject (readonly)

Returns the value of attribute template_raw.



8
9
10
# File 'lib/relaton/render/template/template.rb', line 8

def template_raw
  @template_raw
end

Instance Method Details

#add_field_delim_to_template(template) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/relaton/render/template/template.rb', line 58

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.gsub("\t", " ")
  t.gsub(/\}\}#{FIELD_DELIM}\|/o, "}}#{FIELD_DELIM}\t")
    .gsub(/\|#{FIELD_DELIM}\{\{/o, "\t#{FIELD_DELIM}{{")
end

#customise_liquidObject



29
30
31
32
# File 'lib/relaton/render/template/template.rb', line 29

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.



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/relaton/render/template/template.rb', line 108

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("_", "\\_").gsub(/ /, "_")
  else hash
  end
end

#parse_options(opt) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/relaton/render/template/template.rb', line 16

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)


46
47
48
49
50
51
# File 'lib/relaton/render/template/template.rb', line 46

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

#render(hash) ⇒ Object



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

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



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

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



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

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



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

def template_process(template)
  template.is_a?(String) or return template
  ::Liquid::Template.parse(add_field_delim_to_template(template))
end

#template_select(_hash) ⇒ Object



76
77
78
# File 'lib/relaton/render/template/template.rb', line 76

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