reed | 86217d8 | 2014-10-25 20:44:40 -0700 | [diff] [blame^] | 1 | function 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 |
| 21 | end |
| 22 | |
| 23 | function trim_ws(s) |
| 24 | return s:match("^%s*(.*)") |
| 25 | end |
| 26 | |
| 27 | function count_hypens(s) |
| 28 | local leftover = s:match("^-*(.*)") |
| 29 | return string.len(s) - string.len(leftover) |
| 30 | end |
| 31 | |
| 32 | function parse_file(file) |
| 33 | local slides = {} |
| 34 | local block = {} |
| 35 | |
| 36 | for line in file:lines() do |
| 37 | local s = trim_ws(line) |
| 38 | if #s == 0 then -- done with a block |
| 39 | if #block > 0 then |
| 40 | slides[#slides + 1] = block |
| 41 | block = {} |
| 42 | end |
| 43 | else |
| 44 | local n = count_hypens(s) |
| 45 | block[#block + 1] = { |
| 46 | indent = n, |
| 47 | text = trim_ws(s:sub(n + 1, -1)) |
| 48 | } |
| 49 | end |
| 50 | end |
| 51 | return slides |
| 52 | end |
| 53 | |
| 54 | function pretty_print_slide(slide) |
| 55 | io.write("{\n") |
| 56 | for i = 1, #slide do |
| 57 | local node = slide[i] |
| 58 | for j = 0, node.indent do |
| 59 | io.write(" ") |
| 60 | end |
| 61 | io.write("{ ") |
| 62 | io.write(tostr(node)) |
| 63 | io.write(" },\n") |
| 64 | end |
| 65 | io.write("},\n") |
| 66 | end |
| 67 | |
| 68 | function pretty_print_slides(slides) |
| 69 | io.write("gSlides = {\n") |
| 70 | for i = 1, #slides do |
| 71 | pretty_print_slide(slides[i]) |
| 72 | end |
| 73 | io.write("}\n") |
| 74 | end |
| 75 | |