forked from sendgrid/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
301 lines (241 loc) · 8.28 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
require "rubygems"
require "bundler/setup"
require "nokogiri"
require "nokogiri-pretty"
require "time"
require "shellwords"
require "json"
require "simple-cloudfront-invalidator"
require "cgi"
require "indextank"
public_dir = "public" # compiled site directory
source_dir = "source" # source file directory
server_port = "4000" # port for preview server eg. localhost:4000
#######################
# Working with Jekyll #
#######################
desc "Generate jekyll site"
task :generate do
raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
puts "## Generating Site with Jekyll"
system "jekyll build"
end
desc "Watch the site and regenerate when it changes"
task :watch do
raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
puts "Starting to watch source with Jekyll and Compass."
jekyllPid = Process.spawn({"OCTOPRESS_ENV"=>"preview"}, "jekyll build --watch")
trap("INT") {
[jekyllPid].each { |pid| Process.kill(9, pid) rescue Errno::ESRCH }
exit 0
}
[jekyllPid].each { |pid| Process.wait(pid) }
end
#######################
desc "preview the site in a web browser"
task :preview do
raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
jekyllPid = Process.spawn({"OCTOPRESS_ENV"=>"preview"}, "jekyll serve --watch")
trap("INT") {
[jekyllPid].each { |pid| Process.kill(9, pid) rescue Errno::ESRCH }
exit 0
}
[jekyllPid].each { |pid| Process.wait(pid) }
end
desc "Clean out caches: .pygments-cache, .gist-cache, .sass-cache"
task :clean do
rm_rf [".pygments-cache/**", ".gist-cache/**", ".sass-cache/**", "source/stylesheets/screen.css"]
end
desc "list tasks"
task :list do
puts "Tasks: #{(Rake::Task.tasks - [Rake::Task[:list]]).join(', ')}"
puts "(type rake -T for more detail)\n\n"
end
#####################
# Added by SendGrid #
#####################
desc "Rewrite all github style backtick code blocks with more explicit codeblocks"
task :codeblocks do
htmlfiles = File.join("**", "source", "**", "*.html")
#don't mess with the jekyll stuff
files = FileList[htmlfiles].exclude(/_layouts/).exclude(/_includes/)
files.each do |htmlfile|
next if htmlfile == '.' or htmlfile == '..'
puts "Converting code blocks: " + htmlfile
file = File.open(htmlfile)
contents = file.read
output = "";
i=0
contents.each_line do |line|
if line.index('```')
if i % 2 == 0
puts 'found one: ' + line
line = line.gsub(/(```)\s(.*)/,'{% codeblock lang:\2 %}')
puts 'now: ' + line
else
puts 'found one: ' + line
line = line.gsub(/(```)/,"{% endcodeblock %}")
puts 'now: ' + line
end
i=i+1
end
output = output + line
end
file = File.new(htmlfile,"w")
file.write(output)
end
end
desc "invalidate all files in /public on cloudfront"
task :invalidate_cloudfront do
Dir.chdir('public')
public_files = Dir.glob("**/*.html")
cleaned_files = Array.new
public_files.delete_if{|f| File.directory?(f) }
public_files.each{|f| cleaned_files.push '/' + f}
if ENV["TRAVIS_BRANCH"] == "master"
cf_distro_id = ENV["PROD_CLOUDFRONT_DISTRO"]
else
cf_distro_id = ENV["DEV_CLOUDFRONT_DISTRO"]
end
invalidator = SimpleCloudfrontInvalidator::CloudfrontClient.new(ENV["PROD_ACCESS_KEY"], ENV["PROD_SECRET_KEY"], cf_distro_id)
puts invalidator.invalidate(public_files)
end
desc "index the generated files"
task :index do
htmlfiles = File.join("**", "public", "**", "*.html")
#don't mess with the jekyll stuff
files = FileList[htmlfiles].exclude(/_layouts/).exclude(/_includes/).exclude(/_assets/)
puts 'Indexing pages...'
@storage_dir = File.join(Dir.pwd, '.jekyll_indextank')
@last_indexed_file = File.join(@storage_dir, 'last_index')
begin
Dir.mkdir(@storage_dir) unless File.exists?(@storage_dir)
rescue SystemCallError
puts 'WARNING: cannot create directory to store index timestamps.'
end
begin
@last_indexed = File.open(@last_indexed_file, "rb") {|f| Marshal.load(f)}
rescue
@last_indexed = nil
end
api = IndexTank::Client.new(ENV["INDEXTANK_API_URL"])
@index = api.indexes(ENV["INDEXTANK_INDEX"])
while not @index.running?
# wait for the indextank index to get ready
sleep 0.5
end
files.each do |htmlfile|
next if htmlfile.include?("index.html")
doc = Nokogiri::HTML(File.open(htmlfile))
#remove some elements we don't want to index
doc.search('article code, article code-button').remove
#we only want to index the contents of certain elements in the <article> tag
elements = doc.search('article a, article h1, article h2, article h3, article h4, article h5, article h6, article p, article td').map {|e| e.text}
page_text = elements.join(" ").gsub("\r"," ").gsub("\n"," ")
url = htmlfile.gsub('public','')
@index.document(url).add({
:text => page_text,
:title => doc.title
})
puts 'Indexed ' << htmlfile
end
@last_indexed = Time.now
begin
File.open(@last_indexed_file, 'w') {|f| Marshal.dump(@last_indexed, f)}
rescue
puts 'WARNING: cannot write indexed timestamps file.'
end
puts 'Indexing done'
end
desc "run linklint and fail if errors found"
task :linklint do
puts "Running linklint"
puts `./linklint-2.3.5 @linklint_command`
if File.exist?("linklint_logs/error.txt")
puts File.read("linklint_logs/errorX.txt")
fail "Linklint found broken links or missing files!"
end
end
desc "parse XML and JSON codeblocks and identify invalid blocks"
task :validate_json_xml do
htmlfiles = File.join("**", "source", "**", "*.{html,md}")
#don't mess with the jekyll stuff
files = FileList[htmlfiles].exclude(/_layouts/).exclude(/_includes/).exclude(/_assets/)
json_invalid = 0
json_valid =0
xml_invalid = 0
xml_valid = 0
files.each do |htmlfile|
#hacky. we should read excluded files from a config somewhere
if htmlfile.scan("nodejs").length > 0
next
end
file = File.open(htmlfile, "r:UTF-8")
contents = file.read
file.close
#Validate JSON
contents.gsub!(/({%\s?codeblock lang:json\s?%})(.*?)({\%\s?endcodeblock\s?%})/m) do |match|
is_json = ($2.strip).to_s.is_json?
json = is_json ? JSON.parse($2.strip) : $2
if is_json
json_valid += 1
else
puts "\nINVALID JSON in #{htmlfile}: \n#{$2.strip}\n--------------------------"
json_invalid += 1
end
end
contents.gsub!(/({%\s?response json\s?%})(.*?)({\%\s?endresponse\s?%})/m) do |match|
is_json = ($2.strip).to_s.is_json?
json = is_json ? JSON.parse($2.strip) : $2
if is_json
json_valid += 1
else
puts "\nINVALID JSON in #{htmlfile}: \n#{$2.strip}\n--------------------------"
json_invalid += 1
end
end
#Validate the XML
contents.gsub!(/({%\s?codeblock lang:xml\s?%})(.*?)({\%\s?endcodeblock\s?%})/m) do |match|
begin
xml = $2.to_s.strip
xml = Nokogiri::XML(xml) { |config| config.strict }
rescue Nokogiri::XML::SyntaxError => e
xml_invalid += 1
puts "\nINVALID XML in #{htmlfile}: \n#{$2.to_s.strip}\n"
puts "caught exception: #{e}"
puts "---------------------------"
next
end
xml_valid += 1
end
contents.gsub!(/({%\s?response xml\s?%})(.*?)({\%\s?endreponse\s?%})/m) do |match|
begin
xml = $2.to_s.strip
xml = Nokogiri::XML(xml) { |config| config.strict }
rescue Nokogiri::XML::SyntaxError => e
xml_invalid += 1
puts "\nINVALID XML in #{htmlfile}: \n#{$2.to_s.strip}\n"
puts "caught exception: #{e}"
puts "---------------------------"
next
end
xml_valid += 1
end
end
puts " Valid JSON blocks: #{json_valid}"
puts "Invalid/non-JSON blocks: #{json_invalid}"
puts " Valid XML blocks: #{xml_valid}"
puts " Invalid XML blocks: #{xml_invalid}"
if xml_invalid + json_invalid > 0
fail "#{json_invalid} invalid JSON and #{xml_invalid} invalid XML block(s) found."
end
end
class String
def is_json?
begin
!!JSON.parse(self)
rescue
false
end
end
end