Class: SiteSetting

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

Constant Summary collapse

NOVEL_SITE_SETTING_DIR =
"webnovel/"
EXCLUDE_KEYS =
%w(name version)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ SiteSetting

Returns a new instance of SiteSetting.



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

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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/sitesetting.rb', line 13

def path
  @path
end

#yamlObject (readonly)

Returns the value of attribute yaml.



13
14
15
# File 'lib/sitesetting.rb', line 13

def yaml
  @yaml
end

Class Method Details

.find(toc_url) ⇒ Object



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

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

.load_file(path) ⇒ Object



68
69
70
# File 'lib/sitesetting.rb', line 68

def load_file(path)
  new(path)
end

.load_settingsObject

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

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



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

def load_settings
  result = {}
  load_paths = [
    Narou.script_dir.join(NOVEL_SITE_SETTING_DIR, "*.yaml"),
    Narou.root_dir.join(NOVEL_SITE_SETTING_DIR, "*.yaml")
  ].uniq
  Dir.glob(load_paths) do |path|
    setting = SiteSetting.load_file(path)
    name = setting["name"]
    @narou ||= setting if name == "小説家になろう"
    origin = result[name]
    origin&.merge(setting)
    result[name] ||= setting
  end
  if result.empty?
    error "小説サイトの定義ファイルがひとつもありません"
    exit Narou::EXIT_ERROR_CODE
  end
  unless @narou
    error "小説家になろうの定義ファイルが見つかりませんでした"
    exit Narou::EXIT_ERROR_CODE
  end
  result
end

.narouObject



51
52
53
54
# File 'lib/sitesetting.rb', line 51

def narou
  settings unless @narou
  @narou.clone
end

.settingsObject



47
48
49
# File 'lib/sitesetting.rb', line 47

def settings
  @settings ||= load_settings
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  replace_group_values(key)
end

#[]=(key, value) ⇒ Object



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

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

#clearObject



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

def clear
  @match_values.clear
end

#do_replace(dest, option_values) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/sitesetting.rb', line 144

def do_replace(dest, option_values)
  return dest unless dest.respond_to?(:gsub)
  values = yaml.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



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

def initialize_copy(_obj)
  @match_values = {}
end

#is_container?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#matched?(key) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/sitesetting.rb', line 95

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

#merge(setting) ⇒ Object



161
162
163
164
165
166
# File 'lib/sitesetting.rb', line 161

def merge(setting)
  validate_version(setting) or return
  (setting.yaml.keys - EXCLUDE_KEYS).each do |key|
    yaml[key] = setting.yaml[key]
  end
end

#multi_match(source, *keys) ⇒ Object



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

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



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

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

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



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/sitesetting.rb', line 132

def replace_group_values(key, option_values = {})
  buf = option_values[key] || @match_values[key] || yaml[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



122
123
124
125
126
# File 'lib/sitesetting.rb', line 122

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

#validate_version(setting) ⇒ Object



168
169
170
171
172
173
174
175
# File 'lib/sitesetting.rb', line 168

def validate_version(setting)
  version = setting.yaml["version"]
  return true unless version # version が指定されていない場合は常に上書きを許可する
  return true if version >= yaml["version"]

  error "#{setting.path} の内容が古いため読み込みをスキップしました"
  false
end