Class: Jekyll::TallyTags::BasePage

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

Constant Summary collapse

ATTRIBUTES_FOR_LIQUID =
%w(
  docs
  type
  title
  date
  name
  path
  url
  permalink
  sum
  content
).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, titles, type, docs) ⇒ BasePage

初始化

Parameters:

  • site (Site)
  • titles (Hash{Symbol => String})
  • type (String)
  • docs (Array<Document>)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jekyll-tally-tags/pages/base_page.rb', line 27

def initialize(site, titles, type, docs)
  @site  = site
  @docs  = docs
  @type  = type
  @title = modify_title_hash(titles)

  @config = Utils.get_permalink_config(site.config)
  @slug   = slugify_string_title

  @ext  = File.extname(relative_path)
  @path = relative_path
  @name = File.basename(relative_path, @ext)

  @data = {
    LAYOUT    => layout,
    PERMALINK => @config[PERMALINKS][type]
  }

  # avg

  @content = get_content
end

Instance Attribute Details

#docsObject

Returns the value of attribute docs.



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

def docs
  @docs
end

#slugObject

Returns the value of attribute slug.



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

def slug
  @slug
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#avgObject



68
69
70
71
72
73
74
75
# File 'lib/jekyll-tally-tags/pages/base_page.rb', line 68

def avg
  avg   = []
  dates = @docs.collect { |t| t.date.to_date }.uniq
  sum.each_index do |index|
    avg[index] = Methods.to_f_s(sum[index] / dates.size)
  end
  avg
end

#dateDate

Returns:

  • (Date)


135
136
137
138
139
140
141
142
# File 'lib/jekyll-tally-tags/pages/base_page.rb', line 135

def date
  # if @title.is_a?(Hash)
  #   args = @title.values.map(&:to_i)
  #   Date.new(*args)
  # end
  # TODO: 修复日期
  "2011/01/11"
end

#filtersObject



107
108
109
# File 'lib/jekyll-tally-tags/pages/base_page.rb', line 107

def filters
  @filters
end

#get_contentObject



50
51
52
# File 'lib/jekyll-tally-tags/pages/base_page.rb', line 50

def get_content
  docs_to_yml(get_docs)
end

#inspectString

Returns:

  • (String)


155
156
157
# File 'lib/jekyll-tally-tags/pages/base_page.rb', line 155

def inspect
  "#<TagsLib:Subject @type=#{@type} @title=#{@title} @data=#{@data.inspect}>"
end

#layoutString

Returns:

  • (String)


83
84
85
# File 'lib/jekyll-tally-tags/pages/base_page.rb', line 83

def layout
  @config.dig(LAYOUTS, type) || @config[LAYOUT]
end

#modify_title_hash(titles) ⇒ Object

Parameters:

  • (Hash<Symbol => String, Array>)


122
123
124
125
126
127
128
129
130
131
132
# File 'lib/jekyll-tally-tags/pages/base_page.rb', line 122

def modify_title_hash(titles)
  temp = {}
  titles.each do |k, v|
    if v.is_a?(Array)
      temp[k] = v.join(",")
    else
      temp[k] = v
    end
  end
  temp
end

Returns:

  • (String)


112
113
114
# File 'lib/jekyll-tally-tags/pages/base_page.rb', line 112

def permalink
  data.is_a?(Hash) && data[PERMALINK]
end

#relative_pathString

Returns:

  • (String)


145
146
147
148
149
150
151
152
# File 'lib/jekyll-tally-tags/pages/base_page.rb', line 145

def relative_path
  @relative_path ||=
    begin
      path = URL.unescape_path(url).gsub(%r!^/!, "")
      path = File.join(path, "index.md") if url.end_with?("/")
      path
    end
end

#sumArray<Float>

Returns:

  • (Array<Float>)


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/jekyll-tally-tags/pages/base_page.rb', line 55

def sum
  sum = []
  get_docs.each do |doc|
    if doc.data[COUNT]
      doc.data[COUNT].each_with_index do |count, index|
        sum[index] = 0 if index == 0
        sum[index] += count.to_f
      end
    end
  end
  sum
end

#templateString

Returns:

  • (String)


78
79
80
# File 'lib/jekyll-tally-tags/pages/base_page.rb', line 78

def template
  @config.dig(PERMALINKS, type)
end

#titleString

Returns:

  • (String)


117
118
119
# File 'lib/jekyll-tally-tags/pages/base_page.rb', line 117

def title
  @title if @title.is_a?(String)
end

#urlString

Returns:

  • (String)


97
98
99
100
101
102
103
104
105
# File 'lib/jekyll-tally-tags/pages/base_page.rb', line 97

def url
  u = @url ||= URL.new(
    :template     => template,
    :placeholders => url_placeholders,
    :permalink    => nil
  ).to_s
rescue ArgumentError
  raise ArgumentError, "提供的模板 \"#{template}\" 无效."
end

#url_placeholdersHash{ String => String}

Returns eg: => “ruby”, :title => “something”.

Returns:

  • (Hash{ String => String})

    eg: => “ruby”, :title => “something”



88
89
90
91
92
93
94
# File 'lib/jekyll-tally-tags/pages/base_page.rb', line 88

def url_placeholders
  if @title.is_a? Hash
    @title.merge(:type => @type)
  else
    { :name => @slug, :type => @type }
  end
end