Class: Debride::Erubi

Inherits:
Erubi::Engine
  • Object
show all
Defined in:
lib/debride_erb.rb

Overview

stolen (and munged) from lib/action_view/template/handlers/erb/erubi.rb

Constant Summary collapse

BLOCK_EXPR =
/\s*((\s+|\))do|\{)(\s*\|[^|]*\|)?\s*\Z/

Instance Method Summary collapse

Constructor Details

#initialize(input, properties = {}) ⇒ Erubi

:nodoc: all



32
33
34
35
36
37
38
39
40
# File 'lib/debride_erb.rb', line 32

def initialize(input, properties = {})
  @newline_pending = 0

  properties[:postamble]  = "_buf.to_s"
  properties[:bufvar]     = "_buf"
  properties[:freeze_template_literals] = false

  super
end

Instance Method Details

#add_code(code) ⇒ Object



75
76
77
78
# File 'lib/debride_erb.rb', line 75

def add_code(code)
  flush_newline_if_pending(src)
  super
end

#add_expression(indicator, code) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/debride_erb.rb', line 59

def add_expression(indicator, code)
  flush_newline_if_pending(src)

  if (indicator == "==") || @escape
    src << "_buf.safe_expr_append="
  else
    src << "_buf.append="
  end

  if BLOCK_EXPR.match?(code)
    src << " " << code
  else
    src << "(" << code << ");"
  end
end

#add_postamble(_) ⇒ Object



80
81
82
83
# File 'lib/debride_erb.rb', line 80

def add_postamble(_)
  flush_newline_if_pending(src)
  super
end

#add_text(text) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/debride_erb.rb', line 42

def add_text(text)
  return if text.empty?

  if text == "\n"
    @newline_pending += 1
  else
    src << "_buf.safe_append='"
    src << "\n" * @newline_pending if @newline_pending > 0
    src << text.gsub(/['\\]/, '\\\\\&')
    src << "';"

    @newline_pending = 0
  end
end

#flush_newline_if_pending(src) ⇒ Object



85
86
87
88
89
90
# File 'lib/debride_erb.rb', line 85

def flush_newline_if_pending(src)
  if @newline_pending > 0
    src << "_buf.safe_append='#{"\n" * @newline_pending}';"
    @newline_pending = 0
  end
end