Class: Jekyll::TallyTags::ItemsToPage

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

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ ItemsToPage

Returns a new instance of ItemsToPage.

Parameters:

  • config (Configuration) (defaults to: nil)


39
40
41
42
# File 'lib/jekyll-tally-tags.rb', line 39

def initialize(config = nil)
  @config  = Utils.get_permalink_config(config)
  @enabled = Utils.get_permalink_config(config, ENABLED)
end

Instance Method Details

#generate(site) ⇒ Object

Parameters:

  • site (Site)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jekyll-tally-tags.rb', line 45

def generate(site)
  # 判断是否为空
  return if @config.nil?
  # 开始赋值
  @site  = site
  @posts = site.posts
  @pages = []

  read_all(@config[PERMALINKS], @site.posts.docs)
  # 把所有的拼接到到 `pages` 里面
  @site.pages.concat(@pages)
  # 配置里面也放一份
  @site.config[PAGES] = @pages
end

#read_all(permalinks, scan_docs) ⇒ Object

Parameters:

  • permalinks (Hash{String => String})
  • scan_docs (Array<Document>)


62
63
64
65
66
67
68
69
# File 'lib/jekyll-tally-tags.rb', line 62

def read_all(permalinks, scan_docs)
  permalinks.each do |key, permalink|
    # 判断 链接是否包含 :xxx :(\w+)
    matches = []
    permalink.scan(/:(\w+)/) { |match| matches << match[0] }
    read_loop(key, {}, Array.new(scan_docs), matches, 0)
  end
end

#read_any(docs_hash, symbol, permalink_key, titles, matches, index) ⇒ Object

Parameters:

  • docs_hash (Hash{String => Array<Document>})
  • symbol (Symbol)
  • permalink_key (String)
  • titles (Hash{Symbol => String})
  • matches (Array<String>)
  • index (Integer)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/jekyll-tally-tags.rb', line 101

def read_any(docs_hash, symbol, permalink_key, titles, matches, index)
  docs_hash.each do |key, docs|
    new_titles = titles.merge({ symbol => key })
    # 开启了该字段 同时 是匹配的最后一项的时候 写入数组
    if enabled?(permalink_key) && index == matches.size
      clz = SinglePage
      case permalink_key
      when YEAR
        clz = YearPage
      when MONTH
        clz = MonthPage
      when DAY
        clz = DayPage
      when WEEK
        clz = WeekPage
      when WEEKS
        clz = WeekPage
      end
      @pages << clz.new(@site, new_titles, permalink_key, docs.keep_if { |doc| doc.data[TEMPLATE] })
    end
    read_loop(permalink_key, new_titles, docs, matches, index)
  end
end

#read_loop(permalink_key, titles, docs, matches, index) ⇒ Object

Parameters:

  • permalink_key (String)
  • titles (Hash{Symbol => String})
  • docs (Array<Document>)
  • matches (Array<String>)
  • index (Integer)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/jekyll-tally-tags.rb', line 76

def read_loop(permalink_key, titles, docs, matches, index)
  if index > matches.size - 1
    return
  end
  match = matches[index]
  # 找到对应的 docs
  if DATE_HASH.keys.include?(match)
    docs_hash = date_to_docs_hash(docs, DATE_HASH[match])
    read_any(docs_hash, match.to_sym, permalink_key, titles, matches, index + 1)
  else
    begin
      docs_hash = tags_to_docs_hash(docs, match)
      read_any(docs_hash, match.to_sym, permalink_key, titles, matches, index + 1)
    rescue
      Jekyll.logger.warn CLASSIFY, "没有该keys ':#{match}'"
    end
  end
end