Class Kwartz::Main
In: kwartz/main.rb
Parent: Object

main command

ex.

 Kwartz::Main.add_handler('mylang', MyLangDirectiveHandler, MyLangTranslator)
 Kwartz::Main.main(ARGV)

Methods

execute   main   new  

Public Class methods

[Source]

# File kwartz/main.rb, line 174
    def self.main(argv=ARGV)
      status = 0
      begin
        main = Kwartz::Main.new(argv)
        output = main.execute()
        print output unless output == nil
      rescue Kwartz::KwartzError => ex
        raise ex if $DEBUG
        $stderr.puts ex.to_s
        status = 1
      end
      exit status
    end

[Source]

# File kwartz/main.rb, line 168
    def initialize(argv=ARGV)
      @argv = argv
      @command = File.basename($0)
    end

Public Instance methods

[Source]

# File kwartz/main.rb, line 211
    def execute(argv=@argv)

      ## parse command-line options
      options = CommandOptions.new(@@option_table, properties = {})
      pdata_filenames = options.parse_argv(argv)
      options.help = true if properties[:help]

      ## help
      if options.help || options.version
        puts version() if options.version
        puts help()    if options.help
        return nil
      end

      ## check filenames
      if pdata_filenames.empty?
        raise option_error("filename of presentation data is required.")
      end
      pdata_filenames.each do |filename|
        test(?f, filename)  or raise option_error("#{filename}: file not found.")
      end

      ## options
      $KCODE = options.kanji if options.kanji
      $DEBUG = options.debug if options.debug

      ## parse class, hander class, translator class
      style = options.pstyle || 'css'
      unless parser_class = PresentationLogicParser.get_class(style)
        s = "-#{options.chr(:pstyle)} #{style}"
        raise option_error("#{s}: unknown style name (parser class not registered).")
      end
      lang = options.lang || Config::PROPERTY_LANG      # 'eruby'
      unless handler_class = Handler.get_class(lang)
        s = "-#{options.chr(:lang)} #{lang}"
        raise option_error("#{s}: unknown lang name (handler class not registered).")
      end
      unless translator_class = Translator.get_class(lang)
        s = "-#{options.chr(:lang)} #{lang}"
        raise option_error("#{s}: unknown lang name (translator class not registered).")
      end

      ## require libraries
      if options.requires
        libraries = options.requires
        libraries.split(/,/).each do |library|
          library.strip!
          require library
        end
      end

      ## parse presentation logic file
      ruleset_list = []
      if options.plogics
        parser = parser_class.new(properties)
        options.plogics.split(/,/).each do |filename|
          filename.strip!
          if test(?f, filename)
            # ok
          elsif test(?f, filename + '.plogic')
            filename += '.plogic'
          else
            s = "-#{options.chr(:plogics)} #{filename}[.plogic]"
            raise option_error("#{s}: file not found.")
          end
          plogic = File.read(filename)
          ruleset_list += parser.parse(plogic, filename)
        end
      end

      ## properties
      properties[:escape] = true if options.escape && !properties.key?(:escape)

      ## create converter
      handler = handler_class.new(ruleset_list, properties)
      converter = TextConverter.new(handler, properties)

      ## import-files and layout-file
      import_filenames = []
      if options[?i]
        (import_filenames = options.imports.split(/,/)).each do |filename|
          unless test(?f, filename)
            s = "-#{options.chr(:imports)}"
            raise option_error("#{s} #{filename}: file not found.")
          end
        end
      end
      if options.layout
        unless test(?f, options.layout)
          s = "-#{options.chr(:layout)}"
          raise option_error("#{s} #{options.layout}: file not found.")
        end
        import_filenames += pdata_filenames
        pdata_filenames = [options.layout]
      end
      import_filenames.each do |filename|
        pdata = File.read(filename)
        converter.convert(pdata, filename)
      end

      ## convert presentation data file
      stmt_list = []
      pdata = nil
      pdata_filenames.each do |filename|
        test(?f, filename)  or raise option_error("#{filename}: file not found.")
        pdata = File.read(filename)
        #handler = handler_class.new(ruleset_list)
        #converter = TextConverter.new(handler, properties)
        list = converter.convert(pdata, filename)
        stmt_list.concat(list)
      end

      ## extract element or content
      elem_id = options.extract_cont || options.extract_elem
      if elem_id
        content_only = options.extract_cont ? true : false
        stmt_list = handler.extract(elem_id, content_only)
      end

      ## translate statements into target code(eRuby, PHP, JSP)
      if pdata[pdata.index(?\n) - 1] == ?\r
        properties[:nl] ||= "\r\n"
      end
      translator = translator_class.new(properties)
      if options.notext
        translator.extend(Kwartz::NoTextEnhancer)
      end
      output = translator.translate(stmt_list)

      ## action
      if options.action
        case options.action
        when 'compile'
          # nothing
        when 'defun'
          basename = File.basename(pdata_filenames.first).sub(/\.\w+/, '')
          output = Kwartz::Defun.defun(basename, output, lang, properties)
        else
          option_error("-#{options.chr(:action)} #{options.aciton}: invalid action.")
        end
      end

      ## load YAML file and evaluate eRuby script
      if options.yamlfile
        eruby_script = output
        if lang == 'eruby' || lang == 'rails'
          require 'erb'
          trim_mode = properties.key?(:trim) ? properties[:trim] : 1
          src = ERB.new(eruby_script, $SAFE, trim_mode).src
          mod = ERB::Util
        elsif lang == 'erubis'
          require 'erubis'
          src = Erubis::Eruby.new(eruby_script).src
          mod = Erubis::XmlHelper
        else
          s1 = "-#{options.chr(:yamlfile)}"
          s2 = "-#{options.chr(:lang)} #{lang}"
          option_error("#{s1}: not available with '#{s2}'.")
        end
        unless test(?f, options.yamlfile)
          s = "-#{options.chr(:yamlfile)} #{options.yamlfile}"
          raise option_error("#{s}: file not found.")
        end
        str = File.read(options.yamlfile)
        str = Kwartz::Util.untabify(str) if options.untabify
        require 'yaml'
        ydoc = YAML.load(str)
        unless ydoc.is_a?(Hash)
          s = "-#{options.chr(:yamlfile)} #{options.yamlfile}"
          raise option_error("#{s}: not a mapping.")
        end
        Kwartz::Util.intern_hash_keys(ydoc) if options.intern
        context = Object.new
        ydoc.each do |key, val|
          context.instance_variable_set("@#{key}", val)
        end
        context.extend(mod)    # ERB::Util or Erubis::XmlHelper
        output = context.instance_eval(src)
      end

      return output

    end

[Validate]