forked from sendgrid/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
197 lines (161 loc) · 5.43 KB
/
Rakefile
File metadata and controls
197 lines (161 loc) · 5.43 KB
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
require "rubygems"
require "bundler/setup"
require "nokogiri"
require "nokogiri-pretty"
require "time"
require "shellwords"
require "json"
require "simple-cloudfront-invalidator"
require "cgi"
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}
invalidator = SimpleCloudfrontInvalidator::CloudfrontClient.new(ENV["PROD_ACCESS_KEY"], ENV["PROD_SECRET_KEY"], ENV["CF_DISTRIBUTION_ID"])
puts invalidator.invalidate(public_files)
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")
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")
#don't mess with the jekyll stuff
files = FileList[htmlfiles].exclude(/_layouts/).exclude(/_includes/)
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)
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
#Validate the XML
contents.gsub!(/({%\s?codeblock lang:xml\s?%})(.*?)({\%\s?endcodeblock\s?%})/m) do |match|
begin
xml = Nokogiri.XML($2, nil, "UTF-8") { |config| config.strict }
rescue
xml_invalid += 1
puts "\nINVALID XML in #{htmlfile}: \n#{$2.strip}\n---------------------------"
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