Class: Jekyll::TallyTags::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-tally-tags/utils.rb

Class Method Summary collapse

Class Method Details

.combine_tags(items, deep_key) ⇒ Array<Array<String>>

Parameters:

  • items (Array<String>)

    原始数据

  • deep_key (Integer, Array<Integer>)

    合并类型

Returns:

  • (Array<Array<String>>)


151
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
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/jekyll-tally-tags/utils.rb', line 151

def self.combine_tags(items, deep_key)
  hash    = {}
  is_deep = false
  if items.size > 2
    deep_key = ALL unless deep_key
    # all 情况
    if deep_key.is_a?(String)
      if deep_key == ALL
        is_deep = true
        (2..items.size).each do |deep|
          hash[deep] = self.merge(items, nil, nil, deep, 0, 0, 0)
        end
      end
    end

    # 单个
    if deep_key.is_a?(Integer)
      if deep_key != 0 && deep_key >= 2
        is_deep        = true
        hash[deep_key] = self.merge(items, nil, nil, deep_key, 0, 0, 0)
      end
    end

    # 数组
    if deep_key.is_a?(Array)
      deep_key.each do |deep|
        if deep != 0 && deep >= 2
          is_deep    = true
          hash[deep] = self.merge(items, nil, nil, deep, 0, 0, 0)
        end
      end
    end

    if !is_deep
      Jekyll.logger.warn(COUNTER, "#{deep_key}类型未知")
    end

    all = []
    hash.values.each do |values|
      all += values
    end
    all
  else
    [items]
  end
end

.format_values(values, formatters) ⇒ Object

Parameters:

  • values (Array<String>)
  • formatters (Array<String>)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/jekyll-tally-tags/utils.rb', line 33

def self.format_values(values, formatters)
  results = []
  formatters.each do |formatter|
    # 如果是数字的话
    if formatter.is_a?(Integer)
      results << values[formatter]
      next
    end

    # @type [Hash<Integer => Integer>]
    params = {} #下标 数组
    # @type [Hash<Integer => String>]
    methods = {} #方法 数组

    splits     = self.partition_all(formatter)
    split_hash = {}
    splits.each_index do |index|
      split = splits[index]
      # 保存到 `hash` 内
      split_hash[index] = split
      if split.match?(/^:[a-zA-Z0-9_]+/)
        # 判断是不是满足初始条件
        if split.match?(/^:[0-9]+/)
          value_index   = split.match(/[0-9]+/)[0].to_i
          params[index] = values[value_index]
        else
          methods[index] = split
        end
      end
    end

    result = ""
    if methods.empty?
      splits.each_index do |index|
        if params.has_key?(index)
          result += params[index]
        else
          result += splits[index]
        end
      end
    else
      methods.keys.sort.reverse_each do |index|
        param_count  = methods[index].match(/[0-9]/)[0].to_i
        method_match = methods[index].match(/[a-zA-Z]+/).to_s
        method       = Methods.method(method_match)
        args         = []
        (1..param_count).each do |i|
          if params.include?(index + i)
            args[i - 1] = params.delete(index + i) # 在对应位置上删掉该参数
          end
          if methods.include?(index + i)
            args[i - 1] = methods.delete(index + i) # 在对应位置上删掉该参数
          end
          split_hash.delete(index + i)
        end
        methods[index] = method.call(*args)
      end
      split_hash.keys.sort.each do |index|
        if methods.has_key?(index)
          result += methods[index].to_s
        else
          if params.has_key?(index)
            result += params[index]
          else
            result += split_hash[index]
          end
        end
      end
    end
    results << result
  end
  results
end

Parameters:

  • site_config (Configuration)
  • field (String) (defaults to: nil)


271
272
273
274
275
276
277
278
# File 'lib/jekyll-tally-tags/utils.rb', line 271

def self.get_permalink_config(site_config, field = nil)
  config = site_config[CLASSIFY]
  if field
    config ? config[field] : nil
  else
    config
  end
end

.get_tally_config(site_config, field = nil) ⇒ Object

Parameters:

  • site_config (Configuration)
  • field (String) (defaults to: nil)


227
228
229
230
231
232
233
234
# File 'lib/jekyll-tally-tags/utils.rb', line 227

def self.get_tally_config(site_config, field = nil)
  config = site_config[TALLY]
  if field
    config ? config[field] : nil
  else
    config
  end
end

.get_templates(site_config) ⇒ Hash{String => Hash{String => Hash}}

获取 ‘Tally` 里模板配置

Parameters:

  • site (Site)

Returns:

  • (Hash{String => Hash{String => Hash}})


239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/jekyll-tally-tags/utils.rb', line 239

def self.get_templates(site_config)
  configs = site_config[TALLY]
  return nil unless configs && !configs.empty?
  # 然后获取默认配置 没有也是可以的
  # @type [Hash<String => Array<Integer, String>>]
  default_template = configs[DEFAULT]

  # 初始化默认的模板
  if default_template
    default_template.collect { |t| Utils.to_array(t, nil) }
  end

  # 先判断有没有对应的配置
  # @type [Hash<String => Array<Integer, String>>]
  templates = configs[TEMPLATES]
  # 必须有模板才可以解析
  return {
    DEFAULT => default_template
  } unless templates && !templates.empty?

  # 与默认模板合并
  templates.each_key do |template_key|
    template = templates[template_key].merge!(default_template)
    template.each_key do |key|
      template[key] = Utils.to_array(template[key], default_template[key])
    end
  end
  templates
end

.inner_merge(hash, key, inner_hash) ⇒ Object

Parameters:

  • hash (Hash<Object, Hash<Array>>)
  • key (Object)
  • inner_hash (Hash<Array>)


23
24
25
26
27
28
29
# File 'lib/jekyll-tally-tags/utils.rb', line 23

def self.inner_merge(hash, key, inner_hash)
  if hash.include?(key)
    hash[key].merge!(inner_hash) { |_, o, n| o + n }
  else
    hash[key] = inner_hash
  end
end

Parameters:

  • combines (Array<Array<String>>)
  • items (Array<Hash>)
  • find (String)
  • to (String)


133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/jekyll-tally-tags/utils.rb', line 133

def self.link_combines(combines, items, find, to)
  combines.each do |combine|
    items.each do |item|
      if item[find] & combine
        unless item[to]
          item[to] = []
        end
        unless item[to].include?(combine)
          item[to] << combine
        end
      end
    end
  end
end

.merge(items, deep_items, into_items, deep, cur_deep, s, e) ⇒ Array<Array<String>>

Parameters:

  • items (Array<String>)

    原始数据

  • deep_items (Array<Array<String>>)

    对应深度数据的保存数组

  • into_items (Array<String>)

    遍历时 当前深度 的 保存数组 会 添加到 对应深度数据的保存数组

  • deep (Integer)

    总深度

  • cur_deep (Integer)

    当前深度

  • s (Integer)

    起始下标

  • e (Integer)

    结束下标

Returns:

  • (Array<Array<String>>)


206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/jekyll-tally-tags/utils.rb', line 206

def self.merge(items, deep_items, into_items, deep, cur_deep, s, e)
  unless deep_items
    deep_items = []
    into_items = []
    cur_deep   = 0
    s          = 0
    e          = items.size - deep
  end
  (s..e).each do |i|
    temp = Array.new(into_items) << items[i]
    if cur_deep == deep - 1
      deep_items << temp
    else
      deep_items += self.merge(items, [], temp, deep, cur_deep + 1, i + 1, e + 1)
    end
  end
  deep_items
end

.partition_all(formatter) ⇒ Array<String>

Examples:

":sub_2和:2:3" => [":sub_2", "", ":2", ":3"]

Parameters:

  • (String)

Returns:

  • (Array<String>)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/jekyll-tally-tags/utils.rb', line 111

def self.partition_all(formatter)
  result = []
  temps  = formatter.partition(/:\w+/)
  (0..temps.size - 1).each do |index|
    temp = temps[index]
    if index == temps.size - 1
      unless temp.empty?
        result += self.partition_all(temp)
      end
    else
      if temp && !temp.empty?
        result << temp
      end
    end
  end
  result
end

.to_array(single_or_array, default) ⇒ Array

把配置变成数组配置

Parameters:

  • (Array, Object)

Returns:

  • (Array)


11
12
13
14
15
16
17
18
# File 'lib/jekyll-tally-tags/utils.rb', line 11

def self.to_array(single_or_array, default)
  return default unless single_or_array
  if single_or_array.is_a?(Array)
    single_or_array
  else
    [single_or_array]
  end
end