Class: Relaton::Render::Template::General
- Inherits:
-
Object
- Object
- Relaton::Render::Template::General
- Defined in:
- lib/relaton/render/template/template.rb
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 Method Summary collapse
- #customise_liquid ⇒ Object
-
#initialize(opt = {}) ⇒ General
constructor
A new instance of General.
-
#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.
- #parse_options(opt) ⇒ Object
- #punct_field?(name) ⇒ Boolean
- #render(hash) ⇒ Object
- #template_clean(str) ⇒ Object
-
#template_clean1(str) ⇒ Object
use tab internally for non-spacing delimiter.
- #template_process(template) ⇒ Object
- #template_select(_hash) ⇒ Object
Constructor Details
#initialize(opt = {}) ⇒ General
Returns a new instance of General.
8 9 10 11 12 |
# File 'lib/relaton/render/template/template.rb', line 8 def initialize(opt = {}) @htmlentities = HTMLEntities.new customise_liquid (opt) end |
Instance Method Details
#customise_liquid ⇒ Object
27 28 29 30 |
# File 'lib/relaton/render/template/template.rb', line 27 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.
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/relaton/render/template/template.rb', line 102 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(/ /, "_") else hash end end |
#parse_options(opt) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/relaton/render/template/template.rb', line 14 def (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
44 45 46 47 48 49 |
# File 'lib/relaton/render/template/template.rb', line 44 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
64 65 66 67 68 |
# File 'lib/relaton/render/template/template.rb', line 64 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
74 75 76 77 78 79 80 81 |
# File 'lib/relaton/render/template/template.rb', line 74 def template_clean(str) str = str.gsub(/</i, LT_DELIM).gsub(/>/i, GT_DELIM) str = template_clean1(@htmlentities.decode(str)) /[[:alnum:]]/.match?(str) or return nil str.strip.gsub(/#{LT_DELIM}/o, "<") .gsub(/#{GT_DELIM}/o, ">") .gsub(/&(?!#\S+?;)/, "&") end |
#template_clean1(str) ⇒ Object
use tab internally for non-spacing delimiter
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/relaton/render/template/template.rb', line 84 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
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/relaton/render/template/template.rb', line 51 def template_process(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/, " ") t1 = t.gsub(/\}\}#{FIELD_DELIM}\|/o, "}}#{FIELD_DELIM}\t") .gsub(/\|#{FIELD_DELIM}\{\{/o, "\t#{FIELD_DELIM}{{") ::Liquid::Template.parse(t1) end |
#template_select(_hash) ⇒ Object
70 71 72 |
# File 'lib/relaton/render/template/template.rb', line 70 def template_select(_hash) @template[:default] end |