Class: Jekyll::TallyTags::Item

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

Instance Method Summary collapse

Constructor Details

#initialize(item, site) ⇒ Item

Returns a new instance of Item.

Parameters:

  • item (Hash)
  • site (Site)


10
11
12
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/jekyll-tally-tags/item.rb', line 10

def initialize(item, site)
  @data       = item
  @site       = site
  @path       = "#{site.collections_path}/_posts/#{item[DATE].to_date.to_s}-#{item[ID]}.md"
  @extname    = File.extname(@path)
  @collection = site.posts
  @type       = @collection.label.to_sym

  @has_yaml_header = nil

  if draft?
    categories_from_path("_drafts")
  else
    categories_from_path(collection.relative_directory)
  end

  data.default_proc = proc do |_, key|
    site.frontmatter_defaults.find(relative_path, type, key)
  end

  # 加入 link
  permalinks       = Utils.get_permalink_config(site.config, PERMALINKS)
  item[PERMALINKS] = Hash.new { |h, k| h[k] = [] }
  permalinks.each do |key, permalink|
    permalink.scan(/:(\w+)/) do |match_data|
      match = match_data[0]
      if item.keys.include?(match)
        item[match].each_with_index do |t, index|
          permalink                    = item[PERMALINKS][key][index] ? item[PERMALINKS][key][index] : String.new(permalink)
          item[PERMALINKS][key][index] = permalink.gsub(":#{match}", t.is_a?(Array) ? t.join('-') : t)
        end
      end
    end
  end
  # 获取当前配置
  template = Utils.get_templates(site.config)[item[TEMPLATE]]
  template = {}.merge(template)
  template.keep_if do |key, _|
    key != TITLE && key != COUNT
  end
  # 分析内容
  contents = []
  item[KEYS].each_with_index do |key, index|
    contents[index] = {
      KEY   => key,
      VALUE => item[VALUES][index],
    }
    template.each do |tag, indexes|
      if indexes.include? index
        contents[index][SYMBOL] = tag
        # FIXME: 这里乱序就会出问题
        contents[index][PERMALINK] = item[PERMALINKS][tag].delete_at(0)
        contents[index][MD_URL]    = to_url(contents[index][VALUE], contents[index][PERMALINK])
      end
    end
  end

  year       = item[YEAR][0]
  month      = item[MONTH][0]
  day        = item[DAY][0]
  week       = item[WEEK][0].to_i
  permalinks = item[PERMALINKS]
  # 加入时间
  date_hash      = {
    KEY    => "日期",
    VALUE  => "#{year}/#{month}/#{day}-#{WEEK_VALUE[week]}",
    SYMBOL => DATE,
    MD_URL => "#{to_url(year, permalinks[YEAR][0])}/#{to_url(month, permalinks[MONTH][0])}/#{to_url(day, permalinks[DAY][0])}-#{to_url(WEEK_VALUE[week], permalinks[WEEK][0])}"
  }
  item[CONTENTS] = [] + contents
  contents.insert(0, date_hash)
  item[DATE_TITLE]    = date_hash
  item[DATE_CONTENTS] = contents
end

Instance Method Details

#to_url(text, permalink) ⇒ Object



85
86
87
# File 'lib/jekyll-tally-tags/item.rb', line 85

def to_url(text, permalink)
  "[#{text}](#{permalink})"
end