-
Notifications
You must be signed in to change notification settings - Fork 0
/
xform.rb
66 lines (50 loc) · 933 Bytes
/
xform.rb
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
module Buffer
def add(thing)
@things ||= []
@things << thing
end
end
class Block
include Buffer
attr_accessor :closed
def initialize(block_class)
@block_class = block_class
end
def generate
%Q(
<div class="#{@block_class}" markdown="1">
#{@things.join}
</div>
)
end
end
def doit
block_start = /~~~\.(\S+)/
block_delim = /~~~/
filename = 'index.mdown.erb'
lines = File.readlines ARGV[0]
output = []
lines.each do |line|
case line
when block_start
output << Block.new($~[1])
when block_delim
output.last.closed = true
else
if output.last.is_a?(Block) && ! output.last.closed
output.last.add line
else
output << line
end
end
end
result = output.map do |out_thing|
if out_thing.respond_to? :generate
out_thing.generate
else
out_thing
end
end .join
puts result
end
doit