summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
author(no author) <(no author)@c9e70521-770b-0410-b9ac-ce6205b42a9f>2007-02-04 11:35:04 +0000
committer(no author) <(no author)@c9e70521-770b-0410-b9ac-ce6205b42a9f>2007-02-04 11:35:04 +0000
commitefcdc74be5d59985a43d0a9d44adaa63c11c796a (patch)
tree89b26a741c0485f941d897338701c04ea4bef1f8 /lib
parentfb6328107a71b9ff03a8452192f52b4bec54b44a (diff)
downloaderubis-efcdc74be5d59985a43d0a9d44adaa63c11c796a.tar.gz
- [update] rdoc of erubis.rb
- [enhance] add Erubis::VERSION - [enhance] command-line option '-X' (syntax checking) added - [update] users-guide updated
Diffstat (limited to 'lib')
-rw-r--r--lib/erubis.rb40
-rw-r--r--lib/erubis/context.rb6
-rw-r--r--lib/erubis/main.rb43
3 files changed, 57 insertions, 32 deletions
diff --git a/lib/erubis.rb b/lib/erubis.rb
index 213a5c3..e3f5e6e 100644
--- a/lib/erubis.rb
+++ b/lib/erubis.rb
@@ -7,14 +7,7 @@
##
## an implementation of eRuby
##
-## * class Eruby - normal eRuby class
-## * class XmlEruby - eRuby class which escape '&<>"' into '&amp;&lt;&gt;&quot;'
-## * module StdoutEnhancer - use $stdout instead of String as output
-## * module PrintEnhancer - enable to write print statement in <% ... %>
-## * class OptimizedEruby - optimized Eruby class faster than FastEruby
-## * class OptimizedXmlEruby - optimized XmlEruby class faster than FastXmlEruby
-##
-## example:
+## ex.
## input = <<'END'
## <ul>
## <% for item in @list %>
@@ -24,25 +17,24 @@
## </ul>
## END
## list = ['<aaa>', 'b&b', '"ccc"']
-## eruby = Erubis::Eruby.new()
-## code = eruby.convert(input)
+## eruby = Erubis::Eruby.new(input)
## puts "--- code ---"
-## puts code
+## puts eruby.src
## puts "--- result ---"
-## context = Object.new
-## context.instance_variable_set("@list", list)
-## puts context.instance_eval(code)
-## # or @list = list; puts eval(code, binding())
+## context = Erubis::Context.new() # or new(:list=>list)
+## context[:list] = list
+## puts eruby.evaluate(context)
##
## result:
## --- source ---
-## _buf = ""; _buf << " <ul>\n"
-## for item in list
-## _buf << " <li>"; _buf << ( item ).to_s; _buf << "\n"
-## _buf << " "; _buf << Erubis::XmlEruby.escape( item ); _buf << "</li>\n"
-## end
-## _buf << " </ul>\n"
-## _buf
+## _buf = ''; _buf << '<ul>
+## '; for item in @list
+## _buf << ' <li>'; _buf << ( item ).to_s; _buf << '
+## '; _buf << ' '; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</li>
+## '; end
+## _buf << '</ul>
+## ';
+## _buf.to_s
## --- result ---
## <ul>
## <li><aaa>
@@ -55,6 +47,10 @@
##
+module Erubis
+ VERSION = ('$Release: 0.0.0 $' =~ /([.\d]+)/) && $1
+end
+
require 'erubis/engine'
#require 'erubis/generator'
#require 'erubis/converter'
diff --git a/lib/erubis/context.rb b/lib/erubis/context.rb
index 440d4d6..1f285f4 100644
--- a/lib/erubis/context.rb
+++ b/lib/erubis/context.rb
@@ -57,6 +57,12 @@ module Erubis
end
end
+ def to_hash
+ hash = {}
+ self.keys.each { |key| hash[key] = self[key] }
+ return hash
+ end
+
def update(context_or_hash)
arg = context_or_hash
if arg.is_a?(Hash)
diff --git a/lib/erubis/main.rb b/lib/erubis/main.rb
index baddd9b..2dc6085 100644
--- a/lib/erubis/main.rb
+++ b/lib/erubis/main.rb
@@ -49,12 +49,13 @@ module Erubis
end
def initialize
- @single_options = "hvxTtSbeB"
+ @single_options = "hvxXTtSbeB"
@arg_options = "pcrfKIlaEC"
@option_names = {
?h => :help,
?v => :version,
?x => :source,
+ ?X => :syntax,
?T => :notrim,
?t => :untabify,
?S => :intern,
@@ -113,6 +114,7 @@ module Erubis
## action
action = opts.action
+ action ||= 'syntax' if opts.syntax
action ||= 'convert' if opts.source
## lang
@@ -153,32 +155,39 @@ module Erubis
## convert and execute
val = nil
+ msg = "Syntax OK\n"
if filenames && !filenames.empty?
filenames.each do |filename|
test(?f, filename) or raise CommandOptionError.new("#{filename}: file not found.")
engine.filename = filename
engine.convert!(File.read(filename))
- print val if val = do_action(action, engine, context, opts)
+ val = do_action(action, engine, context, filename, opts)
+ msg = nil if val
end
else
engine.filename = '(stdin)'
engine.convert!($stdin.read())
- print val if val = do_action(action, engine, context, opts)
+ val = do_action(action, engine, context, filename, opts)
+ msg = nil if val
end
+ print msg if action == 'syntax' && msg
end
private
- def do_action(action, engine, context, opts)
+ def do_action(action, engine, context, filename, opts)
case action
when 'convert'
s = engine.src
when nil, 'exec', 'execute'
- s = opts.binding ? engine.result(context) : engine.evaluate(context)
+ s = opts.binding ? engine.result(context.to_hash) : engine.evaluate(context)
+ when 'syntax'
+ s = check_syntax(filename, engine.src)
else
raise "*** internal error"
end
+ print s if s
return s
end
@@ -189,7 +198,8 @@ erubis - embedded program converter for multi-language
Usage: #{command} [..options..] [file ...]
-h, --help : help
-v : version
- -x : converted code
+ -x : show converted code
+ -X : syntax checking
-T : don't trim spaces around '<% %>'
-b : body only (no preamble nor postamble)
-e : escape (equal to '--E Escape')
@@ -261,8 +271,7 @@ END
end
def version
- release = ('$Release: 0.0.0 $' =~ /([.\d]+)/) && $1
- return release
+ return Erubis::VERSION
end
def parse_argv(argv, arg_none='', arg_required='', arg_optional='')
@@ -377,8 +386,7 @@ END
test(?f, filename) or raise CommandOptionError.new("#{filename}: file not found.")
if filename =~ /\.ya?ml$/
if opts.untabify
- str = YAML.load(untabify(File.read(filename)))
- ydoc = YAML.load(str)
+ ydoc = YAML.load(untabify(File.read(filename)))
else
ydoc = YAML.load_file(filename)
end
@@ -436,6 +444,21 @@ END
end
end
+ def check_syntax(filename, src)
+ require 'open3'
+ stdin, stdout, stderr = Open3.popen3('ruby -wc')
+ stdin.write(src)
+ stdin.close
+ result = stdout.read()
+ stdout.close()
+ errmsg = stderr.read()
+ stderr.close()
+ return nil unless errmsg && !errmsg.empty?
+ errmsg =~ /\A-:(\d+): /
+ linenum, message = $1, $'
+ return "#{filename}:#{linenum}: #{message}"
+ end
+
end
end