Tue, 25 Feb 2025 18:22:55 +0100
add dist target
fixes #607
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 { |
42 | 75 | puts(R"(<!DOCTYPE html> |
76 | <html> | |
5 | 77 | <head> |
78 | <style> | |
79 | table.heatmap { | |
80 | table-layout: fixed; | |
81 | border-collapse: collapse; | |
82 | font-family: sans-serif; | |
83 | } | |
84 | table.heatmap td, table.heatmap th { | |
85 | text-align: center; | |
86 | border: solid 1px lightgray; | |
87 | height: 1.5em; | |
88 | } | |
89 | table.heatmap td { | |
90 | border: solid 1px lightgray; | |
91 | width: 1.5em; | |
92 | height: 1.5em; | |
93 | } | |
94 | table.heatmap td.out-of-range { | |
95 | background-color: gray; | |
96 | } | |
7 | 97 | |
98 | table.heatmap td.zero-commits { | |
99 | background-color: white; | |
100 | } | |
101 | ||
102 | table.heatmap td.one-commit { | |
103 | background-color: #80E7A0; | |
104 | } | |
105 | ||
106 | table.heatmap td.up-to-5-commits { | |
107 | background-color: #30D350; | |
108 | } | |
109 | ||
110 | table.heatmap td.up-to-10-commits { | |
111 | background-color: #00BF00; | |
112 | } | |
113 | ||
114 | table.heatmap td.up-to-20-commits { | |
115 | background-color: #00A300; | |
116 | } | |
117 | ||
118 | table.heatmap td.commit-spam { | |
119 | background-color: #008000; | |
120 | } | |
5 | 121 | </style> |
122 | </head> | |
20
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
123 | <body> |
41
19cc90878968
fix wrong escape in raw string
Mike Becker <universe@uap-core.de>
parents:
35
diff
changeset
|
124 | <div class="heatmap-content">)"); |
20
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
125 | indentation = 3; |
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
126 | } |
5 | 127 | } |
128 | ||
20
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
129 | void html::close(bool fragment) { |
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
130 | if (fragment) { |
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
131 | puts("</div>"); |
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
132 | } else { |
22
a9230f197e61
fix inconsistent use of tabs and spaces in indentation
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
133 | puts(" </div>\n </body>\n</html>"); |
20
8639ccd855ba
implement --fragment option
Mike Becker <universe@uap-core.de>
parents:
15
diff
changeset
|
134 | } |
5 | 135 | } |
136 | ||
137 | void html::h1(const std::string& heading) { | |
138 | indent(); | |
139 | printf("<h1>%s</h1>\n", encode(heading).c_str()); | |
140 | } | |
141 | ||
142 | void html::h2(const std::string& heading) { | |
143 | indent(); | |
144 | printf("<h2>%s</h2>\n", encode(heading).c_str()); | |
145 | } | |
146 | ||
13 | 147 | void html::table_begin(year y) { |
6
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
148 | 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
|
149 | // compute the column spans, first |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
150 | unsigned colspans[12] = {}; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
151 | { |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
152 | unsigned total_cols = 0; |
13 | 153 | 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
|
154 | for (unsigned col = 0; col < 12; ++col) { |
13 | 155 | 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
|
156 | ++total_cols; |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
157 | ++colspans[col]; |
13 | 158 | day += days{7}; |
6
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 | |
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
163 | // now render the table heading |
5 | 164 | indent(); |
165 | puts("<table class=\"heatmap\">"); | |
166 | indent(1); | |
167 | puts("<tr>"); | |
168 | indent(1); | |
169 | puts("<th></th>"); | |
170 | for (unsigned i = 0 ; i < 12 ; i++) { | |
171 | indent(); | |
172 | printf("<th scope=\"col\" colspan=\"%d\">%s</th>\n", colspans[i], months[i]); | |
173 | } | |
174 | indent(-1); | |
175 | puts("</tr>"); | |
176 | } | |
177 | ||
178 | void html::table_end() { | |
179 | indentation--; | |
180 | indent(); | |
181 | puts("</table>"); | |
182 | } | |
183 | ||
184 | void html::row_begin(unsigned int row) { | |
185 | indent(); | |
186 | puts("<tr>"); | |
187 | indent(1); | |
188 | printf("<th scope=\"row\">%s</th>\n", weekdays[row]); | |
189 | } | |
190 | ||
191 | void html::row_end() { | |
192 | indent(-1); | |
193 | puts("</tr>"); | |
194 | } | |
195 | ||
196 | void html::cell_out_of_range() { | |
197 | indent(); | |
198 | puts("<td class=\"out-of-range\"></td>"); | |
199 | } | |
200 | ||
13 | 201 | void html::cell(year_month_day ymd, unsigned commits) { |
7 | 202 | const char *color_class; |
203 | if (commits == 0) { | |
204 | color_class = "zero-commits"; | |
205 | } else if (commits == 1) { | |
206 | color_class = "one-commit"; | |
207 | } else if (commits <= 5) { | |
208 | color_class = "up-to-5-commits"; | |
209 | } else if (commits <= 10) { | |
210 | color_class = "up-to-10-commits"; | |
211 | } else if (commits <= 20) { | |
212 | color_class = "up-to-20-commits"; | |
213 | } else { | |
214 | color_class = "commit-spam"; | |
215 | } | |
5 | 216 | indent(); |
35
d75805c1e3b9
fix "1 commits" fixes #601
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
217 | printf("<td class=\"%s\" title=\"%s, %d-%02u-%02u: %u %s\"></td>\n", |
7 | 218 | color_class, |
13 | 219 | weekdays[weekday(ymd).iso_encoding() - 1], |
7 | 220 | static_cast<int>(ymd.year()), |
221 | static_cast<unsigned>(ymd.month()), | |
222 | static_cast<unsigned>(ymd.day()), | |
35
d75805c1e3b9
fix "1 commits" fixes #601
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
223 | commits, |
d75805c1e3b9
fix "1 commits" fixes #601
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
224 | commits == 1 ? "commit" : "commits"); |
5 | 225 | } |