forked from gotgit/gotgit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
174 lines (155 loc) · 5.2 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
require 'fileutils'
require 'erb'
require 'rubygems'
ABS_CSS_FILE = File.expand_path('html/inc/tty.css')
ABS_JS_PROTOTYPE_FILE = File.expand_path('html/inc/prototype.js')
ABS_JS_SHOWTTY_FILE = File.expand_path('html/inc/showtty.js')
ABS_CSS_COMMON_FILE = File.expand_path('html/inc/common.css')
TTYPLAY_TMPL = File.expand_path('html/templates/ttyplay.erb')
SCAST_TMPL = File.expand_path('html/templates/scast.erb')
SCAST_HTML = File.expand_path('screencast.html')
class ArgsBinding
def initialize(args)
args.keys.each do |key|
eval("@%s=%s" % [key, args[key].inspect])
end
end
def get_binding
return binding()
end
end
def strip_common_dir(path1, path2)
if path1+"\t"+path2 =~ %r{^(.*/)(.*?)\t\1(.*)$}
path1, path2 = $2, $3
end
return path1, path2
end
def rel_path(path1, path2)
path1, path2 = strip_common_dir path1, path2
return ('../' * path1.count('/')) + path2
end
def rel_path_dir(dir1, dir2)
dir1 += '/' if dir1[-1,1] != '/'
dir2 += '/' if dir2[-1,1] != '/'
return rel_path dir1, dir2
end
rule( /\.json$/ => [
proc {|tn| tn.sub(/\.[^.]+$/, '.ttyrec').sub(/^html\//, 'ttyrec/') }
]) do |t|
abs_source = File.expand_path(t.source)
abs_name = File.expand_path(t.name)
FileUtils.makedirs File.dirname abs_name
puts "parse #{abs_source}"
sh "(cd jsttyplay; rm -f #{abs_name}; perl preprocess.pl --size 80x25 #{abs_source} #{abs_name})"
end
FileList["ttyrec/**/*.ttyrec"].each do |t|
json = 'html/' + t.sub(/^ttyrec\//, '').sub(/\.[^.]+$/, '.json')
task :json => json
end
desc 'compile ttyrec files to json files'
task :json
desc 'create index.html and other html files for ttyplay.'
task :html => [:html_chunks, :html_screencast, "demo.md" ]
desc 'build site using jekyll.'
task :jekyll do
sh "jekyll"
end
task :html_chunks do
FileList["html/**/*.json"].each do |t|
title = File.basename(t).sub(/\.[^.]+$/, '')
output_file = File.expand_path(t.sub(/\.[^.]+$/, '.html'))
json_file = rel_path(output_file, File.expand_path(t))
css_files = [ rel_path(output_file, ABS_CSS_FILE),
rel_path(output_file, ABS_CSS_COMMON_FILE) ]
js_files = [ rel_path(output_file, ABS_JS_PROTOTYPE_FILE),
rel_path(output_file, ABS_JS_SHOWTTY_FILE) ]
unless uptodate?(output_file, TTYPLAY_TMPL)
File.open(output_file, "w") do |file|
template = ERB.new(File.read(TTYPLAY_TMPL))
file.puts template.result(binding)
end
end
end
end
task :html_screencast => [:html_chunks] do
output_file = SCAST_HTML
ttyrec_list = {}
css_files = [rel_path(output_file, ABS_CSS_COMMON_FILE)]
compile_time = Time.now.strftime("%Y/%m/%d %H:%M:%S")
version = %x[git describe --tags --always].strip.gsub(/^v/, '')
FileList["html/**/*.json"].each do |t|
title = File.basename(t).sub(/\.[^.]+$/, '')
part = t.sub(/.*\/part([0-9]+)\/.*$/, '\1')
part = 'others' unless part =~ /^[0-9]+$/
html_file = rel_path(output_file, File.expand_path(t.sub(/\.[^.]+$/, '.html')))
ttyrec_list[part] = [] unless ttyrec_list.member?(part)
ttyrec_list[part].push( { 'title' => title, 'html' => html_file } )
end
unless uptodate?(output_file, SCAST_TMPL)
File.open(output_file, "w") do |file|
template = ERB.new(File.read(SCAST_TMPL))
file.puts template.result(binding)
end
end
end
file "demo.md" => "bookstore.md" do |t|
sh "sed -e '/^javascripts/ d' < #{t.prerequisites} > #{t.name}"
end
def mkd2html args
begin
require 'redcarpet'
rescue Exception
$stderr.puts ""
$stderr.puts "Error: redcarpet not found! Install redcarpet using:"
$stderr.puts " gem install redcarpet"
$stderr.puts ""
exit 1
end
if not args.key? :source or not args.key? :template or not args.key? :output or not args.key? :title
raise "Incomplete arguments for mkd2html"
end
args[:subtitle] = '' if not args.key? :subtitle
args[:compile_time] = Time.now.strftime("%Y/%m/%d %H:%M:%S") if not args.key? :compile_time
args[:version] = %x[git describe --tags --always].strip.gsub(/^v/, '') if not args.key? :version
args[:css_files] = [rel_path(args[:output], ABS_CSS_COMMON_FILE)]
args[:js_files] = []
if args.key? :extra_css
args[:extra_css].each do |f|
if f =~ /http:\/\//
args[:css_files] << f
else
args[:css_files] << rel_path(args[:output], File.expand_path(f))
end
end
end
if args.key? :extra_js
args[:extra_js].each do |f|
if f =~ /http:\/\//
args[:js_files] << f
else
args[:js_files] << rel_path(args[:output], File.expand_path(f))
end
end
end
args[:markdown] = Redcarpet.new(File.open(args[:source]).read, :tables).to_html
File.open(args[:output], "w") do |file|
template = ERB.new(File.read(args[:template]))
file.puts template.result(ArgsBinding.new(args).get_binding)
end
puts "built %s from %s" % [args[:output], args[:source]]
end
desc 'clean *.json and *.html files'
task :clean => [:clean_json, :clean_html]
task :clean_json do
FileList["html/**/*.json"].each do |t|
File.unlink t
end
end
task :clean_html do
FileList["html/**/*.html"].each do |t|
File.unlink t
end
end
task :default => [:json, :html, :jekyll]