main.rb 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. ###
  2. ### $Release: 2.7.0 $
  3. ### copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
  4. ###
  5. require 'yaml'
  6. require 'erubis'
  7. require 'erubis/tiny'
  8. require 'erubis/engine/enhanced'
  9. require 'erubis/engine/optimized'
  10. require 'erubis/engine/eruby'
  11. require 'erubis/engine/ephp'
  12. require 'erubis/engine/ec'
  13. require 'erubis/engine/ecpp'
  14. require 'erubis/engine/ejava'
  15. require 'erubis/engine/escheme'
  16. require 'erubis/engine/eperl'
  17. require 'erubis/engine/ejavascript'
  18. module Erubis
  19. Ejs = Ejavascript
  20. EscapedEjs = EscapedEjavascript
  21. class CommandOptionError < ErubisError
  22. end
  23. ##
  24. ## main class of command
  25. ##
  26. ## ex.
  27. ## Main.main(ARGV)
  28. ##
  29. class Main
  30. def self.main(argv=ARGV)
  31. status = 0
  32. begin
  33. Main.new.execute(ARGV)
  34. rescue CommandOptionError => ex
  35. $stderr.puts ex.message
  36. status = 1
  37. end
  38. exit(status)
  39. end
  40. def initialize
  41. @single_options = "hvxztTSbeBXNUC"
  42. @arg_options = "pcrfKIlaE" #C
  43. @option_names = {
  44. 'h' => :help,
  45. 'v' => :version,
  46. 'x' => :source,
  47. 'z' => :syntax,
  48. 'T' => :unexpand,
  49. 't' => :untabify, # obsolete
  50. 'S' => :intern,
  51. 'b' => :bodyonly,
  52. 'B' => :binding,
  53. 'p' => :pattern,
  54. 'c' => :context,
  55. #'C' => :class,
  56. 'e' => :escape,
  57. 'r' => :requires,
  58. 'f' => :datafiles,
  59. 'K' => :kanji,
  60. 'I' => :includes,
  61. 'l' => :lang,
  62. 'a' => :action,
  63. 'E' => :enhancers,
  64. 'X' => :notext,
  65. 'N' => :linenum,
  66. 'U' => :unique,
  67. 'C' => :compact,
  68. }
  69. assert unless @single_options.length + @arg_options.length == @option_names.length
  70. (@single_options + @arg_options).each_byte do |ch|
  71. assert unless @option_names.key?(ch.chr)
  72. end
  73. end
  74. def execute(argv=ARGV)
  75. ## parse command-line options
  76. options, properties = parse_argv(argv, @single_options, @arg_options)
  77. filenames = argv
  78. options['h'] = true if properties[:help]
  79. opts = Object.new
  80. arr = @option_names.collect {|ch, name| "def #{name}; @#{name}; end\n" }
  81. opts.instance_eval arr.join
  82. options.each do |ch, val|
  83. name = @option_names[ch]
  84. opts.instance_variable_set("@#{name}", val)
  85. end
  86. ## help, version, enhancer list
  87. if opts.help || opts.version
  88. puts version() if opts.version
  89. puts usage() if opts.help
  90. puts show_properties() if opts.help
  91. puts show_enhancers() if opts.help
  92. return
  93. end
  94. ## include path
  95. opts.includes.split(/,/).each do |path|
  96. $: << path
  97. end if opts.includes
  98. ## require library
  99. opts.requires.split(/,/).each do |library|
  100. require library
  101. end if opts.requires
  102. ## action
  103. action = opts.action
  104. action ||= 'syntax' if opts.syntax
  105. action ||= 'convert' if opts.source || opts.notext
  106. ## lang
  107. lang = opts.lang || 'ruby'
  108. action ||= 'convert' if opts.lang
  109. ## class name of Eruby
  110. #classname = opts.class
  111. classname = nil
  112. klass = get_classobj(classname, lang, properties[:pi])
  113. ## kanji code
  114. $KCODE = opts.kanji if opts.kanji
  115. ## read context values from yaml file
  116. datafiles = opts.datafiles
  117. context = load_datafiles(datafiles, opts)
  118. ## parse context data
  119. if opts.context
  120. context = parse_context_data(opts.context, opts)
  121. end
  122. ## properties for engine
  123. properties[:escape] = true if opts.escape && !properties.key?(:escape)
  124. properties[:pattern] = opts.pattern if opts.pattern
  125. #properties[:trim] = false if opts.notrim
  126. properties[:preamble] = properties[:postamble] = false if opts.bodyonly
  127. properties[:pi] = nil if properties[:pi] == true
  128. ## create engine and extend enhancers
  129. engine = klass.new(nil, properties)
  130. enhancers = get_enhancers(opts.enhancers)
  131. #enhancers.push(Erubis::EscapeEnhancer) if opts.escape
  132. enhancers.each do |enhancer|
  133. engine.extend(enhancer)
  134. engine.bipattern = properties[:bipattern] if enhancer == Erubis::BiPatternEnhancer
  135. end
  136. ## no-text
  137. engine.extend(Erubis::NoTextEnhancer) if opts.notext
  138. ## convert and execute
  139. val = nil
  140. msg = "Syntax OK\n"
  141. if filenames && !filenames.empty?
  142. filenames.each do |filename|
  143. File.file?(filename) or
  144. raise CommandOptionError.new("#{filename}: file not found.")
  145. engine.filename = filename
  146. engine.convert!(File.read(filename))
  147. val = do_action(action, engine, context, filename, opts)
  148. msg = nil if val
  149. end
  150. else
  151. engine.filename = filename = '(stdin)'
  152. engine.convert!($stdin.read())
  153. val = do_action(action, engine, context, filename, opts)
  154. msg = nil if val
  155. end
  156. print msg if action == 'syntax' && msg
  157. end
  158. private
  159. def do_action(action, engine, context, filename, opts)
  160. case action
  161. when 'convert'
  162. s = manipulate_src(engine.src, opts)
  163. when nil, 'exec', 'execute'
  164. s = opts.binding ? engine.result(context.to_hash) : engine.evaluate(context)
  165. when 'syntax'
  166. s = check_syntax(filename, engine.src)
  167. else
  168. raise "*** internal error"
  169. end
  170. print s if s
  171. return s
  172. end
  173. def manipulate_src(source, opts)
  174. flag_linenum = opts.linenum
  175. flag_unique = opts.unique
  176. flag_compact = opts.compact
  177. if flag_linenum
  178. n = 0
  179. source.gsub!(/^/) { n += 1; "%5d: " % n }
  180. source.gsub!(/^ *\d+:\s+?\n/, '') if flag_compact
  181. source.gsub!(/(^ *\d+:\s+?\n)+/, "\n") if flag_unique
  182. else
  183. source.gsub!(/^\s*?\n/, '') if flag_compact
  184. source.gsub!(/(^\s*?\n)+/, "\n") if flag_unique
  185. end
  186. return source
  187. end
  188. def usage(command=nil)
  189. command ||= File.basename($0)
  190. buf = []
  191. buf << "erubis - embedded program converter for multi-language"
  192. buf << "Usage: #{command} [..options..] [file ...]"
  193. buf << " -h, --help : help"
  194. buf << " -v : version"
  195. buf << " -x : show converted code"
  196. buf << " -X : show converted code, only ruby code and no text part"
  197. buf << " -N : numbering: add line numbers (for '-x/-X')"
  198. buf << " -U : unique: compress empty lines to a line (for '-x/-X')"
  199. buf << " -C : compact: remove empty lines (for '-x/-X')"
  200. buf << " -b : body only: no preamble nor postamble (for '-x/-X')"
  201. buf << " -z : syntax checking"
  202. buf << " -e : escape (equal to '--E Escape')"
  203. buf << " -p pattern : embedded pattern (default '<% %>')"
  204. buf << " -l lang : convert but no execute (ruby/php/c/cpp/java/scheme/perl/js)"
  205. buf << " -E e1,e2,... : enhancer names (Escape, PercentLine, BiPattern, ...)"
  206. buf << " -I path : library include path"
  207. buf << " -K kanji : kanji code (euc/sjis/utf8) (default none)"
  208. buf << " -c context : context data string (yaml inline style or ruby code)"
  209. buf << " -f datafile : context data file ('*.yaml', '*.yml', or '*.rb')"
  210. #buf << " -t : expand tab characters in YAML file"
  211. buf << " -T : don't expand tab characters in YAML file"
  212. buf << " -S : convert mapping key from string to symbol in YAML file"
  213. buf << " -B : invoke 'result(binding)' instead of 'evaluate(context)'"
  214. buf << " --pi=name : parse '<?name ... ?>' instead of '<% ... %>'"
  215. #'
  216. # -T : don't trim spaces around '<% %>'
  217. # -c class : class name (XmlEruby/PercentLineEruby/...) (default Eruby)
  218. # -r library : require library
  219. # -a : action (convert/execute)
  220. return buf.join("\n")
  221. end
  222. def collect_supported_properties(erubis_klass)
  223. list = []
  224. erubis_klass.ancestors.each do |klass|
  225. if klass.respond_to?(:supported_properties)
  226. list.concat(klass.supported_properties)
  227. end
  228. end
  229. return list
  230. end
  231. def show_properties
  232. s = "supported properties:\n"
  233. basic_props = collect_supported_properties(Erubis::Basic::Engine)
  234. pi_props = collect_supported_properties(Erubis::PI::Engine)
  235. list = []
  236. common_props = basic_props & pi_props
  237. list << ['(common)', common_props]
  238. list << ['(basic)', basic_props - common_props]
  239. list << ['(pi)', pi_props - common_props]
  240. %w[ruby php c cpp java scheme perl javascript].each do |lang|
  241. klass = Erubis.const_get("E#{lang}")
  242. list << [lang, collect_supported_properties(klass) - basic_props]
  243. end
  244. list.each do |lang, props|
  245. s << " * #{lang}\n"
  246. props.each do |name, default_val, desc|
  247. s << (" --%-23s : %s\n" % ["#{name}=#{default_val.inspect}", desc])
  248. end
  249. end
  250. s << "\n"
  251. return s
  252. end
  253. def show_enhancers
  254. dict = {}
  255. ObjectSpace.each_object(Module) do |mod|
  256. dict[$1] = mod if mod.name =~ /\AErubis::(.*)Enhancer\z/
  257. end
  258. s = "enhancers:\n"
  259. dict.sort_by {|name, mod| name }.each do |name, mod|
  260. s << (" %-13s : %s\n" % [name, mod.desc])
  261. end
  262. return s
  263. end
  264. def version
  265. return Erubis::VERSION
  266. end
  267. def parse_argv(argv, arg_none='', arg_required='', arg_optional='')
  268. options = {}
  269. context = {}
  270. while argv[0] && argv[0][0] == ?-
  271. optstr = argv.shift
  272. optstr = optstr[1, optstr.length-1]
  273. #
  274. if optstr[0] == ?- # context
  275. optstr =~ /\A\-([-\w]+)(?:=(.*))?/ or
  276. raise CommandOptionError.new("-#{optstr}: invalid context value.")
  277. name, value = $1, $2
  278. name = name.gsub(/-/, '_').intern
  279. #value = value.nil? ? true : YAML.load(value) # error, why?
  280. value = value.nil? ? true : YAML.load("---\n#{value}\n")
  281. context[name] = value
  282. #
  283. else # options
  284. while optstr && !optstr.empty?
  285. optchar = optstr[0].chr
  286. optstr = optstr[1..-1]
  287. if arg_none.include?(optchar)
  288. options[optchar] = true
  289. elsif arg_required.include?(optchar)
  290. arg = optstr.empty? ? argv.shift : optstr or
  291. raise CommandOptionError.new("-#{optchar}: #{@option_names[optchar]} required.")
  292. options[optchar] = arg
  293. optstr = nil
  294. elsif arg_optional.include?(optchar)
  295. arg = optstr.empty? ? true : optstr
  296. options[optchar] = arg
  297. optstr = nil
  298. else
  299. raise CommandOptionError.new("-#{optchar}: unknown option.")
  300. end
  301. end
  302. end
  303. #
  304. end # end of while
  305. return options, context
  306. end
  307. def untabify(str, width=8)
  308. list = str.split(/\t/)
  309. last = list.pop
  310. sb = ''
  311. list.each do |s|
  312. column = (n = s.rindex(?\n)) ? s.length - n - 1 : s.length
  313. n = width - (column % width)
  314. sb << s << (' ' * n)
  315. end
  316. sb << last
  317. return sb
  318. end
  319. #--
  320. #def untabify(str, width=8)
  321. # sb = ''
  322. # str.scan(/(.*?)\t/m) do |s, |
  323. # len = (n = s.rindex(?\n)) ? s.length - n - 1 : s.length
  324. # sb << s << (" " * (width - len % width))
  325. # end
  326. # return $' ? (sb << $') : str
  327. #end
  328. #++
  329. def get_classobj(classname, lang, pi)
  330. classname ||= "E#{lang}"
  331. base_module = pi ? Erubis::PI : Erubis
  332. begin
  333. klass = base_module.const_get(classname)
  334. rescue NameError
  335. klass = nil
  336. end
  337. unless klass
  338. if lang
  339. msg = "-l #{lang}: invalid language name (class #{base_module.name}::#{classname} not found)."
  340. else
  341. msg = "-c #{classname}: invalid class name."
  342. end
  343. raise CommandOptionError.new(msg)
  344. end
  345. return klass
  346. end
  347. def get_enhancers(enhancer_names)
  348. return [] unless enhancer_names
  349. enhancers = []
  350. shortname = nil
  351. begin
  352. enhancer_names.split(/,/).each do |name|
  353. shortname = name
  354. enhancers << Erubis.const_get("#{shortname}Enhancer")
  355. end
  356. rescue NameError
  357. raise CommandOptionError.new("#{shortname}: no such Enhancer (try '-h' to show all enhancers).")
  358. end
  359. return enhancers
  360. end
  361. def load_datafiles(filenames, opts)
  362. context = Erubis::Context.new
  363. return context unless filenames
  364. filenames.split(/,/).each do |filename|
  365. filename.strip!
  366. test(?f, filename) or raise CommandOptionError.new("#{filename}: file not found.")
  367. if filename =~ /\.ya?ml$/
  368. if opts.unexpand
  369. ydoc = YAML.load_file(filename)
  370. else
  371. ydoc = YAML.load(untabify(File.read(filename)))
  372. end
  373. ydoc.is_a?(Hash) or raise CommandOptionError.new("#{filename}: root object is not a mapping.")
  374. intern_hash_keys(ydoc) if opts.intern
  375. context.update(ydoc)
  376. elsif filename =~ /\.rb$/
  377. str = File.read(filename)
  378. context2 = Erubis::Context.new
  379. _instance_eval(context2, str)
  380. context.update(context2)
  381. else
  382. CommandOptionError.new("#{filename}: '*.yaml', '*.yml', or '*.rb' required.")
  383. end
  384. end
  385. return context
  386. end
  387. def _instance_eval(_context, _str)
  388. _context.instance_eval(_str)
  389. end
  390. def parse_context_data(context_str, opts)
  391. if context_str[0] == ?{
  392. require 'yaml'
  393. ydoc = YAML.load(context_str)
  394. unless ydoc.is_a?(Hash)
  395. raise CommandOptionError.new("-c: root object is not a mapping.")
  396. end
  397. intern_hash_keys(ydoc) if opts.intern
  398. return ydoc
  399. else
  400. context = Erubis::Context.new
  401. context.instance_eval(context_str, '-c')
  402. return context
  403. end
  404. end
  405. def intern_hash_keys(obj, done={})
  406. return if done.key?(obj.__id__)
  407. case obj
  408. when Hash
  409. done[obj.__id__] = obj
  410. obj.keys.each do |key|
  411. obj[key.intern] = obj.delete(key) if key.is_a?(String)
  412. end
  413. obj.values.each do |val|
  414. intern_hash_keys(val, done) if val.is_a?(Hash) || val.is_a?(Array)
  415. end
  416. when Array
  417. done[obj.__id__] = obj
  418. obj.each do |val|
  419. intern_hash_keys(val, done) if val.is_a?(Hash) || val.is_a?(Array)
  420. end
  421. end
  422. end
  423. def check_syntax(filename, src)
  424. require 'open3'
  425. #command = (ENV['_'] || 'ruby') + ' -wc' # ENV['_'] stores command name
  426. bin = ENV['_'] && File.basename(ENV['_']) =~ /^ruby/ ? ENV['_'] : 'ruby'
  427. command = bin + ' -wc'
  428. stdin, stdout, stderr = Open3.popen3(command)
  429. stdin.write(src)
  430. stdin.close
  431. result = stdout.read()
  432. stdout.close()
  433. errmsg = stderr.read()
  434. stderr.close()
  435. return nil unless errmsg && !errmsg.empty?
  436. errmsg =~ /\A-:(\d+): /
  437. linenum, message = $1, $'
  438. return "#{filename}:#{linenum}: #{message}"
  439. end
  440. if defined?(RUBY_ENGINE) && RUBY_ENGINE == "rbx"
  441. def check_syntax(filename, src)
  442. require 'compiler'
  443. verbose = $VERBOSE
  444. msg = nil
  445. begin
  446. $VERBOSE = true
  447. Rubinius::Compiler.compile_string(src, filename)
  448. rescue SyntaxError => ex
  449. ex_linenum = ex.line
  450. linenum = 0
  451. srcline = src.each_line do |line|
  452. linenum += 1
  453. break line if linenum == ex_linenum
  454. end
  455. msg = "#{ex.message}\n"
  456. msg << srcline
  457. msg << "\n" unless srcline =~ /\n\z/
  458. msg << (" " * (ex.column-1)) << "^\n"
  459. ensure
  460. $VERBOSE = verbose
  461. end
  462. return msg
  463. end
  464. end
  465. end
  466. end