Class: NovelSetting

Inherits:
Object
  • Object
show all
Defined in:
lib/novelsetting.rb

Constant Summary collapse

INI_NAME =
"setting.ini"
INI_ERB_BINARY_VERSION =
1.2
REPLACE_NAME =
"replace.txt"
ORIGINAL_SETTINGS =
{
  name: "enable_yokogaki",
  type: :boolean,
  value: false,
  help: "横書きにする"
},
{
  name: "enable_inspect",
  type: :boolean,
  value: false,
  help: "小説に対する各種調査を実行する。結果を表示するには narou inspect コマンドを使用"
},
{
  name: "enable_convert_num_to_kanji",
  type: :boolean,
  value: true,
  help: "数字の漢数字変換を有効にする"
},
{
  name: "enable_kanji_num_with_units",
  type: :boolean,
  value: true,
  help: "漢数字変換した場合、千・万などに変換する"
},
{
  name: "kanji_num_with_units_lower_digit_zero",
  type: :integer,
  value: 3,
  help: "〇(ゼロ)が最低この数字以上付いてないと千・万などをつける対象にしない"
},
{
  name: "enable_alphabet_force_zenkaku",
  type: :boolean,
  value: false,
  help: "アルファベットを強制的に全角にする。falseの場合英文は半角、それ以外は全角になる"
},
{
  name: "enable_half_indent_bracket",
  type: :boolean,
  value: true,
  help: "行頭かぎ括弧に二分アキを挿入する"
},
{
  name: "enable_auto_indent",
  type: :boolean,
  value: true,
  help: "自動行頭字下げ機能。行頭字下げが行われているかを判断し、適切に行頭字下げをするか"
},
{
  name: "enable_force_indent",
  type: :boolean,
  value: false,
  help: "行頭字下げを必ず行うか。enable_auto_indent の設定は無視される"
},
{
  name: "enable_auto_join_in_brackets",
  type: :boolean,
  value: true,
  help: "かぎ括弧内自動連結を有効にする\n例)\n「~~~!\n ***?」  → 「~~~! ***?」"
},
{
  name: "enable_auto_join_line",
  type: :boolean,
  value: true,
  help: "行末が読点で終わっている部分を出来るだけ連結する"
},
{
  name: "enable_enchant_midashi",
  type: :boolean,
  value: true,
  help: "[#改ページ]直後の行に中見出しを付与する(テキストファイルを直接変換する場合のみの設定)"
},
{
  name: "enable_author_comments",
  type: :boolean,
  value: true,
  help: "作者コメントを検出する(テキストファイルを直接変換する場合のみの設定)"
},
{
  name: "enable_erase_introduction",
  type: :boolean,
  value: false,
  help: "前書きを削除する"
},
{
  name: "enable_erase_postscript",
  type: :boolean,
  value: false,
  help: "後書きを削除する"
},
{
  name: "enable_ruby",
  type: :boolean,
  value: true,
  help: "ルビ処理を有効にする"
},
{
  name: "enable_illust",
  type: :boolean,
  value: true,
  help: "挿絵タグを有効にする(false なら削除)"
},
{
  name: "enable_transform_fraction",
  type: :boolean,
  value: false,
  help: "○/×表記を×分の○表記に変換する。日付表記(10/23)と誤爆しやすいので注意"
},
{
  name: "enable_transform_date",
  type: :boolean,
  value: false,
  help: "日付表記(20yy/mm/dd)を任意の形式(date_formatで指定)に変換する"
},
{
  name: "date_format",
  type: :string,
  value: "%Y年%m月%d日",
  help: "書式は http://bit.ly/1m5e3w7 を参考"
},
{
  name: "enable_convert_horizontal_ellipsis",
  type: :boolean,
  value: true,
  help: "中黒(・)を並べて三点リーダーもどきにしているのを三点リーダーに変換する"
},
{
  name: "enable_convert_page_break",
  type: :boolean,
  value: false,
  help: "`to_page_break_threshold` で設定した個数以上連続する空行を改ページに変換する"
},
{
  name: "to_page_break_threshold",
  type: :integer,
  value: 10,
  help: "ここで設定した値が `enable_convert_page_break` に反映される"
},
{
  name: "enable_dakuten_font",
  type: :boolean,
  value: false,
  help: "濁点表現をNarou.rbで処理する(濁点フォントを使用する)。false の場合はAozoraEpub3に任せる"
},
{
  name: "enable_display_end_of_book",
  type: :boolean,
  value: true,
  help: "小説の最後に本を読み終わった表示をする"
},
{
  name: "enable_add_date_to_title",
  type: :boolean,
  value: false,
  help: "変換後の小説のタイトルに最新話掲載日や更新日等の日付を付加する"
},
{
  name: "title_date_format",
  type: :string,
  value: "(%-m/%-d)",
  help: "enable_add_date_to_title \u3067\u4ED8\u4E0E\u3059\u308B\u65E5\u4ED8\u306E\u30D5\u30A9\u30FC\u30DE\u30C3\u30C8\u3002\u66F8\u5F0F\u306F http://bit.ly/1m5e3w7 \u3092\u53C2\u7167\u3002\nNarou.rb\u5C02\u7528\u306E\u66F8\u5F0F\u3068\u3057\u3066\u4E0B\u8A18\u306E\u3082\u306E\u3082\u4F7F\u7528\u53EF\u80FD\u3002\n$s 2045\u5E74\u307E\u3067\u306E\u6B8B\u308A\u6642\u9593(10\u5206\u5358\u4F4D\u306E4\u6841\u306E36\u9032\u6570)\n$t \u5C0F\u8AAC\u306E\u30BF\u30A4\u30C8\u30EB\n$ns \u5C0F\u8AAC\u304C\u63B2\u8F09\u3055\u308C\u3066\u3044\u308B\u30B5\u30A4\u30C8\u540D\n$nt \u5C0F\u8AAC\u7A2E\u5225\uFF08\u77ED\u7DE8 or \u9023\u8F09\uFF09\n$ntag \u5C0F\u8AAC\u306E\u30BF\u30B0\u3092\u30AB\u30F3\u30DE\u533A\u5207\u308A\u306B\u3057\u305F\u3082\u306E\n  EOS\n},\n{\n  name: \"title_date_align\",\n  type: :select,\n  value: \"right\",\n  help: \"enable_add_date_to_title \u304C\u6709\u52B9\u306A\u5834\u5408\u306B\u4ED8\u4E0E\u3055\u308C\u308B\u65E5\u4ED8\u306E\u4F4D\u7F6E\u3002left(\u30BF\u30A4\u30C8\u30EB\u306E\u524D) \u304B right(\u30BF\u30A4\u30C8\u30EB\u306E\u5F8C)\u3002title_date_format \u3067 $t \u3092\u4F7F\u7528\u3057\u305F\u5834\u5408\u3053\u306E\u8A2D\u5B9A\u306F\u7121\u8996\u3055\u308C\u308B\",\n  select_keys: %w(left right),\n  select_summaries: %w(\u30BF\u30A4\u30C8\u30EB\u306E\u524D \u30BF\u30A4\u30C8\u30EB\u306E\u5F8C)\n},\n{\n  name: \"title_date_target\",\n  type: :select,\n  value: \"general_lastup\",\n  help: \"enable_add_date_to_title \u3067\u4ED8\u4E0E\u3059\u308B\u65E5\u4ED8\u306E\u7A2E\u985E\u3002\\ngeneral_lastup(\u6700\u65B0\u8A71\u63B2\u8F09\u65E5),last_update(\u66F4\u65B0\u65E5),new_arrivals_date(\u65B0\u7740\u3092\u78BA\u8A8D\u3057\u305F\u65E5),convert(\u5909\u63DB\u3057\u305F\u65E5)\",\n  select_keys: %w(general_lastup last_update new_arrivals_date convert),\n  select_summaries: %w(\u6700\u65B0\u8A71\u63B2\u8F09\u65E5 \u66F4\u65B0\u65E5 \u65B0\u7740\u3092\u78BA\u8A8D\u3057\u305F\u65E5 \u5909\u63DB\u3057\u305F\u65E5)\n},\n{\n  name: \"enable_ruby_youon_to_big\",\n  type: :boolean,\n  value: false,\n  help: \"\u30EB\u30D3\u306E\u62D7\u97F3(\u3041\u3001\u3043\u7B49)\u3092\u5546\u696D\u66F8\u7C4D\u306E\u3088\u3046\u306B\u5927\u304D\u304F\u3059\u308B\"\n},\n{\n  name: \"enable_pack_blank_line\",\n  type: :boolean,\n  value: true,\n  help: \"\u7E26\u66F8\u304D\u3067\u8AAD\u307F\u3084\u3059\u3044\u3088\u3046\u306B\u7A7A\u884C\u3092\u6E1B\u3089\u3059\"\n},\n{\n  name: \"enable_kana_ni_to_kanji_ni\",\n  type: :boolean,\n  value: true,\n  help: \"\u6F22\u5B57\u306E\u4E8C\u3068\u9593\u9055\u3048\u3066\u30AB\u30BF\u30AB\u30CA\u306E\u30CB\u3092\u4F7F\u3063\u3066\u3044\u305D\u3046\u306A\u306E\u3092\u3001\u6F22\u5B57\u306B\u76F4\u3059\"\n},\n{\n  name: \"enable_insert_word_separator\",\n  type: :boolean,\n  value: false,\n  help: \"\u5358\u8A9E\u9078\u629E\u304C\u3057\u3084\u3059\u3044\u3088\u3046\u306B\u5358\u8A9E\u5358\u4F4D\u306E\u533A\u5207\u308A\u30C7\u30FC\u30BF\u3092\u633F\u5165\u3059\u308B\uFF08Kindle\u5C02\u7528\uFF09\"\n},\n{\n  name: \"enable_insert_char_separator\",\n  type: :boolean,\n  value: false,\n  help: \"\u6587\u5B57\u9078\u629E\u304C\u3057\u3084\u3059\u3044\u3088\u3046\u306B\uFF11\u6587\u5B57\u305A\u3064\u533A\u5207\u308A\u30C7\u30FC\u30BF\u3092\u633F\u5165\u3059\u308B\uFF08Kindle\u5C02\u7528\u3002enable_insert_word_separator \u304C\u6709\u52B9\u306A\u5834\u5408\u7121\u3053\u306E\u8A2D\u5B9A\u306F\u7121\u8996\u3055\u308C\u308B\uFF09\"\n},\n{\n  name: \"enable_strip_decoration_tag\",\n  type: :boolean,\n  value: false,\n  help: \"HTML\u306E\u88C5\u98FE\u7CFB\u30BF\u30B0\u3092\u524A\u9664\u3059\u308B\uFF08\u4E3B\u306BArcadia\u306E\u4F5C\u54C1\u306B\u5F71\u97FF\uFF09\"\n},\n{\n  name: \"enable_double_dash_to_image\",\n  type: :boolean,\n  value: false,\n  help: \"\uFF12\u500D\u30C0\u30C3\u30B7\u30E5\uFF08\u2015\u2015\uFF09\u3092\u753B\u50CF\u306B\u5DEE\u3057\u66FF\u3048\u308B\u3002Kindle\u306E\u30C7\u30D5\u30A9\u30EB\u30C8\u30D5\u30A9\u30F3\u30C8\u307F\u305F\u3044\u306B\u30C0\u30C3\u30B7\u30E5\u304C\u592A\u304F\u3066\u6C17\u306B\u306A\u308B\u4EBA\u7528\"\n},\n{\n  name: \"enable_add_end_to_title\",\n  type: :boolean,\n  value: false,\n  help: \"\u5B8C\u7D50\u6E08\u307F\u5C0F\u8AAC\u306E\u30BF\u30A4\u30C8\u30EB\u306B(\u5B8C\u7D50)\u3068\u8868\u793A\u3059\u308B\"\n},\n{\n  name: \"cut_old_subtitles\",\n  type: :integer,\n  value: 0,\n  help: \"\uFF11\u8A71\u76EE\u304B\u3089\u6307\u5B9A\u3057\u305F\u8A71\u6570\u5206\u3001\u5909\u63DB\u306E\u5BFE\u8C61\u5916\u306B\u3059\u308B\u3002\" \\\n        \"\u5168\u8A71\u6570\u5206\u4EE5\u4E0A\u306E\u6570\u5024\u3092\u6307\u5B9A\u3057\u305F\u5834\u5408\u3001\u6700\u65B0\u8A71\u3060\u3051\u5909\u63DB\u3059\u308B\"\n},\n{\n  name: \"author_comment_style\",\n  type: :select,\n  value: \"css\",\n  help: \"\u4F5C\u8005\u30B3\u30E1\u30F3\u30C8(\u524D\u66F8\u304D\u30FB\u5F8C\u66F8\u304D)\u306E\u88C5\u98FE\u65B9\u6CD5\u3092\u6307\u5B9A\u3059\u308B\u3002Kobo\u3084Adobe Digital Edition\u3067\u306F\u300CCSS\u3067\u88C5\u98FE\u300D\u306B\u3059\u308B\u3068\u30C7\u30B6\u30A4\u30F3\u304C\u5D29\u308C\u308B\u306E\u3067\u305D\u308C\u4EE5\u5916\u3092\u63A8\u5968\u3002css:CSS\u3067\u88C5\u98FE\u3001simple:\u30B7\u30F3\u30D7\u30EB\u306B\u6BB5\u843D\u3001plain:\u88C5\u98FE\u3057\u306A\u3044\",\n  select_keys: %w(css simple plain),\n  select_summaries: %w(CSS\u3067\u88C5\u98FE \u30B7\u30F3\u30D7\u30EB\u306B\u6BB5\u843D \u88C5\u98FE\u3057\u306A\u3044)\n}\n"
ORIGINAL_SETTINGS_KEY_INDEXES =
{}.tap { |hash|
  ORIGINAL_SETTINGS.each_with_index do |s, i|
    hash[s[:name]] = i
  end
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, ignore_force, ignore_default) ⇒ NovelSetting

Returns a new instance of NovelSetting.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/novelsetting.rb', line 43

def initialize(target, ignore_force, ignore_default)
  if File.directory?(target.to_s)
    archive_path = target
  else
    archive_path = Downloader.get_novel_data_dir_by_target(target).to_s
  end
  @archive_path = File.expand_path(archive_path)
  @ignore_force = ignore_force
  @ignore_default = ignore_default
  @replace_pattern = []
  @settings = {}
end

Instance Attribute Details

#archive_pathObject

Returns the value of attribute archive_path.



15
16
17
# File 'lib/novelsetting.rb', line 15

def archive_path
  @archive_path
end

#authorObject

Returns the value of attribute author.



15
16
17
# File 'lib/novelsetting.rb', line 15

def author
  @author
end

#idObject

Returns the value of attribute id.



15
16
17
# File 'lib/novelsetting.rb', line 15

def id
  @id
end

#replace_patternObject

Returns the value of attribute replace_pattern.



15
16
17
# File 'lib/novelsetting.rb', line 15

def replace_pattern
  @replace_pattern
end

#settingsObject

Returns the value of attribute settings.



15
16
17
# File 'lib/novelsetting.rb', line 15

def settings
  @settings
end

#titleObject

Returns the value of attribute title.



15
16
17
# File 'lib/novelsetting.rb', line 15

def title
  @title
end

Class Method Details

.create(target, ignore_force, ignore_default) ⇒ Object

小説設定オブジェクトを作成する

target には小説ID等の他、小説保存フォルダを指定できる。テキストファイル変換時はデータベースに登録されていないので load ではなくこちらを使用する



35
36
37
38
39
40
41
# File 'lib/novelsetting.rb', line 35

def self.create(target, ignore_force, ignore_default)
  setting = new(target, ignore_force, ignore_default)
  setting.load_settings
  setting.set_attribute
  setting.load_replace_pattern
  setting
end

.get_original_settingsObject



216
217
218
# File 'lib/novelsetting.rb', line 216

def self.get_original_settings
  ORIGINAL_SETTINGS
end

.load(target, ignore_force = false, ignore_default = false) ⇒ Object

データベースに登録されている小説の設定を取得する



20
21
22
23
24
25
26
27
# File 'lib/novelsetting.rb', line 20

def self.load(target, ignore_force = false, ignore_default = false)
  setting = create(target, ignore_force, ignore_default)
  data = Downloader.get_data_by_target(target)
  setting.id = data["id"]
  setting.author = data["author"]
  setting.title = data["title"]
  setting
end

.load_default_settingsObject

default.* 設定を取得



124
125
126
# File 'lib/novelsetting.rb', line 124

def self.load_default_settings
  load_settings_by_pattern("default")
end

.load_force_settingsObject

force.* 設定を取得



117
118
119
# File 'lib/novelsetting.rb', line 117

def self.load_force_settings
  load_settings_by_pattern("force")
end

.load_settings_by_pattern(pattern) ⇒ Object

local_settings から group.name 形式のデータを取得

{ name: value, … } 形式のハッシュとして返す



103
104
105
106
107
108
109
110
111
112
# File 'lib/novelsetting.rb', line 103

def self.load_settings_by_pattern(pattern)
  res = Inventory.load("local_setting").map { |name, value|
    if name =~ /^#{pattern}\.(.+)$/
      [$1, value]
    else
      nil
    end
  }.compact.flatten
  Hash[*res]
end

Instance Method Details

#[](name) ⇒ Object

配列風のアクセサ定義



185
186
187
# File 'lib/novelsetting.rb', line 185

def [](name)
  @settings[name]
end

#[]=(name, value) ⇒ Object



189
190
191
192
193
194
# File 'lib/novelsetting.rb', line 189

def []=(name, value)
  unless value.nil?
    check_value_of_type(name, value)
  end
  @settings[name] = value
end

#check_value_of_type(name, value) ⇒ Object

指定された設定の型チェック



156
157
158
159
160
161
162
# File 'lib/novelsetting.rb', line 156

def check_value_of_type(name, value)
  original = get_value_by_original(name)
  return unless original
  unless type_eq_value(original[:type], value)
    raise Helper::InvalidVariableType, original[:type]
  end
end

#get_value_by_original(name) ⇒ Object



128
129
130
131
# File 'lib/novelsetting.rb', line 128

def get_value_by_original(name)
  index = ORIGINAL_SETTINGS_KEY_INDEXES[name]
  index ? ORIGINAL_SETTINGS[index] : nil
end

#ini_pathObject



56
57
58
# File 'lib/novelsetting.rb', line 56

def ini_path
  File.join(@archive_path, INI_NAME)
end

#load_replace_patternObject

replace.txt による置換定義を読み込む



199
200
201
202
203
204
205
206
# File 'lib/novelsetting.rb', line 199

def load_replace_pattern
  @replace_pattern.clear
  replace_txt_path = File.join(@archive_path, REPLACE_NAME)
  if File.exist?(replace_txt_path)
    @replace_pattern = Narou.parse_replace_txt(File.read(replace_txt_path, mode: "r:BOM|UTF-8"))
  end
  @replace_pattern
end

#load_setting_iniObject



60
61
62
# File 'lib/novelsetting.rb', line 60

def load_setting_ini
  Ini.load_file(ini_path) rescue Ini.load("")
end

#load_settingsObject

小説変換時の設定値読込

設定値の優先順位は

  1. narou setting コマンドで設定した force.*

  2. setting.ini

  3. narou setting コマンドで設定した default.*

  4. ORIGINAL_SETTINGS



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/novelsetting.rb', line 73

def load_settings
  @settings.clear
  ini = load_setting_ini
  force_settings = @ignore_force ? {} : NovelSetting.load_force_settings
  default_settings = @ignore_default ? {} : NovelSetting.load_default_settings
  ORIGINAL_SETTINGS.each do |element|
    name, value, type = element[:name], element[:value], element[:type]
    if force_settings.include?(name)
      @settings[name] = force_settings[name]
    elsif ini["global"].include?(name) && type_eq_value(type, ini["global"][name])
      @settings[name] = ini["global"][name]
    elsif default_settings.include?(name)
      @settings[name] = default_settings[name]
    else
      @settings[name] = value
    end
  end
  # デフォルト設定以外を読み込む
  ini["global"].each do |key, value|
    unless @settings.include?(key)
      @settings[key] = value
    end
  end
end

#save_replace_patternObject

replace.txt に設定を書き戻す



211
212
213
214
# File 'lib/novelsetting.rb', line 211

def save_replace_pattern
  replace_txt_path = File.join(@archive_path, REPLACE_NAME)
  Narou.write_replace_txt(replace_txt_path, @replace_pattern)
end

#save_settingsObject

設定を保存



136
137
138
139
140
141
# File 'lib/novelsetting.rb', line 136

def save_settings
  original_settings = NovelSetting.get_original_settings
  default_settings = NovelSetting.load_default_settings
  novel_setting = self
  Template.write(INI_NAME, @archive_path, binding, INI_ERB_BINARY_VERSION, Template::OVERWRITE)
end

#set_attributeObject

設定データ用アクセサ定義



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/novelsetting.rb', line 167

def set_attribute
  @settings.each_key do |key|
    instance_eval "      def \#{key}\n        @settings[\"\#{key}\"]\n      end\n\n      def \#{key}=(value)\n        check_value_of_type(\"\#{key}\", value)\n        @settings[\"\#{key}\"] = value\n      end\n    EOS\n  end\nend\n"

#type_eq_value(type, value) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/novelsetting.rb', line 143

def type_eq_value(type, value)
  type_of_value = Helper.type_of_value(value)
  case type
  when :string, :select, :multiple
    :string == type_of_value
  else
    type == type_of_value
  end
end