Randomize the transformation HLine a bit.
[matthijs/master-project/report.git] / pret-haskell.lua
1 -- filename : type-haskell.lua
2 -- comment  : Pretty printing of haskell programs. Currently, this is just
3 ---           verbatim printing with the option of applying strikethrough
4 -- author   : Matthijs Kooijman, Universiteit Twente, NL
5 -- copyright: Matthijs Kooijman
6 -- license  : None
7
8 local utf = unicode.utf8
9
10 local visualizer = buffers.newvisualizer('haskell')
11
12 -- Print a string or table of strings with the catcodes set to the default
13 -- context ones. Use this function to print a string that contains context
14 -- macro's that should be interpreted. Does not insert any automatic
15 -- linebreaking.
16 local function ctxsprint(str) 
17     tex.sprint(ctxcatcodes, str)
18 end
19
20 -- Print a string or table of strings almost literal. Each character in the
21 -- string will be printed with the "character" catcode, causing it to show up
22 -- literal in the output. Only the space character is printed with the "space"
23 -- catcode.
24 local function texwrite(str)
25     tex.write(str)
26 end 
27
28 function visualizer.flush_line(str,nested)
29     while str ~= '' do
30         if utf.match(str, '^ ') then 
31             ctxsprint('\\obs ')
32             -- Eat the first character
33             str = utf.sub(str, 2)
34         else
35             local text, rest = utf.match(str, "^%-%-(.-)%-%-(.*)")
36             if text then
37                 ctxsprint('\\strikethrough{')
38                 -- Recursively call ourselves to handle spaces gracefully.
39                 visualizer.flush_line(text)
40                 ctxsprint('}')
41                 -- Eat the processed characters
42                 str = rest
43             else
44                 -- Write the first character
45                 texwrite(utf.sub(str, 1, 1))
46                 -- Eat the first character
47                 str = utf.sub(str, 2)
48             end
49         end
50     end
51 end
52
53 -- vim: set sw=4 sts=4 expandtab ai: