#!/usr/bin/env ruby # encoding: utf-8 # # Copyright (c) 2014-2016 TechnoPark Corp. # Copyright (c) 2014-2016 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 'pdd' require 'slop' require 'pdd/version' require 'nokogiri' args = [] args += File.read('.pdd').split(/\s+/).map(&:strip) if File.exist?('.pdd') args += ARGV opts = Slop.parse(args, strict: true, help: true) do banner "Usage (#{PDD::VERSION}): pdd [options]" on 'v', 'verbose', 'Enable verbose mode' on 'q', 'quiet', 'Enable quiet mode' on 'version', 'Show current version' on( 's', 'source', 'Source directory to parse', argument: :required ) on( 'f', 'file', 'File to save XML into', argument: :required ) on( 'e', 'exclude', 'Glob pattern to exclude', as: Array, argument: :required ) on( 't', 'format', 'Format to use (xml|html)', argument: :required ) on( 'r', 'rule', 'Format to use (xml|html)', argument: :required, as: Array, limit: 1 ) end raise '-f is mandatory when using -v' if opts.verbose? && !opts.file? if opts.help? puts opts exit end if opts.version? puts PDD::VERSION exit end Encoding.default_external = Encoding::UTF_8 Encoding.default_internal = Encoding::UTF_8 file = opts.file? ? File.new(opts[:file], 'w') : STDOUT output = PDD::Base.new(opts).xml if opts[:format] if opts[:format] == 'html' xslt = File.join( File.dirname(File.dirname(__FILE__)), 'assets', 'puzzles.xsl' ) output = Nokogiri::XSLT(File.read(xslt)).transform(Nokogiri::XML(output)) elsif opts[:format] != 'xml' raise 'invalid format, use html or xml' end end file << output