def execute(argv=@argv)
options = CommandOptions.new(@@option_table, properties = {})
pdata_filenames = options.parse_argv(argv)
options.help = true if properties[:help]
if options.help || options.version
puts version() if options.version
puts help() if options.help
return nil
end
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
$KCODE = options.kanji if options.kanji
$DEBUG = options.debug if options.debug
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
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
if options.requires
libraries = options.requires
libraries.split(/,/).each do |library|
library.strip!
require library
end
end
ruleset_list = []
if options.plogics
parser = parser_class.new(properties)
options.plogics.split(/,/).each do |filename|
filename.strip!
if test(?f, filename)
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[:escape] = true if options.escape && !properties.key?(:escape)
handler = handler_class.new(ruleset_list, properties)
converter = TextConverter.new(handler, properties)
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
stmt_list = []
pdata = nil
pdata_filenames.each do |filename|
test(?f, filename) or raise option_error("#{filename}: file not found.")
pdata = File.read(filename)
list = converter.convert(pdata, filename)
stmt_list.concat(list)
end
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
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)
if options.action
case options.action
when 'compile'
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
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)
output = context.instance_eval(src)
end
return output
end