Method: Temple::Utils#indent_dynamic

Defined in:
lib/temple/utils.rb

#indent_dynamic(text, indent_next, indent, pre_tags = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# 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