forked from chef-boneyard/chef-fundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
121 lines (109 loc) · 3.78 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
# -*- coding: utf-8 -*-
require "fileutils"
require 'redcarpet'
require 'json'
desc "Serve the presentation"
task :present do
Dir.chdir("slides")
sh "bundle exec showoff serve"
end
desc "Generate static HTML"
task :static do
Dir.chdir("slides")
sh "bundle exec showoff static"
sh "zip static.zip static/*"
end
desc "Create the directory and initial slides (with SECTION=name)"
task :mksection do
section = ENV['SECTION']
slides_dir = File.join(File.dirname(__FILE__), "slides", section)
guides_dir = File.join(File.dirname(__FILE__), "guides", "student-exercises")
puts "** Creating section #{section} **"
FileUtils.mkdir_p(slides_dir)
FileUtils.mkdir_p(guides_dir)
unless File.exists?(File.join(slides_dir, "01_slide.md"))
puts "- populating slide template #{slides_dir}/01_slide.md"
File.open(File.join(slides_dir, "01_slide.md"), "a+") do |f|
f.puts "# #{section_header(section)}\n\n"
f.puts "Section Objectives:\n"
f.puts copyright_header
f.puts "# Summary\n\n\n"
f.puts "# Questions\n\n*\n\n"
f.puts "# Additional Resources\n\n*\n\n"
f.puts "# Lab Exercise\n\n"
f.puts section_header(section)
end
puts "- populating exercises #{File.join(guides_dir, section)}.md"
File.open(File.join(guides_dir, "#{section}.md"), "a+") do |g|
g.puts exercise_guide(section)
end
end
end
desc "Generate HTML out of Markdown (Slides)"
task :md_to_html do
html_dir = File.join("slides", "html")
sections = showoff_sections
FileUtils.mkdir_p(html_dir)
sections.each_with_index do |s,i|
fn = File.join(html_dir, "section-#{i}-#{s}.html")
%x[grep -v "@@@" slides/#{s}/01_slide.md | redcarpet > #{fn}]
%x[cp #{s}/*.png html 2>/dev/null]
File.open(File.join("slides", "html", "index.html"), "a+") do |h|
h.puts html_list_item s, fn
end
end
end
desc "Generate HTML out of Markdown (Guides)"
task :student_guide do
html_dir = File.join("guides", "html")
guides_dir = File.join("guides", "student-exercises")
sections = showoff_sections
markdown = []
FileUtils.mkdir_p(html_dir)
sections.each do |s|
fn = File.join(guides_dir, "#{s}.md")
markdown << IO.read(fn) if File.exists?(fn)
end
File.open(File.join(html_dir, "Student-Exercises.html"), "a+") do |h|
h.puts Redcarpet::Markdown.new(Redcarpet::Render::HTML).render(markdown.join("\n<hr />\n"))
end
end
desc "Count the number of slides per section"
task :slide_counter do
sections = showoff_sections
puts "** Number of slides in each section:"
sections.each do |section|
puts "- #{section}: #{line_count(File.join("slides", section, "01_slide.md"))}"
end
end
def line_count(slides)
count = 0
begin
f = File.open(slides)
rescue
return "section does not exist, create with `mksection`"
end
f.each do |line|
count += 1 if line =~ /^# /
end
count
end
def html_list_item(section, filename)
%Q{<li><a href="../#{filename}">#{section}</a></li>}
end
def showoff_sections
JSON.parse(IO.read(File.join("slides", "showoff.json")))['sections']
end
def section_header(section = "welcome")
lcase = %w{an of the a}
parts = section.split(/[-_]/)
header = Array(parts).flatten.map {|p| lcase.include?(p) ? p : p.capitalize }
header.join(' ')
end
def copyright_header
"\n.notes These course materials are Copyright © 2010-2012 Opscode, Inc. All rights reserved.
This work is licensed under a Creative Commons Attribution Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us; or send a letter to Creative Commons, 171 2nd Street, Suite 300, San Francisco, California, 94105, USA.\n\n"
end
def exercise_guide(section = "welcome")
"#{section_header(section)}\n======================\n\n## Objectives\n\n## Acceptance Criteria\n\n## Questions\n"
end