Class: SiteSetting

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

Constant Summary collapse

NOVEL_SITE_SETTING_DIR =
"webnovel/"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ SiteSetting

Returns a new instance of SiteSetting.



83
84
85
86
# File 'lib/sitesetting.rb', line 83

def initialize(path)
  @match_values = {}
  @yaml_setting = YAML.load_file(path).freeze
end

Class Method Details

.find(toc_url) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sitesetting.rb', line 54

def find(toc_url)
  result = nil
  settings.each do |s|
    setting = s.clone
    if setting.multi_match(toc_url, "url")
      result = setting
      break
    end
  end
  result
end

.load_file(path) ⇒ Object



66
67
68
# File 'lib/sitesetting.rb', line 66

def load_file(path)
  new(path)
end

.load_settingsObject

小説サイトの定義ファイルを全部読み込む

スクリプト同梱の設定ファイルを読み込んだあと、ユーザの小説の管理ディレクトリ内にある webnovel ディレクトリからも定義ファイルを読み込む



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sitesetting.rb', line 19

def load_settings
  result = []
  load_paths = [
    File.join(Narou.get_script_dir, NOVEL_SITE_SETTING_DIR, "*.yaml"),
    File.join(Narou.get_root_dir, NOVEL_SITE_SETTING_DIR, "*.yaml")
  ].uniq.join("\0")
  Dir.glob(load_paths) do |path|
    setting = SiteSetting.load_file(path)
    if setting["name"] == "小説家になろう"
      @narou = setting
    end
    result << setting
  end
  if result.empty?
    error "小説サイトの定義ファイルがひとつもありません"
    exit Narou::EXIT_ERROR_CODE
  end
  unless @narou
    error "小説家になろうの定義ファイルが見つかりませんでした"
    exit Narou::EXIT_ERROR_CODE
  end
  # TODO: 配列の並び順で先頭をなろうにしておく。find するときになろうがすぐ引っかかるので効率的
  # 設定ファイルに priority を追加して、それでソートする
  result
end

.narouObject



49
50
51
52
# File 'lib/sitesetting.rb', line 49

def narou
  settings unless @narou
  @narou.clone
end

.settingsObject



45
46
47
# File 'lib/sitesetting.rb', line 45

def settings
  @settings ||= load_settings
end

Instance Method Details

#[](key) ⇒ Object



71
72
73
# File 'lib/sitesetting.rb', line 71

def [](key)
  replace_group_values(key)
end

#[]=(key, value) ⇒ Object



75
76
77
# File 'lib/sitesetting.rb', line 75

def []=(key, value)
  @match_values[key] = value
end

#clearObject



79
80
81
# File 'lib/sitesetting.rb', line 79

def clear
  @match_values.clear
end

#do_replace(dest, option_values) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/sitesetting.rb', line 141

def do_replace(dest, option_values)
  return dest unless dest.respond_to?(:gsub)
  values = @yaml_setting.merge(@match_values).merge(option_values)
  dest.gsub(/\\\\k<(.+?)>/) do |match|
    value = values[$1]
    if value
      value.gsub(/\\\\k<(.+?)>/) do
        replace_group_values($1, option_values)
      end
    else
      match
    end
  end
end

#initialize_copy(_obj) ⇒ Object



88
89
90
# File 'lib/sitesetting.rb', line 88

def initialize_copy(_obj)
  @match_values = {}
end

#is_container?(value) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/sitesetting.rb', line 125

def is_container?(value)
  value.is_a?(Hash) || value.is_a?(Narou::API)
end

#matched?(key) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/sitesetting.rb', line 92

def matched?(key)
  @match_values[key]
end

#multi_match(source, *keys) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sitesetting.rb', line 96

def multi_match(source, *keys)
  match_data = nil
  keys.each do |key|
    setting_value = self[key] or next
    [*setting_value].each do |value|
      match_data = source.match(/#{value}/m)
      if match_data
        @match_values[key] = value       # yamlのキーでもmatch_valuesに設定しておくが、
        update_match_values(match_data)  # ←ここで同名のグループ名が定義されていたら上書きされるので注意
                                         # 例えば、title: <title>(?<title>.+?)</title> と定義されていた場合、
                                         # @match_values["title"] には (?<title>.+?) 部分の要素が反映される
        break
      end
    end
  end
  match_data
end

#multi_match_once(source, *keys) ⇒ Object



114
115
116
117
# File 'lib/sitesetting.rb', line 114

def multi_match_once(source, *keys)
  clear
  multi_match(source, *keys)
end

#replace_group_values(key, option_values = {}) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/sitesetting.rb', line 129

def replace_group_values(key, option_values = {})
  buf = option_values[key] || @match_values[key] || @yaml_setting[key]
  return buf if is_container?(buf)
  if buf.is_a?(Array)
    buf.map do |dest|
      do_replace(dest, option_values)
    end
  else
    do_replace(buf, option_values)
  end
end

#update_match_values(match_data) ⇒ Object



119
120
121
122
123
# File 'lib/sitesetting.rb', line 119

def update_match_values(match_data)
  match_data.names.each do |name|
    @match_values[name] = match_data[name] || ""
  end
end