Module: RageRender::TemplateFunctions

Defined in:
lib/ragerender/functions.rb

Constant Summary collapse

STRIP_HTML_BLOCKS =
Regexp.union(
  %r{<script.*?</script>}m,
  /<!--.*?-->/m,
  %r{<style.*?</style>}m,
)
STRIP_HTML_TAGS =
/<.*?>/m

Instance Method Summary collapse

Instance Method Details

#js(str) ⇒ Object



8
9
10
11
12
# File 'lib/ragerender/functions.rb', line 8

def js str
  JSON.generate(str, script_safe: true, ascii_only: true).gsub(/[<>'&]|\\"/) do |m|
    '\u' + sprintf('%04x', m.chars.last.ord).upcase
  end
end

#randomnumber(a, b) ⇒ Object



14
15
16
# File 'lib/ragerender/functions.rb', line 14

def randomnumber a, b
  rand a.to_i..b.to_i
end

#rawhtml(str) ⇒ Object

Unescape all HTML entities in the input



19
20
21
# File 'lib/ragerender/functions.rb', line 19

def rawhtml str
  CGI.unescape_html str
end

#removehtmltags(str) ⇒ Object

This is only used for ERB – for Liquid, we use the native strip_html This pretty much mirrors the Liquid implementation.



34
35
36
# File 'lib/ragerender/functions.rb', line 34

def removehtmltags str
  str.gsub(STRIP_HTML_BLOCKS, '').gsub(STRIP_HTML_TAGS, '')
end