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
|
# File 'lib/leg/commands/build.rb', line 22
def run
needs! :config, :repo
tutorial = @git.load!(full_diffs: true, diffs_ignore_whitespace: true) do |step_num|
output "\r\e[K[repo/ -> Tutorial] Step #{step_num}"
end
output "\n"
num_steps = tutorial.num_steps
if @config.options[:diff_transformers].nil?
@config.options[:diff_transformers] = [
{ 'FoldSections' => {
unfold_before_new_section: true,
section_types: [
{ name: 'comments', start: "^/\\*\\*\\*.+\\*\\*\\*/$", end: nil },
{ name: 'braces', start: "^\\S.*{$", end: "^}( \\w+)?;?$" }
]
}},
'TrimBlankLines',
'OmitAdjacentRemovals'
]
end
if @config.options[:diff_transformers]
transformers = @config.options[:diff_transformers].map do |transformer_config|
if transformer_config.is_a? String
transformer = transformer_config
options = {}
else
transformer = transformer_config.keys.first
options = transformer_config.values.first
end
Leg::DiffTransformers.const_get(transformer).new(options)
end
tutorial.transform_diffs(transformers) do |step_num|
output "\r\e[K[Transform diffs] Step #{step_num}/#{num_steps}"
end
output "\n"
end
templates = Dir[File.join(@config.path, "template{,-?*}")].map do |template_dir|
[template_dir, File.basename(template_dir).split("-")[1] || "html"]
end
if templates.empty?
templates = [[nil, "html"], [nil, "md"]]
end
FileUtils.rm_rf(File.join(@config.path, "build"))
templates.each do |template_dir, format|
FileUtils.cd(@config.path) do
FileUtils.mkdir_p("build/#{format}")
include_default_css = (format == "html")
page_template = Leg::DefaultTemplates::PAGE[format]
if template_dir && File.exist?(File.join(template_dir, "page.#{format}.erb"))
page_template = File.read(File.join(template_dir, "page.#{format}.erb"))
include_default_css = false
end
page_template.gsub!(/\\\s*/, "")
step_template = Leg::DefaultTemplates::STEP[format]
if template_dir && File.exist?(File.join(template_dir, "step.#{format}.erb"))
step_template = File.read(File.join(template_dir, "step.#{format}.erb"))
end
step_template.gsub!(/\\\s*/, "")
tutorial.pages.each do |page|
output "\r\e[K[Tutorial -> build/] Page #{page.filename}"
content = Leg::Template.render_page(page_template, step_template, format, page, tutorial, @config)
File.write("build/#{format}/#{page.filename}.#{format}", content)
end
output "\n"
if template_dir
FileUtils.cd(template_dir) do
Dir["*"].each do |f|
name = File.basename(f)
next if ["page.#{format}.erb", "step.#{format}.erb"].include? name
next if name.start_with? "_"
if name.end_with? ".erb"
content = Leg::Template.render(File.read(f), tutorial, @config)
File.write("../build/#{format}/#{name[0..-5]}", content)
else
FileUtils.cp_r(f, "../build/#{format}/#{name}")
end
end
end
end
if include_default_css && !File.exist?("build/#{format}/style.css")
content = Leg::Template.render(Leg::DefaultTemplates::CSS, tutorial, @config)
File.write("build/#{format}/style.css", content)
end
end
end
end
|