forked from bitcoin-dot-org/Bitcoin.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomparelinks.rb
98 lines (77 loc) · 2.13 KB
/
comparelinks.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# This file is licensed under the MIT License (MIT) available on
# http://opensource.org/licenses/MIT.
## This script is used to compare all links between two branches of the
## website. Each branches are built and compared.
## Example: ruby ./_contrib/comparelinks.rb master newbranch > ../diff
require 'tmpdir'
def prompt(*args)
print(*args)
gets
end
if !ARGV.empty? && !ARGV[0].empty? && !ARGV[1].empty?
srcbr = ARGV[0]
dstbr = ARGV[1]
else
print "Usage: comparelinks.rb oldbranch newbranch \n"
exit
end
if !File.exist?('_config.yml')
print "Wrong working directory. \n"
exit
end
def fetchlinks()
# Fetch new list of links
links = {}
dirs = Dir.glob(WORKDIR + "/_site/**/*.html").each { |file|
content = File.read(file)
content.scan(/ href *= *"(.*?)"/).each { |link|
link = link[0].to_s.gsub(/^(https?:\/\/(www\.)?bitcoin\.org)?\//,'/')
next if (link.match(/^#|^http:\/\/www.meetup.com\//))
if(!link.match(/^https?:\/\/|^\/[^\/]|^mailto:/))
link = File.dirname(file.sub(WORKDIR + '/_site','')) + '/' + File.basename(link)
end
links[link] = "0"
}
content.scan(/ src *= *"(.*?)"/).each { |link|
link = link[0].to_s.gsub(/^(https?:\/\/(www\.)?bitcoin\.org)?\//,'/')
links[link] = "0"
}
}
return links
end
Dir.mktmpdir{|workdir|
WORKDIR=workdir.gsub("\n",'')
# Copy current repository to a temporary directory
`rsync -a ./ "#{WORKDIR}/"`
# Build both version of the website and fetch all links
oldlinks = {}
newlinks = {}
Dir.chdir(WORKDIR) do
`git checkout #{srcbr}`
`jekyll`
oldlinks = fetchlinks()
`git checkout #{dstbr}`
`jekyll`
newlinks = fetchlinks()
end
# Find added links, removed links
diffaddlinks = []
diffrmlinks = []
newlinks.each { |link, etag|
next if oldlinks.has_key?(link)
diffaddlinks.push(link)
}
oldlinks.each { |link, etag|
next if newlinks.has_key?(link)
diffrmlinks.push(link)
}
# Display resulting diff
diff = ''
if diffaddlinks.length > 0
diff = diff + "## links added\n\n" + diffaddlinks.join("\n") + "\n\n"
end
if diffrmlinks.length > 0
diff = diff + "## links removed\n\n" + diffrmlinks.join("\n") + "\n\n"
end
print diff
}