forked from hacketyhack/hacketyhack
/
lesson-md-gen.rbx
114 lines (92 loc) · 2.14 KB
/
lesson-md-gen.rbx
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
# TODO don't forget to delete this file, and all the old DSL lesson .rb files.
module HH
class STATIC
def self.to_s
#Dir.pwd + '/static'
'static'
end
end
end
module MarkdownTranslator
def lesson_set name
puts "\n# #{name}"
yield
end
def lesson name
puts "\n## #{name}"
end
def page name
puts "\n### #{name}"
yield
end
def flow
puts
yield
end
# via http://blog.macromates.com/2006/wrapping-text-with-regular-expressions/
def wrap_text(txt, col = 80)
txt.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/, "\\1\\3\n")
end
def para *lines
lines = lines.to_a.map { |line| line.to_s } * ''
puts "\n" + wrap_text(lines)
end
def em text
"_#{text}_"
end
def strong text
"__#{text}__"
end
def icon_button(name_symbol, not_sure_but_is_nil, &block)
msg = yield if block_given?
puts "\n\n![#{msg || ''}](/icon_button/#{name_symbol})\n"
end
def alert(txt)
txt
end
def image(path, opts={}, &block)
msg = yield if block_given?
puts "![#{msg || ''}](#{path})"
end
# On a page, this is basically a way of saying,
# "Hey, when someone's on this page right here,
# if they click the TAB that I'm naming here,
# you should advance the lesson to the next page,
# whatever it is."
def next_when(tab_opened, tab_name)
puts "<<Also, on this page ONLY, wire up the #{tab_name} tab to advance to the next page.>>"
end
def embed_code(code, opts={})
code = code.lines.map { |line| " #{line}" } * ''
puts "\n#{code}\n"
end
def link(text, opts={})
url = opts[:click]
"[#{text}](#{url})"
end
def code(text)
"`#{text}`"
end
end
def ld
load __FILE__
end
class MarkdownEmitter
include MarkdownTranslator
def puts(*s)
@f.puts(*s)
end
def initialize(lesson_file)
markdown_file = lesson_file.sub(File.extname(lesson_file), '.md')
p "#{lesson_file} -> #{markdown_file}"
File.open(markdown_file, 'w') do |f|
@f = f
eval(File.read(lesson_file))
end
end
end
def run
Dir.glob("#{File.dirname(__FILE__)}/**/*.rb").each do |lesson|
MarkdownEmitter.new(lesson)
end
end