/
related.rake
81 lines (71 loc) · 2.21 KB
/
related.rake
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'awesome_print'
# bundle exec rake related
CONTENT_SERVER = ENV["CONTENT_SERVER"] || "http://localhost:3000"
desc "Generate the related sidebar content"
task :related => :environment do
find_reference_links
find_book_links
# TODO: git-ref?
# TODO: screencasts?
# TODO: blog posts?
end
CMD_IGNORE = ['aware', 'binaries', 'ci', 'co', 'com', 'directory', 'feature',
'gitolite', 'gitosis-init', 'installed', 'last', 'library', 'my',
'mygrit', 'project', 'prune', 'rack', 'repository', 'stash-unapply',
'tarball', 'that', 'user', 'visual', 'will', 'world', 'unstage']
# book content
# - reference calls
def find_book_links
aindex = {}
book = Book.where(:code => 'en').first
book.sections.each do |section|
content = section.html
content.scan(/git (\-+[a-z\-=]+ )*([a-z][a-z\-]+)/) do |match|
next if CMD_IGNORE.include? match[1]
aindex[match[1]] ||= []
aindex[match[1]] << section.id
end
end
aindex.each do |command, ids|
command = "git-#{command}"
sec_ids = {}
ids.each do |id|
sec_ids[id] ||= 0
sec_ids[id] += 1
end
sec_ids.each do |id, score|
if section = Section.find(id)
puts "linking #{section.title} with #{command}"
from = ['book', section.title, section.slug, "/book/en/#{section.slug}", score]
to = ['reference', command, command, "/ref/#{command}", score]
RelatedItem.create_both(from, to)
end
end
end
end
# index all reference pages
# - linked to/from other pages
def find_reference_links
v = Version.latest_version
v.doc_versions.each do |dv|
f = dv.doc_file
doc = dv.doc
name = f.name
matches = doc.plain.scan(/linkgit:(.*?)\[(\d)\]/)
m = {}
matches.each do |command, number|
m[command] ||= 0
m[command] += 1
end
related = m.sort { |a, b| b[1] <=> a[1] }[0, 5]
related.each do |command, score|
next if command == name
if rdv = DocVersion.latest_for(command)
puts "linking #{name} with #{command}"
from = ['reference', name, name, "/ref/#{name}", score]
to = ['reference', command, command, "/ref/#{command}", score]
RelatedItem.create_both(from, to)
end
end
end
end