-
Notifications
You must be signed in to change notification settings - Fork 10
/
Rakefile
36 lines (30 loc) · 1.01 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
require 'rubygems'
require 'rake'
task :default => :update_toc
desc "Regenerate the table of contents to include links to newest tutorials"
task :update_toc do
require 'jekyll'
require 'tempfile'
require 'fileutils'
site = Jekyll::Site.new(Jekyll.configuration({})) and site.read
pages = site.pages.reject do |page|
page.data["ignore"]
end.sort_by! do |page|
File.mtime(File.join(FileUtils.pwd, "pages", page.name)).to_i
end.reverse
toc_path = File.join(FileUtils.pwd, "pages", "table-of-contents.md")
temp = Tempfile.new("table_of_contents.md")
begin
File.readlines(toc_path).each do |line|
line =~ /^<!--- BEGIN TOC -->$/ ? break : temp.puts(line)
end
temp.puts "<!--- BEGIN TOC -->"
pages.each_with_index do |page, index|
temp.puts "* [#{page.data["title"]}](#{page.destination('')}?ts=#{File.mtime(File.join(FileUtils.pwd, "pages", page.name)).to_i})"
end
temp.puts "<!--- END TOC -->"
FileUtils.mv(temp.path, toc_path)
ensure
temp.delete
end
end