Skip to content

Commit

Permalink
[enhance] support C++
Browse files Browse the repository at this point in the history
  • Loading branch information
kwatch committed Mar 22, 2011
1 parent 2e1d6c3 commit b45ab4a
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 2 deletions.
113 changes: 113 additions & 0 deletions lib/erubis/engine/ecpp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
##
## $Release:$
## $Copyright$
##

require 'erubis/engine'
require 'erubis/enhancer'


module Erubis


module CppGenerator
include Generator

def self.supported_properties() # :nodoc:
return [
[:indent, '', "indent spaces (ex. ' ')"],
[:bufvar, '_buf', "buffer variable name"],
]
end

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

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

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

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

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

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

def add_expr_literal(src, code)
src << @indent if src.empty? || src[-1] == ?\n
src << " _buf << (" << code.strip << ");"
end

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

def add_expr_debug(src, code)
code.strip!
src << @indent if src.empty? || src[-1] == ?\n
src << " std::cerr << \"*** debug: #{code.gsub(/(")/, '\\\&')}=\" << (#{code});"
end

def add_postamble(src)
# empty
end

end


##
## engine for C
##
class Ecpp < Basic::Engine
include CppGenerator
end


class EscapedEcpp < Ecpp
include EscapeEnhancer
end


#class XmlEcpp < Ecpp
# include EscapeEnhancer
#end

class PI::Ecpp < PI::Engine
include CppGenerator

def init_converter(properties={})
@pi = 'cpp'
super(properties)
end

end


end
5 changes: 3 additions & 2 deletions lib/erubis/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require 'erubis/engine/eruby'
require 'erubis/engine/ephp'
require 'erubis/engine/ec'
require 'erubis/engine/ecpp'
require 'erubis/engine/ejava'
require 'erubis/engine/escheme'
require 'erubis/engine/eperl'
Expand Down Expand Up @@ -231,7 +232,7 @@ def usage(command=nil)
buf << " -z : syntax checking"
buf << " -e : escape (equal to '--E Escape')"
buf << " -p pattern : embedded pattern (default '<% %>')"
buf << " -l lang : convert but no execute (ruby/php/c/java/scheme/perl/js)"
buf << " -l lang : convert but no execute (ruby/php/c/cpp/java/scheme/perl/js)"
buf << " -E e1,e2,... : enhancer names (Escape, PercentLine, BiPattern, ...)"
buf << " -I path : library include path"
buf << " -K kanji : kanji code (euc/sjis/utf8) (default none)"
Expand Down Expand Up @@ -269,7 +270,7 @@ def show_properties
list << ['(common)', common_props]
list << ['(basic)', basic_props - common_props]
list << ['(pi)', pi_props - common_props]
%w[ruby php c java scheme perl javascript].each do |lang|
%w[ruby php c cpp java scheme perl javascript].each do |lang|
klass = Erubis.const_get("E#{lang}")
list << [lang, collect_supported_properties(klass) - basic_props]
end
Expand Down
31 changes: 31 additions & 0 deletions test/test-engines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'erubis/engine/eruby'
require 'erubis/engine/ephp'
require 'erubis/engine/ec'
require 'erubis/engine/ecpp'
require 'erubis/engine/ejava'
require 'erubis/engine/escheme'
require 'erubis/engine/eperl'
Expand Down Expand Up @@ -163,6 +164,36 @@ def _test()
"</table>\n", stdout);
fprintf(stderr, "*** debug: i=" "%d", i); fputs("\n", stdout);
##
- name: cpp1
lang: cpp
class: Ecpp
options: { :filename: foo.html, :indent: ' ' }
input: |4
<table>
<tbody>
<% for (i = 0; i < n; i++) { %>
<tr>
<td><%= i %></td>
<td><%== list[i] %></td>
</tr>
<% } %>
</tbody>
</table>
<%=== i %>
expected: |
#line 1 "foo.html"
_buf << "<table>\n"
" <tbody>\n";
for (i = 0; i < n; i++) {
_buf << " <tr>\n"
" <td>"; _buf << (i); _buf << "</td>\n"
" <td>"; escape(list[i]); _buf << "</td>\n"
" </tr>\n";
}
_buf << " </tbody>\n"
"</table>\n";
std::cerr << "*** debug: i=" << (i); _buf << "\n";
##
- name: java1
lang: java
class: Ejava
Expand Down

0 comments on commit b45ab4a

Please sign in to comment.