Class: Jekyll::TallyTags::DocsToItems
- Inherits:
-
Object
- Object
- Jekyll::TallyTags::DocsToItems
- Defined in:
- lib/jekyll-tally-tags/docs_to_items.rb
Class Method Summary collapse
- .combine_tags(items, site) ⇒ Object
- .create_items(items, site) ⇒ Object
-
.insert_to_date_hash(templates, docs) ⇒ Hash{Date => Hash}
“‘ yml 2001-01-01: - template_key: - A B C D E F G - a b c d e f g “`.
- .read(site) ⇒ Object
- .read_csv(date_hash, templates) ⇒ Array[Hash{String => Object}]
Class Method Details
.combine_tags(items, site) ⇒ Object
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 |
# File 'lib/jekyll-tally-tags/docs_to_items.rb', line 32 def self.(items, site) configs = site.config.fetch(TALLY, {}) return nil unless configs && !configs.empty? if configs[COMBINE] && configs[COMBINE].is_a?(Array) configs[COMBINE].each do |config| find = config[FIND] to = config[TO] deep = config[DEEP] finds = [] items.each do |item| if item.keys.include?(find) # 找到所有 `key` finds += item[find] end end finds.uniq! if finds.size >= 2 items.each { |item| item[to] = [] } combines = Utils.(finds, deep) Utils.link_combines(combines, items, find, to) end end end items end |
.create_items(items, site) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/jekyll-tally-tags/docs_to_items.rb', line 22 def self.create_items(items, site) item_objects = [] items.each do |item| item_objects << Item.new(item, site) end site.posts.docs += item_objects end |
.insert_to_date_hash(templates, docs) ⇒ Hash{Date => Hash}
“‘ yml 2001-01-01:
- template_key:
- A B C D E F G
- a b c d e f g
“‘
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/jekyll-tally-tags/docs_to_items.rb', line 119 def self.insert_to_date_hash(templates, docs) date_hash = Hash.new { |h, k| h[k] = {} } docs.each do |doc| need_delete = false # 找出 第一层就是模板的 items = doc.data.select { |k, _| templates.keys.include?(k) } # 合并到第二层去 Utils.inner_merge(date_hash, doc.date.to_date, items) # 把所有的日期合并到一起 doc.data.each do |key, value| if key.is_a?(Date) need_delete = true Utils.inner_merge(date_hash, key.to_date, value) end end # 这个文档可以被处理 if need_delete || items docs.delete(doc) end end # 把模板内自带有 `keys` 的加入进来 date_hash.each do |date, hash| hash.each do |temp_key, csv_list| if templates[temp_key][KEYS] csv_list.insert(0, templates[temp_key][KEYS].join(" ")) end end end date_hash end |
.read(site) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/jekyll-tally-tags/docs_to_items.rb', line 9 def self.read(site) templates = Utils.get_templates(site.config) return unless templates date_hash = insert_to_date_hash(templates, site.posts.docs) # 排序 date_hash.sort_by { |a, b| a } items = read_csv(date_hash, templates) items = (items, site) create_items(items, site) end |
.read_csv(date_hash, templates) ⇒ Array[Hash{String => Object}]
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 106 107 |
# File 'lib/jekyll-tally-tags/docs_to_items.rb', line 61 def self.read_csv(date_hash, templates) items = [] date_hash.each do |date, hash| hash.each do |temp, csv_list| keys = nil csv_list.each_with_index do |csv, index| # 正则处理 把",,,"这样的 分割成" , , , " csv.gsub!(/[,|,|\s]+/, " , ") # lstrip rstrip 去掉前后空格 values = csv.split(',').each(&:lstrip!).each(&:rstrip!) if !keys && index == 0 keys = values next end dates = { DATES => date.to_s, YEAR => date.strftime("%Y"), MONTH => date.strftime("%m"), DAY => date.strftime("%d"), WEEKS => date.strftime("%W"), WEEK => date.strftime("%w"), } dates_array = {} dates.each { |k, v| dates_array[k] = [v] } item = { ID => items.size, PERMALINK => "/#{ID}/#{items.size}", SLUG => dates[DATES], DATE => date.to_time, TEMPLATE => temp, KEYS => keys, KEYS_EXTEND => dates.keys + keys, VALUES => values, VALUES_EXTEND => dates.values + values, LAYOUT => DEFAULT }.merge(dates_array) # 附加内容 templates[temp].keep_if { |k, _| k != KEYS }.each do |key, formatters| item[key] = Utils.format_values(values, formatters) end items << item end end end items end |