-
Notifications
You must be signed in to change notification settings - Fork 15
/
worksheet.lua
251 lines (209 loc) · 5.43 KB
/
worksheet.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
function dirtree(dir)
if string.sub(dir, -1) == "/" then
dir=string.sub(dir, 1, -2)
end
local function yieldtree(dir)
for entry in lfs.dir(dir) do
if not entry:match("^%.") then
entry=dir.."/"..entry
if lfs.isdir(entry) then
yieldtree(entry)
else
coroutine.yield(entry)
end
end
end
end
return coroutine.wrap(function() yieldtree(dir) end)
end
--- Check if a file or directory exists in this path
function exists(file)
local ok, err, code = os.rename(file, file)
if not ok then
if code == 13 then
-- Permission denied, but it exists
return true
end
end
return ok, err
end
function openable(file)
local ok, err, code = os.rename(file, file)
return ok
end
--- Check if a directory exists in this path
function isdir(path)
-- "/" works on both Unix and Windows
return exists(path.."/")
end
function readlines(filename)
local function yieldline(filename)
local file = io.open(filename, "r")
for line in string.split(file:read("*a"),"\n") do
coroutine.yield(line)
end
io.close(file)
end
return coroutine.wrap(function() yieldline(filename) end)
end
exercises = {}
flavor = {}
page_numbers = {}
section_numbers = {}
exercise_numbers = {}
references = {}
exercise_order = {}
for i in dirtree('./') do
local filename = i
if filename:match(".aux$") then
for line in io.lines(filename) do
local c, _, m1, m2, m3, m4, m5 = string.find( line,
"newlabel{([^}]*)}{{([0-9]*)}{([0-9]*)}{}{exercise.exercise.([0-9]*).([0-9]*)." )
if c then
section_numbers[m1] = m4 .. "." .. m5
exercise_numbers[m1] = m2
table.insert( exercise_order, m1 )
end
local c, _, m1, m2, m3 = string.find( line,
"newlabel{([^}]*)}{{([0-9]*)}{([0-9]*)}" )
if c then
page_numbers[m1] = m3
exercise_numbers[m1] = m2
table.insert( exercise_order, m1 )
end
local c, _, m1, m2 = string.find( line,
"newlabel{([^}]*)}{({[0-9.]*}{[0-9.]*})" )
if c then
references[m1] = "{" .. m2 .. "}"
end
end
end
end
function table.clone(org)
return {table.unpack(org)}
end
SOLUTIONS = true
for i in dirtree('./') do
local f = i
if f:match(".tex$") then
local depth = 0
local solutioning = false
local label = nil
local output = {}
local paragraph = {}
local restart = false
local preamble = true
for line in io.lines(f) do
--line = string.gsub(line, "\\%.*", "")
if line:match("\\begin *{document}") then
preamble = false
goto continue
end
if preamble == true then
goto continue
end
if line:match( "^[ ]*$" ) then
restart = true
end
if line:match( "\\begin *{exercise}" ) or line:match( "\\begin *{computerExercise}" ) then
depth = depth + 1
output = {}
end
if depth == 0 and line:match( "[A-z]" ) then
if restart then
paragraph = {}
restart = false
end
table.insert( paragraph, line )
end
if line:match( "\\begin *{solution}" ) then
solutioning = true
end
if SOLUTIONS or (not solutioning) then
table.insert( output, line )
end
if depth > 0 then
local c, _, m1 = string.find( line, "\\label[ ]*{([^}]*)}" )
if c and label == nil then
label = m1
end
end
if line:match( "\\end *{solution}" ) then
solutioning = false
end
if line:match( "\\end *{exercise}" ) or line:match( "\\end *{computerExercise}" ) then
depth = depth - 1
if depth == 0 then
if exercises[label] ~= nil then
print("WARNING: in " .. f .. " the exercise " .. label .. " appears more than once.")
end
if label == nil then
print("WARNING: in " .. f .. " an exercise is missing a label.")
else
exercises[label] = table.clone(output)
flavor[label] = table.clone(paragraph)
label = nil
end
end
end
::continue::
end
end
end
function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
--tex.sprint( section_numbers )
-- table.sort(filenames)
-- for i, v in pairs(filenames) do
-- tex.sprint("\\subfile{" .. dirname .. "/" .. v .. "}")
-- end
--end
flavors = {}
function insert_exercise(label)
if exercises[label] == nil then
tex.print("")
tex.print("")
tex.print("\\textbf{[MISSING " .. label .. "]}")
tex.print("")
tex.print("")
return
end
if table.concat(exercises[label],"\n"):match('computerExercise') then
tex.print("")
tex.print("")
tex.print("\\matlabproblemlabel")
tex.print("")
tex.print("")
else
tex.print("")
tex.print("")
tex.print("\\problemlabel")
tex.print("")
tex.print("")
end
-- only output the flavor text once
if flavors[table.concat(flavor[label],"\n")] == nil then
for i = 1, #flavor[label] do
tex.print(flavor[label][i])
end
flavors[table.concat(flavor[label],"\n")] = true
end
tex.print("")
tex.print("")
tex.print("\\exerciselabel{" .. exercise_numbers[label] .. "}{" .. section_numbers[label] .. "}")
for i = 1, #exercises[label] do
tex.print(exercises[label][i])
end
tex.print("")
tex.print("")
end