Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 213 lines (193 sloc) 6.904 kB
bbb815f @runpaint Rakefile: clobber output files before making PDF.
authored
1 require 'rake/clean'
721314f @runpaint Generate web version of book in HTML.
authored
2 require 'hpricot'
1581494 @runpaint Rakefile: Use ERB instead of Erubis for .deb.
authored
3 require 'erb'
bbb815f @runpaint Rakefile: clobber output files before making PDF.
authored
4
f79cd1f @runpaint Rakefile: Copy images to output/; tweak logic.
authored
5 SOURCE_HTML = FileList['text/**/*.html']
721314f @runpaint Generate web version of book in HTML.
authored
6 WWW_HTML = FileList['output/**/**/*.html']
f79cd1f @runpaint Rakefile: Copy images to output/; tweak logic.
authored
7 IMAGES = FileList['images/*']
dc815dc @runpaint Rakefile: constantised name of HTML output file.
authored
8 OUTPUT_HTML = 'output/all.html'
612e433 @runpaint Rakefile: clobber deb/ .
authored
9 CLOBBER.include('output','deb')
4663b0b @runpaint Created Rakefile for generating PDF.
authored
10
1581494 @runpaint Rakefile: Use ERB instead of Erubis for .deb.
authored
11 def template(filename,hash)
12 ERB.new(File.open(filename).read).result(
13 OpenStruct.new(hash).send(:binding)
14 )
15 end
16
f79cd1f @runpaint Rakefile: Copy images to output/; tweak logic.
authored
17 directory "output"
18
9f5cf90 @runpaint Rakefile: add descriptions for tasks.
authored
19 desc "Copy images to output directory"
f79cd1f @runpaint Rakefile: Copy images to output/; tweak logic.
authored
20 task :images => :output
21 task :images => IMAGES do |t|
22 IMAGES.each do |image|
23 cp image, "output"
24 end
25 end
26
9f5cf90 @runpaint Rakefile: add descriptions for tasks.
authored
27 desc "Combine source HTML into single HTML file"
83f4642 @runpaint Rakefile: Remove :ilinks task.
authored
28 task OUTPUT_HTML => [:output, :images]
bd8beb0 @runpaint Rakefile: use SOURCE_HTML consistently.
authored
29 file OUTPUT_HTML => SOURCE_HTML do |t|
4663b0b @runpaint Created Rakefile for generating PDF.
authored
30 File.open(t.name,'w') do |out|
f79cd1f @runpaint Rakefile: Copy images to output/; tweak logic.
authored
31 SOURCE_HTML.sort.each do |source|
4663b0b @runpaint Created Rakefile for generating PDF.
authored
32 out.puts File.open(source).read
33 end
34 end
35 end
36
dc815dc @runpaint Rakefile: constantised name of HTML output file.
authored
37 file 'output/vim-recipes.pdf' => OUTPUT_HTML do |t|
4663b0b @runpaint Created Rakefile for generating PDF.
authored
38 system("prince #{t.prerequisites.first} #{t.name}")
39 end
40
9f5cf90 @runpaint Rakefile: add descriptions for tasks.
authored
41 desc "Generate the PDF"
bbb815f @runpaint Rakefile: clobber output files before making PDF.
authored
42 task :pdf => [:clobber, 'output/vim-recipes.pdf']
5871e9e @runpaint Add :ilinks task to display broken internal links.
authored
43
1155b9a @runpaint Rakefile: Generate a Sitemap, ping search engines.
authored
44 desc "Generate the Sitemap"
45 task :sitemap do
46 sh "sitemap_gen.py --config=sitemap_config.xml --testing"
47 end
48
49 desc "Notify search engines about Sitemap"
50 task :sitemap_notify do
51 sh "sitemap_gen.py --config=sitemap_config.xml"
52 end
53
1186956 @runpaint Generate Atom feed, and link to it from pages.
authored
54 def commit_time(file)
5cbe7e4 @runpaint Rakefile: Build on systems without Git.
authored
55 begin
56 Time.at(`git log -r --name-only --no-color --pretty=raw -z #{file}`.
1186956 @runpaint Generate Atom feed, and link to it from pages.
authored
57 to_a.grep(/^committer/).last.match(/ (\d+) /)[1].to_i)
5cbe7e4 @runpaint Rakefile: Build on systems without Git.
authored
58 rescue
59 $stderr.puts "Couldn't get commit time for #{file}"
60 Time.now
61 end
1186956 @runpaint Generate Atom feed, and link to it from pages.
authored
62 end
63
721314f @runpaint Generate web version of book in HTML.
authored
64 def make_toc
65 toc = []
66 section = 'Preliminaries'
67 section_id = 'preliminaries'
68 SOURCE_HTML.sort.each do |f|
69 source = File.open(f).read
70 doc = Hpricot(source)
8108378 @runpaint Rakefile: Store subsections in ToC; fix '#' links.
authored
71 recipe_id = nil
72 doc.search('h1, h2, h3, h4, h5, h6').each do |h_tag|
1186956 @runpaint Generate Atom feed, and link to it from pages.
authored
73 title = h_tag ? h_tag.inner_text : 'Untitled'
8108378 @runpaint Rakefile: Store subsections in ToC; fix '#' links.
authored
74 id = h_tag['id'] if h_tag
75 next unless id && h_tag
76 type = :subsection
77 if h_tag.name == 'h2'
78 section = title
79 section_id = h_tag['id']
80 type = :section
81 elsif h_tag.name == 'h3'
82 type = :recipe
83 recipe_id = h_tag['id']
84 end
1186956 @runpaint Generate Atom feed, and link to it from pages.
authored
85 toc << { :id => h_tag['id'], :title => title, :file => f,
8108378 @runpaint Rakefile: Store subsections in ToC; fix '#' links.
authored
86 :type => type, :section_name => section, :section_id => section_id,
1186956 @runpaint Generate Atom feed, and link to it from pages.
authored
87 :recipe_id => recipe_id, :time => commit_time(f),
88 :target_path => "output/#{section_id}/#{h_tag['id']}/index.html" }
8108378 @runpaint Rakefile: Store subsections in ToC; fix '#' links.
authored
89 end
721314f @runpaint Generate web version of book in HTML.
authored
90 end
91 toc
92 end
93
94 desc "Generate the HTML version"
95 task :html => SOURCE_HTML do |t|
96 toc = make_toc()
97 toc.each_with_index do |entry,idx|
8108378 @runpaint Rakefile: Store subsections in ToC; fix '#' links.
authored
98 next if entry[:type] == :subsection
721314f @runpaint Generate web version of book in HTML.
authored
99 source = File.open(entry[:file]).read
100 doc = Hpricot(source)
101
102 if (entry[:type] == :section) || (entry[:id] == 'introduction')
1581494 @runpaint Rakefile: Use ERB instead of Erubis for .deb.
authored
103 page = template('templates/chapter.html',
104 {:title => entry[:id] == 'introduction' ? 'Preliminaries' : entry[:title],
105 :recipes => toc.select do |e|
721314f @runpaint Generate web version of book in HTML.
authored
106 (e[:section_id] == entry[:section_id]) &&
107 (e[:type] == :recipe)
108 end
1581494 @runpaint Rakefile: Use ERB instead of Erubis for .deb.
authored
109 })
721314f @runpaint Generate web version of book in HTML.
authored
110 path = "output/#{entry[:section_id]}/index.html"
111 mkdir_p File.dirname(path)
112 File.open(path,'w'){|f| f.puts page}
960f158 @runpaint Rakefile: Fix logic bug stopping Introduction gen.
authored
113 end
114 if entry[:type] == :recipe
721314f @runpaint Generate web version of book in HTML.
authored
115 doc.search('h1, h2, h3, h4, h5, h6').each do |tag|
116 new_tag = tag.name.sub(/(\d)/) {|m| m.to_i - 2}
8108378 @runpaint Rakefile: Store subsections in ToC; fix '#' links.
authored
117 tag.swap("<#{new_tag} id='#{tag['id']}'>#{tag.inner_html}</#{new_tag}>")
721314f @runpaint Generate web version of book in HTML.
authored
118 end
119
120 doc.search('a[@href*=#]').each do |a|
121 id = a['href'].sub(/^#/,'')
122 sections = toc.select {|e| e[:id] == id}
8108378 @runpaint Rakefile: Store subsections in ToC; fix '#' links.
authored
123 raise "Broken link (#{a}) in #{entry[:file]}" if sections.empty?
721314f @runpaint Generate web version of book in HTML.
authored
124 next unless sections.size == 1
8108378 @runpaint Rakefile: Store subsections in ToC; fix '#' links.
authored
125 sec = sections.first
126 target = sec[:section_id] + '/' + id + '/'
127 if sec[:type] == :subsection
128 target = sec[:section_id] + '/' + sec[:recipe_id] + '/#' + sec[:id]
129 end
130 a.swap("<a href='/#{target}'>#{a.inner_html}</a>")
721314f @runpaint Generate web version of book in HTML.
authored
131 end
132
133 doc.search('img').each do |img|
134 # FIXME: Awful hack: junks other attributes, assumes images to be in
135 # root directory.
136 img.swap("<img src='/#{img['src']}' />")
137 end
138
139 nxt = idx + 1 >= toc.size ? toc[0] : toc[(idx + 1)..-1].select{
140 |e| e[:type] == :recipe}.first
141 prv = idx == 0 ? toc[-1] : toc[0..(idx - 1)].select{
142 |e| e[:type] == :recipe}[-1]
1581494 @runpaint Rakefile: Use ERB instead of Erubis for .deb.
authored
143 page = template('templates/recipe.html',
721314f @runpaint Generate web version of book in HTML.
authored
144 {:body => doc.to_s, :title => entry[:title], :id => entry[:id],
1581494 @runpaint Rakefile: Use ERB instead of Erubis for .deb.
authored
145 :section_id => entry[:section_id], :section => entry[:section_name],
146 :next_e => nxt, :prev_e => prv})
1186956 @runpaint Generate Atom feed, and link to it from pages.
authored
147 #FIXME: Use :target_path instead:
721314f @runpaint Generate web version of book in HTML.
authored
148 path = "output/#{entry[:section_id]}/#{entry[:id]}/index.html"
149 mkdir_p File.dirname(path)
150 File.open(path,'w') {|file| file.puts page}
1186956 @runpaint Generate Atom feed, and link to it from pages.
authored
151 entry[:body] = doc.to_s
721314f @runpaint Generate web version of book in HTML.
authored
152 end
153 end
1581494 @runpaint Rakefile: Use ERB instead of Erubis for .deb.
authored
154 page = template('templates/toc.html',
155 {:toc => toc.dup.reject{|e| e[:type] == :subsection}})
721314f @runpaint Generate web version of book in HTML.
authored
156 mkdir_p 'output/toc'
157 File.open('output/toc/index.html','w') {|file| file.puts page}
8f68074 @runpaint Feed: Use full URL for entry links; set <updated>.
authored
158 recipes_by_time = toc.reject{|e| e[:type] == :subsection}.
159 sort_by{|e| e[:time]}.reverse
1581494 @runpaint Rakefile: Use ERB instead of Erubis for .deb.
authored
160 page = template('templates/atom.atom',
161 {:toc => recipes_by_time, :updated => recipes_by_time.first[:time]})
1186956 @runpaint Generate Atom feed, and link to it from pages.
authored
162 File.open('output/index.atom','w') {|file| file.puts page}
721314f @runpaint Generate web version of book in HTML.
authored
163 end
164
165 directory 'output/css'
26c0e85 @runpaint Rakefile: tasks for making and uploading the site.
authored
166 desc "Generate the website"
721314f @runpaint Generate web version of book in HTML.
authored
167 task :www => ['output/vim-recipes.pdf',:html, 'output/css'] do
130e63e @runpaint Rakefile: Copy .htaccess into output/.
authored
168 FileList['www/*', 'www/.[a-z]*'].each {|f| cp f, 'output/'}
721314f @runpaint Generate web version of book in HTML.
authored
169 File.open('output/css/style.css','w') do |merged|
231bd2b @runpaint Rakefile: Update to merge main.css and web.css.
authored
170 ['main','web'].each do |name|
721314f @runpaint Generate web version of book in HTML.
authored
171 merged.print File.open('templates/' + name + '.css').read
172 end
173 end
231bd2b @runpaint Rakefile: Update to merge main.css and web.css.
authored
174 cp_r 'js', 'output/'
26c0e85 @runpaint Rakefile: tasks for making and uploading the site.
authored
175 end
176
177 desc "Upload the website"
1155b9a @runpaint Rakefile: Generate a Sitemap, ping search engines.
authored
178 task :upload => [:www, :sitemap] do
e319454 @runpaint Rakefile: PDF task shouldn't delete its input.
authored
179 rm OUTPUT_HTML
721314f @runpaint Generate web version of book in HTML.
authored
180 sh "rsync -vaz output/ vim.runpaint.org:/home/public/"
1155b9a @runpaint Rakefile: Generate a Sitemap, ping search engines.
authored
181 Rake::Task['sitemap_notify'].invoke
1e21ca4 @runpaint Rakefile: After upload run `git push`.
authored
182 sh 'git push'
26c0e85 @runpaint Rakefile: tasks for making and uploading the site.
authored
183 end
721314f @runpaint Generate web version of book in HTML.
authored
184
a2f5b36 @runpaint Rakefile: Initial task to generate .deb.
authored
185 desc "Generate the .deb"
186 task :deb => [:www] do
19dfec6 @runpaint Rakefile: :deb - Copy/Gzip PDF.
authored
187 deb_dir = 'deb/usr/share/doc/vimrecipes'
188 html_dir = deb_dir + '/html'
a2f5b36 @runpaint Rakefile: Initial task to generate .deb.
authored
189 mkdir_p html_dir
190 cp 'output/toc/index.html', html_dir
1581494 @runpaint Rakefile: Use ERB instead of Erubis for .deb.
authored
191 #sh "gzip -c output/vim-recipes.pdf >#{deb_dir}/vim-recipes.pdf.gz"
a8af98e @runpaint Rakefile: :deb Handle links, images, and JS.
authored
192 FileList['output/*/','output/*.png'].each {|d| cp_r d, html_dir}
cf5418c @runpaint Rakefile: :deb - Make internal links relative.
authored
193 FileList["#{html_dir}/*.html", "#{html_dir}/*/*.html",
194 "#{html_dir}/*/*/*.html"].each do |file|
195 prefix = '../' * (file.count('/') - html_dir.count('/') - 1)
196 doc = Hpricot(File.open(file).read)
a8af98e @runpaint Rakefile: :deb Handle links, images, and JS.
authored
197 doc.search('link[@rel=alternate]').remove
198 doc.search('form, script, noscript').remove
199 if doc.at('#disqus_thread')
200 doc.at('#disqus_thread').after('<script src="/js/footnotes.js">')
201 end
202 %w{href src}.each do |attr|
203 doc.search("*[@#{attr}]").each do |tag|
204 next unless tag[attr].start_with? '/'
205 tag[attr] = prefix + tag[attr][1..-1]
206 tag[attr] += 'index.html' if tag[attr].end_with? '/'
207 tag[attr] = tag[attr].sub('/toc','') || tag[attr]
208 end
cf5418c @runpaint Rakefile: :deb - Make internal links relative.
authored
209 end
210 File.open(file,'w').puts doc
211 end
a2f5b36 @runpaint Rakefile: Initial task to generate .deb.
authored
212 end
Something went wrong with that request. Please try again.