forked from illinois-cs241/illinois-cs241.github.io
/
Rakefile
179 lines (158 loc) · 4.13 KB
/
Rakefile
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Including only the changed build task
require 'rake'
require 'jekyll'
require 'html-proofer'
require 'nokogiri'
require 'open-uri'
require 'json'
require 'optparse'
require 'jemoji'
require 'rake'
require 'pigments'
require 'htmlentities'
require_relative '_scripts/spell_check.rb'
require 'etc'
require 'yaml'
require 'fileutils'
is_travis = ENV['TRAVIS'] == 'true'
main_json_file = '_data/man.json'
coursebook_dir = '_coursebook'
$config = Jekyll.configuration({
:source => './',
:destination => './_site',
:timezone => 'America/Chicago',
:safe => false,
})
multitask default: [
'pre_build:gen_man',
'pre_build:gen_coursebook',
] do
site = Jekyll::Site.new($config)
Jekyll::Commands::Build.build site, $config
cp './.travis.yml', './_site/.travis.yml'
cp './CNAME', './_site/CNAME'
end
multitask serve: [
'default',
] do
site = Jekyll::Site.new($config)
Jekyll::Commands::Serve.process $config
end
namespace :pre_build do
desc 'Houses all pre build tasks'
sections = [1, 2, 3, 4]
base_uri = 'https://linux.die.net/man/'
cache_time = 30 # days
task :gen_man, [:file] do |_t, args|
file = args[:file]
if file.nil?
file = main_json_file
puts "Using default file #{file}"
end
# Man pages don't change that often
if File.exist?(file) && ((File.mtime(file) <=> DateTime.now - cache_time) == 1)
puts 'Using cached file'
next
end
puts 'Updating file'
urls = sections.map do |e|
base_uri + e.to_s + '/'
end
output = {}
urls.each do |url|
page = Nokogiri::HTML(open(url))
page.css('dt a').each do |link|
output[link.inner_html] = url + link['href']
end
end
file_opts = File::RDWR | File::CREAT
File.open(file, file_opts, 0o644) do |f|
f.truncate 0
f.write(JSON.fast_generate(output))
puts "Successfully wrote #{file}"
end
end
def title_from_html(text)
file_no_ext = File.basename(text, '.md')
file_no_ext.tr('-', ' ')
end
def link_patterns(file, pattern_map)
f = File.open(file, 'r')
contents = f.read
f.close
new_contents = contents
pattern_map.each do |link, pattern|
new_contents = new_contents.gsub(pattern, link)
end
f = File.open(file, File::RDWR)
f.seek(0)
f.write(new_contents)
f.close
end
def prepend(file, string)
obj_file = Tempfile.new('')
f = File.open(file, 'r')
begin
obj_file.write(string)
obj_file.write(f.read)
f.close
obj_file.close
FileUtils.cp(obj_file.path, file)
ensure
obj_file.unlink
end
end
coursebook_dir = "_coursebook"
task :gen_coursebook, [:folder] do |_t, args|
folder = args[:folder]
if folder.nil?
folder = coursebook_dir
puts "Using default Folder #{folder}"
end
system "cd #{coursebook_dir} && git clean -fq && git reset --hard HEAD"
Dir.glob("#{coursebook_dir}/*md").each do |file|
file_contents = File.read(file)
matches = file_contents.match(/(.*)\n={3,}/)
if matches
page_title = matches.captures[0]
else
page_title = File.basename(file, '.md')
end
meta = {
'layout' => 'doc',
'title' => page_title,
'toc' => false,
}
prepend(file, "#{meta.to_yaml}\n---\n\n")
end
FileUtils.mv("#{coursebook_dir}/Home.md", "#{coursebook_dir}/index.md")
end
end
task :test_html do
Dir.mktmpdir do |dir|
to_copy = Dir.glob('_site/*html')
to_copy += ['images', './images', '_site/js', '_site/css', '_site/resources', '_site/slides/']
FileUtils.cp_r(to_copy, dir)
options = {
assume_extension: true,
allow_hash_href: true,
href_ignore: [
'#',
'?'
],
http_status_ignore: [0],
url_ignore: [/https:\/\/github.com\/angrave\/SystemProgramming\/wiki\//, /coursebook/],
parallel: { in_processes: 3 }
}
HTMLProofer.check_directory(dir, options).run
end
end
task :spell_check do
md_files = Dir.glob('*.md')
md_files += Dir.glob('_docs/*.md')
open_dictionaries do |dicts|
Parallel.map(md_files, in_threads: Etc.nprocessors) do |md_file|
check_spelling(md_file, dicts)
end
end
end