Module: Relaton::Render::Template::CustomFilters

Defined in:
lib/relaton/render/template/liquid.rb

Instance Method Summary collapse

Instance Method Details

#capitalize_first(words) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/relaton/render/template/liquid.rb', line 5

def capitalize_first(words)
  words.nil? and return nil
  # Split while preserving delimiters (spaces/underscores) and extracting XML tags
  ret = words.split(/(<[^>]+>|[ _])/).reject(&:empty?)
  # Find and capitalize the first element that is not a delimiter or XML tag
  ret.each do |element|
    element.match?(/^[ _]$/) || element.match?(/^<[^>]+>$/) and next
    element.capitalize!
    break
  end
  # Join with empty string since delimiters are preserved
  ret.join.sub(/^[ _]+/, "").sub(/[ _]+$/, "")
end

#selective_tag(text, tag) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/relaton/render/template/liquid.rb', line 32

def selective_tag(text, tag)
  return nil if text.nil?

  ret = text.split(/(\+\+\+[^+]+?\+\+\+)/)
  ret.map do |n|
    if m = /^\+\+\+(.+)\+\+\+$/.match(n)
      m[1]
    else
      closetag = tag.sub(/^</, "</")
      "#{tag}#{n}#{closetag}"
    end
  end.join
end

#selective_upcase(text) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/relaton/render/template/liquid.rb', line 19

def selective_upcase(text)
  text.nil? and return nil
  # Split to extract both +++...+++ sections and XML tags
  text.split(/(\+\+\+[^+]+?\+\+\+|<[^<>]+>)/).map do |n|
    if m = /^\+\+\+(.+)\+\+\+$/.match(n)
      m[1] # Keep content inside +++ unchanged
    elsif n.match?(/^<[^>]+>$/)
      n # Keep XML tags unchanged
    else n.upcase # Upcase everything else
    end
  end.join
end