blob: 3b1230c10b33dd7cf393411ee574b4f8cd410baa [file] [log] [blame]
reed86217d82014-10-25 20:44:40 -07001function tostr(t)
2 local str = ""
3 for k, v in next, t do
4 if #str > 0 then
5 str = str .. ", "
6 end
7 if type(k) == "number" then
8 str = str .. "[" .. k .. "] = "
9 else
10 str = str .. tostring(k) .. " = "
11 end
12 if type(v) == "table" then
13 str = str .. "{ " .. tostr(v) .. " }"
14 elseif type(v) == "string" then
15 str = str .. '"' .. v .. '"'
16 else
17 str = str .. tostring(v)
18 end
19 end
20 return str
21end
22
23function trim_ws(s)
24 return s:match("^%s*(.*)")
25end
26
27function count_hypens(s)
28 local leftover = s:match("^-*(.*)")
29 return string.len(s) - string.len(leftover)
30end
31
reed86217d82014-10-25 20:44:40 -070032function pretty_print_slide(slide)
33 io.write("{\n")
reed07dada72014-10-29 20:36:05 -070034 if slide.transition then
35 io.write(" transition = \"", slide.transition, "\",\n")
36 end
reed86217d82014-10-25 20:44:40 -070037 for i = 1, #slide do
38 local node = slide[i]
39 for j = 0, node.indent do
40 io.write(" ")
41 end
42 io.write("{ ")
43 io.write(tostr(node))
44 io.write(" },\n")
45 end
46 io.write("},\n")
47end
48
49function pretty_print_slides(slides)
50 io.write("gSlides = {\n")
51 for i = 1, #slides do
52 pretty_print_slide(slides[i])
53 end
54 io.write("}\n")
55end
56
reedbb8a0ab2014-11-03 22:32:07 -080057function parse_attr(s, lvalue)
58 local ts = "^<%s*" .. lvalue .. "%s*=%s*(%a+)%s*>$"
59 return s:match(ts)
reed07dada72014-10-29 20:36:05 -070060end
61
reedbb8a0ab2014-11-03 22:32:07 -080062function flush(slides, block)
63 if #block > 0 then
64 slides[#slides + 1] = block
65 return {}
66 end
67 return block
reedde330ff2014-11-02 19:19:34 -080068end
69
reed07dada72014-10-29 20:36:05 -070070function parse_file(file)
71 local slides = {}
72 local block = {}
73
74 for line in file:lines() do
75 local s = trim_ws(line)
76 if #s == 0 then -- done with a block
reedbb8a0ab2014-11-03 22:32:07 -080077 block = flush(slides, block)
reed07dada72014-10-29 20:36:05 -070078 else
reedbb8a0ab2014-11-03 22:32:07 -080079 local transition_type = parse_attr(s, "transition")
80 local blockstyle = parse_attr(s, "blockstyle")
reed07dada72014-10-29 20:36:05 -070081 if transition_type then
82 block["transition"] = transition_type
reedde330ff2014-11-02 19:19:34 -080083 elseif blockstyle then
84 block["blockstyle"] = blockstyle
reed07dada72014-10-29 20:36:05 -070085 else
reedde330ff2014-11-02 19:19:34 -080086 if block.blockstyle == "code" then
87 block[#block + 1] = { text = line }
88 else
89 local n = count_hypens(s)
90 block[#block + 1] = {
91 indent = n,
92 text = trim_ws(s:sub(n + 1, -1))
93 }
94 end
reed07dada72014-10-29 20:36:05 -070095 end
96 end
97 end
reed7b864662014-11-04 13:24:47 -080098 flush(slides, block)
99
reed07dada72014-10-29 20:36:05 -0700100 return slides
101end
102