forked from mislav/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
57 lines (49 loc) · 1.65 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
desc "Show man page"
task :man => "man:build" do
exec "man man/hub.1"
end
desc "Build man pages"
task "man:build" => ["man/hub.1", "man/hub.1.html"]
# split readme in sections
# and return the specified section
def split_readme(file, index)
File.read(file).split(/^-{4,}$/)[index].strip
end
def extract_configs(readme_file)
configs = split_readme(readme_file, 4)
configs.gsub!(/\*\*(.+?)\*\*/, '<\1>') # replace **xx** with <xx>
configs.sub!(/\n+.+\Z/, '') # remove last line
configs
end
def extract_examples(readme_file)
examples = split_readme(readme_file, 3)
examples.sub!(/^.+?(###)/m, '\1') # strip intro paragraph
examples.sub!(/\n+.+\Z/, '') # remove last line
examples
end
# inject configs and examples from README file to .ronn source
def compiled_source(source, readme)
configs = extract_configs(readme)
examples = extract_examples(readme)
compiled = File.read(source)
compiled.sub!('{{CONFIGS}}', configs)
compiled.sub!('{{README}}', examples)
compiled
end
# generate man page with ronn
def compile_ronn(destination, type, contents)
File.popen("ronn --pipe --#{type} --organization=GITHUB --manual='Hub Manual'", 'w+') { |io|
io.write contents
io.close_write
File.open(destination, 'w') { |f| f << io.read }
}
abort "ronn --#{type} conversion failed" unless $?.success?
end
file "man/hub.1" => ["man/hub.1.ronn", "README.md"] do |task|
contents = compiled_source(*task.prerequisites)
compile_ronn(task.name, 'roff', contents)
compile_ronn("#{task.name}.html", 'html', contents)
end
file "man/hub.1.html" => ["man/hub.1.ronn", "README.md"] do |task|
Rake::Task["man/hub.1"].invoke
end