Class: ConverterBase
- Inherits:
-
Object
- Object
- ConverterBase
- Defined in:
- lib/converterbase.rb
Constant Summary collapse
- KANJI_NUM =
"〇一二三四五六七八九"
- KANJI_NUM_UNITS =
%w(万 億 兆 京).unshift("")
- KANJI_KURAI =
%w(十 百 千).unshift("")
- KANJI_NUM_UNITS_DIGIT =
{ "十" => 1, "百" => 2, "千" => 3, "万" => 4, "億" => 8, "兆" => 12, "京" => 16 }
- ROME_NUM_ALPHABET =
%w(II III IV VI VII VIII IX ii iii iv vi vii viii ix)
- ROME_NUM =
%w(Ⅱ Ⅲ Ⅳ Ⅵ Ⅶ Ⅷ Ⅸ ⅱ ⅲ ⅳ ⅵ ⅶ ⅷ ⅸ)
- HALF_INDENT_TARGET =
/^[ \t]*([「『((【〈《≪〝])/
- FULL_INDENT_TARGET =
/^[ \t]*(――)/
- AUTHOR_INTRODUCTION_SPLITTER =
前書き・後書きの検出及び処理 ==============================
/^ *[\**]{44}$/
- AUTHOR_POSTSCRIPT_SPLITTER =
/^ *[\**]{48}$/
- AUTHOR_COMMENT_CHUKI =
{ introduction: { open: "[#ここから前書き]", close: "[#ここで前書き終わり]" }, postscript: { open: "[#ここから後書き]", close: "[#ここで後書き終わり]" } }
- BRACKETS =
[%w(「 」), %w(『 』)
- OPENCLOSE_REGEXPS =
ネストに対応したかぎ括弧の正規表現
BRACKETS.map { |bracket| bo, bc = bracket /(?<oc>#{bo}[^#{bo+bc}]*(?:\g<oc>[^#{bo+bc}]*)*#{bc})/m }
- CHARACTER_OF_RUBY =
"一-龠A-Za-zA-Za-z"
Instance Attribute Summary collapse
-
#use_dakuten_font ⇒ Object
readonly
Returns the value of attribute use_dakuten_font.
Class Method Summary collapse
Instance Method Summary collapse
- #__calc_kanji_num_with_unit(string) ⇒ Object
- #after(io, text_type) ⇒ Object
- #after_convert(io) ⇒ Object
-
#alphabet_to_zenkaku(data, force = false) ⇒ Object
半角アルファベットを全角に変換する.
- #author_comment_force_close ⇒ Object
-
#auto_indent(data) ⇒ Object
行頭字下げ.
-
#auto_join_in_brackets(data) ⇒ Object
かぎ括弧内自動連結.
-
#auto_join_line(data) ⇒ Object
手動折り返しの自動連結.
- #before(io, text_type) ⇒ Object
- #before_convert(io) ⇒ Object
- #blank_line?(line) ⇒ Boolean
- #border_symbol?(line) ⇒ Boolean
-
#comments_block?(line) ⇒ Boolean
コメントブロックを検出する.
- #convert(text, text_type) ⇒ Object
-
#convert_dakuten_char_to_font(data) ⇒ Object
濁点のついてない文字に濁点をつける表現を対応.
-
#convert_for_all_data(data) ⇒ Object
小説データ全体に対して施す変換.
-
#convert_fraction_and_date(data) ⇒ Object
分数表記を○分の○表記に変更、及び日付表記を検出.
-
#convert_horizontal_ellipsis(data) ⇒ Object
中黒(・)を並べて三点リーダーもどきにしているのを三点リーダーに変換.
-
#convert_kanji_num_with_unit(data, lower_digit_zero = 0) ⇒ Object
漢数字を単位を使った表現に変換.
-
#convert_main(io) ⇒ Object
変換処理本体.
-
#convert_novel_rule(data) ⇒ Object
小説のルールに沿うように変換.
-
#convert_numbers(data) ⇒ Object
数字の変換.
-
#convert_rome_numeric(data) ⇒ Object
ローマ数字っぽいアルファベットをローマ数字に変換.
-
#convert_special_characters(data) ⇒ Object
特定の表現・記号を変換していく.
-
#convert_tatechuyoko(data) ⇒ Object
縦中横にすべき表現を変換.
- #delay_outputs(data = "") ⇒ Object
-
#enchant_midashi(data) ⇒ Object
[#改ページ]直後の行を見出しに設定する.
-
#erase_comments_block(data) ⇒ Object
コメントブロックを削除する.
-
#erase_introduction(data) ⇒ Object
前書きを削除する.
-
#erase_postscript(data) ⇒ Object
後書きを削除する.
-
#exception_reconvert_kanji_to_num(data) ⇒ Object
アラビア数字を使うべきところはアラビア数字に戻す.
-
#find_introduction? ⇒ Boolean
前書きの検出.
-
#force_indent_special_chapter(line) ⇒ Object
章見出しっぽい文字列を字下げして前後に空行を入れる.
-
#half_indent_bracket(data) ⇒ Object
行頭かぎ括弧(等)に二分アキを追加する.
-
#hankaku_num_to_zenkaku_num(data) ⇒ Object
3桁以上の半角数字を全角アラビア数字に.
- #inclusion_author_comment_block?(line) ⇒ Boolean
-
#initialize(setting, inspector, illustration) ⇒ ConverterBase
constructor
A new instance of ConverterBase.
- #initialize_member_values ⇒ Object
-
#insert_blank_line_to_border_symbol(line) ⇒ Object
■などの区切りの前後には空行が必ず存在するようにする.
-
#insert_separate_space(data) ⇒ Object
特定の記号の直後は全角アキを挿入する.
-
#jisage(line, num) ⇒ Object
行頭空白を考慮した字下げ.
-
#join_inner_bracket(str) ⇒ Object
改行を連結した文章を作る.
- #kanji_num_to_integer(string) ⇒ Object
- #leave_author_comment_block?(line) ⇒ Boolean
- #midashi(str) ⇒ Object
-
#narou_ruby(data) ⇒ Object
小説家になろうのルビ対策.
-
#num_to_kanji(data) ⇒ Object
アラビア数字を漢数字に.
- #object_of_ruby?(char) ⇒ Boolean
- #outputs(data = "", force = false) ⇒ Object
-
#page_break?(line) ⇒ Boolean
改ページある?.
- #process_author_comment(line) ⇒ Object
-
#rebuild_english_sentences(data) ⇒ Object
英文を再構成する.
- #rebuild_illust(data) ⇒ Object
- #rebuild_kanji_num(data) ⇒ Object
- #rebuild_url(data) ⇒ Object
-
#replace_by_replace_txt(text) ⇒ Object
replace.txt により単純置換.
-
#replace_illust_tag(data) ⇒ Object
なろうの挿絵タグを挿絵注釈に変換 挿絵画像が存在しなければダウンロードして保存する.
-
#replace_narou_tag(data) ⇒ Object
小説家になろう専用タグを置換.
- #replace_tatesen(str) ⇒ Object
-
#replace_url(data) ⇒ Object
URL っぽい文字列を一旦別のIDに置き換えてあとで復元することで、変換処理の影響を受けさせない.
-
#rstrip_all_lines(data) ⇒ Object
すべての行の行末空白を削除.
- #sesame(str, ten) ⇒ Object
- #stash_kanji_num(data) ⇒ Object
-
#symbols_to_zenkaku(data) ⇒ Object
記号を全角に変換.
-
#tcy(str) ⇒ Object
縦中横注記取得.
- #to_ruby(match, m1, m2, openclose_symbols) ⇒ Object
-
#zenkaku_num_to_hankaku_num(num) ⇒ Object
全角数字を半角アラビア数字に.
-
#zenkaku_num_to_kanji(str) ⇒ Object
全角アラビア数字を漢数字に.
-
#zenkaku_rstrip(line) ⇒ Object
全角版 String#rstrip!.
Constructor Details
#initialize(setting, inspector, illustration) ⇒ ConverterBase
Returns a new instance of ConverterBase.
27 28 29 30 31 32 33 |
# File 'lib/converterbase.rb', line 27 def initialize(setting, inspector, illustration) @setting = setting @inspector = inspector @illustration = illustration @use_dakuten_font = false initialize_member_values end |
Instance Attribute Details
#use_dakuten_font ⇒ Object (readonly)
Returns the value of attribute use_dakuten_font.
14 15 16 |
# File 'lib/converterbase.rb', line 14 def use_dakuten_font @use_dakuten_font end |
Class Method Details
.rebuild_brackets(data, stack) ⇒ Object
685 686 687 688 689 |
# File 'lib/converterbase.rb', line 685 def self.rebuild_brackets(data, stack) data.gsub(/[#かぎ括弧=(\d+)]/) do stack[$1.to_i] end end |
Instance Method Details
#__calc_kanji_num_with_unit(string) ⇒ Object
128 129 130 131 132 133 134 135 136 |
# File 'lib/converterbase.rb', line 128 def __calc_kanji_num_with_unit(string) total = 0 string.scan(/([#{KANJI_NUM}]*)([十百千]*)/) do |num, units| break if num + units == "" num = "1" if num.empty? total += (num.tr(KANJI_NUM, "0-9") + units.each_char.map { |c| "0" * KANJI_NUM_UNITS_DIGIT[c] }.join).to_i end total end |
#after(io, text_type) ⇒ Object
23 24 25 |
# File 'lib/converterbase.rb', line 23 def after(io, text_type) io end |
#after_convert(io) ⇒ Object
912 913 914 |
# File 'lib/converterbase.rb', line 912 def after_convert(io) after(io, @text_type) end |
#alphabet_to_zenkaku(data, force = false) ⇒ Object
半角アルファベットを全角に変換する
force : 強制的に全アルファベットを全角にするか?
false の場合、英文章(半角スペースで区切られた2単語以上)を半角のままにする
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/converterbase.rb', line 392 def alphabet_to_zenkaku(data, force = false) if force data.gsub!(/[a-zA-Z]+/) do |match| match.tr("a-zA-Z", "a-zA-Z") end else data.gsub!(/[\w.,!?' ]+/) do |match| if match.split(" ").count > 1 @english_sentences << match "[#英文=#{@english_sentences.count - 1}]" else match.tr("a-zA-Z", "a-zA-Z") end end end end |
#author_comment_force_close ⇒ Object
627 628 629 630 631 |
# File 'lib/converterbase.rb', line 627 def if @in_author_comment_block outputs(AUTHOR_COMMENT_CHUKI[@in_author_comment_block][:close]) end end |
#auto_indent(data) ⇒ Object
行頭字下げ
必ず下げなければいけないところは強制的に字下げ 他の部分は全体的に判断して字下げ
473 474 475 476 477 478 479 480 |
# File 'lib/converterbase.rb', line 473 def auto_indent(data) data.gsub!(FULL_INDENT_TARGET, " \\1") if @setting.enable_auto_indent && @inspector.inspect_indent(data) data.gsub!(/^([^#{Inspector::IGNORE_INDENT_CHAR}])/) do $1 == " " ? " " : " #{$1}" end end end |
#auto_join_in_brackets(data) ⇒ Object
かぎ括弧内自動連結
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 |
# File 'lib/converterbase.rb', line 661 def auto_join_in_brackets(data) if !@setting.enable_auto_join_in_brackets && !@setting.enable_inspect_invalid_openclose_brackets return end OPENCLOSE_REGEXPS.each_with_index do |openclose, i| stack = {} data.gsub!(openclose).with_index do |match, j| joined_str = join_inner_bracket(match) if @setting.enable_auto_join_in_brackets && joined_str error = @inspector.validate_joined_inner_brackets(match, joined_str, BRACKETS[i]) stack[j] = error ? match : joined_str else stack[j] = match end "[#かぎ括弧=#{j}]" end if @setting.enable_inspect_invalid_openclose_brackets # 正しく閉じてないかぎ括弧だけが data に残ってる @inspector.inspect_invalid_openclose_brackets(data, BRACKETS[i], stack) end data.replace(ConverterBase.rebuild_brackets(data, stack)) end end |
#auto_join_line(data) ⇒ Object
手動折り返しの自動連結
694 695 696 697 698 699 700 |
# File 'lib/converterbase.rb', line 694 def auto_join_line(data) # 次の行の冒頭が開き記号だったら意図的な改行だと判断して連結しない data.gsub!(/、\n ([^「『((【<<〈《≪])/, "、\\1") # TODO: 文章の途中で不自然に改行しているところを連結 # 不自然な改行の検出方法が不明 #data.gsub!(/[^。…―-\-」』))>>]\]〉》】〕}\}0-90-9]/ end |
#before(io, text_type) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/converterbase.rb', line 16 def before(io, text_type) data = io.string data.gsub!("\n\n", "\n") data.gsub!("\n\n\n", "\n\n") io end |
#before_convert(io) ⇒ Object
908 909 910 |
# File 'lib/converterbase.rb', line 908 def before_convert(io) before(io, @text_type) end |
#blank_line?(line) ⇒ Boolean
512 513 514 |
# File 'lib/converterbase.rb', line 512 def blank_line?(line) line =~ /\A[ \t]*$/ end |
#border_symbol?(line) ⇒ Boolean
505 506 507 508 509 510 |
# File 'lib/converterbase.rb', line 505 def border_symbol?(line) @@symbols ||= open(File.join(Narou.get_preset_dir, "bordersymbols.txt"), "r:BOM|UTF-8") { |fp| fp.read.strip } line =~ /^[ \t]*[#{@@symbols}]+$/ end |
#comments_block?(line) ⇒ Boolean
コメントブロックを検出する
コメントブロックの定義は - のみが50回以上連続された行に囲まれている間
423 424 425 426 427 428 429 |
# File 'lib/converterbase.rb', line 423 def comments_block?(line) if line =~ /^-{50,}$/ @in_comment_block ^= 1 return true end @in_comment_block end |
#convert(text, text_type) ⇒ Object
916 917 918 919 920 921 922 923 924 |
# File 'lib/converterbase.rb', line 916 def convert(text, text_type) return "" if text == "" @text_type = text_type io = StringIO.new(rstrip_all_lines(text)) (io = before_convert(io)).rewind (io = convert_main(io)).rewind (io = after_convert(io)).rewind return io.read end |
#convert_dakuten_char_to_font(data) ⇒ Object
濁点のついてない文字に濁点をつける表現を対応
濁点つきフォントに部分的に切り替える
350 351 352 353 354 355 356 357 358 359 |
# File 'lib/converterbase.rb', line 350 def convert_dakuten_char_to_font(data) data.gsub!(/(.)[゛゙]/) do if $1 =~ /[ぁ-んァ-ヶι]/ @use_dakuten_font = true "[#濁点]#{$1}[#濁点終わり]" else tcy($1 + "゛") end end end |
#convert_for_all_data(data) ⇒ Object
小説データ全体に対して施す変換
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 |
# File 'lib/converterbase.rb', line 884 def convert_for_all_data(data) auto_join_in_brackets(data) auto_join_line(data) if @setting.enable_auto_join_line erase_comments_block(data) replace_illust_tag(data) replace_url(data) replace_narou_tag(data) convert_rome_numeric(data) alphabet_to_zenkaku(data, @setting.enable_alphabet_force_zenkaku) convert_numbers(data) exception_reconvert_kanji_to_num(data) if @setting.enable_kanji_num_with_units convert_kanji_num_with_unit(data, @setting.kanji_num_with_units_lower_digit_zero) end insert_separate_space(data) convert_special_characters(data) convert_fraction_and_date(data) if @text_type == "body" || @text_type == "textfile" half_indent_bracket(data) auto_indent(data) end convert_dakuten_char_to_font(data) end |
#convert_fraction_and_date(data) ⇒ Object
分数表記を○分の○表記に変更、及び日付表記を検出
スラッシュで区切られた数字が2個なら分数、3個なら日付と定義
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 |
# File 'lib/converterbase.rb', line 203 def convert_fraction_and_date(data) if !@setting.enable_transform_fraction && !@setting.enable_transform_date return end target_num = "\d0-9#{KANJI_NUM}十百千万億兆京垓" data.gsub!(/[#{target_num}\//]+/) do |match| numerics = match.split(/[\//]/) case numerics.count when 2 # 分数 if @setting.enable_transform_fraction "#{zenkaku_num_to_kanji(numerics[1])}分の#{zenkaku_num_to_kanji(numerics[0])}" else match end when 3 # 日付 if @setting.enable_transform_date begin date = Date.new(*numerics.map { |s| s.tr!("0-90-9#{KANJI_NUM}", "0-90-90-9") s.to_i }) rescue ArgumentError match else convert_numbers(date.strftime(@setting.date_format)) end end else match end end end |
#convert_horizontal_ellipsis(data) ⇒ Object
中黒(・)を並べて三点リーダーもどきにしているのを三点リーダーに変換
813 814 815 816 817 818 819 820 821 822 823 |
# File 'lib/converterbase.rb', line 813 def convert_horizontal_ellipsis(data) return if !@setting.enable_convert_horizontal_ellipsis || @text_type == "subtitle" data.gsub!(/・{3,}/) do |match| pre_char, post_char = $`[-1], $'[0] if pre_char == "―" || post_char == "―" match else "…" * ((match.length / 3.0 / 2).ceil * 2) end end end |
#convert_kanji_num_with_unit(data, lower_digit_zero = 0) ⇒ Object
漢数字を単位を使った表現に変換
800万1000 といったような表現は、内部一度で 8001000 に変換する。 lower_digit_zero はこの最後の 000 に適用される
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 |
# File 'lib/converterbase.rb', line 152 def convert_kanji_num_with_unit(data, lower_digit_zero = 0) #data.gsub!(/([#{KANJI_NUM}]+)([十百千万億兆京]?)/) do |match| data.gsub!(/([#{KANJI_NUM}十百千万億兆京]+)/) do |match| total = kanji_num_to_integer($1) m1 = total.to_s.tr("0-9", KANJI_NUM) if m1 =~ /〇{#{lower_digit_zero},}$/ digits = m1.reverse.scan(/.{1,4}/).map(&:reverse).reverse # 下の桁から4桁ずつ区切った配列を作成 keta = digits.count - 1 digits.map.with_index { |nums, keta_i| four_digit_num = nums.scan(/./).map.with_index { |d, di| next "" if d == "〇" kurai = KANJI_KURAI[nums.length - di - 1] if d == "一" # 4桁の千の前は一は必須ではなく、5桁以上の場合の千の前には一をつける # 1100 → 千百、11100 → 一万一千百 if kurai != "" && !(keta > 0 && kurai == "千") d = "" end end d + kurai }.join if four_digit_num.length > 0 four_digit_num + KANJI_NUM_UNITS[keta - keta_i] else "" end }.join else match end end end |
#convert_main(io) ⇒ Object
変換処理本体
@text_type: 渡されるテキストの種類。subtitle, introduction, body, postscript, textfile のどれか
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 |
# File 'lib/converterbase.rb', line 931 def convert_main(io) @write_fp = StringIO.new case @text_type when "introduction" return @write_fp if @setting.enable_erase_introduction when "postscript" return @write_fp if @setting.enable_erase_postscript end if @text_type == "textfile" @write_fp.puts(io.gets + io.gets) # タイトル・著者名スキップ data = io.read = ProgressBar.new(data.count("\n") + 1) .output(0) else data = io.read end initialize_member_values convert_for_all_data(data) @read_fp = StringIO.new(data) @read_fp.each_with_index do |line, i| .output(i) if @text_type == "textfile" @request_skip_output_line = false zenkaku_rstrip(line) if @request_insert_blank_next_line outputs unless blank_line?(line) @request_insert_blank_next_line = false end (line) if @text_type == "textfile" insert_blank_line_to_border_symbol(line) force_indent_special_chapter(line) outputs(line) unless @delay_outputs_buffer.empty? @write_fp.write(@delay_outputs_buffer) @before_line = @delay_outputs_buffer @delay_outputs_buffer = "" else @before_line = line end end if @text_type == "textfile" @write_fp.rewind data = @write_fp.string if @text_type == "textfile" if @setting. erase_introduction(data) if @setting.enable_erase_introduction erase_postscript(data) if @setting.enable_erase_postscript end if @setting.enable_enchant_midashi enchant_midashi(data) end end rebuild_illust(data) rebuild_url(data) rebuild_english_sentences(data) rebuild_kanji_num(data) # 再構築された文章にルビがふられる可能性を考慮して、 # この位置でルビの処理を行う narou_ruby(data) if @setting.enable_ruby # 三点リーダーの変換は、ルビで圏点として・・・を使っている場合を考慮して、ルビ処理後にする convert_horizontal_ellipsis(data) data.strip! .clear if @text_type == "textfile" @write_fp end |
#convert_novel_rule(data) ⇒ Object
小説のルールに沿うように変換
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/converterbase.rb', line 364 def convert_novel_rule(data) # 括弧の閉じの直前の句点を消す data.gsub!(/。([」』)])/, "\\1") # 原則偶数個を1セットで使うべき文字を偶数個に補正 # MEMO:(―も偶数個セットにするべきだが、記号的な意味で使われる場合もあるので無視) %w(…‥).each do |target| data.gsub!(/#{target}+/) do |match| len = match.length len += 1 if len.odd? target * len end end # たまに見かける誤字対策 # MEMO: 。。。 検索結果:355作品 # 、、、 検索結果:171作品 # 上記の三点リーダー的使い方は考慮しない data.gsub!(/。+/, "。") data.gsub!(/、+/, "、") data.gsub!(/。 /, "。") data.gsub!(/([?!])。/, "\\1") end |
#convert_numbers(data) ⇒ Object
数字の変換
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/converterbase.rb', line 70 def convert_numbers(data) # 小数点を・に data.gsub!(/([\d0-9#{KANJI_NUM}]+?)[\..]([\d0-9#{KANJI_NUM}]+?)/, "\\1・\\2") if @setting.enable_convert_num_to_kanji num_to_kanji(data) else hankaku_num_to_zenkaku_num(data) end data end |
#convert_rome_numeric(data) ⇒ Object
ローマ数字っぽいアルファベットをローマ数字に変換
※alphabet_to_zenkaku の前に実行する必要あり
269 270 271 272 273 |
# File 'lib/converterbase.rb', line 269 def convert_rome_numeric(data) ROME_NUM_ALPHABET.each_with_index do |rome, i| data.gsub!(/([^a-zA-Z])#{rome}([^a-zA-Z])/, "\\1#{ROME_NUM[i]}\\2") end end |
#convert_special_characters(data) ⇒ Object
特定の表現・記号を変換していく
278 279 280 281 282 |
# File 'lib/converterbase.rb', line 278 def convert_special_characters(data) symbols_to_zenkaku(data) convert_tatechuyoko(data) convert_novel_rule(data) end |
#convert_tatechuyoko(data) ⇒ Object
縦中横にすべき表現を変換
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/converterbase.rb', line 308 def convert_tatechuyoko(data) # 感嘆符及び疑問符の縦中横化 # AozoraEPUB3の縦中横設定を使えば明示的に注記を使う必要はないが、 # 見出しの中では自動で縦中横にはならないため、明示的指定をしておく # 事前に !? は全角にしておく data.gsub!(/!+/) do |match| if "#{$`[-1]}#{$'[0]}".include?("?") next match end len = match.length if len == 3 tcy("!!!") elsif len >= 4 # 4個以上なら偶数になるように調整(奇数だった場合増やす方向(+1))して2個ずつ縦中横 len += 1 if len.odd? tcy("!!") * (len / 2) else match end end data.gsub!(/[!?]+/) do |match| case match.length when 2 tcy(match.tr("!?", "!?")) when 3 # 見た目的にこのパターンだけ縦中横化を許容する if %w(!!? ?!!).find { |v| v == match } tcy(match.tr("!?", "!?")) else match end else match end end end |
#delay_outputs(data = "") ⇒ Object
54 55 56 57 58 |
# File 'lib/converterbase.rb', line 54 def delay_outputs(data = "") unless @request_skip_output_line @delay_outputs_buffer << data + "\n" end end |
#enchant_midashi(data) ⇒ Object
[#改ページ]直後の行を見出しに設定する
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 |
# File 'lib/converterbase.rb', line 828 def enchant_midashi(data) def midashi(str) midashi_title = str.gsub("[#半字下げ]", "").gsub(/^[ \s]+/, "").gsub(/[ \s]+$/, "") @inspector.subtitle = midashi_title "[#3字下げ][#ここから中見出し]#{midashi_title}[#ここで中見出し終わり]" end data.gsub!(/[#改ページ]\n(.+?)\n/) do |match| m1 = $1 rest = $' if $1 =~ /#{AUTHOR_COMMENT_CHUKI[:introduction][:open]}/ match else # 見出しの次の行が空行ではない場合空行を追加する add_tail = rest =~ /\A$/ ? "" : "\n\n" "[#改ページ]\n#{midashi(m1)}\n#{add_tail}" end end data.gsub!(/([#改ページ]\n)(#{AUTHOR_COMMENT_CHUKI[:introduction][:open]}.+?#{AUTHOR_COMMENT_CHUKI[:introduction][:close]}\n)(.+?\n)/m) do m1, m2, m3 = $1, $2, $3 add_tail = $' =~ /\A$/ ? "" : "\n" "#{m1 + midashi(m3) + m2}#{add_tail}" # 前書き→見出しの順番を見出し→前書きに入れ替えて置換 end end |
#erase_comments_block(data) ⇒ Object
コメントブロックを削除する
434 435 436 |
# File 'lib/converterbase.rb', line 434 def erase_comments_block(data) data.gsub!(/^-{50,}\n.*^-{50,}\n/m, "") end |
#erase_introduction(data) ⇒ Object
前書きを削除する
856 857 858 859 860 861 862 863 864 865 |
# File 'lib/converterbase.rb', line 856 def erase_introduction(data) del_count = 0 data.gsub!(/([#改ページ])\n#{AUTHOR_COMMENT_CHUKI[:introduction][:open]}.+?#{AUTHOR_COMMENT_CHUKI[:introduction][:close]}/m) do del_count += 1 $1 end if del_count > 0 @inspector.info("前書きをすべて削除しました。削除した数は#{del_count}個です。") end end |
#erase_postscript(data) ⇒ Object
後書きを削除する
870 871 872 873 874 875 876 877 878 879 |
# File 'lib/converterbase.rb', line 870 def erase_postscript(data) del_count = 0 data.gsub!(/#{AUTHOR_COMMENT_CHUKI[:postscript][:open]}.+?#{AUTHOR_COMMENT_CHUKI[:postscript][:close]}\n([#改ページ]|\z)/m) do del_count += 1 $1 end if del_count > 0 @inspector.info("後書きをすべて削除しました。削除した数は#{del_count}個です。") end end |
#exception_reconvert_kanji_to_num(data) ⇒ Object
アラビア数字を使うべきところはアラビア数字に戻す
188 189 190 191 192 193 194 195 196 |
# File 'lib/converterbase.rb', line 188 def exception_reconvert_kanji_to_num(data) return unless @setting.enable_convert_num_to_kanji data.gsub!(/([A-Za-z])([#{KANJI_NUM}・]+)/) do # ver1・01 のようなパターンも許容する $1 + $2.tr(KANJI_NUM, "0-9") end data.gsub!(/([#{KANJI_NUM}]+)([A-Za-z])/) do $1.tr(KANJI_NUM, "0-9") + $2 end end |
#find_introduction? ⇒ Boolean
前書きの検出
584 585 586 587 588 589 590 591 592 593 594 595 596 |
# File 'lib/converterbase.rb', line 584 def find_introduction? pos = @read_fp.pos result = false @read_fp.each do |line| break if page_break?(line) if line =~ AUTHOR_INTRODUCTION_SPLITTER result = true break end end @read_fp.pos = pos result end |
#force_indent_special_chapter(line) ⇒ Object
章見出しっぽい文字列を字下げして前後に空行を入れる
485 486 487 488 489 490 491 492 493 494 495 496 |
# File 'lib/converterbase.rb', line 485 def force_indent_special_chapter(line) line.sub!(/^[ \t]*([-―<<〈-])([0-90-9#{KANJI_NUM}]+)([-―>>〉-])$/) do @request_insert_blank_next_line = true top, chapter, bottom = $1, $2, $3 if "―--".include?(top) top = "― " bottom = " ―" end (blank_line?(@before_line) ? "" : "\n") + " [#ゴシック体]" + \ top + zenkaku_num_to_hankaku_num(chapter) + bottom + "[#ゴシック体終わり]" end end |
#half_indent_bracket(data) ⇒ Object
行頭かぎ括弧(等)に二分アキを追加する
「や(などの前にカスタム注記([#二分アキ])を追加し、半文字分字下げする(二分アキ)。 kindle paperwhite で鍵括弧のインデントがおかしいことへの対応
463 464 465 |
# File 'lib/converterbase.rb', line 463 def half_indent_bracket(data) data.gsub!(HALF_INDENT_TARGET, "[#二分アキ]\\1") if @setting.enable_half_indent_bracket end |
#hankaku_num_to_zenkaku_num(data) ⇒ Object
3桁以上の半角数字を全角アラビア数字に
448 449 450 451 452 453 |
# File 'lib/converterbase.rb', line 448 def hankaku_num_to_zenkaku_num(data) data.gsub!(/\d{3,}/) do |num| num.tr("0-9", "0-9") end data end |
#inclusion_author_comment_block?(line) ⇒ Boolean
598 599 600 601 602 603 604 605 606 607 608 609 610 |
# File 'lib/converterbase.rb', line 598 def (line) result = false if page_break?(line) if find_introduction? @in_author_comment_block = :introduction result = true end elsif line =~ AUTHOR_POSTSCRIPT_SPLITTER @in_author_comment_block = :postscript result = true end result end |
#initialize_member_values ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/converterbase.rb', line 35 def initialize_member_values @request_insert_blank_next_line = false @request_skip_output_line = false @before_line = "" @delay_outputs_buffer = "" @in_comment_block = false @english_sentences = [] @url_list = [] @illust_chuki_list = [] @kanji_num_list = {} @in_author_comment_block = nil end |
#insert_blank_line_to_border_symbol(line) ⇒ Object
■などの区切りの前後には空行が必ず存在するようにする
519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/converterbase.rb', line 519 def insert_blank_line_to_border_symbol(line) result = "" if border_symbol?(line) unless blank_line?(@before_line) result << "\n" end @request_insert_blank_next_line = true jisage(line, 4) end line.sub!(/\A/, result) end |
#insert_separate_space(data) ⇒ Object
特定の記号の直後は全角アキを挿入する
241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/converterbase.rb', line 241 def insert_separate_space(data) data.gsub!(/([!?!?]+)([^!?!?])/) do m1, m2 = $1, $2 m2 = " " if m2 == " " if m2 =~ /[^」]』】〉》〕>>≫))"”’〟 ☆★♪[―]/ "#{m1} #{m2}" else "#{m1}#{m2}" end end end |
#jisage(line, num) ⇒ Object
行頭空白を考慮した字下げ
501 502 503 |
# File 'lib/converterbase.rb', line 501 def jisage(line, num) line.sub!(/^[ \t]*/, " " * num) end |
#join_inner_bracket(str) ⇒ Object
改行を連結した文章を作る
改行がひとつもなかった場合は nil を返す
648 649 650 651 652 653 654 655 656 |
# File 'lib/converterbase.rb', line 648 def join_inner_bracket(str) joined_str = str.dup return nil if str.count("\n") == 0 joined_str.gsub!(/([…―])\n/, "\\1。\n") joined_str = joined_str.split("\n").map { |s| s.sub(/^ +/, "") }.join joined_str end |
#kanji_num_to_integer(string) ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/converterbase.rb', line 138 def kanji_num_to_integer(string) total = 0 string.scan(/([#{KANJI_NUM}十百千]+)([万億兆京]*)/) do |num, units| total += (__calc_kanji_num_with_unit(num).to_s + units.each_char.map { |c| "0" * KANJI_NUM_UNITS_DIGIT[c] }.join).to_i end total end |
#leave_author_comment_block?(line) ⇒ Boolean
612 613 614 615 616 617 618 619 620 621 622 623 624 625 |
# File 'lib/converterbase.rb', line 612 def (line) result = false case @in_author_comment_block when :introduction if line =~ AUTHOR_INTRODUCTION_SPLITTER result = true end when :postscript if page_break?(line) result = true end end result end |
#midashi(str) ⇒ Object
829 830 831 832 833 |
# File 'lib/converterbase.rb', line 829 def midashi(str) midashi_title = str.gsub("[#半字下げ]", "").gsub(/^[ \s]+/, "").gsub(/[ \s]+$/, "") @inspector.subtitle = midashi_title "[#3字下げ][#ここから中見出し]#{midashi_title}[#ここで中見出し終わり]" end |
#narou_ruby(data) ⇒ Object
小説家になろうのルビ対策
705 706 707 708 709 710 711 712 713 714 715 716 717 718 |
# File 'lib/converterbase.rb', line 705 def narou_ruby(data) # 《》なルビの対処 data.gsub!(/(.+?)≪(.+?)≫/) do |match| to_ruby(match, $1, $2, ["≪", "≫"]) end # ()なルビの対処 if @text_type != "subtitle" data.gsub!(/(.+?)(([ぁ-んァ-ヶーゝゞ・A-Za-zA-Za-z ]{,20}))/) do |match| to_ruby(match, $1, $2, ["(", ")"]) end end data.replace(replace_tatesen(data)) data.gsub!("[#ルビ用縦線]", "|") end |
#num_to_kanji(data) ⇒ Object
アラビア数字を漢数字に
カンマ区切りの数字はアラビア数字のままにしておく もともと漢数字なのは他の変換を受けないように退避させておく
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/converterbase.rb', line 87 def num_to_kanji(data) stash_kanji_num(data) data.gsub!(/[\d0-9,,]+/) do |match| if match =~ /[,,]/ match else zenkaku_num_to_kanji(match.tr("0-9", KANJI_NUM)) end end data end |
#object_of_ruby?(char) ⇒ Boolean
722 723 724 |
# File 'lib/converterbase.rb', line 722 def object_of_ruby?(char) char =~ /[#{CHARACTER_OF_RUBY}]/ end |
#outputs(data = "", force = false) ⇒ Object
48 49 50 51 52 |
# File 'lib/converterbase.rb', line 48 def outputs(data = "", force = false) if !@request_skip_output_line || force @write_fp.puts(data) end end |
#page_break?(line) ⇒ Boolean
改ページある?
534 535 536 |
# File 'lib/converterbase.rb', line 534 def page_break?(line) line =~ /[#改ページ]/ end |
#process_author_comment(line) ⇒ Object
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
# File 'lib/converterbase.rb', line 553 def (line) if @setting. if @in_author_comment_block if (line) outputs(AUTHOR_COMMENT_CHUKI[@in_author_comment_block][:close]) if @in_author_comment_block == :introduction @request_skip_output_line = true line.clear @in_author_comment_block = nil elsif @in_author_comment_block == :postscript @in_author_comment_block = nil # [#改ページ](前書きの開始位置)を検出したため、 # 改めて前書きの検出をする (line) end end else if (line) # outputs を使うと改ページより前に注記が入ってしまうため、 # delay_outputs を使って出力を line 出力の後に遅らせる delay_outputs(AUTHOR_COMMENT_CHUKI[@in_author_comment_block][:open]) if @in_author_comment_block == :postscript @request_skip_output_line = true line.clear end end end end end |
#rebuild_english_sentences(data) ⇒ Object
英文を再構成する
412 413 414 415 416 |
# File 'lib/converterbase.rb', line 412 def rebuild_english_sentences(data) @english_sentences.each_with_index do |sentence, id| data.sub!("[#英文=#{convert_numbers(id.to_s)}]", sentence) end end |
#rebuild_illust(data) ⇒ Object
804 805 806 807 808 |
# File 'lib/converterbase.rb', line 804 def rebuild_illust(data) @illust_chuki_list.each_with_index do |chuki, id| data.sub!("[#挿絵=#{convert_numbers(id.to_s)}]", chuki) end end |
#rebuild_kanji_num(data) ⇒ Object
109 110 111 112 113 |
# File 'lib/converterbase.rb', line 109 def rebuild_kanji_num(data) data.gsub!(/[#漢数字=(.+?)]/) do @kanji_num_list[$1] end end |
#rebuild_url(data) ⇒ Object
780 781 782 783 784 785 |
# File 'lib/converterbase.rb', line 780 def rebuild_url(data) @url_list.each_with_index do |url, id| data.sub!("[#URL=#{convert_numbers(id.to_s)}]", "<a href=\"#{url}\">#{url}</a>") end end |
#replace_by_replace_txt(text) ⇒ Object
replace.txt により単純置換
1001 1002 1003 1004 1005 1006 |
# File 'lib/converterbase.rb', line 1001 def replace_by_replace_txt(text) @setting.replace_pattern.each do |pattern| src, dst = pattern text.gsub!(src, dst) end end |
#replace_illust_tag(data) ⇒ Object
なろうの挿絵タグを挿絵注釈に変換 挿絵画像が存在しなければダウンロードして保存する
791 792 793 794 795 796 797 798 799 800 801 802 |
# File 'lib/converterbase.rb', line 791 def replace_illust_tag(data) data.gsub!(/^(<i[0-9]+\|[0-9]+>)\n?/m) do next "" unless @setting.enable_narou_illust chuki = @illustration.get($1) if chuki @illust_chuki_list << chuki "[#挿絵=#{@illust_chuki_list.count - 1}]\n" else "" end end end |
#replace_narou_tag(data) ⇒ Object
小説家になろう専用タグを置換
256 257 258 259 260 |
# File 'lib/converterbase.rb', line 256 def replace_narou_tag(data) data.gsub!("【改ページ】", "") data.gsub!(/<KBR>/i, "") data.gsub!(/<PBR>/i, "\n") end |
#replace_tatesen(str) ⇒ Object
734 735 736 |
# File 'lib/converterbase.rb', line 734 def replace_tatesen(str) str.gsub("|", "※[#縦線]") end |
#replace_url(data) ⇒ Object
URL っぽい文字列を一旦別のIDに置き換えてあとで復元することで、変換処理の影響を受けさせない
773 774 775 776 777 778 |
# File 'lib/converterbase.rb', line 773 def replace_url(data) data.gsub!(URI.regexp) do |match| @url_list << match "[#URL=#{@url_list.count - 1}]" end end |
#rstrip_all_lines(data) ⇒ Object
すべての行の行末空白を削除
63 64 65 |
# File 'lib/converterbase.rb', line 63 def rstrip_all_lines(data) data.gsub(/[ \t]+$/m, "") end |
#sesame(str, ten) ⇒ Object
726 727 728 729 730 731 732 |
# File 'lib/converterbase.rb', line 726 def sesame(str, ten) if str.include?("|") str.sub("|", "[#傍点]") + "[#傍点終わり]" else str.insert(-(ten.length + 1), "[#傍点]") + "[#傍点終わり]" end end |
#stash_kanji_num(data) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/converterbase.rb', line 99 def stash_kanji_num(data) data.gsub!(/[#{KANJI_NUM}十百千万億兆京]+/).with_index do |match, i| if "#{$`[-1]}#{$'[0]}" =~ /[\d0-9]/ next match end @kanji_num_list[convert_numbers(i.to_s)] = match "[#漢数字=#{i}]" end end |
#symbols_to_zenkaku(data) ⇒ Object
記号を全角に変換
287 288 289 290 291 292 293 294 295 296 |
# File 'lib/converterbase.rb', line 287 def symbols_to_zenkaku(data) data.tr!("“”‘’〝〟", %!""''""!) data.gsub!(/"([^"\n]+)"/, "〝\\1〟") data.gsub!(/'([^'\n]+)'/, "〝\\1〟") # MEMO: シングルミュート(ノノカギ)を表示出来るフォントはほとんど無い data.gsub!("≪", "※[#始め二重山括弧]") data.gsub!("≫", "※[#終わり二重山括弧]") data.tr!("-=+/*《》'\"%$#&!?、<><>()|‐,._;:", "-=+/*≪≫’”%$#&!?、〈〉〈〉()|-,._;:") data.gsub!("\\", "¥") end |
#tcy(str) ⇒ Object
縦中横注記取得
301 302 303 |
# File 'lib/converterbase.rb', line 301 def tcy(str) "[#縦中横]#{str}[#縦中横終わり]" end |
#to_ruby(match, m1, m2, openclose_symbols) ⇒ Object
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 |
# File 'lib/converterbase.rb', line 738 def to_ruby(match, m1, m2, openclose_symbols) last_char = m1[-1] case when last_char == "|" # 直前に|がある場合ルビ化は抑制される "#{m1[0...-1]}#{openclose_symbols[0]}#{m2}#{openclose_symbols[1]}" when m2 =~ /^・+$/ # ルビが・だけの場合、傍点と判断 sesame(m1, m2) when m1.include?("|") "#{m1.sub(/|([^|]*)$/, "[#ルビ用縦線]\\1")}《#{m2}》" when object_of_ruby?(last_char) # なろうのルビ対象文字を辿って|を挿入する(青空文庫となろうのルビ仕様の差異吸収のため) m1.gsub(/([#{CHARACTER_OF_RUBY} ]+)$/) { if $1[0] == " " " [#ルビ用縦線]#{$1[1..-1]}" else "[#ルビ用縦線]#{$1}" end } + "《#{m2}》" else match end end |
#zenkaku_num_to_hankaku_num(num) ⇒ Object
全角数字を半角アラビア数字に
441 442 443 |
# File 'lib/converterbase.rb', line 441 def zenkaku_num_to_hankaku_num(num) num.tr("0-9#{KANJI_NUM}", "0-90-9") end |
#zenkaku_num_to_kanji(str) ⇒ Object
全角アラビア数字を漢数字に
118 119 120 |
# File 'lib/converterbase.rb', line 118 def zenkaku_num_to_kanji(str) str.tr("0-9", KANJI_NUM) end |
#zenkaku_rstrip(line) ⇒ Object
全角版 String#rstrip!
766 767 768 |
# File 'lib/converterbase.rb', line 766 def zenkaku_rstrip(line) line.gsub!(/[ \s]+\z/, "") end |