-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck_tags.rb
57 lines (35 loc) · 1.1 KB
/
check_tags.rb
1
2
3
4
5
6
7
8
9
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
require 'find'
require 'yaml'
allowed_tags = YAML.safe_load(File.read('_data/tags.yml'))['allowed-tags']
tag_usage_numbers = Hash.new(0)
puts "\n"
out_lines = []
Find.find('./pages') do |file_name|
next unless file_name.end_with?('.md') || file_name.end_with?('.html')
text = File.read(file_name)
m = text.match(/^---\r?\n([\s\S]+)\r?\n---/)
tags = m && YAML.safe_load(m[1])['tags']
next unless tags
tags.each { |t| tag_usage_numbers[t] += 1 }
disallowed = tags.reject { |t| allowed_tags.include? t }
next if disallowed.empty?
out_lines << "* #{file_name}\n #{disallowed.join('', '')}"
end
puts "---\n\n"
unless out_lines.empty?
puts "# Invalid tags\n\n"
puts out_lines.join("\n\n")
puts "\n---\n\n"
end
unused_tags = allowed_tags.filter { |t| tag_usage_numbers[t].zero? }
unless unused_tags.empty?
puts "# Unused tags\n\n"
puts unused_tags.map { |l| '* ' + l } .join("\n")
puts "\n---\n\n"
end
puts "# Page count by tag\n\n"
tag_usage_numbers
.to_a
.sort_by { |kv| 0 - kv[1] }
.each { |kv| puts "* #{kv[0]} - #{kv[1]}" }
puts "\n"