Class: Jekyll::TallyTags::YearPage

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

Constant Summary

Constants inherited from BasePage

BasePage::ATTRIBUTES_FOR_LIQUID

Instance Attribute Summary

Attributes inherited from BasePage

#docs, #slug, #type

Instance Method Summary collapse

Methods inherited from BasePage

#avg, #date, #filters, #initialize, #inspect, #layout, #modify_title_hash, #permalink, #relative_path, #sum, #template, #title, #url, #url_placeholders

Constructor Details

This class inherits a constructor from Jekyll::TallyTags::BasePage

Instance Method Details

#docs_to_content(docs) ⇒ Object

Parameters:

  • docs (Array<Document>)


13
14
15
16
17
18
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/jekyll-tally-tags/pages/year_page.rb', line 13

def docs_to_content(docs)
  yaml_mode     = true
  md_table_mode = true
  dates         = docs.collect { |doc| doc.data[SLUG] }.uniq
  align         = yaml_mode ? "" : ":----:"
  sep           = yaml_mode ? " " : " | "
  yml_temp_hash = Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = { VALUES => [], MD_URL => [] } } }
  dates.each do |date|
    # 找到当前遍历的 符合 `date` 的所有 `docs`
    docs.find_all { |doc| doc.data[SLUG] == date }.each do |doc|
      template_key = doc.data[TEMPLATE]
      keys         = doc.data[CONTENTS].map { |hash| hash[KEY] }
      values       = doc.data[CONTENTS].map { |hash| hash[VALUE] }
      urls         = doc.data[CONTENTS].map { |hash| hash[MD_URL] ? hash[MD_URL] : hash[VALUE] }
      yml_temp_hash[date][{ template_key => keys }][VALUES] << values
      yml_temp_hash[date][{ template_key => keys }][MD_URL] << urls
    end
    # 计算 对齐空格数
    yml_temp_hash[date].each do |temp_keys, hash|
      values_list = hash[VALUES]
      lengths     = []
      keys        = temp_keys.values[0]
      keys.each_index do |index|
        # k_length       = get_length(keys[index])
        v_max          = values_list.collect() { |values| values[index] }.max_by() { |value| get_length(value) }
        lengths[index] = [get_length(v_max), get_length(align)].max()
      end
      yml_temp_hash[date][temp_keys][MAX_LENGTH] = lengths
    end
  end
  content = ""
  if yaml_mode
    content += "```yml\n"
    yml_temp_hash.keys.reverse_each do |date|
      content        += "#{date}:\n"
      templates_hash = yml_temp_hash[date]
      templates_hash.each_key do |temp_keys|
        temp    = temp_keys.keys[0]
        content += "  #{temp}:\n"
        keys    = temp_keys.values[0]
        hash    = templates_hash[temp_keys]
        lengths = hash[MAX_LENGTH]
        # content += "    - #{center_values(keys, lengths, sep)}\n"
        hash[VALUES].each_index do |index|
          values  = hash[VALUES][index]
          content += "    - #{center_values(values, lengths, sep)}\n"
        end
        content += "\n"
      end
    end
    content += "```"
  end
  content
end

#get_contentObject



8
9
10
# File 'lib/jekyll-tally-tags/pages/year_page.rb', line 8

def get_content
  docs_to_content(get_docs)
end