-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
138 lines (119 loc) · 3.43 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
#!/usr/bin/env ruby
require 'rake'
require 'ostruct'
require 'yaml'
require 'hpricot'
require 'tilt'
require 'haml'
require 'fileutils'
require 'pry'
task :default => :render
# These are possible values for config that can be overidden by ENV vars
# If you use a new config value, make sure to add it here
DEFAULT_CONFIG = {
:author => nil,
:description => nil,
:title => nil,
:theme => nil,
:out => 'out'
}
task :render do
# Copy reveal.js
dest = config.out
FileUtils.rm_r(dest) if Dir.exists?(dest)
FileUtils.cp_r 'reveal.js', dest
FileUtils.rm Dir.glob("#{dest}/.git*")
# Load the templates
@doc = load_template
# Modify the values
update_doc("//meta[@name=author]",config.author,"content")
update_doc("//meta[@name=description]",config.description.strip,"content")
update_doc("//title",config.title)
update_doc("//link#theme",("css/theme/%s.css" % config.theme),"href") if config.theme
puts "Rendering templates"
section_template = Tilt.new('templates/section.html.haml')
slides = []
get_slides.each do |slide|
split_slide(slide) do |path|
content = Tilt.new(path).render(self)
content.gsub!(/~~~CONFIG:(.*)~~~/) do
config.send($1)
end
slides << section_template.render(self, :content => content, :config => config)
end
end
update_doc("div.slides","\n#{slides.join("\n")}\n")
File.open(File.join(dest,'index.html'),'w') do |f|
f.write @doc.to_html
end
end
def get_slides
Dir.glob("slides/**/**").select{|file| File.file?(file) }.sort
end
def split_slide(slide)
# Use this for creating tempfiles
filetype = File.extname(slide)
# Split files in case we declare a !SLIDE
slides = File.read(slide).split(/\n(?=^!SLIDE)/)
# TODO: For now, we won't deal with slide info
slides.map!{|x| x.gsub(/^!SLIDE.*\n/,'') }
slides.each do |content|
Tempfile.open(['temp',filetype]) do |file|
file.write(content)
file.close
yield file.path
end
end
end
task :publish do
dest = config.out
if Dir.exists?(dest)
gh_pages(dest)
else
abort("Rendered directory does not exist: %s. Run `rake render` first" % dest)
end
end
def gh_pages(dir)
`git add #{dir}`
sha = `git write-tree`.chomp
tree_sha = `git rev-parse #{sha}:#{dir}`.chomp
`git read-tree HEAD` # reset staging to last-commit
ghp_sha = `git rev-parse gh-pages 2>/dev/null`.chomp
extra = ghp_sha != 'gh-pages' ? "-p #{ghp_sha}" : ''
commit_sha = `echo 'static presentation' | git commit-tree #{tree_sha} #{extra}`.chomp
`git update-ref refs/heads/gh-pages #{commit_sha}`
`git push origin gh-pages -f`
end
def git_commit(path = nil)
`git add #{path}`
`git commit -m "made commit"`
rev_parse(path)
end
def rev_parse(rev = nil)
commit = "HEAD"
commit << ":#{rev}" if rev
`git rev-parse #{commit}`.chomp
end
def config
@config ||= parse_config
end
def parse_config
puts "Parsing configuration"
# Parse the config file
config = YAML.load_file('config.yml')
# Get any env variables that also appear in the default config
env = Hash[ENV.select{|k,v| DEFAULT_CONFIG.has_key?(k.to_sym)}.map{|k,v| [k.to_sym,v]}]
# Merge them all together
merged = [DEFAULT_CONFIG,config,env].reduce({},:merge)
OpenStruct.new(merged)
end
def load_template
puts "Loading reveal.js template"
open('reveal.js/index.html'){|f| Hpricot(f)}
end
def update_doc(tag,value,attr = nil)
el = @doc.at(tag)
attr.nil? ?
el.innerHTML = value :
el.attributes[attr] = value
end