Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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