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 | |
reed | 86217d8 | 2014-10-25 20:44:40 -0700 | [diff] [blame] | 32 | function pretty_print_slide(slide) |
| 33 | io.write("{\n") |
reed | 07dada7 | 2014-10-29 20:36:05 -0700 | [diff] [blame] | 34 | if slide.transition then |
| 35 | io.write(" transition = \"", slide.transition, "\",\n") |
| 36 | end |
reed | 86217d8 | 2014-10-25 20:44:40 -0700 | [diff] [blame] | 37 | 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") |
| 47 | end |
| 48 | |
| 49 | function 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") |
| 55 | end |
| 56 | |
reed | 07dada7 | 2014-10-29 20:36:05 -0700 | [diff] [blame] | 57 | function parse_transition_type(s) |
| 58 | return s:match("^<%s*transition%s*=%s*(%a+)%s*>$") |
| 59 | end |
| 60 | |
reed | de330ff | 2014-11-02 19:19:34 -0800 | [diff] [blame^] | 61 | function parse_blockstyle_type(s) |
| 62 | return s:match("^<%s*blockstyle%s*=%s*(%a+)%s*>$") |
| 63 | end |
| 64 | |
reed | 07dada7 | 2014-10-29 20:36:05 -0700 | [diff] [blame] | 65 | function parse_file(file) |
| 66 | local slides = {} |
| 67 | local block = {} |
| 68 | |
| 69 | for line in file:lines() do |
| 70 | local s = trim_ws(line) |
| 71 | if #s == 0 then -- done with a block |
| 72 | if #block > 0 then |
| 73 | slides[#slides + 1] = block |
| 74 | block = {} |
| 75 | end |
| 76 | else |
| 77 | local transition_type = parse_transition_type(s) |
reed | de330ff | 2014-11-02 19:19:34 -0800 | [diff] [blame^] | 78 | local blockstyle = parse_blockstyle_type(s) |
reed | 07dada7 | 2014-10-29 20:36:05 -0700 | [diff] [blame] | 79 | if transition_type then |
| 80 | block["transition"] = transition_type |
reed | de330ff | 2014-11-02 19:19:34 -0800 | [diff] [blame^] | 81 | elseif blockstyle then |
| 82 | block["blockstyle"] = blockstyle |
reed | 07dada7 | 2014-10-29 20:36:05 -0700 | [diff] [blame] | 83 | else |
reed | de330ff | 2014-11-02 19:19:34 -0800 | [diff] [blame^] | 84 | if block.blockstyle == "code" then |
| 85 | block[#block + 1] = { text = line } |
| 86 | else |
| 87 | local n = count_hypens(s) |
| 88 | block[#block + 1] = { |
| 89 | indent = n, |
| 90 | text = trim_ws(s:sub(n + 1, -1)) |
| 91 | } |
| 92 | end |
reed | 07dada7 | 2014-10-29 20:36:05 -0700 | [diff] [blame] | 93 | end |
| 94 | end |
| 95 | end |
| 96 | -- pretty_print_slides(slides) |
| 97 | return slides |
| 98 | end |
| 99 | |