Class: JekyllStats::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-stats/formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stats) ⇒ Formatter

Returns a new instance of Formatter.



7
8
9
# File 'lib/jekyll-stats/formatter.rb', line 7

def initialize(stats)
  @stats = stats
end

Instance Attribute Details

#statsObject (readonly)

Returns the value of attribute stats.



5
6
7
# File 'lib/jekyll-stats/formatter.rb', line 5

def stats
  @stats
end

Instance Method Details

#averages_lineObject



55
56
57
58
59
# File 'lib/jekyll-stats/formatter.rb', line 55

def averages_line
  avg = stats[:average_words]
  longest = stats[:longest_post]
  "Avg: #{avg} words | Longest: \"#{truncate(longest[:title], 30)}\" (#{format_number(longest[:words])} words)"
end

#categories_lineObject



96
97
98
99
100
# File 'lib/jekyll-stats/formatter.rb', line 96

def categories_line
  cats = stats[:categories]
  cat_strs = cats.map { |c| "#{c[:name]} (#{c[:count]})" }
  "Categories:\n  #{cat_strs.join(" | ")}"
end

#date_range_lineObject



61
62
63
64
65
66
# File 'lib/jekyll-stats/formatter.rb', line 61

def date_range_line
  first = stats[:first_post][:date]
  last = stats[:last_post][:date]
  years = stats[:years_active]
  "First: #{first} | Last: #{last} (#{years} years)"
end

#format_number(n) ⇒ Object



102
103
104
# File 'lib/jekyll-stats/formatter.rb', line 102

def format_number(n)
  n.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
end

#format_reading_time(minutes) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/jekyll-stats/formatter.rb', line 106

def format_reading_time(minutes)
  if minutes >= 60
    hours = minutes / 60
    mins = minutes % 60
    "#{hours}h #{mins}m"
  else
    "#{minutes}m"
  end
end

#frequency_lineObject



68
69
70
# File 'lib/jekyll-stats/formatter.rb', line 68

def frequency_line
  "Frequency: #{stats[:posts_per_month]} posts/month"
end

#post_summaryObject



48
49
50
51
52
53
# File 'lib/jekyll-stats/formatter.rb', line 48

def post_summary
  total = stats[:total_posts]
  words = format_number(stats[:total_words])
  time = format_reading_time(stats[:reading_minutes])
  "Posts: #{total} (#{words} words, ~#{time} read time)"
end

#posts_by_year_chartObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/jekyll-stats/formatter.rb', line 72

def posts_by_year_chart
  years = stats[:posts_by_year]
  return "" if years.empty?

  max_count = years.map { |y| y[:count] }.max
  bar_width = 20

  lines = ["Posts by Year:"]
  years.first(10).each do |year_data|
    year = year_data[:year]
    count = year_data[:count]
    bar_length = ((count.to_f / max_count) * bar_width).round
    bar = "\u2588" * bar_length
    lines << "  #{year}: #{bar} #{count}"
  end
  lines.join("\n")
end

#to_terminalObject



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
# File 'lib/jekyll-stats/formatter.rb', line 11

def to_terminal
  return "No posts found." if stats[:total_posts].zero?

  lines = []
  lines << ""
  lines << "\u{1F4CA} Site Statistics"
  lines << "\u2500" * 35

  lines << post_summary
  lines << averages_line
  lines << date_range_line
  lines << frequency_line

  lines << ""
  lines << posts_by_year_chart

  if stats[:tags].any?
    lines << ""
    lines << top_tags
  end

  if stats[:categories].any?
    lines << ""
    lines << categories_line
  end

  if stats[:drafts_count].positive?
    lines << ""
    lines << "Drafts: #{stats[:drafts_count]}"
  end

  lines << "\u2500" * 35
  lines << ""

  lines.join("\n")
end

#top_tagsObject



90
91
92
93
94
# File 'lib/jekyll-stats/formatter.rb', line 90

def top_tags
  tags = stats[:tags].first(10)
  tag_strs = tags.map { |t| "#{t[:name]} (#{t[:count]})" }
  "Top #{[10, stats[:tags].size].min} Tags:\n  #{tag_strs.join(" | ")}"
end

#truncate(str, length) ⇒ Object



116
117
118
119
120
# File 'lib/jekyll-stats/formatter.rb', line 116

def truncate(str, length)
  return str if str.length <= length

  "#{str[0, length]}..."
end