Class: Command::Convert
- Inherits:
-
CommandBase
- Object
- CommandBase
- Command::Convert
- Defined in:
- lib/command/convert.rb
Class Method Summary collapse
Instance Method Summary collapse
- #convert_novels(argv) ⇒ Object
-
#convert_txt(target) ⇒ Object
直接指定されたテキストファイルを変換する.
-
#convert_txt_to_ebook_file ⇒ Object
変換された整形済みテキストファイルをデバイスに対応した書籍データに変換する.
-
#copy_to_converted_file(src_path) ⇒ Object
convert.copy_to で指定されたディレクトリに書籍データをコピーする.
- #execute(argv) ⇒ Object
-
#initialize ⇒ Convert
constructor
A new instance of Convert.
Methods inherited from CommandBase
execute!, #force_change_settings_function, help, #hook_call, #load_local_settings, #tagname_to_ids
Constructor Details
#initialize ⇒ Convert
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 |
# File 'lib/command/convert.rb', line 18 def initialize super("<target> [<target2> ...] [options]") @opt.separator "\n \u30FB\u6307\u5B9A\u3057\u305F\u5C0F\u8AAC\u3092\u7E26\u66F8\u304D\u7528\u306B\u6574\u5F62\u53CA\u3073EPUB\u3001MOBI\u306B\u5909\u63DB\u3057\u307E\u3059\u3002\n \u30FB\u5909\u63DB\u3057\u305F\u3044\u5C0F\u8AAC\u306EN\u30B3\u30FC\u30C9\u3001URL\u3001\u30BF\u30A4\u30C8\u30EB\u3082\u3057\u304F\u306FID\u3092\u6307\u5B9A\u3057\u3066\u4E0B\u3055\u3044\u3002\nID\u306F \#{@opt.program_name} list \u3092\u53C2\u7167\u3057\u3066\u4E0B\u3055\u3044\u3002\n \u30FB\u4E00\u5EA6\u306B\u8907\u6570\u306E\u5C0F\u8AAC\u3092\u6307\u5B9A\u3059\u308B\u5834\u5408\u306F\u7A7A\u767D\u3067\u533A\u5207\u3063\u3066\u4E0B\u3055\u3044\u3002\n \u203B-o\u30AA\u30D7\u30B7\u30E7\u30F3\u304C\u306A\u3044\u5834\u5408\u3001[\u4F5C\u8005\u540D] \u5C0F\u8AAC\u540D.txt\u304C\u5C0F\u8AAC\u306E\u4FDD\u5B58\u30D5\u30A9\u30EB\u30C0\u306B\u51FA\u529B\u3055\u308C\u307E\u3059\n \u30FB\u7BA1\u7406\u5C0F\u8AAC\u4EE5\u5916\u306B\u3082\u30C6\u30AD\u30B9\u30C8\u30D5\u30A1\u30A4\u30EB\u3092\u5909\u63DB\u51FA\u6765\u307E\u3059\u3002\n\u30C6\u30AD\u30B9\u30C8\u30D5\u30A1\u30A4\u30EB\u306E\u30D5\u30A1\u30A4\u30EB\u30D1\u30B9\u3092\u6307\u5B9A\u3057\u307E\u3059\u3002\n \u203B\u8907\u6570\u6307\u5B9A\u3057\u305F\u5834\u5408\u306B-o\u30AA\u30D7\u30B7\u30E7\u30F3\u304C\u3042\u3063\u305F\u5834\u5408\u3001\u30D5\u30A1\u30A4\u30EB\u540D\u306B\u9023\u756A\u304C\u3064\u304D\u307E\u3059\u3002\n \u30FBMOBI\u5316\u3059\u308B\u5834\u5408\u306F narou setting device=kindle \u3092\u3057\u3066\u4E0B\u3055\u3044\u3002\n \u30FBdevice=kobo \u306E\u5834\u5408\u3001.kepub.epub \u3092\u51FA\u529B\u3057\u307E\u3059\u3002\n\n Examples:\nnarou convert n9669bk\nnarou convert http://ncode.syosetu.com/n9669bk/\nnarou convert \u7570\u4E16\u754C\u8FF7\u5BAE\u3067\u5974\u96B7\u30CF\u30FC\u30EC\u30E0\u3092\nnarou convert 1 -o \"\u30CF\u30FC\u30EC\u30E0 -\u5909\u63DB\u6E08\u307F-.txt\"\nnarou convert mynovel.txt --enc sjis\n\n Options:\n EOS\n @opt.on(\"-o FILE\", \"--output FILE\", \"\u51FA\u529B\u30D5\u30A1\u30A4\u30EB\u540D\u3092\u6307\u5B9A\u3059\u308B\u3002\u30D5\u30A9\u30EB\u30C0\u30D1\u30B9\u90E8\u5206\u306F\u7121\u8996\u3055\u308C\u308B\") { |filename|\n @options[\"output\"] = filename\n }\n @opt.on(\"-e ENCODING\", \"--enc ENCODING\",\n \"\u30C6\u30AD\u30B9\u30C8\u30D5\u30A1\u30A4\u30EB\u6307\u5B9A\u6642\u306E\u6587\u5B57\u30B3\u30FC\u30C9\u3092\u6307\u5B9A\u3059\u308B\u3002\u30C7\u30D5\u30A9\u30EB\u30C8\u306FUTF-8\") { |encoding|\n encoding = \"utf-8\" if encoding =~ /UTF8/i\n @options[\"encoding\"] = encoding\n }\n @opt.on(\"--no-epub\", \"AozoraEpub3\u3067EPUB\u5316\u3057\u306A\u3044\") {\n @options[\"no-epub\"] = true\n }\n @opt.on(\"--no-mobi\", \"kindlegen\u3067MOBI\u5316\u3057\u306A\u3044\") {\n @options[\"no-mobi\"] = true\n }\n @opt.on(\"--no-strip\", \"MOBI\u3092strip\u3057\u306A\u3044\") {\n @options[\"no-strip\"] = true\n }\n @opt.on(\"--no-zip\", \"i\u6587\u5EAB\u7528\u306Ezip\u30D5\u30A1\u30A4\u30EB\u3092\u4F5C\u3089\u306A\u3044\") {\n @options[\"no-zip\"] = true\n }\n @opt.on(\"--no-open\", \"\u51FA\u529B\u6642\u306B\u4FDD\u5B58\u30D5\u30A9\u30EB\u30C0\u3092\u958B\u304B\u306A\u3044\") {\n @options[\"no-open\"] = true\n }\n @opt.on(\"-i\", \"--inspect\", \"\u5C0F\u8AAC\u72B6\u614B\u306E\u8ABF\u67FB\u7D50\u679C\u3092\u8868\u793A\u3059\u308B\") {\n @options[\"inspect\"] = true\n }\n @opt.on(\"-v\", \"--verbose\", \"AozoraEpub3, kindlegen \u306E\u6A19\u6E96\u51FA\u529B\u3092\u5168\u3066\u8868\u793A\u3059\u308B\") {\n @options[\"verbose\"] = true\n }\n @opt.on(\"--ignore-force\", \"setting\u30B3\u30DE\u30F3\u30C9\u306Eforce\u7CFB\u8A2D\u5B9A\u3092\u7121\u8996\u3059\u308B\") {\n @options[\"ignore-force\"] = true\n }\n @opt.separator <<-EOS\n\n Configuration:\n--no-epub, --no-mobi, --no-strip, --no-open , --inspect \u306F narou setting \u30B3\u30DE\u30F3\u30C9\u3067\u6052\u5E38\u7684\u306A\u8A2D\u5B9A\u306B\u3059\u308B\u3053\u3068\u304C\u53EF\u80FD\u3067\u3059\u3002\nconvert.copy_to \u3092\u8A2D\u5B9A\u3059\u308C\u3070\u5909\u63DB\u3057\u305FEPUB/MOBI\u3092\u6307\u5B9A\u306E\u30D5\u30A9\u30EB\u30C0\u306B\u81EA\u52D5\u3067\u30B3\u30D4\u30FC\u51FA\u6765\u307E\u3059\u3002\ndevice \u3067\u8A2D\u5B9A\u3057\u305F\u7AEF\u672B\u304C\u63A5\u7D9A\u3055\u308C\u3066\u3044\u305F\u5834\u5408\u3001\u5BFE\u5FDC\u3059\u308B\u30C7\u30FC\u30BF\u3092\u81EA\u52D5\u9001\u4FE1\u3057\u307E\u3059\u3002\n\u8A73\u3057\u304F\u306F narou setting --help \u3092\u53C2\u7167\u3057\u3066\u4E0B\u3055\u3044\u3002\n EOS\nend\n" |
Class Method Details
.oneline_help ⇒ Object
14 15 16 |
# File 'lib/command/convert.rb', line 14 def self.oneline_help "小説を変換します。管理小説以外にテキストファイルも変換可能" end |
Instance Method Details
#convert_novels(argv) ⇒ Object
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 |
# File 'lib/command/convert.rb', line 110 def convert_novels(argv) tagname_to_ids(argv) argv.each.with_index(1) do |target, i| Helper.print_horizontal_rule if i > 1 if @basename @basename << " (#{i})" if argv.length > 1 @output_filename = @basename + @ext end if File.file?(target.to_s) @argument_target_type = :file res = convert_txt(target) else @argument_target_type = :novel unless Downloader.novel_exists?(target) error "#{target} は存在しません" next end res = NovelConverter.convert(target, { output_filename: @output_filename, display_inspector: ["inspect"], ignore_force: ["ignore-force"], }) @novel_data = Downloader.get_data_by_target(target) end next unless res @converted_txt_path = res[:converted_txt_path] @use_dakuten_font = res[:use_dakuten_font] ebook_file = hook_call(:convert_txt_to_ebook_file) next if ebook_file.nil? if ebook_file copied_file_path = copy_to_converted_file(ebook_file) if copied_file_path puts copied_file_path.encode(Encoding::UTF_8) + " へコピーしました" end if @device && @device.physical_support? && @device.connecting? && File.extname(ebook_file) == @device.ebook_file_ext if @argument_target_type == :novel Send.execute!([@device.name, target]) else puts @device.name + "へ送信しています" copy_to_path = @device.copy_to_documents(ebook_file) if copy_to_path puts copy_to_path.encode(Encoding::UTF_8) + " へコピーしました" else error "送信に失敗しました" end end end end unless ["no-open"] Helper.open_directory(File.dirname(@converted_txt_path), "小説の保存フォルダを開きますか") end end rescue Interrupt puts puts "変換を中断しました" exit 1 end |
#convert_txt(target) ⇒ Object
直接指定されたテキストファイルを変換する
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/command/convert.rb', line 175 def convert_txt(target) return NovelConverter.convert_file(target, { encoding: @enc, output_filename: @output_filename, display_inspector: ["inspect"], ignore_force: ["ignore-force"], }) rescue ArgumentError => e if e. =~ /invalid byte sequence in UTF-8/ error "テキストファイルの文字コードがUTF-8ではありません。" + "--enc オプションでテキストの文字コードを指定して下さい" warn "(#{e.message})" return nil else raise end rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError warn "#{target}:" error "テキストファイルの文字コードは#{@options["encoding"]}ではありませんでした。" + "正しい文字コードを指定して下さい" return nil end |
#convert_txt_to_ebook_file ⇒ Object
変換された整形済みテキストファイルをデバイスに対応した書籍データに変換する
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/command/convert.rb', line 201 def convert_txt_to_ebook_file return false if ["no-epub"] # epub status = NovelConverter.txt_to_epub(@converted_txt_path, @use_dakuten_font, nil, @device, ["verbose"]) return nil if status != :success if @device && @device.kobo? epub_ext = @device.ebook_file_ext else epub_ext = ".epub" end epub_path = @converted_txt_path.sub(/.txt$/, epub_ext) if !@device || !@device.kindle? || ["no-mobi"] puts File.basename(epub_path) + " を出力しました" puts "<bold><green>EPUBファイルを出力しました</green></bold>".termcolor return epub_path end # mobi status = NovelConverter.epub_to_mobi(epub_path, ["verbose"]) return nil if status != :success mobi_path = epub_path.sub(/\.epub$/, @device.ebook_file_ext) # strip unless ["no-strip"] puts "kindlestrip実行中" begin SectionStripper.strip(mobi_path, nil, false) rescue StripException => e error "#{e.message}" end end puts File.basename(mobi_path).encode(Encoding::UTF_8) + " を出力しました" puts "<bold><green>MOBIファイルを出力しました</green></bold>".termcolor return mobi_path end |
#copy_to_converted_file(src_path) ⇒ Object
convert.copy_to で指定されたディレクトリに書籍データをコピーする
242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/command/convert.rb', line 242 def copy_to_converted_file(src_path) copy_to_dir = ["copy_to"] return nil unless copy_to_dir if File.directory?(copy_to_dir) FileUtils.copy(src_path, copy_to_dir) return File.join(copy_to_dir, File.basename(src_path)) else error "#{copy_to_dir} はフォルダではないかすでに削除されています。コピー出来ませんでした" return nil end end |
#execute(argv) ⇒ Object
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 |
# File 'lib/command/convert.rb', line 84 def execute(argv) super if argv.empty? puts @opt.help return end @output_filename = ["output"] if @output_filename @ext = File.extname(@output_filename) @basename = File.basename(@output_filename, @ext) else @basename = nil end if ["encoding"] @enc = Encoding.find(["encoding"]) rescue nil unless @enc error "--enc で指定された文字コードは存在しません。sjis, eucjp, utf-8 等を指定して下さい" return end end @device = Narou.get_device self.extend(@device.get_hook_module) if @device hook_call(:change_settings) convert_novels(argv) end |