Class: Kwartz::Main
- Inherits:
-
Object
- Object
- Kwartz::Main
- Defined in:
- lib/kwartz/main.rb
Overview
Constant Summary collapse
- @@option_table =
[ [ ?h, :help, nil ], [ ?v, :version, nil ], [ ?e, :escape, nil ], [ ?D, :debug, nil ], [ ?t, :untabify, nil ], [ ?S, :intern, nil ], [ ?N, :notext, nil ], [ ?l, :lang, 'lang name' ], [ ?k, :kanji, 'kanji code' ], [ ?a, :action, 'action name' ], [ ?r, :requires, 'library name' ], [ ?p, :plogics, 'file name' ], [ ?P, :pstyle, 'parser style' ], [ ?x, :extract_cont, 'element id' ], [ ?X, :extract_elem, 'element id' ], [ ?i, :imports, 'file name' ], [ ?L, :layout, 'file name' ], [ ?f, :yamlfile, 'yaml file' ], ]
Class Method Summary collapse
Instance Method Summary collapse
- #execute(argv = @argv) ⇒ Object
-
#initialize(argv = ARGV) ⇒ Main
constructor
A new instance of Main.
Constructor Details
#initialize(argv = ARGV) ⇒ Main
Returns a new instance of Main.
169 170 171 172 |
# File 'lib/kwartz/main.rb', line 169 def initialize(argv=ARGV) @argv = argv @command = File.basename($0) end |
Class Method Details
.main(argv = ARGV) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/kwartz/main.rb', line 175 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 |
Instance Method Details
#execute(argv = @argv) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/kwartz/main.rb', line 212 def execute(argv=@argv) ## parse command-line options = CommandOptions.new(@@option_table, properties = {}) pdata_filenames = .parse_argv(argv) .help = true if properties[:help] ## help if .help || .version puts version() if .version puts help() if .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 = .kanji if .kanji $DEBUG = .debug if .debug ## parse class, hander class, translator class style = .pstyle || 'css' unless parser_class = PresentationLogicParser.get_class(style) s = "-#{.chr(:pstyle)} #{style}" raise option_error("#{s}: unknown style name (parser class not registered).") end lang = .lang || Config::PROPERTY_LANG # 'eruby' unless handler_class = Handler.get_class(lang) s = "-#{.chr(:lang)} #{lang}" raise option_error("#{s}: unknown lang name (handler class not registered).") end unless translator_class = Translator.get_class(lang) s = "-#{.chr(:lang)} #{lang}" raise option_error("#{s}: unknown lang name (translator class not registered).") end ## require libraries if .requires libraries = .requires libraries.split(/,/).each do |library| library.strip! require library end end ## parse presentation logic file ruleset_list = [] if .plogics parser = parser_class.new(properties) .plogics.split(/,/).each do |filename| filename.strip! if test(?f, filename) # ok elsif test(?f, filename + '.plogic') filename += '.plogic' else s = "-#{.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 .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 [?i] (import_filenames = .imports.split(/,/)).each do |filename| unless test(?f, filename) s = "-#{.chr(:imports)}" raise option_error("#{s} #{filename}: file not found.") end end end if .layout unless test(?f, .layout) s = "-#{.chr(:layout)}" raise option_error("#{s} #{.layout}: file not found.") end import_filenames += pdata_filenames pdata_filenames = [.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 = .extract_cont || .extract_elem if elem_id content_only = .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 .notext translator.extend(Kwartz::NoTextEnhancer) end output = translator.translate(stmt_list) ## action if .action case .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("-#{.chr(:action)} #{.aciton}: invalid action.") end end ## load YAML file and evaluate eRuby script if .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 = "-#{.chr(:yamlfile)}" s2 = "-#{.chr(:lang)} #{lang}" option_error("#{s1}: not available with '#{s2}'.") end unless test(?f, .yamlfile) s = "-#{.chr(:yamlfile)} #{.yamlfile}" raise option_error("#{s}: file not found.") end str = File.read(.yamlfile) str = Kwartz::Util.untabify(str) if .untabify require 'yaml' ydoc = YAML.load(str) unless ydoc.is_a?(Hash) s = "-#{.chr(:yamlfile)} #{.yamlfile}" raise option_error("#{s}: not a mapping.") end Kwartz::Util.intern_hash_keys(ydoc) if .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 |