-
Notifications
You must be signed in to change notification settings - Fork 6
/
make_redirects.rb
92 lines (77 loc) · 1.86 KB
/
make_redirects.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
require 'optparse'
require 'json'
FILE_PATTERNS = [
'*.html',
'*.htm',
'*.DOCX',
'*.GIF',
'*.JPG',
'*.ai',
'*.bmp',
'*.doc',
'*.docx',
'*.gif',
'*.jpe',
'*.jpeg',
'*.jpg',
'*.pdf',
'*.png',
'*.psd',
'*.svg',
'*.thmx',
'*.tiff',
'*.txt',
'*.wpd',
].freeze
def parse_options
options = {}
OptionParser.new do |opts|
opts.banner = 'Usage: example.rb [options]'
opts.on('-sSRC', '--src=SRC', 'The directory to read from (default: ../ada)') do |src|
options[:src] = src
end
opts.on('-dDEST', '--dest=DEST', 'Set an output file (default: .') do |dest|
options[:dest] = dest
end
opts.on('-pPREFIX', '--prefix=PREFIX', 'Set a prefix (default: https://archive.ada.gov)') do |prefix|
options[:prefix] = prefix
end
opts.on('-h', '--help', 'Prints this help') do
puts opts
exit
end
end.parse!
options[:dest] ||= '.'
options[:src] ||= '../ada'
options[:prefix] ||= 'https://archive.ada.gov'
options
end
def get_files_to_redirect(options)
oldpwd = Dir.getwd
Dir.chdir(options[:src])
files = FILE_PATTERNS.flat_map do |pattern|
Dir.glob('**/' << pattern)
end
Dir.chdir(oldpwd)
files
end
def make_map(files, options)
files.map do |file|
['/' << file, File.join(options[:prefix], file)]
end.sort.to_h
end
def write_redirects(redirects, dest, options, prefix = '', suffix = '')
redirects_json = JSON.pretty_generate(redirects)
File.open(dest, 'w') do |file|
file.write(prefix << redirects_json << suffix)
end
puts "Overwrote #{dest} with #{redirects.length} redirects to #{options[:prefix]} from #{options[:src]}"
end
def main
options = parse_options
dest = File.join(options[:dest], '_data/generated_redirects.json')
files = get_files_to_redirect(options)
redirects = make_map(files, options)
write_redirects(redirects, dest, options)
end
main