module Temple::Utils

@api public

Constants

ESCAPE_HTML

Used by escape_html @api private

ESCAPE_HTML_PATTERN

Public Instance Methods

empty_exp?(exp) click to toggle source

Check if expression is empty

@param exp [Array] Temple expression @return true if expression is empty

# File lib/temple/utils.rb, line 66
def empty_exp?(exp)
  case exp[0]
  when :multi
    exp[1..-1].all? {|e| empty_exp?(e) }
  when :newline
    true
  else
    false
  end
end
escape_html(html) click to toggle source

Returns an escaped copy of ‘html`.

@param html [String] The string to escape @return [String] The escaped string

# File lib/temple/utils.rb, line 27
def escape_html(html)
  CGI.escapeHTML(html.to_s)
end
escape_html_safe(html) click to toggle source

Returns an escaped copy of ‘html`. Strings which are declared as html_safe are not escaped.

@param html [String] The string to escape @return [String] The escaped string

# File lib/temple/utils.rb, line 17
def escape_html_safe(html)
  s = html.to_s
  s.html_safe? || html.html_safe? ? s : escape_html(s)
end
indent_dynamic(text, indent_next, indent, pre_tags = nil) click to toggle source
# File lib/temple/utils.rb, line 77
def indent_dynamic(text, indent_next, indent, pre_tags = nil)
  text = text.to_s
  safe = text.respond_to?(:html_safe?) && text.html_safe?
  return text if pre_tags && text =~ pre_tags

  level = text.scan(/^\s*/).map(&:size).min
  text = text.gsub(/(?!\A)^\s{#{level}}/, '') if level > 0

  text = text.sub(/\A\s*\n?/, "\n".freeze) if indent_next
  text = text.gsub("\n".freeze, indent)

  safe ? text.html_safe : text
end
unique_name(prefix = nil) click to toggle source

Generate unique variable name

@param prefix [String] Variable name prefix @return [String] Variable name

# File lib/temple/utils.rb, line 56
def unique_name(prefix = nil)
  @unique_name ||= 0
  prefix ||= (@unique_prefix ||= self.class.name.gsub('::'.freeze, '_'.freeze).downcase)
  "_#{prefix}#{@unique_name += 1}"
end