Method: ConverterBase#enchant_midashi

Defined in:
lib/converterbase.rb

#enchant_midashi(data) ⇒ Object

[#改ページ]直後の行を見出しに設定する



1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
# File 'lib/converterbase.rb', line 1022

def enchant_midashi(data)
  def midashi(str)
    midashi_title = str.gsub("[#半字下げ]", "").gsub(/^[ \s]+/, "").gsub(/[ \s]+$/, "")
    @inspector.subtitle = midashi_title
    "[#3字下げ][#中見出し]#{midashi_title}[#中見出し終わり]"
  end

  def calc_cr_count(str)
    head_cr_count = str.index(/[^\n]/)
    head_cr_count > 2 ? 2 : head_cr_count
  end

  # 実際に見出しを付与する
  data.gsub!(/[#改ページ]\n(.+?)\n/) do |match|
    m1 = $1
    rest = $'
    # 前書きがある場合は今回は保留して、次の処理で見出しを付与する
    if $1 =~ /#{AUTHOR_COMMENT_CHUKI[:introduction][:open]}/
      match
    else
      # 見出しの次の行が空行ではない場合空行を追加する
      add_tail = "\n" * (2 - calc_cr_count(rest))
      # 見出しと本文の間には空行を2行挟む
      "[#改ページ]\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