X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fmaster-project%2Freport.git;a=blobdiff_plain;f=pret-trans.lua;h=a96002f1cb894ca9db119d60a0e9dc98464511c4;hp=9c6847e9bf68e5a9fead39032f93f8d1003b8a14;hb=4beb49a9e72cb653bcbbd053e3a1dba523b53f8f;hpb=c4abca08c46358134e3f73b2904293ef7a21b8b7 diff --git a/pret-trans.lua b/pret-trans.lua index 9c6847e..a96002f 100644 --- a/pret-trans.lua +++ b/pret-trans.lua @@ -9,9 +9,7 @@ local utf = unicode.utf8 -if not buffers then buffers = { } end -if not buffers.visualizers then buffers.visualizers = { } end -if not buffers.visualizers.trans then buffers.visualizers.trans = { } end +local vis = buffers.newvisualizer("trans") local commands = {} -- A command to create a horizontal rule. @@ -40,52 +38,53 @@ end -- Insert n blank lines local function blanks(n) - print("Inserting " .. n .. " blanks") for i = 1,n do - buffers.visualizers.default.empty_line() + buffers.visualizers.handlers.default.empty_line() end end -- Prettyprint the given lines using the given pretty printer local function prettyprint(ppr, lines) -- Change the current visualizer - oldvisualizer = buffers.currentvisualizer - buffers.currentvisualizer = 'lam' + buffers.setvisualizer('lam') -- Output the lines - buffers.hooks.begin_of_buffer('buffer', 'pret-trans-internal') + buffers.hooks.begin_of_display() line = 0 for i = 1,#lines do _, line = buffers.typeline(lines[i], i, #lines, line) end - buffers.hooks.end_of_buffer('buffer', 'pret-trans-internal') + buffers.hooks.end_of_display() -- Change the visualizer back - buffers.currentvisualizer = oldvisualizer + buffers.setvisualizer('trans') end -- Capture all lines, without generating any output -function buffers.visualizers.trans.begin_of_buffer(type, name) +function vis.begin_of_display() lines = {} + -- Let all the lambda pretty printing in this buffer use shared subscript + -- detection + buffers.visualizers.handlers.lam.begin_of_block() end -function buffers.visualizers.trans.begin_of_line(n) +function vis.begin_of_line(n) -- Don't generate output here end -function buffers.visualizers.trans.flush_line(str, nested) +function vis.flush_line(str, nested) table.insert(lines, str) -- Don't generate output here end -function buffers.visualizers.trans.end_of_line(n) +function vis.end_of_line(n) -- Don't generate output here end -function buffers.visualizers.empty_line() +function vis.empty_line() table.insert(lines, '') -- Don't generate output here end -- We do the actual work here. Process all the lines in the buffer and -- generate output for them. -function buffers.visualizers.trans.end_of_buffer() +function vis.end_of_display() -- Find the horizontal rule, and see how long it is. len = nil for i = 1,#lines do @@ -95,8 +94,11 @@ function buffers.visualizers.trans.end_of_buffer() break end end - print("Rule length: " .. len) - + + if not len then + error("No horizontal separator found in:\n" .. table.concat(lines, "\n")) + end + -- Split the input in three parts. Stuff before the line, stuff -- after the line, stuff to the right of the line. before, after, rights = {}, {}, {} @@ -109,7 +111,6 @@ function buffers.visualizers.trans.end_of_buffer() if utf.match(left, "^%-%-%-*") then found_line = true else - print("Looking at " .. line .. "('" .. left .. "', '" .. right .. "')") if utf.len(left) > 0 then if not found_line then table.insert(before, left) @@ -154,7 +155,7 @@ function buffers.visualizers.trans.end_of_buffer() -- Print the conditions for i = 1,#rights do tex.sprint(rights[i]) - buffers.visualizers.default.end_of_line() + buffers.visualizers.handlers.default.end_of_line() end -- Fill up the remaining space with blanks @@ -167,6 +168,7 @@ function buffers.visualizers.trans.end_of_buffer() -- Clean up lines = {} + buffers.visualizers.handlers.lam.end_of_block() end -- vim: set sw=4 sts=4 expandtab ai: