Class: Pirka::App::Detect
Constant Summary collapse
- PROGRAM_NAME =
"detect"
- DESCRIPTION =
_("Detects source code from EPUB file and generate library file")
- ARGS =
%w[EPUB_FILE]
- SELECTOR =
"code"
Instance Method Summary collapse
-
#initialize(config) ⇒ Detect
constructor
A new instance of Detect.
- #run(argv) ⇒ Object
Methods included from Subcommand
Constructor Details
#initialize(config) ⇒ Detect
Returns a new instance of Detect.
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 |
# File 'app/detect.rb', line 23 def initialize(config) super @library_path = nil @interactive = false @selector = SELECTOR @default_language = nil @available_lexers = Rouge::Lexer.all.sort_by(&:tag).each_with_object({}).with_index {|(lexer, lexers), index| lexers[(index + 1).to_s] = lexer } initial = nil @lexers_display = @available_lexers.collect {|(index, lexer)| init = lexer.title[0].upcase if initial == init option = "" else option = "\n" initial = init end option << "#{index})".bold << " " << lexer.title option << "(#{lexer.aliases.join(', ')})" unless lexer.aliases.empty? option }.join(" ") @commands = { "s" => _("skip"), "q" => _("quit"), "c" => _("show code"), "o" => _("show options") }.collect {|(key, command)| "#{key})".bold << " " << command }.join(" ") end |
Instance Method Details
#run(argv) ⇒ Object
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 |
# File 'app/detect.rb', line 57 def run(argv) argv epub_path = argv.shift raise ArgumentError, _('Specify EPUB file') unless epub_path begin # @todo Make this optional require 'epub/maker/ocf/physical_container/zipruby' EPUB::OCF::PhysicalContainer.adapter = :Zipruby rescue LoadError end epub = EPUB::Parser.parse(epub_path) $stderr.puts _("Detecting code from \"%{title}\"").encode(__ENCODING__) % {title: epub.title} codelist = {} library = Library.new library.["Release Identifier"] = epub.release_identifier library.["title"] = epub.title catch do |quit| EPUB::Searcher.search_element(epub, css: @selector).each do |result| item = result[:itemref].item if @interactive catch do |skip| show_item item show_code result[:element] show_commands i = ask while true case i when "s" throw skip when "q" throw quit when "c" show_item item show_code(result[:element]) show_commands i = ask when "o" show_commands i = ask else lexer = @available_lexers[i] unless lexer i = ask next end library.codelist[result[:location]] = {"language" => lexer.tag} break end end end else library.codelist[result[:location]] = ({ "language" => @default_language, "item" => result[:itemref].item.entry_name, "code" => result[:element].content }) end end path = library.save(@library_path) $stderr.puts _("Library file was saved to:") $stdout.puts path end end |