Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 176 lines (155 sloc) 5.242 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'
bbb815f @runpaint Rakefile: clobber output files before making PDF.
authored
8 CLOBBER.include('output')
4663b0b @runpaint Created Rakefile for generating PDF.
authored
9
721314f @runpaint Generate web version of book in HTML.
authored
10 #def www_path(local_path)
11 # www_path = local_path.dup
12 # www_path.sub!(/^text\//,'')
13 # www_path.gsub!(/[0-9]+_?/,'')
14 # www_path.sub!(/\.html$/,'')
15 # www_path.sub!(/\/$/,'')
16 # www_path + '/index.html'
17 #end
18
f79cd1f @runpaint Rakefile: Copy images to output/; tweak logic.
authored
19 directory "output"
20
9f5cf90 @runpaint Rakefile: add descriptions for tasks.
authored
21 desc "Copy images to output directory"
f79cd1f @runpaint Rakefile: Copy images to output/; tweak logic.
authored
22 task :images => :output
23 task :images => IMAGES do |t|
24 IMAGES.each do |image|
25 cp image, "output"
26 end
27 end
28
9f5cf90 @runpaint Rakefile: add descriptions for tasks.
authored
29 desc "Combine source HTML into single HTML file"
a341bdf @runpaint Rakefile: Check for broken links when making PDF
authored
30 task OUTPUT_HTML => [:output, :images, :ilinks]
bd8beb0 @runpaint Rakefile: use SOURCE_HTML consistently.
authored
31 file OUTPUT_HTML => SOURCE_HTML do |t|
4663b0b @runpaint Created Rakefile for generating PDF.
authored
32 File.open(t.name,'w') do |out|
f79cd1f @runpaint Rakefile: Copy images to output/; tweak logic.
authored
33 SOURCE_HTML.sort.each do |source|
4663b0b @runpaint Created Rakefile for generating PDF.
authored
34 out.puts File.open(source).read
35 end
36 end
37 end
38
dc815dc @runpaint Rakefile: constantised name of HTML output file.
authored
39 file 'output/vim-recipes.pdf' => OUTPUT_HTML do |t|
4663b0b @runpaint Created Rakefile for generating PDF.
authored
40 system("prince #{t.prerequisites.first} #{t.name}")
1155b9a @runpaint Rakefile: Generate a Sitemap, ping search engines.
authored
41 rm OUTPUT_HTML
4663b0b @runpaint Created Rakefile for generating PDF.
authored
42 end
43
9f5cf90 @runpaint Rakefile: add descriptions for tasks.
authored
44 desc "Generate the PDF"
bbb815f @runpaint Rakefile: clobber output files before making PDF.
authored
45 task :pdf => [:clobber, 'output/vim-recipes.pdf']
5871e9e @runpaint Add :ilinks task to display broken internal links.
authored
46
1155b9a @runpaint Rakefile: Generate a Sitemap, ping search engines.
authored
47 desc "Generate the Sitemap"
48 task :sitemap do
49 sh "sitemap_gen.py --config=sitemap_config.xml --testing"
50 end
51
52 desc "Notify search engines about Sitemap"
53 task :sitemap_notify do
54 sh "sitemap_gen.py --config=sitemap_config.xml"
55 end
56
721314f @runpaint Generate web version of book in HTML.
authored
57 def make_toc
58 toc = []
59 section = 'Preliminaries'
60 section_id = 'preliminaries'
61 SOURCE_HTML.sort.each do |f|
62 #next if f =~ %r{/[0-9]+_p(re|ost)/}
63 source = File.open(f).read
64 doc = Hpricot(source)
65 h_tag = doc.search('h1, h2, h3, h4').first
66 title = h_tag ? h_tag.inner_html : 'Untitled'
67 id = h_tag['id'] if h_tag
68 next unless id && h_tag
69 if h_tag.name == 'h2'
70 section = title
71 section_id = h_tag['id']
72 end
73 toc << { :id => h_tag['id'], :title => h_tag.inner_html, :file => f,
74 :type => h_tag.name == 'h2' ? :section : :recipe,
75 :section_name => section, :section_id => section_id }
76 end
77 toc
78 end
79
80 desc "Generate the HTML version"
81 task :html => SOURCE_HTML do |t|
82 require 'erubis'
83 require 'hpricot'
84 template = File.open('templates/recipe.html').read
85 toc = make_toc()
86 toc.each_with_index do |entry,idx|
87 source = File.open(entry[:file]).read
88 doc = Hpricot(source)
89
90 if (entry[:type] == :section) || (entry[:id] == 'introduction')
91 page = Erubis::Eruby.new(File.open('templates/chapter.html').read).result(
92 {:title => entry[:id] == 'introduction' ? 'Preliminaries' : entry[:title],
93 :recipes => toc.select do |e|
94 (e[:section_id] == entry[:section_id]) &&
95 (e[:type] == :recipe)
96 end
97 }
98 )
99 path = "output/#{entry[:section_id]}/index.html"
100 mkdir_p File.dirname(path)
101 File.open(path,'w'){|f| f.puts page}
102 else
103 doc.search('h1, h2, h3, h4, h5, h6').each do |tag|
104 new_tag = tag.name.sub(/(\d)/) {|m| m.to_i - 2}
105 tag.swap("<#{new_tag}>#{tag.inner_html}</#{new_tag}>")
106 end
107
108 doc.search('a[@href*=#]').each do |a|
109 id = a['href'].sub(/^#/,'')
110 sections = toc.select {|e| e[:id] == id}
111 next unless sections.size == 1
112 a.swap("<a href='/#{sections.first[:section_id]}/#{id}/'>#{a.inner_html}</a>")
113 end
114
115 doc.search('img').each do |img|
116 # FIXME: Awful hack: junks other attributes, assumes images to be in
117 # root directory.
118 img.swap("<img src='/#{img['src']}' />")
119 end
120
121 nxt = idx + 1 >= toc.size ? toc[0] : toc[(idx + 1)..-1].select{
122 |e| e[:type] == :recipe}.first
123 prv = idx == 0 ? toc[-1] : toc[0..(idx - 1)].select{
124 |e| e[:type] == :recipe}[-1]
125 page = Erubis::Eruby.new(template).result(
126 {:body => doc.to_s, :title => entry[:title], :id => entry[:id],
127 :section_id => entry[:section_id], :section => entry[:section_name],
128 :next_e => nxt, :prev_e => prv})
129
130 path = "output/#{entry[:section_id]}/#{entry[:id]}/index.html"
131 mkdir_p File.dirname(path)
132 File.open(path,'w') {|file| file.puts page}
133 end
134 end
135 page = Erubis::Eruby.new(File.open('templates/toc.html').read).result({:toc => toc.dup})
136 mkdir_p 'output/toc'
137 File.open('output/toc/index.html','w') {|file| file.puts page}
138 end
139
9f5cf90 @runpaint Rakefile: add descriptions for tasks.
authored
140 desc "Check for broken internal links"
a341bdf @runpaint Rakefile: Check for broken links when making PDF
authored
141 task :ilinks do |t|
5871e9e @runpaint Add :ilinks task to display broken internal links.
authored
142 require 'hpricot'
143 target = {}
144 source = {}
bd8beb0 @runpaint Rakefile: use SOURCE_HTML consistently.
authored
145 SOURCE_HTML.each do |file|
5871e9e @runpaint Add :ilinks task to display broken internal links.
authored
146 doc = Hpricot(File.open(file, 'r'))
147 doc.search("a[@href*='#']").each do |a|
148 source[a['href'][/[^#]+/]] = file
149 end
150 doc.search("*[@id]").each do |e|
151 target[e['id']] = 1
152 end
153 end
154 source.keys.each do |s|
155 $stderr.puts "Link #{s} from #{source[s]} is broken" unless target.key? s
156 end
157 end
26c0e85 @runpaint Rakefile: tasks for making and uploading the site.
authored
158
721314f @runpaint Generate web version of book in HTML.
authored
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
162 FileList['www/*'].each {|f| cp f, 'output/'}
163 File.open('output/css/style.css','w') do |merged|
164 ['layout','user','web'].each do |name|
165 merged.print File.open('templates/' + name + '.css').read
166 end
167 end
26c0e85 @runpaint Rakefile: tasks for making and uploading the site.
authored
168 end
169
170 desc "Upload the website"
1155b9a @runpaint Rakefile: Generate a Sitemap, ping search engines.
authored
171 task :upload => [:www, :sitemap] do
721314f @runpaint Generate web version of book in HTML.
authored
172 sh "rsync -vaz output/ vim.runpaint.org:/home/public/"
1155b9a @runpaint Rakefile: Generate a Sitemap, ping search engines.
authored
173 Rake::Task['sitemap_notify'].invoke
26c0e85 @runpaint Rakefile: tasks for making and uploading the site.
authored
174 end
721314f @runpaint Generate web version of book in HTML.
authored
175
Something went wrong with that request. Please try again.