Method: NovelConverter#convert_main

Defined in:
lib/novelconverter.rb

#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 = ProgressBar.new(@toc["subtitles"].size)
    @toc["subtitles"].each_with_index do |subinfo, i|
      progressbar.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
    progressbar.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 = get_title_and_author_by_text(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