diff options
author | makoto kuwata <kwa@kuwata-lab.com> | 2006-05-18 12:55:35 +0000 |
---|---|---|
committer | makoto kuwata <kwa@kuwata-lab.com> | 2006-05-18 12:55:35 +0000 |
commit | b61c543ab3b960c7b4d816794d0dd77809193b63 (patch) | |
tree | 3fb8d5965a5fc2a678a1837de8d17a6eafb99ce4 | |
parent | 38a6e0cdd97f780764c6bff548c185dae2d18dcf (diff) | |
download | erubis-b61c543ab3b960c7b4d816794d0dd77809193b63.tar.gz |
- [change] engine/xxx.rb is renamed to engine/exxx.rb
- [enhance] add 'bin/notext' command
-rw-r--r-- | ChangeLog.txt | 4 | ||||
-rw-r--r-- | Rookbook.yaml | 2 | ||||
-rw-r--r-- | benchmark/erubybench.rb | 8 | ||||
-rwxr-xr-x | bin/notext | 139 | ||||
-rw-r--r-- | doc/users-guide.html | 103 | ||||
-rw-r--r-- | doc/users-guide.txt | 58 | ||||
-rw-r--r-- | lib/erubis.rb | 40 | ||||
-rw-r--r-- | lib/erubis/engine/ec.rb (renamed from lib/erubis/engine/c.rb) | 0 | ||||
-rw-r--r-- | lib/erubis/engine/ejava.rb (renamed from lib/erubis/engine/java.rb) | 0 | ||||
-rw-r--r-- | lib/erubis/engine/ejavascript.rb (renamed from lib/erubis/engine/javascript.rb) | 0 | ||||
-rw-r--r-- | lib/erubis/engine/enhanced.rb | 7 | ||||
-rw-r--r-- | lib/erubis/engine/eperl.rb (renamed from lib/erubis/engine/perl.rb) | 0 | ||||
-rw-r--r-- | lib/erubis/engine/ephp.rb (renamed from lib/erubis/engine/php.rb) | 0 | ||||
-rw-r--r-- | lib/erubis/engine/eruby.rb (renamed from lib/erubis/engine/ruby.rb) | 0 | ||||
-rw-r--r-- | lib/erubis/engine/escheme.rb (renamed from lib/erubis/engine/scheme.rb) | 0 | ||||
-rw-r--r-- | lib/erubis/engine/optimized.rb | 2 | ||||
-rw-r--r-- | lib/erubis/enhancer.rb | 35 | ||||
-rw-r--r-- | lib/erubis/main.rb | 14 | ||||
-rw-r--r-- | test/test-engines.rb | 14 |
19 files changed, 345 insertions, 81 deletions
diff --git a/ChangeLog.txt b/ChangeLog.txt index 2c15cc7..cdaa4ca 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -2,6 +2,10 @@ .?lastupdate: $Date$ .?version: $Rev$ +.: Rev.19 (2005-05-18) + .- [change] engine/xxx.rb is renamed to engine/exxx.rb + .- [enhance] add 'bin/notext' command + .: Rev.18 (2005-05-07) .- [enhance] new module NoTextEnhancer added .- [change] Eruby#add_stmt() desn't add ';' when the last character is ?\n diff --git a/Rookbook.yaml b/Rookbook.yaml index 60a28e9..b392c5d 100644 --- a/Rookbook.yaml +++ b/Rookbook.yaml @@ -109,7 +109,7 @@ recipes: rm_rf dir if test(?d, dir) mkdir_p dir # - store 'lib/**/*', 'bin/*', 'test/**/*', text_files, dir do |f| + store 'lib/**/*', 'bin/erubis', 'test/**/*', text_files, dir do |f| base = File.basename(f) !exclude_libs.include?(base) end diff --git a/benchmark/erubybench.rb b/benchmark/erubybench.rb index eabb40d..ae0def4 100644 --- a/benchmark/erubybench.rb +++ b/benchmark/erubybench.rb @@ -6,6 +6,8 @@ require 'eruby' require 'erb' +require 'stringio' + require 'erubis' require 'erubis/engine/enhanced' require 'erubis/engine/optimized' @@ -13,6 +15,7 @@ require 'erubis/tiny' require 'erubybench-lib' + ## default values filename = 'erubybench.rhtml' datafile = 'erubybench.yaml' @@ -138,6 +141,11 @@ testdefs_str = <<END return: str skip: no +- name: ErubisStringIO + class: Erubis::StringIOEruby + return: str + skip: yes + - name: ErubisSimplified class: Erubis::SimplifiedEruby return: str diff --git a/bin/notext b/bin/notext new file mode 100755 index 0000000..e6d0b54 --- /dev/null +++ b/bin/notext @@ -0,0 +1,139 @@ +#!/usr/bin/env ruby + +### +### notext - retrieve embedded code from eRuby, PHP, and so on +### +### $Rev$ +### $Release$ +### $Copyright$ +### + +require 'erubis' +require 'erubis/engine/eruby' +require 'erubis/engine/ephp' +require 'erubis/engine/ec' +require 'erubis/engine/ejava' +require 'erubis/engine/escheme' +require 'erubis/engine/eperl' +require 'erubis/engine/ejavascript' + + +def help() + command = File.basename($0) + s = <<END +#{command} - retrieve embedded code from eRuby, PHP, and so on. +Usage: #{command} [-hncu] [-l lang] [-p pattern] file.php ... + -h : help + -n : show line number + -c : compact mode (delete empty lines) + -u : uniq mode (compress repeated empty lines to a line) + -l lang : 'ruby' or 'php' + -p pattern : embedded pattern (default '<% %>' or '<\\?(?:php|=|\\s) \\?>') +END + return s +end + + +## Engine classes which include NoTextEnhancer +klass_table = {} +lang_list = %w[ruby php c java scheme perl javascript] +lang_list.each do |lang| + eval <<-END + class NoTextE#{lang} < Erubis::E#{lang} + include Erubis::NoTextEnhancer + end + END + klass_table[lang] = Object.const_get("NoTextE#{lang}") +end + + +## parse command-line options +options = {} +while !ARGV.empty? && ARGV[0][0] == ?- + optstr = ARGV.shift + optstr = optstr.sub(/^-/, '') + while !optstr.empty? + optch = optstr[0] + optstr = optstr[1, optstr.length-1] + case optch + when ?h, ?n, ?c, ?u # help, linenum, compact, uniq + options[optch] = true + when ?l, ?p # lang, pattern + arg = !optstr.empty? ? optstr : ARGV.shift + unless arg + words = { ?l => 'lang name', ?p => 'pattern' } + $stderr.puts "-#{optch.chr}: #{words[optch]} required." + exit(1) + end + options[optch] = arg + else + $stderr.puts "-#{optch.chr}: unknown option." + exit(1) + end + end +end + +opt_help = options[?h] +opt_linenum = options[?n] +opt_compact = options[?c] +opt_unique = options[?u] +opt_pattern = options[?p] +opt_lang = options[?l] + +if opt_lang + unless lang_list.include?(opt_lang) + $stderr.puts "-l #{opt_lang}: not supported." + exit(1) + end +end + + +## help message +if opt_help + print help() + exit(0) +end + + +## determine pattern and lang +pattern = opt_pattern +lang = opt_lang +unless lang + filename = ARGV[0] + if filename =~ /\.(\w+)$/ + case suffix = $1 + when 'php', 'php5', 'inc' ; lang = 'php' + when 'ec' ; lang = 'c' + when 'ejava' ; lang = 'java' + when 'escheme', 'escm' ; lang = 'scheme' + when 'eperl' ; lang = 'perl' + when 'js', 'javascript' ; lang = 'javascript' + else ; lang = 'ruby' + end + else + lang = 'ruby' + end +end +pattern ||= lang == 'php' ? '<\?(?:php\b|=|\s) \?>' : '<% %>' + + +## read input and create engine +klass = klass_table[lang] +input = ARGF.read() +engine = klass.new(input, :pattern=>pattern, :trim=>false) + + +## print embedded code +n = 0 +prev_empty = false +engine.src.each_line do |line| + n += 1 + if line =~ /^\s*$/ + next if opt_compact || (opt_unique && prev_empty) + print opt_linenum && !opt_unique ? ("%3d: \n" % n) : "\n" + prev_empty = true + else + print opt_linenum ? ("%3d: %s" % [n, line]) : line + prev_empty = false + end +end diff --git a/doc/users-guide.html b/doc/users-guide.html index 239bf73..8f678ea 100644 --- a/doc/users-guide.html +++ b/doc/users-guide.html @@ -183,7 +183,7 @@ $ contrib/inline-require -I lib bin/erubis > contrib/erubis <h2 class="section1">Tutorial</h2> <a name="tut-basic"></a> <h3 class="section2">Basic Example</h3> -<p>Here is a most basic example of Erubis. +<p>Here is a basic example of Erubis. </p> <a name="example1.eruby"></a> <div class="program_caption"> @@ -248,19 +248,48 @@ _out.join <a name="tut-trim"></a> <h3 class="section2">Trimming Spaces</h3> <p>Erubis deletes spaces around '<% %>' automatically, while it leaves spaces around '<%= %>'. -If you want leave spaces around '<% %>', add <code>:trim=>false</code> option to Erubis::Eruby.new(). </p> <a name="example2.eruby"></a> <div class="program_caption"> example2.eruby</div> <pre class="program"><ul> - <% for item in list %> + <% for item in list %> # trimmed <li> - <%= item %> + <%= item %> # not trimmed </li> - <% end %> + <% end %> # trimmed </ul> </pre> +<div class="terminal_caption"> +compiled source code</div> +<pre class="terminal">$ erubis -s example2.eruby +_out = []; _out << '<ul> +'; for item in list + _out << ' <li> +'; _out << ' '; _out << ( item ).to_s; _out << ' +'; _out << ' </li> +'; end + _out << '</ul> +'; +_out.join +</pre> +<p>If you want leave spaces around '<% %>', add command-line option '-T'. +</p> +<div class="terminal_caption"> +compiled source code with command-line option '-T'</div> +<pre class="terminal">$ erubis -s <b>-T</b> example2.eruby +_out = []; _out << '<ul> +'; _out << ' '; for item in list ; _out << ' +'; _out << ' <li> +'; _out << ' '; _out << ( item ).to_s; _out << ' +'; _out << ' </li> +'; _out << ' '; end ; _out << ' +'; _out << '</ul> +'; +_out.join +</pre> +<p>Or add option <code>:trim=>false</code> to Erubis::Eruby.new(). +</p> <a name="example2.rb"></a> <div class="program_caption"> example2.rb</div> @@ -420,7 +449,7 @@ end <a name="tut-pattern"></a> <h3 class="section2">Embedded Pattern</h3> -<p>You can change embedded pattern '<code><% %></code>' to another. +<p>You can change embedded pattern '<code><% %></code>' to another with command-line option '-p' or option '<code>:pattern=>...</code>' of Erubis::Eruby.new(). </p> <a name="example4.eruby"></a> <div class="program_caption"> @@ -429,6 +458,14 @@ example4.eruby</div> <p><b><!--%=</b> item <b>%--></b></p> <b><!--%</b> end <b>%--></b> </pre> +<div class="terminal_caption"> +compiled source code with command-line option '-p'</div> +<pre class="terminal">$ erubis -s <b>-p '<!--% %-->'</b> example4.eruby +_out = []; for item in list + _out << ' <p>'; _out << ( item ).to_s; _out << '</p> +'; end +_out.join +</pre> <a name="example4.rb"></a> <div class="program_caption"> example4.rb</div> @@ -657,7 +694,7 @@ Command-line option '-B' invokes 'Erubis::Eruby#result(binding())' instead of 'E <p>The first line ('_out = [];') in the compiled source code is called preamble and the last line ('_out.join') is called postamble. </p> -<p>You can specify Eruby not to print preamble nor postamble with option :preamble and :postamble. +<p>Command-line option '-b' skips the output of preamble and postamble. </p> <a name="example8.eruby"></a> <div class="program_caption"> @@ -666,6 +703,22 @@ example8.eruby</div> <b><%= item %></b> <% end %> </pre> +<p>compiled source code with and without command-line option '-b' +</p> +<pre class="terminal">## without '-b' +$ erubis -s example8.eruby +<b>_out = [];</b> for item in @list + _out << ' <b>'; _out << ( item ).to_s; _out << '</b> +'; end +<b>_out.join</b> +## with '-b' +$ erubis -s <b>-b</b> example8.eruby + for item in @list + _out << ' <b>'; _out << ( item ).to_s; _out << '</b> +'; end +</pre> +<p>Erubis::Eruby.new option '<code>:preamble=>false</code>' and '<code>:postamble=>false</code>' also suppress output of preamble or postamle. +</p> <a name="example8.rb"></a> <div class="program_caption"> example8.rb</div> @@ -690,8 +743,6 @@ _out.join _out << ' <b>'; _out << ( item ).to_s; _out << '</b> '; end </pre> -<p>The command-line option '-b' specify both :preamble and :postamble to false. -</p> <br> @@ -970,21 +1021,21 @@ notext-example.eruby</div> </pre> <div class="terminal_caption"> output example of NoTextEnhancer</div> -<pre class="terminal">$ erubis <b>-se NoText</b> notext-example.eruby +<pre class="terminal">$ erubis -Tse NoText notext-example.eruby _out = []; - if !@list || @list.empty? + if !@list || @list.empty? ; - else + else ; - @list.each_with_index do |item, i| - _out << ( i%2 == 0 ? '#FFCCCC' : '#CCCCFF' ).to_s; - _out << ( item ).to_s; + @list.each_with_index do |item, i| ; + _out << ( i%2 == 0 ? '#FFCCCC' : '#CCCCFF' ).to_s; + _out << ( item ).to_s; - end + end ; - end + end ; _out.join </pre> <p>NoTextEnhancer is language-independent. It is useful even if you are PHP user, see <a href="#topics-php">this section</a>. @@ -1688,20 +1739,20 @@ NoTextEnhancer can separate PHP code from HTML document. </p> <div class="terminal_caption"> example of using NoTextEnhancer with PHP file</div> -<pre class="terminal">$ erubis -l php -e NoText -p '<\?php \?>' notext-example.php | uniq +<pre class="terminal">$ erubis -T -l php -e NoText -p '<\?php \?>' notext-example.php | uniq -<?php if (!$list || count($list) == 0) { ?> + <?php if (!$list || count($list) == 0) { ?> -<?php } else { ?> + <?php } else { ?> -<?php $i = 0; ?> -<?php foreach ($list as $item) { ?> -<?php echo ++$i % 2 == 1 ? '#FFCCCC' : '#CCCCFF'; ?> -<?php echo $item; ?> + <?php $i = 0; ?> + <?php foreach ($list as $item) { ?> + <?php echo ++$i % 2 == 1 ? '#FFCCCC' : '#CCCCFF'; ?> + <?php echo $item; ?> -<?php } ?> + <?php } ?> -<?php } ?> + <?php } ?> </pre> <br> diff --git a/doc/users-guide.txt b/doc/users-guide.txt index ebe64c6..30911c3 100644 --- a/doc/users-guide.txt +++ b/doc/users-guide.txt @@ -114,19 +114,34 @@ $ erubis {{*-s*}} example1.eruby .$$ Trimming Spaces | tut-trim Erubis deletes spaces around '<% %>' automatically, while it leaves spaces around '<%= %>'. -If you want leave spaces around '<% %>', add {{,:trim=>false,}} option to Erubis::Eruby.new(). .? example2.eruby .-------------------- example2.eruby <ul> - <% for item in list %> + <% for item in list %> # trimmed <li> - <%= item %> + <%= item %> # not trimmed </li> - <% end %> + <% end %> # trimmed </ul> .-------------------- +.? compiled source code +.==================== +$ erubis -s example2.eruby +.<<<:! (ruby -pi.bak -e 'sub! /\s*\#.*$/, ""' guide.d/example2.eruby; erubis -s guide.d/example2.eruby) +.==================== + +If you want leave spaces around '<% %>', add command-line option '-T'. + +.? compiled source code with command-line option '-T' +.==================== +$ erubis -s {{*-T*}} example2.eruby +.<<<:! erubis -s -T guide.d/example2.eruby +.==================== + +Or add option {{,:trim=>false,}} to Erubis::Eruby.new(). + .? example2.rb .-------------------- example2.rb require 'erubis' @@ -277,7 +292,7 @@ end .$$ Embedded Pattern | tut-pattern -You can change embedded pattern '{{,<% %>,}}' to another. +You can change embedded pattern '{{,<% %>,}}' to another with command-line option '-p' or option '{{,:pattern=>...,}}' of Erubis::Eruby.new(). .? example4.eruby .-------------------- example4.eruby @@ -286,6 +301,12 @@ You can change embedded pattern '{{,<% %>,}}' to another. {{*<!--%*}} end {{*%-->*}} .-------------------- +.? compiled source code with command-line option '-p' +.==================== +$ erubis -s {{*-p '<!--% %-->'*}} example4.eruby +.<<<:! erubis -s -p '<!--% %-->' guide.d/example4.eruby +.==================== + .? example4.rb .-------------------- example4.rb require 'erubis' @@ -477,7 +498,7 @@ Command-line option '-B' invokes 'Erubis::Eruby#result(binding())' instead of 'E The first line ('_out = [];') in the compiled source code is called preamble and the last line ('_out.join') is called postamble. -You can specify Eruby not to print preamble nor postamble with option :preamble and :postamble. +Command-line option '-b' skips the output of preamble and postamble. .? example8.eruby .-------------------- example8.eruby @@ -486,6 +507,18 @@ You can specify Eruby not to print preamble nor postamble with option :preamble <% end %> .-------------------- +compiled source code with and without command-line option '-b' +.==================== +## without '-b' +$ erubis -s example8.eruby +.<<<:! erubis -s guide.d/example8.eruby | ruby -pe 'sub! /^_out.*?(;|$)/, "{{*\\&*}}"' +## with '-b' +$ erubis -s {{*-b*}} example8.eruby +.<<<:! erubis -s -b guide.d/example8.eruby +.==================== + +Erubis::Eruby.new option '{{,:preamble=>false,}}' and '{{,:postamble=>false,}}' also suppress output of preamble or postamle. + .? example8.rb .-------------------- example8.rb require 'erubis' @@ -504,7 +537,7 @@ $ ruby example8.rb .<<<:! (cd guide.d; ruby example8.rb) .==================== -The command-line option '-b' specify both :preamble and :postamble to false. +.#The command-line option '-b' specify both :preamble and :postamble to false. @@ -809,8 +842,8 @@ This is useful especially when debugging a complex eRuby script. .? output example of NoTextEnhancer .==================== -$ erubis {{*-se NoText*}} notext-example.eruby -.<<<:! (cd guide.d; erubis -se NoText notext-example.eruby) +$ erubis -Tse NoText notext-example.eruby +.<<<:! (cd guide.d; erubis -Tse NoText notext-example.eruby) .==================== NoTextEnhancer is language-independent. It is useful even if you are PHP user, see {{<this section|#topics-php>}}. @@ -1357,11 +1390,8 @@ NoTextEnhancer can separate PHP code from HTML document. .? example of using NoTextEnhancer with PHP file .==================== -.#$ erubis -l php -e NoText -p '<\?php \?>' notext-example.php -.#.<<<:! erubis -l php -e NoText -p '<\?php \?>' guide.d/notext-example.php -$ erubis -l php -e NoText -p '<\?php \?>' notext-example.php | uniq -.<<<:! erubis -l php -e NoText -p '<\?php \?>' guide.d/notext-example.php | uniq -.#erubis -l php -e NoText -p '<\?php \?>' guide.d/notext-example.php | ruby -ne 'print unless /^\s*$/' +$ erubis -T -l php -e NoText -p '<\?php \?>' notext-example.php | uniq +.<<<:! erubis -T -l php -e NoText -p '<\?php \?>' guide.d/notext-example.php | uniq .==================== diff --git a/lib/erubis.rb b/lib/erubis.rb index 75f4895..b026dc9 100644 --- a/lib/erubis.rb +++ b/lib/erubis.rb @@ -15,39 +15,39 @@ ## * class OptimizedXmlEruby - optimized XmlEruby class faster than FastXmlEruby ## ## example: -## list = ['<aaa>', 'b&b', '"ccc"'] ## input = <<'END' ## <ul> -## <% for item in list %> +## <% for item in @list %> ## <li><%= item %> ## <%== item %></li> ## <% end %> ## </ul> ## END -## eruby = Erubis::XmlEruby.new(input) # or try OptimizedXmlEruby +## list = ['<aaa>', 'b&b', '"ccc"'] +## eruby = Erubis::Eruby.new(input) ## puts "--- source ---" ## puts eruby.src ## puts "--- result ---" -## puts eruby.result(binding()) -## # or puts eruby.evaluate(:list=>list) +## puts eruby.evaluate(:list=>list) +## # or puts eruby.result(binding()) ## ## result: ## --- source --- ## _out = ""; _out << " <ul>\n" ## for item in list -## _out << " <li>"; _out << Erubis::XmlEruby.escape( item ); _out << "\n" -## _out << " "; _out << ( item ).to_s; _out << "</li>\n" +## _out << " <li>"; _out << ( item ).to_s; _out << "\n" +## _out << " "; _out << Erubis::XmlEruby.escape( item ); _out << "</li>\n" ## end ## _out << " </ul>\n" ## _out ## --- result --- ## <ul> -## <li><aaa> -## <aaa></li> -## <li>b&b -## b&b</li> -## <li>"ccc" -## "ccc"</li> +## <li><aaa> +## <aaa></li> +## <li>b&b +## b&b</li> +## <li>"ccc" +## "ccc"</li> ## </ul> ## @@ -56,15 +56,15 @@ require 'erubis/engine' require 'erubis/helper' require 'erubis/enhancer' #require 'erubis/tiny' -require 'erubis/engine/ruby' +require 'erubis/engine/eruby' #require 'erubis/engine/enhanced' # enhanced eruby engines #require 'erubis/engine/optimized' # generates optimized ruby code -#require 'erubis/engine/php' -#require 'erubis/engine/c' -#require 'erubis/engine/java' -#require 'erubis/engine/scheme' -#require 'erubis/engine/perl' -#require 'erubis/engine/javascript' +#require 'erubis/engine/ephp' +#require 'erubis/engine/ec' +#require 'erubis/engine/ejava' +#require 'erubis/engine/escheme' +#require 'erubis/engine/eperl' +#require 'erubis/engine/ejavascript' require 'erubis/local-setting' diff --git a/lib/erubis/engine/c.rb b/lib/erubis/engine/ec.rb index 8149dba..8149dba 100644 --- a/lib/erubis/engine/c.rb +++ b/lib/erubis/engine/ec.rb diff --git a/lib/erubis/engine/java.rb b/lib/erubis/engine/ejava.rb index 9d21bd0..9d21bd0 100644 --- a/lib/erubis/engine/java.rb +++ b/lib/erubis/engine/ejava.rb diff --git a/lib/erubis/engine/javascript.rb b/lib/erubis/engine/ejavascript.rb index b5958b2..b5958b2 100644 --- a/lib/erubis/engine/javascript.rb +++ b/lib/erubis/engine/ejavascript.rb diff --git a/lib/erubis/engine/enhanced.rb b/lib/erubis/engine/enhanced.rb index e0cb0e3..f2a91fa 100644 --- a/lib/erubis/engine/enhanced.rb +++ b/lib/erubis/engine/enhanced.rb @@ -5,7 +5,7 @@ ## require 'erubis/enhancer' -require 'erubis/engine/ruby' +require 'erubis/engine/eruby' module Erubis @@ -57,6 +57,11 @@ module Erubis end + class StringIOEruby < Eruby + include StringIOEnhancer + end + + class NoTextEruby < Eruby include NoTextEnhancer end diff --git a/lib/erubis/engine/perl.rb b/lib/erubis/engine/eperl.rb index 465c5ff..465c5ff 100644 --- a/lib/erubis/engine/perl.rb +++ b/lib/erubis/engine/eperl.rb diff --git a/lib/erubis/engine/php.rb b/lib/erubis/engine/ephp.rb index 1da8722..1da8722 100644 --- a/lib/erubis/engine/php.rb +++ b/lib/erubis/engine/ephp.rb diff --git a/lib/erubis/engine/ruby.rb b/lib/erubis/engine/eruby.rb index 6a93175..6a93175 100644 --- a/lib/erubis/engine/ruby.rb +++ b/lib/erubis/engine/eruby.rb diff --git a/lib/erubis/engine/scheme.rb b/lib/erubis/engine/escheme.rb index d11497c..d11497c 100644 --- a/lib/erubis/engine/scheme.rb +++ b/lib/erubis/engine/escheme.rb diff --git a/lib/erubis/engine/optimized.rb b/lib/erubis/engine/optimized.rb index 28799e4..b544794 100644 --- a/lib/erubis/engine/optimized.rb +++ b/lib/erubis/engine/optimized.rb @@ -5,7 +5,7 @@ ## -require 'erubis/engine/ruby' +require 'erubis/engine/eruby' module Erubis diff --git a/lib/erubis/enhancer.rb b/lib/erubis/enhancer.rb index db518f7..f2fa0b9 100644 --- a/lib/erubis/enhancer.rb +++ b/lib/erubis/enhancer.rb @@ -180,14 +180,14 @@ module Erubis ## - ## use array buffer instead of string buffer (included in Eruby by default) + ## use an Array object for buffering (included in Eruby by default) ## ## this is only for Eruby. ## module ArrayBufferEnhancer def self.desc # :nodoc: - "use array buffer instead of string (included in Eruby by default)" + "use an Array object for buffering (included in Eruby by default)" end def add_preamble(src) @@ -203,14 +203,14 @@ module Erubis ## - ## use string buffer instead of array buffer + ## use String class for buffering ## ## this is only for Eruby. ## module StringBufferEnhancer def self.desc # :nodoc: - "use string buffer instead of array buffer" + "use a String object for buffering" end def add_preamble(src) @@ -226,6 +226,29 @@ module Erubis ## + ## use StringIO class for buffering + ## + ## this is only for Eruby. + ## + module StringIOEnhancer # :nodoc: + + def self.desc # :nodoc: + "use a StringIO object for buffering" + end + + def add_preamble(src) + src << "_out = StringIO.new;" + end + + def add_postamble(src) + src << "\n" unless src[-1] == ?\n + src << "_out.string\n" + end + + end + + + ## ## remove text and leave code, especially useful when debugging. ## ## ex. @@ -241,6 +264,10 @@ module Erubis def add_text(src, text) src << ("\n" * text.count("\n")) + if text[-1] != ?\n + text =~ /^(.*?)\z/ + src << (' ' * $1.length) + end end end diff --git a/lib/erubis/main.rb b/lib/erubis/main.rb index 77822c2..843d31c 100644 --- a/lib/erubis/main.rb +++ b/lib/erubis/main.rb @@ -9,13 +9,13 @@ require 'erubis' require 'erubis/tiny' require 'erubis/engine/enhanced' require 'erubis/engine/optimized' -require 'erubis/engine/ruby' -require 'erubis/engine/php' -require 'erubis/engine/c' -require 'erubis/engine/java' -require 'erubis/engine/scheme' -require 'erubis/engine/perl' -require 'erubis/engine/javascript' +require 'erubis/engine/eruby' +require 'erubis/engine/ephp' +require 'erubis/engine/ec' +require 'erubis/engine/ejava' +require 'erubis/engine/escheme' +require 'erubis/engine/eperl' +require 'erubis/engine/ejavascript' module Erubis diff --git a/test/test-engines.rb b/test/test-engines.rb index dfb8813..4b274d7 100644 --- a/test/test-engines.rb +++ b/test/test-engines.rb @@ -7,13 +7,13 @@ require "#{File.dirname(__FILE__)}/test.rb" require 'erubis' -require 'erubis/engine/ruby' -require 'erubis/engine/php' -require 'erubis/engine/c' -require 'erubis/engine/java' -require 'erubis/engine/scheme' -require 'erubis/engine/perl' -require 'erubis/engine/javascript' +require 'erubis/engine/eruby' +require 'erubis/engine/ephp' +require 'erubis/engine/ec' +require 'erubis/engine/ejava' +require 'erubis/engine/escheme' +require 'erubis/engine/eperl' +require 'erubis/engine/ejavascript' class EnginesTest < Test::Unit::TestCase |