lib/kitabu/cli.rb in kitabu-2.1.0 vs lib/kitabu/cli.rb in kitabu-3.0.0
- old
+ new
@@ -1,18 +1,25 @@
-# -*- encoding: utf-8 -*-
+# frozen_string_literal: true
+
module Kitabu
class Cli < Thor
- FORMATS = %w[pdf html epub mobi txt]
+ FORMATS = %w[pdf html epub mobi].freeze
check_unknown_options!
def self.exit_on_failure?
true
end
def initialize(args = [], options = {}, config = {})
- if (config[:current_task] || config[:current_command]).name == "new" && args.empty?
- raise Error, "The e-Book path is required. For details run: kitabu help new"
+ has_no_args = (
+ config[:current_task] ||
+ config[:current_command]
+ ).name == "new" && args.empty?
+
+ if has_no_args
+ raise Error,
+ "The e-Book path is required. For details run: kitabu help new"
end
super
end
@@ -23,25 +30,29 @@
generator.destination_root = path
generator.invoke_all
end
desc "export [OPTIONS]", "Export e-book"
- method_option :only, :type => :string, :desc => "Can be one of: #{FORMATS.join(", ")}"
- method_option :open, :type => :boolean, :desc => "Automatically open PDF (Preview.app for Mac OS X and xdg-open for Linux)"
+ method_option :only, type: :string,
+ desc: "Can be one of: #{FORMATS.join(', ')}"
+ method_option :open, type: :boolean,
+ desc: "Automatically open PDF (Preview.app for " \
+ "Mac OS X and xdg-open for Linux)"
def export
if options[:only] && !FORMATS.include?(options[:only])
- raise Error, "The --only option need to be one of: #{FORMATS.join(", ")}"
+ raise Error,
+ "The --only option need to be one of: #{FORMATS.join(', ')}"
end
inside_ebook!
Kitabu::Exporter.run(root_dir, options)
end
desc "version", "Prints the Kitabu's version information"
- map %w(-v --version) => :version
+ map %w[-v --version] => :version
def version
say "Kitabu version #{Version::STRING}"
end
@@ -49,28 +60,30 @@
def check
result = []
result << {
- :description => "Prince XML: Converts HTML files into PDF files.",
- :installed => Kitabu::Dependency.prince?
+ description: "Prince XML: Converts HTML files into PDF files.",
+ installed: Kitabu::Dependency.prince?
}
result << {
- :description => "KindleGen: Converts ePub e-books into .mobi files.",
- :installed => Kitabu::Dependency.kindlegen?
+ description: "Calibre's ebook-convert: Converts ePub e-books into " \
+ ".mobi files.",
+ installed: Kitabu::Dependency.calibre?
}
- result << {
- :description => "html2text: Converts HTML documents into plain text.",
- :installed => Kitabu::Dependency.html2text?
- }
+ result.each do |info|
+ state = if info[:installed]
+ color("Installed.", :green)
+ else
+ color("Not installed.", :red)
+ end
- result.each do |result|
- text = color(result[:name], :blue)
- text << "\n" << result[:description]
- text << "\n" << (result[:installed] ? color("Installed.", :green) : color("Not installed.", :red))
+ text = color(info[:name], :blue)
+ text << "\n" << info[:description]
+ text << "\n" << state
text << "\n"
say(text)
end
end
@@ -108,33 +121,35 @@
"Footnotes: #{stats.footnotes}",
"Code blocks: #{stats.code_blocks}"
].join("\n")
end
- private
- def inside_ebook!
- unless File.exist?(config_path)
- raise Error, "You have to run this command from inside an e-book directory."
+ no_commands do
+ def inside_ebook!
+ return if File.exist?(config_path)
+
+ raise Error,
+ "You have to run this command from inside an e-book directory."
end
- end
- def config
- YAML.load_file(config_path).with_indifferent_access
- end
+ def config
+ YAML.load_file(config_path).with_indifferent_access
+ end
- def config_path
- root_dir.join("config/kitabu.yml")
- end
+ def config_path
+ root_dir.join("config/kitabu.yml")
+ end
- def root_dir
- @root ||= Pathname.new(Dir.pwd)
- end
+ def root_dir
+ @root_dir ||= Pathname.new(Dir.pwd)
+ end
- def color(text, color)
- color? ? shell.set_color(text, color) : text
- end
+ def color(text, color)
+ color? ? shell.set_color(text, color) : text
+ end
- def color?
- shell.instance_of?(Thor::Shell::Color)
+ def color?
+ shell.instance_of?(Thor::Shell::Color)
+ end
end
end
end