Debian Codegraph @

texmf-dist/tex/lualatex/semesterplannerlua/semesterplannerlua_calendar.lua

raw
1 local dateLib = require "date"
2 function init(clear)
3 -- clean up first
4 -- global variable
5 if clear then
6 EVENTS = {}
7 end
8 end
9
10 text = {
11 print = function(s)
12 -- print("\"" .. s .. "\"")
13 tex.print(s)
14 end
15 }
16
17 function genDot(opts)
18 dot = ""
19 if opts.draw then
20 dot = string.format([[\tikz[baseline=(X.base)]\node (X) [fill opacity=.5,fill=red,circle,inner sep=0mm, %s] {\phantom{D}};]], opts.tikz)
21 end
22 return dot
23 end
24
25 function addEvent(opts)
26 opts.inputlineno = tex.inputlineno
27 -- print(string.format("collecting from line %d", opts.inputlineno))
28 if opts.draw then
29 assert(opts.date ~= nil and opts.tikz ~= nil, "date and tikz has to be given")
30 if opts.endDate == nil or opts.endDate == '' then
31 table.insert(EVENTS, {shift=opts.shift,date=dateLib(opts.date), tikz=opts.tikz, period=opts.period, endDate=nil, inputlineno=opts.inputlineno})
32 else
33 table.insert(EVENTS, {shift=opts.shift,date=dateLib(opts.date), tikz=opts.tikz, period=opts.period, endDate=dateLib(opts.endDate), inputlineno=opts.inputlineno})
34 end
35 end
36 end
37
38 function addAppointment(opts)
39 addEvent(opts)
40 dot = genDot(opts)
41 if opts.print then
42 tex.sprint(string.format([[\textit{%s} & %s & %s%s & %s & %s & %s\\]], opts.date, opts.time, dot, opts.course, opts.desc, opts.room, opts.prio))
43 else
44 tex.sprint("%")
45 end
46 end
47
48 function addExam(opts)
49 addEvent(opts)
50 dot = genDot(opts)
51 if opts.print then
52 tex.sprint(string.format([[\textit{%s} & %s & %s%s & %s & %s \\]], opts.date, opts.time, dot, opts.course, opts.type, opts.desc))
53 else
54 tex.sprint("%")
55 end
56 end
57
58 function addDeadline(opts)
59 addEvent(opts)
60 dot = genDot(opts)
61 if opts.print then
62 tex.sprint(string.format([[\textit{%s} & %s%s & %s & %s \\]], opts.date, dot, opts.course, opts.desc, opts.prio))
63 else
64 tex.sprint("%")
65 end
66 end
67
68 function drawCalendar(minDate, maxDate, cols)
69 minDate = dateLib(minDate)
70 maxDate = dateLib(maxDate)
71 text.print([[\begin{tikzpicture}[every calendar/.style={day headings=red!50,day letter headings,inner sep=2pt, week list, month label above centered, month text={\textcolor{red}{\%mt} \%y-}, every month/.style={yshift=3ex}}] ]])
72 text.print([[\matrix[column sep=1em, row sep=1em]{]])
73 local i = 1
74 running = true
75 while running do
76 -- derive end from start, then check if maxDate is reached
77 endDate = minDate:copy():addmonths(1):setday(1):adddays(-1)
78 if endDate >= maxDate then
79 endDate = maxDate
80 running = false
81 end
82 text.print(string.format(
83 [[\calendar (%04d-%02d) [dates=%04d-%02d-%02d to %04d-%02d-%02d] if (Sunday) [red] if (Saturday) [red!50!white] if (equals=\year-\month-\day) [nodes={inner sep=.25em,rectangle,line width=1pt,draw}] if (at least=\year-\month-\day) {} else [nodes={strike out, draw}]; ]],
84 minDate:getyear(), minDate:getmonth(), minDate:getyear(), minDate:getmonth(), minDate:getday(), endDate:getyear(), endDate:getmonth(), endDate:getday()))
85
86 minDate:addmonths(1)
87 minDate:setday(1)
88
89 if i % cols == 0 or not running then
90 text.print([[\\]])
91 else
92 text.print([[&]])
93 end
94 i = i + 1
95 end
96 text.print([[ }; ]])
97
98 local usedDates = {}
99 text.print([[\begin{scope}[on background layer] ]])
100 for i,ele in ipairs(EVENTS) do
101 -- print(string.format("Drawing item from line %d", ele.inputlineno))
102 while ele.date <= maxDate and (ele.endDate == nil or ele.date <= ele.endDate) do
103 local xshift = 0
104 if ele.shift then
105 if usedDates[tostring(ele.date)] ~= nil then
106 xshift = math.ceil(usedDates[tostring(ele.date)] / 2)
107 if usedDates[tostring(ele.date)] % 2 == 0 then
108 xshift = -xshift
109 end
110 usedDates[tostring(ele.date)] = usedDates[tostring(ele.date)] + 1
111 else
112 usedDates[tostring(ele.date)] = 1
113 end
114 end
115 text.print(string.format([[\node[xshift=%d mm, fill opacity=.5,fill=red,circle,text width=0ex,inner sep=1.1ex, %s] at (%04d-%02d-%04d-%02d-%02d) {};]],
116 xshift, ele.tikz, ele.date:getyear(), ele.date:getmonth(), ele.date:getyear(), ele.date:getmonth(), ele.date:getday()))
117 if ele.period == nil then break end
118 ele.date:adddays(ele.period)
119 end
120 end
121 text.print([[\end{scope}]])
122 text.print([[\end{tikzpicture}]])
123 end
124
125 semesterplannerLuaCal = {
126 init = init,
127 addAppointment = addAppointment,
128 addDeadline = addDeadline,
129 addExam = addExam,
130 drawCalendar = drawCalendar,
131 }
132 return semesterplannerLuaCal