| Class | Erubis::Main |
| In: |
erubis/main.rb
|
| Parent: | Object |
# File erubis/main.rb, line 40 def self.main(argv=ARGV) status = 0 begin Main.new.execute(ARGV) rescue CommandOptionError => ex $stderr.puts ex.message status = 1 end exit(status) end
# File erubis/main.rb, line 51 def initialize @single_options = "hvxTtSbeB" @arg_options = "pcrfKIlaE" @option_names = { ?h => :help, ?v => :version, ?x => :source, ?T => :notrim, ?t => :untabify, ?S => :intern, ?b => :bodyonly, ?B => :binding, ?p => :pattern, ?c => :class, ?e => :escape, ?r => :requires, ?f => :yamlfiles, ?K => :kanji, ?I => :includes, ?l => :lang, ?a => :action, ?E => :enhancers, } assert unless @single_options.length + @arg_options.length == @option_names.length (@single_options + @arg_options).each_byte do |ch| assert unless @option_names.key?(ch) end end
# File erubis/main.rb, line 81 def execute(argv=ARGV) ## parse command-line options options, properties = parse_argv(argv, @single_options, @arg_options) filenames = argv options[?h] = true if properties[:help] opts = Object.new arr = @option_names.collect { |ch, name| "def #{name}; @#{name}; end\n" } opts.instance_eval arr.join options.each do |ch, val| name = @option_names[ch] opts.instance_variable_set("@#{name}", val) end ## help, version, enhancer list if opts.help || opts.version puts version() if opts.version puts usage() if opts.help puts show_properties() if opts.help puts show_enhancers() if opts.help return end ## include path opts.includes.split(/,/).each do |path| $: << path end if opts.includes ## require library opts.requires.split(/,/).each do |library| require library end if opts.requires ## action action = opts.action action ||= 'compile' if opts.source ## lang lang = opts.lang || 'ruby' action ||= 'compile' if opts.lang ## class name of Eruby classname = opts.class klass = get_classobj(classname, lang) ## kanji code $KCODE = opts.kanji if opts.kanji ## read context values from yaml file yamlfiles = opts.yamlfiles context = load_yamlfiles(yamlfiles, opts) ## properties for engine properties[:pattern] = opts.pattern if opts.pattern properties[:trim] = false if opts.notrim properties[:preamble] = properties[:postamble] = false if opts.bodyonly ## create engine and extend enhancers engine = klass.new(nil, properties) enhancers = get_enhancers(opts.enhancers) enhancers.push(Erubis::EscapeEnhancer) if opts.escape enhancers.each do |enhancer| engine.extend(enhancer) engine.bipattern = properties[:bipattern] if enhancer == Erubis::BiPatternEnhancer end ## compile and execute val = nil if filenames && !filenames.empty? filenames.each do |filename| test(?f, filename) or raise CommandOptionError.new("#{filename}: file not found.") engine.filename = filename engine.compile!(File.read(filename)) print val if val = do_action(action, engine, context, opts) end else engine.filename = '(stdin)' engine.compile!($stdin.read()) print val if val = do_action(action, engine, context, opts) end end