Module: Repub::App::Options
Constant Summary
Constants included from Logger
Logger::LOGGER_NORMAL, Logger::LOGGER_QUIET, Logger::LOGGER_VERBOSE
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
Methods included from Logger
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/repub/app/options.rb', line 8 def @options end |
Instance Method Details
#help(opts) ⇒ Object
201 202 203 204 205 206 207 |
# File 'lib/repub/app/options.rb', line 201 def help(opts) puts opts puts puts " Current profile (#{[:profile]}):" dump_profile([:profile]) puts end |
#parse_options(args) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/repub/app/options.rb', line 10 def (args) # Default options @options = { :add => [], :after => [], :before => [], :browser => false, :css => nil, :encoding => nil, :fixup => true, :helper => 'wget', :metadata => {}, :output_path => Dir.getwd, :profile => 'default', :remove => [], :rx => [], :selectors => Parser::Selectors, :url => nil, :verbosity => Repub::App::Logger::LOGGER_NORMAL, } # Load default profile if load_profile([:profile]).empty? write_profile([:profile]) end # Parse command line parser = OptionParser.new do |opts| opts. = <<-BANNER.gsub(/^ /,'') Repub is a simple HTML to ePub converter. Usage: #{App.name} [options] url General options: BANNER opts.on("-D", "--downloader NAME ", ['wget', 'httrack'], "Which downloader to use to get files (wget or httrack).", "Default is #{[:helper]}." ) { |value| [:helper] = value } opts.on("-o", "--output PATH", String, "Output path for generated ePub file.", "Default is #{[:output_path]}/<Parsed_Title>.epub" ) { |value| [:output_path] = File.(value) } opts.on("-w", "--write-profile NAME", String, "Save given options for later reuse as profile NAME." ) { |value| [:profile] = value; write_profile(value) } opts.on("-l", "--load-profile NAME", String, "Load options from saved profile NAME." ) { |value| [:profile] = value; load_profile(value) } opts.on("-W", "--write-default", "Save given options for later reuse as default profile." ) { write_profile } opts.on("-L", "--list-profiles", "List saved profiles." ) { list_profiles; exit 1 } opts.on("-C", "--cleanup", "Clean up download cache." ) { Fetcher::Cache.cleanup; exit 1 } opts.on("-v", "--verbose", "Turn on verbose output." ) { [:verbosity] = Repub::App::Logger::LOGGER_VERBOSE } opts.on("-q", "--quiet", "Turn off any output except errors." ) { [:verbosity] = Repub::App::Logger::LOGGER_QUIET } opts.on("-V", "--version", "Show version." ) { puts Repub.version; exit 1 } opts.on("-h", "--help", "Show this help message." ) { help opts; exit 1 } opts.separator "" opts.separator " Parser options:" opts.on("-x", "--selector NAME:VALUE", String, "Set parser XPath selector NAME to VALUE.", "Recognized selectors are: [title toc toc_item toc_section]" ) do |value| begin name, value = value.match(/([^:]+):(.*)/)[1, 2] rescue log.fatal "ERROR: invalid argument: -x '#{value}'. See '#{App.name} --help'." end [:selectors][name.to_sym] = value end opts.on("-m", "--meta NAME:VALUE", String, "Set publication information metadata NAME to VALUE.", "Valid metadata names are: [creator date description", "language publisher relation rights subject title]" ) do |value| begin name, value = value.match(/([^:]+):(.*)/)[1, 2] rescue log.fatal "ERROR: invalid argument: -m '#{value}'. See '#{App.name} --help'." end [:metadata][name.to_sym] = value end opts.on("-F", "--no-fixup", "Do not attempt to make document meet XHTML 1.0 Strict.", "Default is to try and fix things that are broken. " ) { |value| [:fixup] = false } opts.on("-e", "--encoding NAME", String, "Set source document encoding. Default is to autodetect." ) { |value| [:encoding] = value } opts.separator "" opts.separator " Post-processing options:" opts.on("-s", "--stylesheet PATH", String, "Use custom stylesheet at PATH. Use -s- to remove", "all links to stylesheets and <style> blocks from the source." ) { |value| [:css] = value == '-' ? value : File.(value) } opts.on("-a", "--add PATH", String, "Add external file to the generated ePub." ) { |value| [:add] << File.(value) } opts.on("-N", "--new-fragment XHTML", String, "Prepare document fragment for -A and -P operations." ) do |value| begin @fragment = Nokogiri::HTML.fragment(value) rescue Exception => ex log.fatal "ERROR: invalid fragment: #{ex.to_s}" end end opts.on("-A", "--after SELECTOR", String, "Insert fragment after element with XPath selector." ) do |value| log.fatal "ERROR: -A requires a fragment. See '#{App.name} --help'." if !@fragment @options[:after] << {value => @fragment.clone} end opts.on("-P", "--before SELECTOR", String, "Insert fragment before element with XPath selector." ) do |value| log.fatal "ERROR: -P requires a fragment. See '#{App.name} --help'." if !@fragment @options[:before] << {value => @fragment.clone} end opts.on("-X", "--remove SELECTOR", String, "Remove source element using XPath selector.", "Use -X- to ignore stored profile." ) { |value| value == '-' ? [:remove] = [] : [:remove] << value } opts.on("-R", "--rx /PATTERN/REPLACEMENT/", String, "Edit source HTML using regular expressions.", "Use -R- to ignore stored profile." ) { |value| value == '-' ? [:rx] = [] : [:rx] << value } opts.on("-B", "--browser", "After processing, open resulting HTML in default browser." ) { |value| [:browser] = true } end if args.empty? help parser exit 1 end begin parser.parse! args rescue OptionParser::ParseError => ex log.fatal "ERROR: #{ex.to_s}. See '#{App.name} --help'." end [:url] = args.last if [:url].nil? || [:url].empty? help parser log.fatal "ERROR: Please specify an URL." end end |