#!/usr/bin/env ruby # encoding: utf-8 # # Copyright (c) 2014-2017 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_relative '../lib/pdd' require_relative '../lib/pdd/version' args = [] args += File.read('.pdd').split(/\s+/).map(&:strip) if File.exist?('.pdd') args += ARGV opts = Slop.parse(args, strict: true, help: true) do |o| o.banner = "Usage (#{PDD::VERSION}): pdd [options]" o.bool '-h', '--help', 'Show these instructions' o.bool '-v', '--verbose', 'Enable verbose mode' o.bool '-q', '--quiet', 'Enable quiet mode' o.bool '--version', 'Show current version' o.string '-s', '--source', 'Source directory to parse' o.string '-f', '--file', 'File to save XML into' o.array '-e', '--exclude', 'Glob pattern to exclude' o.string '-t', '--format', 'Format to use (xml|html)' o.array '-r', '--rule', 'Rule to apply' 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