forked from sparklemotion/nokogiri.org-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
104 lines (88 loc) · 2.7 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
require 'rubygems'
gem 'rcodetools'
gem 'maruku', '>= 0.6.0'
def markdown_file_list
files = File.read("content/toc").split("\n")
files.collect! {|file| file.downcase.downcase.gsub(/[\W ]/,"_") }
files.collect! {|file| File.join("content", "#{file}.md")}
files = files.select do |file|
if File.exists? file
true
else
STDERR.puts "WARNING: file #{file} does not exist!"
false
end
end
end
def output_directory
"html"
end
def format_output_filename(markdown_filename)
File.join(output_directory, File.basename(markdown_filename).gsub(/\.md$/, ".html"))
end
def run(cmd)
puts "=> #{cmd}"
system(cmd) || raise("command failed: #{cmd}")
end
def sub_do(tag, content, &block)
content.gsub!(/^~~~ #{tag} (.*)/) do |match|
asset_name = "content/#{$1}"
raise "Could not find asset #{asset_name}" unless File.exists?(asset_name)
puts " #{tag} file: #{asset_name}"
yield(asset_name).gsub!(/^/,' ')
end
end
def sub_inline_docs!(content)
sub_do('inline', content) do |asset_name|
"[#{File.basename(asset_name)}]\n" + File.read(asset_name)
end
end
def sub_inline_ruby!(content)
sub_do('ruby', content) do |asset_name|
output = IO.popen("xmpfilter -a --cd content/assets/ #{asset_name}", "r").read
raise "Error running inline ruby:\n#{output}" if output =~ /Error/
output.gsub!(/^.*:startdoc:[^\n]*/m, '') if output =~ /:startdoc:/
output.gsub!(/^.*:nodoc:.*$\n?/, '')
output.gsub!(/\\n/,"\n# ")
output.chomp!
end
end
def slurp_file(markdown_file)
content = File.read(markdown_file)
sub_inline_docs! content
sub_inline_ruby! content
content
end
task :default => :html
desc "Remove all generated files"
task :clean do
FileUtils.rm_rf output_directory
end
desc "Compile an html version of the book"
task :html do
STDOUT.sync = true
FileUtils.mkdir_p output_directory
output_filenames = []
markdown_file_list.each do |markdown_filename|
output_filename = format_output_filename(markdown_filename)
output_filenames << output_filename
if FileUtils.uptodate?(output_filename, markdown_filename)
puts "(#{output_filename} is up to date with #{markdown_filename})"
else
puts "writing to #{output_filename} from #{markdown_filename} ..."
IO.popen("maruku > #{output_filename}", "w") do |f|
f.write slurp_file(markdown_filename)
end
end
end
all_tutorials = "#{output_directory}/all_tutorials.html"
system "> #{all_tutorials}"
output_filenames.each do |fn|
system "cat #{fn} >> #{all_tutorials}"
end
puts "complete doc is #{all_tutorials}"
end
desc "Describe the tutorials in YAML"
task :describe do
puts markdown_file_list.collect { |f| format_output_filename(f) }.to_yaml
end