Class: NovelConverter
- Inherits:
-
Object
- Object
- NovelConverter
- Defined in:
- lib/novelconverter.rb
Constant Summary collapse
- NOVEL_TEXT_TEMPLATE_NAME =
"novel.txt"
- NOVEL_TEXT_TEMPLATE_NAME_FOR_IBUNKO =
"ibunko_novel.txt"
- @@site_settings =
Downloader.load_settings
Instance Attribute Summary collapse
-
#use_dakuten_font ⇒ Object
readonly
Returns the value of attribute use_dakuten_font.
Class Method Summary collapse
-
.convert(target, options = {}) ⇒ Object
指定の小説を整形・変換する.
-
.convert_file(filename, options = {}) ⇒ Object
テキストファイルを整形・変換する.
-
.epub_to_mobi(epub_path, verbose = false) ⇒ Object
EPUBファイルをkindlegenでMOBIへ AozoraEpub3.jar と同じ場所に kindlegen が無ければ何もしない.
-
.get_cover_filename(archive_path) ⇒ Object
表紙用の画像名取得.
- .stash_aozora_fonts_directory ⇒ Object
-
.txt_to_epub(filename, use_dakuten_font = false, dst_dir = nil, device = nil, verbose = false) ⇒ Object
AozoraEpub3でEPUBファイル作成.
- .visible_aozora_fonts_directory ⇒ Object
Instance Method Summary collapse
-
#convert_main(text = nil) ⇒ Object
変換処理メイン.
-
#create_cover_chuki ⇒ Object
表紙用挿絵注記作成.
- #create_novel_text_by_template(sections) ⇒ Object
- #find_site_setting ⇒ Object
-
#get_title_and_author_by_text(text) ⇒ Object
テキストデータ先頭二行からタイトルと作者名を取得.
-
#initialize(setting, output_filename = nil, display_inspector = false) ⇒ NovelConverter
constructor
A new instance of NovelConverter.
- #inspect_novel(text) ⇒ Object
-
#listup_midashi(data) ⇒ Object
見出しのリストを取得する.
- #load_novel_section(subtitle_info) ⇒ Object
- #midashi_save(text) ⇒ Object
-
#update_latest_convert_novel ⇒ Object
最近変換した小説IDを記録更新.
Constructor Details
#initialize(setting, output_filename = nil, display_inspector = false) ⇒ NovelConverter
Returns a new instance of NovelConverter.
248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/novelconverter.rb', line 248 def initialize(setting, output_filename = nil, display_inspector = false) @setting = setting @novel_id = setting.id @novel_author = setting. @novel_title = setting.title @output_filename = output_filename @inspector = Inspector.new(@setting) @illustration = Illustration.new(@setting, @inspector) @display_inspector = display_inspector @use_dakuten_font = false end |
Instance Attribute Details
#use_dakuten_font ⇒ Object (readonly)
Returns the value of attribute use_dakuten_font.
23 24 25 |
# File 'lib/novelconverter.rb', line 23 def use_dakuten_font @use_dakuten_font end |
Class Method Details
.convert(target, options = {}) ⇒ Object
指定の小説を整形・変換する
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/novelconverter.rb', line 32 def self.convert(target, = {}) = { # default paraeters output_filename: nil, display_inspector: false, ignore_force: false, }.merge() setting = NovelSetting.load(target, [:ignore_force]) if setting novel_converter = new(setting, [:output_filename], [:display_inspector]) return { converted_txt_path: novel_converter.convert_main, use_dakuten_font: novel_converter.use_dakuten_font } end nil end |
.convert_file(filename, options = {}) ⇒ Object
テキストファイルを整形・変換する
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/novelconverter.rb', line 51 def self.convert_file(filename, = {}) = { # default parameters encoding: nil, output_filename: nil, display_inspector: false, ignore_force: false, }.merge() output_filename = [:output_filename] if output_filename archive_path = File.dirname(output_filename) + "/" else archive_path = File.dirname(filename) + "/" end setting = NovelSetting.new(archive_path, [:ignore_force]) setting. = "" setting.title = File.basename(filename) novel_converter = new(setting, output_filename, [:display_inspector]) text = open(filename, "r:BOM|UTF-8") { |fp| fp.read }.gsub("\r", "") if [:encoding] text.force_encoding([:encoding]).encode!(Encoding::UTF_8) end { converted_txt_path: novel_converter.convert_main(text), use_dakuten_font: novel_converter.use_dakuten_font } end |
.epub_to_mobi(epub_path, verbose = false) ⇒ Object
EPUBファイルをkindlegenでMOBIへ AozoraEpub3.jar と同じ場所に kindlegen が無ければ何もしない
返り値:正常終了 :success、エラー終了 :error、中断終了 :abort、kindlegenがなかった nil
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 238 239 240 241 242 243 244 245 246 |
# File 'lib/novelconverter.rb', line 202 def self.epub_to_mobi(epub_path, verbose = false) kindlegen_path = File.join(File.dirname(Narou.get_aozoraepub3_path), "kindlegen") if Dir.glob(kindlegen_path + "*").empty? error "kindlegenが見つかりませんでした。AozoraEpub3と同じディレクトリにインストールして下さい" return :error end if Helper.os_cygwin? epub_path = Helper.convert_to_windows_path(epub_path) end command = %!"#{kindlegen_path}" -locale ja "#{epub_path}"! if Helper.os_windows? command.encode!(Encoding::Windows_31J) end print "kindlegen実行中" res = Helper::AsyncCommand.exec(command) do print "." end stdout_capture, _, proccess_status = res stdout_capture.force_encoding(Encoding::UTF_8) if verbose puts puts "==== kindlegen stdout capture " + "=" * 49 puts stdout_capture.gsub("\n\n", "\n").strip puts "=" * 79 end if proccess_status.exited? if proccess_status.exitstatus == 2 puts "" error "kindlegen実行中にエラーが発生したため、MOBIが出力出来ませんでした" if stdout_capture.scan(/(エラー\(.+?\):\w+?:.+)$/) error $1 end return :error end else puts "" error "kindlegenが中断させられたぽいのでMOBIは出力出来ませんでした" return :abort end puts "変換しました" :success end |
.get_cover_filename(archive_path) ⇒ Object
表紙用の画像名取得
300 301 302 303 304 305 306 307 308 309 |
# File 'lib/novelconverter.rb', line 300 def self.get_cover_filename(archive_path) [".jpg", ".png", ".jpeg"].each do |ext| filename = "cover#{ext}" cover_path = File.join(archive_path, filename) if File.exists?(cover_path) return filename end end nil end |
.stash_aozora_fonts_directory ⇒ Object
76 77 78 79 80 |
# File 'lib/novelconverter.rb', line 76 def self.stash_aozora_fonts_directory fonts_path = File.join(File.dirname(Narou.get_aozoraepub3_path), "template/OPS/fonts") return unless File.exists?(fonts_path) FileUtils.mv(fonts_path, fonts_path + "_hide") end |
.txt_to_epub(filename, use_dakuten_font = false, dst_dir = nil, device = nil, verbose = false) ⇒ Object
AozoraEpub3でEPUBファイル作成
AozoraEpub3は.jarがあるところがカレントディレクトリじゃないとうまく動かない MEMO: 逆にカレントディレクトリにAozoraEpub3の必須ファイルを置いて手を加えることで、 テンプレート等の差し替えが容易になる
返り値:正常終了 :success、エラー終了 :error、AozoraEpub3が見つからなかった nil
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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/novelconverter.rb', line 98 def self.txt_to_epub(filename, use_dakuten_font = false, dst_dir = nil, device = nil, verbose = false) abs_srcpath = File.(filename) src_dir = File.dirname(abs_srcpath) cover_option = "" # MEMO: 外部実行からだと -c FILENAME, -c 1 オプションはぬるぽが出て動かない cover_filename = get_cover_filename(src_dir) if cover_filename cover_option = "-c 0" # 先頭の挿絵を表紙として利用 end dst_option = "" if dst_dir dst_option = %!-dst "#{File.(dst_dir)}"! end ext_option = "" device_option = "" if device case device.name when "Kobo" ext_option = "-ext " + device.ebook_file_ext when "Kindle" device_option = "-device kindle" end end pwd = Dir.pwd aozoraepub3_path = Narou.get_aozoraepub3_path unless aozoraepub3_path error "AozoraEpub3が見つからなかったのでEPUBが出力出来ませんでした。" + "narou initでAozoraEpub3の設定を行なって下さい" return nil end aozoraepub3_basename = File.basename(aozoraepub3_path) aozoraepub3_dir = File.dirname(aozoraepub3_path) java_encoding = "-Dfile.encoding=UTF-8" if Helper.os_cygwin? abs_srcpath = Helper.convert_to_windows_path(abs_srcpath) end Dir.chdir(aozoraepub3_dir) command = %!java #{java_encoding} -cp #{aozoraepub3_basename} AozoraEpub3 -enc UTF-8 -of #{device_option} ! + %!#{cover_option} #{dst_option} #{ext_option} "#{abs_srcpath}"! if Helper.os_windows? command = "cmd /c " + command.encode(Encoding::Windows_31J) end stash_aozora_fonts_directory unless use_dakuten_font print "AozoraEpub3でEPUBに変換しています" begin res = Helper::AsyncCommand.exec(command) do print "." end ensure visible_aozora_fonts_directory unless use_dakuten_font Dir.chdir(pwd) end stdout_capture = res[0] # Javaの実行環境に由来するであろうエラー if stdout_capture =~ /Error occurred during initialization of VM/ puts warn stdout_capture.strip warn "-" * 70 error "Javaの実行エラーが発生しました。EPUBを作成出来ませんでした\n" \ "Hint: 複数のJava環境が混じっていると起きやすいエラーのようです" return :error end if verbose puts puts "==== AozoraEpub3 stdout capture " + "=" * 47 puts stdout_capture.strip puts "=" * 79 end error_list = stdout_capture.scan(/^(?:\[ERROR\]|エラーが発生しました :).+$/) warn_list = stdout_capture.scan(/^\[WARN\].+$/) info_list = stdout_capture.scan(/^\[INFO\].+$/) if !error_list.empty? || !warn_list.empty? || !info_list.empty? puts puts error_list, warn_list, info_list unless error_list.empty? # AozoraEpub3 のエラーにはEPUBが出力されないエラーとEPUBが出力されるエラーの2種類ある。 # EPUBが出力される場合は「変換完了」という文字があるのでそれを検出する if stdout_capture !~ /^変換完了/ error "AozoraEpub3実行中にエラーが発生したため、EPUBが出力出来ませんでした" return :error end end end puts "変換しました" :success end |
.visible_aozora_fonts_directory ⇒ Object
82 83 84 85 86 |
# File 'lib/novelconverter.rb', line 82 def self.visible_aozora_fonts_directory fonts_path = File.join(File.dirname(Narou.get_aozoraepub3_path), "template/OPS/fonts") return unless File.exists?(fonts_path + "_hide") FileUtils.mv(fonts_path + "_hide", fonts_path) end |
Instance Method Details
#convert_main(text = nil) ⇒ Object
変換処理メイン
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/novelconverter.rb', line 330 def convert_main(text = nil) print "ID:#{@novel_id} " if @novel_id puts "#{@novel_title} の変換を開始" sections = [] @cover_chuki = create_cover_chuki conv = load_converter(@novel_title, @setting.archive_path).new(@setting, @inspector, @illustration) if text result = conv.convert(text, "textfile") unless @setting.enable_enchant_midashi @inspector.info "テキストファイルの処理を実行しましたが、改行直後の見出し付与は有効になっていません。" + "setting.ini の enable_enchant_midashi を true にすることをお薦めします。" end splited = result.split("\n", 3) result = [splited[0], splited[1], @cover_chuki, splited[2]].join("\n") # 表紙の挿絵注記を3行目に挟み込む else @section_save_dir = Downloader.get_novel_section_save_dir(@setting.archive_path) @toc = Downloader.get_toc_data(@setting.archive_path) @toc["story"] = conv.convert(@toc["story"], "story") html = HTML.new site_setting = find_site_setting html.set_illust_setting({current_url: site_setting["illust_current_url"], grep_pattern: site_setting["illust_grep_pattern"]}) = ProgressBar.new(@toc["subtitles"].count) @toc["subtitles"].each_with_index do |subinfo, i| .output(i) section = load_novel_section(subinfo) if section["chapter"].length > 0 section["chapter"] = conv.convert(section["chapter"], "chapter") end @inspector.subtitle = section["subtitle"] element = section["element"] data_type = element.delete("data_type") || "text" element.each do |text_type, elm_text| if data_type == "html" html.string = elm_text elm_text = html.to_aozora end element[text_type] = conv.convert(elm_text, text_type) end section["subtitle"] = conv.convert(section["subtitle"], "subtitle") sections << section end .clear result = create_novel_text_by_template(sections) end @use_dakuten_font = conv.use_dakuten_font conv.replace_by_replace_txt(result) midashi_save(result) inspect_novel(result) if @output_filename save_path = File.join(@setting.archive_path, File.basename(@output_filename)) else if text info = (result) else info = { "author" => @novel_author, "title" => @novel_title } end save_filename = Narou.create_novel_filename(info) save_path = File.join(@setting.archive_path, save_filename) if save_path !~ /\.\w+$/ save_path += ".txt" end end File.write(save_path, result) puts "縦書用の変換が終了しました" update_latest_convert_novel save_path end |
#create_cover_chuki ⇒ Object
表紙用挿絵注記作成
314 315 316 317 318 319 320 321 |
# File 'lib/novelconverter.rb', line 314 def create_cover_chuki cover_filename = self.class.get_cover_filename(@setting.archive_path) if cover_filename "[#挿絵(#{cover_filename})入る]" else "" end end |
#create_novel_text_by_template(sections) ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/novelconverter.rb', line 266 def create_novel_text_by_template(sections) toc = @toc cover_chuki = @cover_chuki device = Narou.get_device setting = @setting processed_title = toc["title"] data = Database.instance.get_data("id", @novel_id) # タイトルに新着更新日を付加する if @setting.enable_add_date_to_title new_arrivals_date = data["new_arrivals_date"] || data["last_update"] date_str = new_arrivals_date.strftime(@setting.title_date_format) if @setting.title_date_align == "left" processed_title = date_str + processed_title else # right processed_title += date_str end end # タイトルに完結したかどうかを付加する flags = data["flags"] || {} = data["tags"] || [] flags["end"] ||= .include?("end") if flags["end"] processed_title += " (完結)" end # タイトルがルビ化されてしまうのを抑制 processed_title = processed_title.gsub("《", "※[#始め二重山括弧]") .gsub("》", "※[#終わり二重山括弧]") tempalte_name = (device && device.ibunko? ? NOVEL_TEXT_TEMPLATE_NAME_FOR_IBUNKO : NOVEL_TEXT_TEMPLATE_NAME) Template.get(tempalte_name, binding) end |
#find_site_setting ⇒ Object
323 324 325 |
# File 'lib/novelconverter.rb', line 323 def find_site_setting @@site_settings.find { |s| s.multi_match(@toc["toc_url"], "url") } end |
#get_title_and_author_by_text(text) ⇒ Object
テキストデータ先頭二行からタイトルと作者名を取得
408 409 410 411 |
# File 'lib/novelconverter.rb', line 408 def (text) title, = text.split("\n", 3) { "title" => title, "author" => } end |
#inspect_novel(text) ⇒ Object
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'lib/novelconverter.rb', line 419 def inspect_novel(text) # 行末読点の現在状況を調査する @inspector.inspect_end_touten_conditions(text) @inspector.countup_return_in_brackets(text) if !@display_inspector unless @inspector.empty? puts "小説状態の調査結果を #{Inspector::INSPECT_LOG_NAME} に出力しました" end else # 小説の監視・検査状況を表示する if @inspector.error? || @inspector.warning? puts "<bold><yellow>―――― 小説にエラーもしくは警告が存在します ――――</yellow></bold>".termcolor puts "" @inspector.display(Inspector::ERROR | Inspector::WARNING) puts "" end if @inspector.info? puts "<bold><yellow>―――― 小説の検査状況を表示します ――――</yellow></bold>".termcolor puts "" @inspector.display(Inspector::INFO) puts "" end end @inspector.save end |
#listup_midashi(data) ⇒ Object
見出しのリストを取得する
450 451 452 453 454 455 456 457 458 459 |
# File 'lib/novelconverter.rb', line 450 def listup_midashi(data) list = "" data.each_line.with_index(1) do |line, no| if line =~ /[#ここから(?<scale>[大中小])見出し](?<title>.+?)[#ここで\g<scale>見出し終わり]/ || line =~ /[#「(?<title>.+?)は(?<scale>[大中小])見出し]/ list << "#{$~[:scale]}:#{no}:#{$~[:title]}\n" end end list end |
#load_novel_section(subtitle_info) ⇒ Object
260 261 262 263 264 |
# File 'lib/novelconverter.rb', line 260 def load_novel_section(subtitle_info) file_subtitle = subtitle_info["file_subtitle"] || subtitle_info["subtitle"] # 互換性維持のため path = File.join(@section_save_dir, "#{subtitle_info["index"]} #{file_subtitle}.yaml") YAML.load_file(path) end |
#midashi_save(text) ⇒ Object
413 414 415 416 417 |
# File 'lib/novelconverter.rb', line 413 def midashi_save(text) midashi_list = listup_midashi(text) path = File.join(@setting.archive_path, "見出しリスト.txt") File.write(path, midashi_list) end |
#update_latest_convert_novel ⇒ Object
最近変換した小説IDを記録更新
464 465 466 467 468 469 470 |
# File 'lib/novelconverter.rb', line 464 def update_latest_convert_novel id = Downloader.get_id_by_target(@novel_title) Inventory.load("latest_convert", :local).tap { |inv| inv["id"] = id inv.save } end |