| 1 |
function init(opts) |
| 2 |
tex.print([[\newwrite\timetableATdataOutput \immediate\openout\timetableATdataOutput=\jobname-data.dat]]) |
| 3 |
tex.print([[\immediate\write\timetableATdataOutput{return \iftrue\string{\else}\fi}]]) |
| 4 |
if(not checkKeys(opts, {"days", "min", "max", "dayse"})) then |
| 5 |
error("missing argument") |
| 6 |
end |
| 7 |
-- clean up first |
| 8 |
-- global variables |
| 9 |
EVENTS={} |
| 10 |
DAYS = prepareDays(opts.days) -- header with names of the days set from tex currently |
| 11 |
DAYSE = prepareDays(opts.dayse) -- day representation in source code |
| 12 |
MIN = 25*60 -- bigger than any allowed value could be |
| 13 |
MAX = 0 |
| 14 |
MIN_BYPASS = false -- weather min is fixed by the user |
| 15 |
MAX_BYPASS = false -- weather max is fixed by the user |
| 16 |
|
| 17 |
if(opts.min == "") then |
| 18 |
else |
| 19 |
assert(opts.min:match("^%d+"), "start time has to be an integer representing the HH*60+MM of the desired start time") |
| 20 |
MIN = tonumber(opts.min) |
| 21 |
MIN_BYPASS = true |
| 22 |
end |
| 23 |
|
| 24 |
if(opts.max == "") then |
| 25 |
else |
| 26 |
assert(opts.max:match("^%d+"), "end time has to be an integer representing the HH*60+MM of the desired end time") |
| 27 |
MAX = tonumber(opts.max) |
| 28 |
MAX_BYPASS = true |
| 29 |
end |
| 30 |
end |
| 31 |
|
| 32 |
function defaultFormatter(opts) |
| 33 |
local ret = "" |
| 34 |
for k,v in pairs(opts) do |
| 35 |
if type(k) == "string" then k = k:gsub("[_^]", "") end |
| 36 |
if type(v) == "string" then v = v:gsub("[_^]", "") end |
| 37 |
ret = string.format("%s, %s: %s", ret, tostring(k), tostring(v)) |
| 38 |
end |
| 39 |
-- print(ret) |
| 40 |
return ret |
| 41 |
end |
| 42 |
|
| 43 |
function timetableformatter(opts) |
| 44 |
return string.format( |
| 45 |
[[\textcolor{%s}{\textbf{%s}\\[.2em]\raggedright{%s}\\[0.5em]\raggedright{%s}\hfil\raggedright{%s}\\[0.5em]\raggedright{%s}}]], |
| 46 |
opts.textcolor, opts.title, opts.speaker, opts.prio, opts.location, opts.time) |
| 47 |
end |
| 48 |
-- result are the global variables EVENTS, MIN and MAX |
| 49 |
function addEvent(opts) |
| 50 |
-- print("Reading event on line ", tex.inputlineno) |
| 51 |
opts.inputlineno = tex.inputlineno |
| 52 |
if(not checkKeys(opts, {"time", "day", "tikz"})) then |
| 53 |
error("missing argument") |
| 54 |
end |
| 55 |
|
| 56 |
if opts.content == nil then |
| 57 |
if opts.formatter == nil then |
| 58 |
opts.content = defaultFormatter(opts) |
| 59 |
else |
| 60 |
opts.content = opts.formatter(opts) |
| 61 |
end |
| 62 |
end |
| 63 |
|
| 64 |
opts.from,opts.to = dur2Int(opts.time) |
| 65 |
|
| 66 |
tex.print(string.format( |
| 67 |
[[\immediate\write\timetableATdataOutput{\unexpanded{{["start"]=%q, ["end"]=%q, ["title"]=%q, ["location"]=%q, ["password"]=%q, ["type"]=%q},}}]], |
| 68 |
opts.from + 24*60*day2Int(opts.day), |
| 69 |
opts.to + 24*60*day2Int(opts.day), |
| 70 |
opts.title, |
| 71 |
opts.location:match([[\href{(.*)}{.*}]]) or opts.location, |
| 72 |
opts.password, |
| 73 |
opts.type |
| 74 |
)) |
| 75 |
|
| 76 |
if(not MIN_BYPASS and opts.from < MIN) then MIN = opts.from end |
| 77 |
if(not MAX_BYPASS and opts.to > MAX) then MAX = opts.to end |
| 78 |
assert(opts.from < opts.to, "From has to be before to") |
| 79 |
|
| 80 |
table.insert(EVENTS, opts) |
| 81 |
end |
| 82 |
-- parameters are all global variables |
| 83 |
function draw(length, width) |
| 84 |
-- copy relevant variables for working on local copies |
| 85 |
local events = copy_array(EVENTS) |
| 86 |
local days = copy_array(DAYS) |
| 87 |
local min, minH, max, maxH = prepareMinMax(MIN, MAX) |
| 88 |
|
| 89 |
assert(length:match("%d*%.?%d*"), "Length must be a valid length measured in cm") |
| 90 |
length = tonumber(length) |
| 91 |
|
| 92 |
textwidth = width |
| 93 |
|
| 94 |
tex.print([[\begin{tikzpicture}]]) |
| 95 |
tex.print([[\tikzset{defStyle/.style={font=\tiny,anchor=north west,fill=blue!50,draw=black,rectangle}}]]) |
| 96 |
-- print the tabular with the weekday headers |
| 97 |
tex.print(string.format( |
| 98 |
[[\foreach \week [count=\x from 0, evaluate=\x as \y using \x+0.5] in {%s}{ ]], |
| 99 |
table.concat(days, ",") |
| 100 |
) |
| 101 |
) |
| 102 |
tex.print(string.format( |
| 103 |
[[\node[anchor=south] at (\y/%d* %s, 0) {\week};]], #days, textwidth)) |
| 104 |
tex.print(string.format( |
| 105 |
[[\draw (\x/%d * %s, 0cm) -- (\x/%d * %s, %dcm);]], |
| 106 |
#days, |
| 107 |
textwidth, |
| 108 |
#days, |
| 109 |
textwidth, -length |
| 110 |
) |
| 111 |
) |
| 112 |
tex.print("}") |
| 113 |
tex.print(string.format( |
| 114 |
[[\draw (%s, 0) -- (%s,%dcm);]], |
| 115 |
textwidth, |
| 116 |
textwidth, |
| 117 |
-length |
| 118 |
) |
| 119 |
) |
| 120 |
|
| 121 |
for i=minH,maxH do |
| 122 |
tex.print(string.format( |
| 123 |
[[\node[anchor=east] at (0,%fcm ) {%d:00};]], |
| 124 |
minuteToFrac(i*60,min,max)*-length, i |
| 125 |
) |
| 126 |
) |
| 127 |
tex.print(string.format( |
| 128 |
[[\draw (0,%fcm ) -- (%s,%fcm );]], |
| 129 |
minuteToFrac(i*60,min,max)*-length, |
| 130 |
textwidth, |
| 131 |
minuteToFrac(i*60,min,max)*-length |
| 132 |
) |
| 133 |
) |
| 134 |
end |
| 135 |
|
| 136 |
local d |
| 137 |
local red = 0.3333 -- calculated in em from inner sep |
| 138 |
local red_y = 0.25 -- calculated in em |
| 139 |
for _,e in ipairs(events) do |
| 140 |
if e.from < max and e.to > min then -- only draw if event is in scope (part of the comp is done in addEvent from < to |
| 141 |
if e.to > max then e.to = max end |
| 142 |
if e.from < min then e.from = min end |
| 143 |
-- print("Drawing event on line ", e.inputlineno) |
| 144 |
d = day2Int(e.day) |
| 145 |
tex.print(string.format( |
| 146 |
[[\node[defStyle,text width=-%fem+%f%s/%d, text depth=%fcm-%fem, text height=%fem, %s] at (%f*%s,%fcm) {%s};]], |
| 147 |
2*red, -- text width |
| 148 |
e.scale_width, -- text width |
| 149 |
textwidth, |
| 150 |
#days, -- text width |
| 151 |
length*(e.to-e.from)/(max-min), -- text depth |
| 152 |
2*red+red_y, -- text depth |
| 153 |
red_y, -- text height |
| 154 |
e.tikz, -- free tikz code |
| 155 |
(d+e.offset)/#days, -- xcoord |
| 156 |
textwidth, |
| 157 |
minuteToFrac(e.from,min,max)*-length, -- ycoord |
| 158 |
e.content -- content |
| 159 |
) |
| 160 |
) |
| 161 |
end |
| 162 |
end |
| 163 |
tex.print([[\end{tikzpicture}]]) |
| 164 |
tex.print([[\immediate\write\timetableATdataOutput{\iffalse{\else\string}\fi}]]) |
| 165 |
end |
| 166 |
function search_array(t, s) |
| 167 |
for k,v in ipairs(t) do |
| 168 |
if(v == s) then return k end |
| 169 |
end |
| 170 |
return nil |
| 171 |
end |
| 172 |
|
| 173 |
function minuteToFrac(minute, min, max) |
| 174 |
return (minute-min)/(max-min) |
| 175 |
end |
| 176 |
function prepareMinMax(min, max) |
| 177 |
local minH = math.floor(min/60) |
| 178 |
local maxH = math.ceil(max/60) |
| 179 |
local min = minH*60 |
| 180 |
local max = maxH*60 |
| 181 |
return min, minH, max, maxH |
| 182 |
end |
| 183 |
function checkKeys(t, k) |
| 184 |
for _,x in ipairs(k) do |
| 185 |
if(t[x] == nil) then |
| 186 |
return false |
| 187 |
end |
| 188 |
end |
| 189 |
return true |
| 190 |
end |
| 191 |
function dur2Int(clk) |
| 192 |
local f1,f2, t1,t2 = clk:match("^(%d%d?):(%d%d)-(%d%d?):(%d%d)$") |
| 193 |
if(f1 ~= nil and f2 ~= nil and t1 ~= nil and t2 ~= nil) then |
| 194 |
f1 = tonumber(f1) f2 = tonumber(f2) |
| 195 |
t1 = tonumber(t1) t2 = tonumber(t2) |
| 196 |
assert(f1 >= 0 and f1 < 24, "Hours have to be >= 0 && < 24") |
| 197 |
assert(f2 >= 0 and f2 < 60, "Mins have to be >= 0 && < 60") |
| 198 |
assert(t1 >= 0 and t1 < 24, "Hours have to be >= 0 && < 24") |
| 199 |
assert(t2 >= 0 and t2 < 60, "Mins have to be >= 0 && < 60") |
| 200 |
return f1*60 + f2, t1*60 + t2 |
| 201 |
else |
| 202 |
error("clk string \"" .. clk .. "\" was no valid clock string") |
| 203 |
end |
| 204 |
end |
| 205 |
function prepareDays(days) |
| 206 |
local ret = {} |
| 207 |
for m in days:gmatch("[^,]+") do |
| 208 |
table.insert(ret, m) |
| 209 |
end |
| 210 |
return ret |
| 211 |
end |
| 212 |
function day2Int(day) |
| 213 |
return search_array(DAYSE, day) - 1 |
| 214 |
end |
| 215 |
|
| 216 |
function copy_array(obj) |
| 217 |
if type(obj) ~= 'table' then return obj end |
| 218 |
local res = {} |
| 219 |
for k, v in pairs(obj) do |
| 220 |
local c = copy_array(v) |
| 221 |
res[copy_array(k)] = c |
| 222 |
end |
| 223 |
return res |
| 224 |
end |
| 225 |
|
| 226 |
semesterplannerLua = { |
| 227 |
init = init, |
| 228 |
addEvent = addEvent, |
| 229 |
draw = draw, |
| 230 |
day2Int = day2Int, |
| 231 |
} |
| 232 |
return semesterplannerLua |
| 233 |
|