Module: Repub::App::Options

Includes:
Logger
Included in:
Repub::App
Defined in:
lib/repub/app/options.rb

Constant Summary

Constants included from Logger

Logger::LOGGER_NORMAL, Logger::LOGGER_QUIET, Logger::LOGGER_VERBOSE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#log

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/repub/app/options.rb', line 8

def options
  @options
end

Instance Method Details

#help(opts) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/repub/app/options.rb', line 170

def help(opts)
  puts opts
  puts
  puts "  Current profile (#{options[:profile]}):"
  dump_profile(options[: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
# File 'lib/repub/app/options.rb', line 10

def parse_options(args)

  # Default options
  @options = {
    :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(options[:profile]).empty?
    write_profile(options[:profile])
  end
  
  # Parse command line
  parser = OptionParser.new do |opts|
    opts.banner = <<-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 #{options[:helper]}."
    ) { |value| options[:helper] = value }

    opts.on("-o", "--output PATH", String,
      "Output path for generated ePub file.",
      "Default is #{options[:output_path]}/<Parsed_Title>.epub"
    ) { |value| options[:output_path] = File.expand_path(value) }

    opts.on("-w", "--write-profile NAME", String,
      "Save given options for later reuse as profile NAME."
    ) { |value| options[:profile] = value; write_profile(value) }

    opts.on("-l", "--load-profile NAME", String,
      "Load options from saved profile NAME."
    ) { |value| options[: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."
    ) { options[:verbosity] = Repub::App::Logger::LOGGER_VERBOSE }

    opts.on("-q", "--quiet",
      "Turn off any output except errors."
    ) { options[: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
      options[: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
      options[: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| options[:fixup] = false }

    opts.on("-e", "--encoding NAME", String,
      "Set source document encoding. Default is to autodetect."
    ) { |value| options[:encoding] = value }

    opts.separator ""
    opts.separator "  Post-processing options:"
    
    opts.on("-s", "--stylesheet PATH", String,
      "Use custom stylesheet at PATH to add or override existing",
      "CSS references in the source document."
    ) { |value| options[:css] = File.expand_path(value) }

    opts.on("-X", "--remove SELECTOR", String,
      "Remove source element using XPath selector.",
      "Use -X- to ignore stored profile."
    ) { |value| value == '-' ? options[:remove] = [] : options[:remove] << value }
    
    opts.on("-R", "--rx /PATTERN/REPLACEMENT/", String,
      "Edit source HTML using regular expressions.",
      "Use -R- to ignore stored profile."
    ) { |value| value == '-' ? options[:rx] = [] : options[:rx] << value }

    opts.on("-B", "--browse",
      "After processing, open resulting HTML in default browser."
    ) { |value| options[: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

  options[:url] = args.last
  if options[:url].nil? || options[:url].empty?
    help parser
    log.fatal "ERROR: Please specify an URL."
  end
end