24 |
24 |
25 #include "html.h" |
25 #include "html.h" |
26 |
26 |
27 #include <cstdio> |
27 #include <cstdio> |
28 |
28 |
29 namespace chrono = std::chrono; |
29 using namespace std::chrono; |
30 using chrono::operator ""d; |
|
31 |
30 |
32 namespace html { |
31 namespace html { |
33 static constexpr const char* weekdays[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; |
32 static constexpr const char* weekdays[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; |
34 |
33 |
35 static unsigned indentation; |
34 static unsigned indentation; |
133 void html::h2(const std::string& heading) { |
132 void html::h2(const std::string& heading) { |
134 indent(); |
133 indent(); |
135 printf("<h2>%s</h2>\n", encode(heading).c_str()); |
134 printf("<h2>%s</h2>\n", encode(heading).c_str()); |
136 } |
135 } |
137 |
136 |
138 void html::table_begin(int year) { |
137 void html::table_begin(year y) { |
139 static constexpr const char* months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
138 static constexpr const char* months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
140 // compute the column spans, first |
139 // compute the column spans, first |
141 unsigned colspans[12] = {}; |
140 unsigned colspans[12] = {}; |
142 { |
141 { |
143 unsigned total_cols = 0; |
142 unsigned total_cols = 0; |
144 chrono::sys_days day{chrono::year_month_day{chrono::year{year}, chrono::January, 1d}}; |
143 sys_days day{year_month_day{y, January, 1d}}; |
145 if (chrono::weekday{day}.iso_encoding() != 1) { |
144 if (weekday{day}.iso_encoding() != 1) { |
146 colspans[0] = 1; |
145 colspans[0] = 1; |
147 total_cols = 1; |
146 total_cols = 1; |
148 } |
147 } |
149 for (unsigned col = 0; col < 12; ++col) { |
148 for (unsigned col = 0; col < 12; ++col) { |
150 while (total_cols < html::columns && chrono::year_month_day{day}.month() <= chrono::month{col + 1}) { |
149 while (total_cols < html::columns && year_month_day{day}.month() <= month{col + 1}) { |
151 ++total_cols; |
150 ++total_cols; |
152 ++colspans[col]; |
151 ++colspans[col]; |
153 day += chrono::days{7}; |
152 day += days{7}; |
154 } |
153 } |
155 } |
154 } |
156 } |
155 } |
157 |
156 |
158 // now render the table heading |
157 // now render the table heading |
191 void html::cell_out_of_range() { |
190 void html::cell_out_of_range() { |
192 indent(); |
191 indent(); |
193 puts("<td class=\"out-of-range\"></td>"); |
192 puts("<td class=\"out-of-range\"></td>"); |
194 } |
193 } |
195 |
194 |
196 void html::cell(std::chrono::year_month_day ymd, unsigned commits) { |
195 void html::cell(year_month_day ymd, unsigned commits) { |
197 const char *color_class; |
196 const char *color_class; |
198 if (commits == 0) { |
197 if (commits == 0) { |
199 color_class = "zero-commits"; |
198 color_class = "zero-commits"; |
200 } else if (commits == 1) { |
199 } else if (commits == 1) { |
201 color_class = "one-commit"; |
200 color_class = "one-commit"; |
209 color_class = "commit-spam"; |
208 color_class = "commit-spam"; |
210 } |
209 } |
211 indent(); |
210 indent(); |
212 printf("<td class=\"%s\" title=\"%s, %d-%u-%u: %u commits\"></td>\n", |
211 printf("<td class=\"%s\" title=\"%s, %d-%u-%u: %u commits\"></td>\n", |
213 color_class, |
212 color_class, |
214 weekdays[chrono::weekday(ymd).iso_encoding() - 1], |
213 weekdays[weekday(ymd).iso_encoding() - 1], |
215 static_cast<int>(ymd.year()), |
214 static_cast<int>(ymd.year()), |
216 static_cast<unsigned>(ymd.month()), |
215 static_cast<unsigned>(ymd.month()), |
217 static_cast<unsigned>(ymd.day()), |
216 static_cast<unsigned>(ymd.day()), |
218 commits); |
217 commits); |
219 } |
218 } |