Wed, 19 Feb 2025 18:32:17 +0100
fix date filters
fixes #599
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 | ||
13 | 29 | using namespace std::chrono; |
6
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
30 | |
5 | 31 | namespace html { |
7 | 32 | static constexpr const char* weekdays[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; |
33 | ||
5 | 34 | static unsigned indentation; |
22
a9230f197e61
fix inconsistent use of tabs and spaces in indentation
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
35 | static const char *tabs = " "; |
5 | 36 | static void indent(int change = 0) { |
37 | indentation += change; | |
22
a9230f197e61
fix inconsistent use of tabs and spaces in indentation
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
38 | fwrite(tabs, 4, indentation, stdout); |
5 | 39 | } |
40 | ||
41 | static std::string encode(const std::string &data) { | |
42 | std::string buffer; | |
43 | buffer.reserve(data.size()+16); | |
44 | for (const char &pos: data) { | |
45 | switch (pos) { | |
46 | case '&': | |
47 | buffer.append("&"); | |
48 | break; | |
49 | case '\"': | |
50 | buffer.append("""); | |
51 | break; | |
52 | case '\'': | |
53 | buffer.append("'"); | |
54 | break; | |
55 | case '<': | |
56 | buffer.append("<"); | |
57 | break; | |
58 | case '>': | |
59 | buffer.append(">"); | |
60 | break; | |
61 | default: | |
62 | buffer.append(&pos, 1); | |
63 | break; | |
64 | } | |
65 | } | |
66 | return buffer; | |
67 | } | |
68 | } | |
69 | ||
20
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
70 | void html::open(bool fragment) { |
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
71 | if (fragment) { |
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
72 | puts("<div class=\"heatmap-content\">"); |
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
73 | indentation = 1; |
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
74 | } else { |
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
75 | puts(R"(<html> |
5 | 76 | <head> |
77 | <style> | |
78 | table.heatmap { | |
79 | table-layout: fixed; | |
80 | border-collapse: collapse; | |
81 | font-family: sans-serif; | |
82 | } | |
83 | table.heatmap td, table.heatmap th { | |
84 | text-align: center; | |
85 | border: solid 1px lightgray; | |
86 | height: 1.5em; | |
87 | } | |
88 | table.heatmap td { | |
89 | border: solid 1px lightgray; | |
90 | width: 1.5em; | |
91 | height: 1.5em; | |
92 | } | |
93 | table.heatmap td.out-of-range { | |
94 | background-color: gray; | |
95 | } | |
7 | 96 | |
97 | table.heatmap td.zero-commits { | |
98 | background-color: white; | |
99 | } | |
100 | ||
101 | table.heatmap td.one-commit { | |
102 | background-color: #80E7A0; | |
103 | } | |
104 | ||
105 | table.heatmap td.up-to-5-commits { | |
106 | background-color: #30D350; | |
107 | } | |
108 | ||
109 | table.heatmap td.up-to-10-commits { | |
110 | background-color: #00BF00; | |
111 | } | |
112 | ||
113 | table.heatmap td.up-to-20-commits { | |
114 | background-color: #00A300; | |
115 | } | |
116 | ||
117 | table.heatmap td.commit-spam { | |
118 | background-color: #008000; | |
119 | } | |
5 | 120 | </style> |
121 | </head> | |
20
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
122 | <body> |
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
123 | <div class=\"heatmap-content\">)"); |
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
124 | indentation = 3; |
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
125 | } |
5 | 126 | } |
127 | ||
20
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
128 | void html::close(bool fragment) { |
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
129 | if (fragment) { |
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
130 | puts("</div>"); |
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
131 | } else { |
22
a9230f197e61
fix inconsistent use of tabs and spaces in indentation
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
132 | puts(" </div>\n </body>\n</html>"); |
20
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
133 | } |
5 | 134 | } |
135 | ||
136 | void html::h1(const std::string& heading) { | |
137 | indent(); | |
138 | printf("<h1>%s</h1>\n", encode(heading).c_str()); | |
139 | } | |
140 | ||
141 | void html::h2(const std::string& heading) { | |
142 | indent(); | |
143 | printf("<h2>%s</h2>\n", encode(heading).c_str()); | |
144 | } | |
145 | ||
13 | 146 | void html::table_begin(year y) { |
6
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
147 | 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
|
148 | // compute the column spans, first |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
149 | unsigned colspans[12] = {}; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
150 | { |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
151 | unsigned total_cols = 0; |
13 | 152 | sys_days day{year_month_day{y, January, 1d}}; |
6
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
153 | for (unsigned col = 0; col < 12; ++col) { |
13 | 154 | while (total_cols < html::columns && year_month_day{day}.month() <= month{col + 1}) { |
6
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
155 | ++total_cols; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
156 | ++colspans[col]; |
13 | 157 | day += days{7}; |
6
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
158 | } |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
159 | } |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
160 | } |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
161 | |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
162 | // now render the table heading |
5 | 163 | indent(); |
164 | puts("<table class=\"heatmap\">"); | |
165 | indent(1); | |
166 | puts("<tr>"); | |
167 | indent(1); | |
168 | puts("<th></th>"); | |
169 | for (unsigned i = 0 ; i < 12 ; i++) { | |
170 | indent(); | |
171 | printf("<th scope=\"col\" colspan=\"%d\">%s</th>\n", colspans[i], months[i]); | |
172 | } | |
173 | indent(-1); | |
174 | puts("</tr>"); | |
175 | } | |
176 | ||
177 | void html::table_end() { | |
178 | indentation--; | |
179 | indent(); | |
180 | puts("</table>"); | |
181 | } | |
182 | ||
183 | void html::row_begin(unsigned int row) { | |
184 | indent(); | |
185 | puts("<tr>"); | |
186 | indent(1); | |
187 | printf("<th scope=\"row\">%s</th>\n", weekdays[row]); | |
188 | } | |
189 | ||
190 | void html::row_end() { | |
191 | indent(-1); | |
192 | puts("</tr>"); | |
193 | } | |
194 | ||
195 | void html::cell_out_of_range() { | |
196 | indent(); | |
197 | puts("<td class=\"out-of-range\"></td>"); | |
198 | } | |
199 | ||
13 | 200 | void html::cell(year_month_day ymd, unsigned commits) { |
7 | 201 | const char *color_class; |
202 | if (commits == 0) { | |
203 | color_class = "zero-commits"; | |
204 | } else if (commits == 1) { | |
205 | color_class = "one-commit"; | |
206 | } else if (commits <= 5) { | |
207 | color_class = "up-to-5-commits"; | |
208 | } else if (commits <= 10) { | |
209 | color_class = "up-to-10-commits"; | |
210 | } else if (commits <= 20) { | |
211 | color_class = "up-to-20-commits"; | |
212 | } else { | |
213 | color_class = "commit-spam"; | |
214 | } | |
5 | 215 | indent(); |
35
d75805c1e3b9
fix "1 commits" fixes #601
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
216 | printf("<td class=\"%s\" title=\"%s, %d-%02u-%02u: %u %s\"></td>\n", |
7 | 217 | color_class, |
13 | 218 | weekdays[weekday(ymd).iso_encoding() - 1], |
7 | 219 | static_cast<int>(ymd.year()), |
220 | static_cast<unsigned>(ymd.month()), | |
221 | static_cast<unsigned>(ymd.day()), | |
35
d75805c1e3b9
fix "1 commits" fixes #601
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
222 | commits, |
d75805c1e3b9
fix "1 commits" fixes #601
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
223 | commits == 1 ? "commit" : "commits"); |
5 | 224 | } |