#!/usr/bin/env ruby $LOAD_PATH << File.expand_path('../../lib', __FILE__) require 'gli' require 'klipbook' include GLI::App program_desc "Collates the clippings you've saved on your Kindle into a nice html summary for each book." SOURCE_HELP = "Two sources are currently supported: a file from a Kindle device and the Kindle site itself.\n\n" + "Example file formats:\n\n" + " file:path/to/my-clippings-file.txt\n\n" + " site:my-kindle-user@blah.com:my-kindle-password\n\n" + "Note that source and output file defaults can be stored in a file called ~/.klipbookrc\n\n" + "This file is YAML formatted e.g.\n\n" + "source: site:my-kindle-user@blah.com:my-kindle-password\n\n" + "output: my/default/output/directory" version Klipbook::VERSION desc 'Number of books to process' arg_name 'count' default_value 1 flag [:n, :'num-books'] desc 'Collate your clippings into a single json file' arg_name 'source' long_desc "Clippings are fetched from the specified source, collated, and written into a single json file.\n\n" + SOURCE_HELP command :collate do |c| c.desc 'Specify the name of the json file to be written to.' c.arg_name 'File-name' c.flag [:c, :'output-file'] c.desc 'Force overwrite of any existing book entries within the output file' c.switch [:f, :force] c.action do |globals,options,args| book_file_path = output_file(options) books = fetch_books(args, globals) Klipbook::Commands::Collate.new(books, book_file(book_file_path)).call(book_file_path, options[:f]) end end desc 'Pretty print your clippings into a html file for each book' arg_name 'source' long_desc "Clippings are fetched from the specified source before being written to html " + "files in the output directory.\n\n" + SOURCE_HELP command :pprint do |c| c.desc 'Override the directory path where html files are written' c.arg_name 'Directory' c.flag [:o, :'output-dir'] c.desc 'Force overwrite of any existing files' c.switch [:f, :force] c.action do |globals,options,args| books = fetch_books(args, globals) Klipbook::Commands::PrettyPrint.new(books).call(output_dir(options), options[:f]) end end desc 'List available books' arg_name 'source' long_desc "List the books from the specified source to screen.\n\n" + SOURCE_HELP command :list do |c| c.action do |globals,options,args| books = fetch_books(args, globals) Klipbook::Commands::ListBooks.new(books).call end end pre do |globals,command,options,args| unless source_spec(args) raise InvalidSourceError end odir = output_dir(options) if odir && !File.exists?(odir) raise "Output directory doesn't exist: #{odir}" end if globals[:n].to_i == 0 raise 'Specify a number of books greater than 0' end true end on_error do |exception| true end def book_file(file_path) raw_json = if File.exist?(file_path) File.open(file_path, 'r') do |f| f.read end else '' end Klipbook::Collate::BookFile.from_json(raw_json) end def source_spec(args) @source_spec ||= (args[0] || Klipbook::Config.new.read[:source]) end def fetch_books(args, globals) Klipbook::Sources::BookSource.new(source_spec(args), globals[:n].to_i).books end def output_dir(options) @output_dir ||= (options[:o] || Klipbook::Config.new.read[:output] || Dir.pwd) end def output_file(options) @output_file ||= (options[:c] || Klipbook::Config.new.read[:output_file] || File.join(Dir.pwd, 'books.json')) end exit run(ARGV)