Module: Erubis::CGenerator

Includes:
Generator
Included in:
Ec, PI::Ec
Defined in:
lib/erubis/engine/ec.rb

Instance Attribute Summary

Attributes included from Generator

#escapefunc

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.supported_propertiesObject

:nodoc:



16
17
18
19
20
21
# File 'lib/erubis/engine/ec.rb', line 16

def self.supported_properties()  # :nodoc:
  return [
          [:indent, '',       "indent spaces (ex. '  ')"],
          [:out,    'stdout', "output file pointer name"],
        ]
end

Instance Method Details

#add_expr_debug(src, code) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/erubis/engine/ec.rb', line 72

def add_expr_debug(src, code)
  code.strip!
  s = nil
  if code =~ /\A\".*?\"\s*,\s*(.*)/
    s = $1.gsub(/[%"]/, '\\\1') + '='
  end
  src << @indent if src.empty? || src[-1] == ?\n
  src << " fprintf(stderr, \"*** debug: #{s}\" #{code});"
end

#add_expr_escaped(src, code) ⇒ Object



67
68
69
70
# File 'lib/erubis/engine/ec.rb', line 67

def add_expr_escaped(src, code)
  src << @indent if src.empty? || src[-1] == ?\n
  src << ' ' << escaped_expr(code) << ';'
end

#add_expr_literal(src, code) ⇒ Object



62
63
64
65
# File 'lib/erubis/engine/ec.rb', line 62

def add_expr_literal(src, code)
  src << @indent if src.empty? || src[-1] == ?\n
  src << " fprintf(#{@out}, " << code.strip << ');'
end

#add_postamble(src) ⇒ Object



82
83
84
# File 'lib/erubis/engine/ec.rb', line 82

def add_postamble(src)
  # empty
end

#add_preamble(src) ⇒ Object



30
31
32
# File 'lib/erubis/engine/ec.rb', line 30

def add_preamble(src)
  src << "#line 1 \"#{self.filename}\"\n" if self.filename
end

#add_stmt(src, code) ⇒ Object



58
59
60
# File 'lib/erubis/engine/ec.rb', line 58

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

#add_text(src, text) ⇒ Object



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

def add_text(src, text)
  return if text.empty?
  src << (src.empty? || src[-1] == ?\n ? @indent : ' ')
  src << "fputs("
  i = 0
  text.each_line do |line|
    src << "\n" << @indent << '      ' if i > 0
    i += 1
    src << '"' << escape_text(line) << '"'
  end
  src << ", #{@out});"   #<< (text[-1] == ?\n ? "\n" : "")
  src << "\n" if text[-1] == ?\n
end

#escape_text(text) ⇒ Object



34
35
36
37
38
# File 'lib/erubis/engine/ec.rb', line 34

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

#escaped_expr(code) ⇒ Object



40
41
42
# File 'lib/erubis/engine/ec.rb', line 40

def escaped_expr(code)
  return "#{@escapefunc}(#{code.strip}, #{@out})"
end

#init_generator(properties = {}) ⇒ Object



23
24
25
26
27
28
# File 'lib/erubis/engine/ec.rb', line 23

def init_generator(properties={})
  super
  @escapefunc ||= "escape"
  @indent = properties[:indent] || ''
  @out = properties[:out] || 'stdout'
end