-
Notifications
You must be signed in to change notification settings - Fork 15
/
Rakefile
52 lines (42 loc) · 1.08 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
task :default do
md = File.read("docs/reference.md").gsub(/^#/, "###")
codes = md.scan(/(```rust\n(.*?)\n```)/m)
File.open("markup/examples/reference.rs", "w") do |f|
f << "use std::io::Write;\n\n"
codes.each.with_index do |code, i|
f << "fn _#{i}() {\n#{code[1]}\n}\n\n"
end
f << "fn main() {"
0.upto(codes.size - 1).each do |i|
f << "_#{i}(); println!(\"---\");"
end
f << "}"
end
system "rustfmt markup/examples/reference.rs"
output = `cargo run --example reference`.strip.split("---").map(&:strip)
if codes.size != output.size
raise "number of codes and outputs do not match"
end
codes.zip(output).each do |code, output|
md = md.sub(code[0], "\
<table>
<tr><th>Code</th></tr>
<tr><td width=\"1000px\">
```rust
#{indent(code[1], with: " ")}
```
</td></tr>
<tr><th>Output</th></tr>
<tr><td width=\"1000px\">
```html
#{indent(output, with: " ")}
```
</td></tr>
</table>
")
end
puts md
end
def indent(string, with:)
string.split("\n").map { |line| line.strip.empty? ? "" : "#{with}#{line}" }.join("\n")
end