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



65
66
67
68
69
# File 'lib/jekyll-stats/formatter.rb', line 65

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



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

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

#date_range_lineObject



71
72
73
74
75
76
# File 'lib/jekyll-stats/formatter.rb', line 71

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



130
131
132
# File 'lib/jekyll-stats/formatter.rb', line 130

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

#format_reading_time(minutes) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/jekyll-stats/formatter.rb', line 134

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



78
79
80
# File 'lib/jekyll-stats/formatter.rb', line 78

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

#most_linked_postsObject



112
113
114
115
116
117
118
119
# File 'lib/jekyll-stats/formatter.rb', line 112

def most_linked_posts
  links = stats[:internal_links].first(5)
  lines = ["Most Linked Posts:"]
  links.each do |l|
    lines << "  #{l[:inbound_count].to_s.rjust(2)}  #{truncate(l[:title], 50)}"
  end
  lines.join("\n")
end

#post_summaryObject



58
59
60
61
62
63
# File 'lib/jekyll-stats/formatter.rb', line 58

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/jekyll-stats/formatter.rb', line 82

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
47
48
49
50
51
52
53
54
55
56
# 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[:internal_links].any?
    lines << ""
    lines << most_linked_posts
  end

  if stats[:external_links][:domains].any?
    lines << ""
    lines << top_external_domains
  end

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

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

  lines.join("\n")
end

#top_external_domainsObject



121
122
123
124
125
126
127
128
# File 'lib/jekyll-stats/formatter.rb', line 121

def top_external_domains
  ext = stats[:external_links]
  lines = ["External Links: #{format_number(ext[:total])} (#{format_number(ext[:unique])} unique, #{ext[:domains].size} hosts)"]
  ext[:domains].first(5).each do |d|
    lines << "  #{d[:count].to_s.rjust(4)}  #{d[:host]} (#{d[:posts]} posts)"
  end
  lines.join("\n")
end

#top_tagsObject



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

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



144
145
146
147
148
# File 'lib/jekyll-stats/formatter.rb', line 144

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

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