Fri, 31 Jan 2025 22:27:28 +0100
improve alignment of month headers
5 | 1 | /* Copyright 2025 Mike Becker. All rights reserved. |
2 | * | |
3 | * Redistribution and use in source and binary forms, with or without | |
4 | * modification, are permitted provided that the following conditions are met: | |
5 | * | |
6 | * 1. Redistributions of source code must retain the above copyright | |
7 | * notice, this list of conditions and the following disclaimer. | |
8 | * | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * | |
13 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
16 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
17 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
18 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
19 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
20 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
21 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
22 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
23 | */ | |
24 | ||
25 | #include "html.h" | |
26 | ||
27 | #include <cstdio> | |
28 | ||
6
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
29 | #include <chrono> |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
30 | namespace chrono = std::chrono; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
31 | |
5 | 32 | namespace html { |
33 | static unsigned indentation; | |
34 | static const char *tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; | |
35 | static void indent(int change = 0) { | |
36 | indentation += change; | |
37 | fwrite(tabs, 1, indentation, stdout); | |
38 | } | |
39 | ||
40 | static std::string encode(const std::string &data) { | |
41 | std::string buffer; | |
42 | buffer.reserve(data.size()+16); | |
43 | for (const char &pos: data) { | |
44 | switch (pos) { | |
45 | case '&': | |
46 | buffer.append("&"); | |
47 | break; | |
48 | case '\"': | |
49 | buffer.append("""); | |
50 | break; | |
51 | case '\'': | |
52 | buffer.append("'"); | |
53 | break; | |
54 | case '<': | |
55 | buffer.append("<"); | |
56 | break; | |
57 | case '>': | |
58 | buffer.append(">"); | |
59 | break; | |
60 | default: | |
61 | buffer.append(&pos, 1); | |
62 | break; | |
63 | } | |
64 | } | |
65 | return buffer; | |
66 | } | |
67 | } | |
68 | ||
69 | void html::open() { | |
70 | puts( | |
71 | R"(<html> | |
72 | <head> | |
73 | <style> | |
74 | table.heatmap { | |
75 | table-layout: fixed; | |
76 | border-collapse: collapse; | |
77 | font-family: sans-serif; | |
78 | } | |
79 | table.heatmap td, table.heatmap th { | |
80 | text-align: center; | |
81 | border: solid 1px lightgray; | |
82 | height: 1.5em; | |
83 | } | |
84 | table.heatmap td { | |
85 | border: solid 1px lightgray; | |
86 | width: 1.5em; | |
87 | height: 1.5em; | |
88 | } | |
89 | table.heatmap td.out-of-range { | |
90 | background-color: gray; | |
91 | } | |
92 | </style> | |
93 | </head> | |
94 | <body>)"); | |
95 | indentation = 2; | |
96 | } | |
97 | ||
98 | void html::close() { | |
99 | puts("\t</body>\n</html>"); | |
100 | } | |
101 | ||
102 | void html::h1(const std::string& heading) { | |
103 | indent(); | |
104 | printf("<h1>%s</h1>\n", encode(heading).c_str()); | |
105 | } | |
106 | ||
107 | void html::h2(const std::string& heading) { | |
108 | indent(); | |
109 | printf("<h2>%s</h2>\n", encode(heading).c_str()); | |
110 | } | |
111 | ||
6
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
112 | void html::table_begin(int year) { |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
113 | static constexpr const char* months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
114 | // compute the column spans, first |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
115 | unsigned colspans[12] = {}; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
116 | { |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
117 | unsigned total_cols = 0; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
118 | chrono::sys_days day{chrono::year_month_day{chrono::year{year}, chrono::month{1}, chrono::day{1}}}; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
119 | if (chrono::weekday{day}.iso_encoding() != 1) { |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
120 | colspans[0] = 1; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
121 | total_cols = 1; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
122 | } |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
123 | for (unsigned col = 0; col < 12; ++col) { |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
124 | while (total_cols < html::columns && chrono::year_month_day{day}.month() <= chrono::month{col + 1}) { |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
125 | ++total_cols; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
126 | ++colspans[col]; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
127 | day += chrono::days{7}; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
128 | } |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
129 | } |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
130 | } |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
131 | |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
132 | // now render the table heading |
5 | 133 | indent(); |
134 | puts("<table class=\"heatmap\">"); | |
135 | indent(1); | |
136 | puts("<tr>"); | |
137 | indent(1); | |
138 | puts("<th></th>"); | |
139 | for (unsigned i = 0 ; i < 12 ; i++) { | |
140 | indent(); | |
141 | printf("<th scope=\"col\" colspan=\"%d\">%s</th>\n", colspans[i], months[i]); | |
142 | } | |
143 | indent(-1); | |
144 | puts("</tr>"); | |
145 | } | |
146 | ||
147 | void html::table_end() { | |
148 | indentation--; | |
149 | indent(); | |
150 | puts("</table>"); | |
151 | } | |
152 | ||
153 | void html::row_begin(unsigned int row) { | |
154 | static constexpr const char* weekdays[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; | |
155 | indent(); | |
156 | puts("<tr>"); | |
157 | indent(1); | |
158 | printf("<th scope=\"row\">%s</th>\n", weekdays[row]); | |
159 | } | |
160 | ||
161 | void html::row_end() { | |
162 | indent(-1); | |
163 | puts("</tr>"); | |
164 | } | |
165 | ||
166 | void html::cell_out_of_range() { | |
167 | indent(); | |
168 | puts("<td class=\"out-of-range\"></td>"); | |
169 | } | |
170 | ||
171 | void html::cell(unsigned commits) { | |
172 | indent(); | |
173 | // TODO: use nice coloring and tooltips instead of printing the commits | |
174 | printf("<td>%u</td>\n", commits); | |
175 | } |