1 -- filename : type-trans.lua
2 -- comment : Pretty printing of core transformations. Parses a specific
3 -- syntax, consisting of a before / after with a dashed lined in
4 -- between, and some extra conditions to the right of the line.
5 -- Recreates this layout using frames and line commands.
6 -- author : Matthijs Kooijman, Universiteit Twente, NL
7 -- copyright: Matthijs Kooijman
10 local utf = unicode.utf8
12 if not buffers then buffers = { } end
13 if not buffers.visualizers then buffers.visualizers = { } end
14 if not buffers.visualizers.trans then buffers.visualizers.trans = { } end
17 -- A command to create a horizontal rule.
18 commands.rule = "\\blackrule[height=0.5pt,depth=0pt,width=.45\\textwidth]"
19 -- Pretty printer to use for the stuff before and after the line
20 commands.before_pret = "lam"
21 commands.after_pret = "lam"
22 -- Frame commands to use for the left (before + line + after) and right
23 -- (conditions) parts. Should include an opening {, which will be closed
25 commands.leftframe = "\\framed[offset=0mm,location=middle,strut=no,align=right,frame=off,width=.5\\textwidth]{\\sans"
26 commands.rightframe = "\\framed[offset=0mm,location=middle,strut=no,align=right,frame=off,width=.5\\textwidth]{"
28 -- A table to keep the lines in this buffer, so we can process them all at
32 -- Some helper functions
33 local function ltrim(s)
34 return (string.gsub(s, "^%s*", ""))
37 local function rtrim(s)
38 return (string.gsub(s, "%s*$", ""))
41 -- Insert n blank lines
42 local function blanks(n)
43 print("Inserting " .. n .. " blanks")
45 buffers.visualizers.default.empty_line()
49 -- Prettyprint the given lines using the given pretty printer
50 local function prettyprint(ppr, lines)
51 -- Change the current visualizer
52 oldvisualizer = buffers.currentvisualizer
53 buffers.currentvisualizer = 'lam'
56 buffers.hooks.begin_of_buffer('buffer', 'pret-trans-internal')
59 _, line = buffers.typeline(lines[i], i, #lines, line)
61 buffers.hooks.end_of_buffer('buffer', 'pret-trans-internal')
63 -- Change the visualizer back
64 buffers.currentvisualizer = oldvisualizer
67 -- Capture all lines, without generating any output
68 function buffers.visualizers.trans.begin_of_buffer(type, name)
71 function buffers.visualizers.trans.begin_of_line(n)
72 -- Don't generate output here
74 function buffers.visualizers.trans.flush_line(str, nested)
75 table.insert(lines, str)
76 -- Don't generate output here
78 function buffers.visualizers.trans.end_of_line(n)
79 -- Don't generate output here
81 function buffers.visualizers.empty_line()
82 table.insert(lines, '')
83 -- Don't generate output here
86 -- We do the actual work here. Process all the lines in the buffer and
87 -- generate output for them.
88 function buffers.visualizers.trans.end_of_buffer()
89 -- Find the horizontal rule, and see how long it is.
92 match = utf.match(lines[i], "^%-%-%-*")
98 print("Rule length: " .. len)
100 -- Split the input in three parts. Stuff before the line, stuff
101 -- after the line, stuff to the right of the line.
102 before, after, rights = {}, {}, {}
106 -- Split the line into a left and right part
107 left = rtrim(utf.sub(line, 1, len))
108 right = ltrim(utf.sub(line, len + 1))
109 if utf.match(left, "^%-%-%-*") then
112 print("Looking at " .. line .. "('" .. left .. "', '" .. right .. "')")
113 if utf.len(left) > 0 then
114 if not found_line then
115 table.insert(before, left)
117 table.insert(after, left)
121 if utf.len(right) > 0 then
122 table.insert(rights, right)
127 -- Real output starts here
130 -- Ensure the left and right frames end up next to each other.
131 tex.sprint("\\dontleavehmode")
133 -- Open and fill the left frame
134 tex.sprint(commands.leftframe)
136 prettyprint('lam', before)
138 tex.sprint(commands.rule)
140 prettyprint('lam', after)
142 -- Close the left frame
145 -- Open and fill the right frame
146 tex.sprint(commands.rightframe)
148 -- Insert spacer blank lines to align the middle condition with the
150 n_blanks = #before - math.ceil((#rights - 1) / 2)
151 n_blanks = math.max(0, n_blanks)
154 -- Print the conditions
156 tex.sprint(rights[i])
157 buffers.visualizers.default.end_of_line()
160 -- Fill up the remaining space with blanks
161 n_blanks = (#before + 1 + #after) - (n_blanks + #rights)
162 n_blanks = math.max(0, n_blanks)
165 -- Close the right frame
172 -- vim: set sw=4 sts=4 expandtab ai: