Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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