Module: Erubis::JavascriptGenerator

Includes:
Generator
Included in:
Ejavascript, PI::Ejavascript
Defined in:
lib/erubis/engine/ejavascript.rb

Instance Attribute Summary

Attributes included from Generator

#escapefunc

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Generator

#escaped_expr

Class Method Details

.supported_propertiesObject

:nodoc:



17
18
19
20
21
22
23
# File 'lib/erubis/engine/ejavascript.rb', line 17

def self.supported_properties()   # :nodoc:
  list = []
  #list << [:indent,   '',       "indent spaces (ex. '  ')"]
  #list << [:buf,      '_buf',   "output buffer name"]
  list << [:docwrite, true,     "use 'document.write()' when true"]
  return list
end

Instance Method Details

#add_expr_debug(src, code) ⇒ Object



73
74
75
76
77
# File 'lib/erubis/engine/ejavascript.rb', line 73

def add_expr_debug(src, code)
  add_indent(src, @indent)
  code.strip!
  src << "alert(\"*** debug: #{code}=\"+(#{code}));"
end

#add_expr_escaped(src, code) ⇒ Object



69
70
71
# File 'lib/erubis/engine/ejavascript.rb', line 69

def add_expr_escaped(src, code)
  add_expr_literal(src, escaped_expr(code))
end

#add_expr_literal(src, code) ⇒ Object



63
64
65
66
67
# File 'lib/erubis/engine/ejavascript.rb', line 63

def add_expr_literal(src, code)
  add_indent(src, @indent)
  code.strip!
  src << "#{@buf}.push(#{code});"
end

#add_indent(src, indent) ⇒ Object



42
43
44
# File 'lib/erubis/engine/ejavascript.rb', line 42

def add_indent(src, indent)
  src << (src.empty? || src[-1] == ?\n ? indent : ' ')
end

#add_postamble(src) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/erubis/engine/ejavascript.rb', line 79

def add_postamble(src)
  src << "\n" if src[-1] == ?;
  if @docwrite
    src << @indent << 'document.write(' << @buf << ".join(\"\"));\n"
  else
    src << @indent << @buf << ".join(\"\");\n"
  end
end

#add_preamble(src) ⇒ Object



33
34
35
# File 'lib/erubis/engine/ejavascript.rb', line 33

def add_preamble(src)
  src << "#{@indent}var #{@buf} = [];"
end

#add_stmt(src, code) ⇒ Object



59
60
61
# File 'lib/erubis/engine/ejavascript.rb', line 59

def add_stmt(src, code)
  src << code
end

#add_text(src, text) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/erubis/engine/ejavascript.rb', line 46

def add_text(src, text)
  return if text.empty?
  add_indent(src, @indent)
  src << @buf << '.push("'
  s = escape_text(text)
  if s[-1] == ?\n
    s[-2, 2] = ''
    src << s << "\");\n"
  else
    src << s << "\");"
  end
end

#escape_text(text) ⇒ Object



37
38
39
40
# File 'lib/erubis/engine/ejavascript.rb', line 37

def escape_text(text)
  @@table_ ||= { "\r"=>"\\r", "\n"=>"\\n\\\n", "\t"=>"\\t", '"'=>'\\"', "\\"=>"\\\\" }
  return text.gsub!(/[\r\n\t"\\]/) { |m| @@table_[m] } || text
end

#init_generator(properties = {}) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/erubis/engine/ejavascript.rb', line 25

def init_generator(properties={})
  super
  @escapefunc ||= 'escape'
  @indent = properties[:indent] || ''
  @buf = properties[:out] || '_buf'
  @docwrite = properties[:docwrite] != false  # '!= false' will be removed in the next release
end