#!/usr/bin/env ruby # encoding: utf-8 # # Copyright (c) 2014 TechnoPark Corp. # Copyright (c) 2014 Yegor Bugayenko # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the 'Software'), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. STDOUT.sync = true require 'slop' require 'nokogiri' require 'est' require 'est/version' opts = Slop.parse(ARGV, strict: true, help: true) do banner "Usage (#{Est::VERSION}): est [options]" on 'v', 'verbose', 'Enable verbose mode' on 'version', 'Show current version' on( 'd', 'dir', 'Source directory to parse', argument: :required ) on( 'f', 'file', 'File to save output into', argument: :required ) on( 't', 'format', 'Format to use (xml|html|text)', argument: :required ) end fail '-f is mandatory when using -v' if opts.verbose? && !opts.file? if opts.help? puts opts exit end if opts.version? puts Est::VERSION exit end Encoding.default_external = Encoding::UTF_8 Encoding.default_internal = Encoding::UTF_8 if opts.file? file = File.new(opts[:file], 'w') Est.log.info "output saving into #{file.path}" else file = STDOUT end output = Est::Base.new(opts).xml if opts[:format].nil? || opts[:format] == 'text' xslt = File.join( File.dirname(File.dirname(__FILE__)), 'assets', 'est-text.xsl' ) output = Nokogiri::XSLT(File.read(xslt)).apply_to(Nokogiri::XML(output)) elsif opts[:format] == 'html' Est.log.info 'using HTML format' xslt = File.join( File.dirname(File.dirname(__FILE__)), 'assets', 'est.xsl' ) output = Nokogiri::XSLT(File.read(xslt)).transform(Nokogiri::XML(output)) elsif opts[:format] == 'xml' Est.log.info 'using XML format' else fail 'invalid format, use html, text, or xml' end file << output