izzi
SVG SUBSET C++ API
Loading...
Searching...
No Matches
izzi-tables.h
Go to the documentation of this file.
1// izzi HTML tables -*- mode: C++ -*-
2
3// Copyright (c) 2025-2026, Benjamin De Kosnik <b.dekosnik@gmail.com>
4
5// This file is part of the alpha60 library. This library is free
6// software; you can redistribute it and/or modify it under the terms
7// of the GNU General Public License as published by the Free Software
8// Foundation; either version 3, or (at your option) any later
9// version.
10
11// This library is distributed in the hope that it will be useful, but
12// WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15
16#ifndef izzi_TABLES_H
17#define izzi_TABLES_H 1
18
19#include "a60-svg.h"
20#include <string_view>
21
22namespace svg {
23
24using std::ostringstream;
25
26/// Converts ISO datestamp to human-readable long form with month names.
27string
29{
30 // Build the HTML time element
31 string html_element;
32
33 string ds_long("");
34 std::chrono::year_month_day ymd;
35 std::stringstream ss(ds);
36 // %F is the standard specifier for YYYY-MM-DD (ISO 8601)
37 if (std::chrono::from_stream(ss, "%F", ymd))
38 ds_long = std::format("{:%B %d, %Y}", ymd);
39 else
40 {
41 std::cerr << "datestamp_to_html_time:: error with datestamp '"
42 << ds << "'" << std::endl;
43 }
44
45 // <time datetime="YYYY-MM-DD" aria-label="Month Day, Year">Month Day, Year</time>
46 html_element = "<time datetime=";
47 html_element += k::quote;
48 html_element += ds;
49 html_element += k::quote;
50 html_element += k::space;
51 html_element += "aria-label=";
52 html_element += k::quote;
53 html_element += ds_long;
54 html_element += k::quote;
55 html_element += ">";
56 html_element += k::space;
57 html_element += ds;
58 html_element += k::space;
59 html_element += "</time>";
60
61 return html_element;
62}
63
64
65// Image row in table.
66string
67serialize_row_2c_4f(string_view img1src, string_view img1alt,
68 string_view img2src, string_view img2alt)
69{
70 // td elements == left
71 // th elements == center
72
73 // NB: Use {{}} for literal quoting, as src may have '/' and '&' etc.
74 constexpr const char* tdblank = R"_delimiter_(<th style="padding: 5px;"><img src="{{}}" alt="{{}}" width="50%"></th>)_delimiter_";
75
76 ostringstream oss;
77 oss << "<tr>" << svg::k::newline;
78 oss << std::format(tdblank, img1src, img1alt) << svg::k::newline;
79 oss << std::format(tdblank, img2src, img2alt) << svg::k::newline;
80 oss << "</tr>" << svg::k::newline;
81 return oss.str();
82}
83
84
85/// Serialize borderless image table.
86void
87serialize_2_image_table(const string& gtitlelc)
88{
89 const string tblstart = R"_delimiter_(<table style="border-collapse: collapse; width: 100%;">)_delimiter_";
90 const string tblend = "</table>";
91
92 string img1 = "";
93 string img1alt = "";
94 string img2 = "";
95 string img2alt = "";
96
97 ostringstream oss;
98 oss << tblstart << svg::k::newline;
99 oss << serialize_row_2c_4f(img1, img1alt, img2, img2alt);
100 oss << tblend << svg::k::newline;
101
102 const string metricf = gtitlelc + "-image-table.html";
103 std::ofstream ofs(metricf);
104 if (ofs.good())
105 ofs << oss.str() << svg::k::newline;
106}
107
108
109/// Serialize information about the analysis passes as an html table.
110/// Flatten all objects into one total number.
111void
112serialize_meta_collection_table(const string& gtitlelc, const string sdur,
113 const uint btihasz,
114 const uint dl, const uint ul)
115{
116 /*
117 HTML table element, simplified as
118 <table>
119 <thead>
120 </thead>
121 <tbody>
122 <tr>
123 <td>HTML tables</td>
124 </tr>
125 </tbody>
126 </table>
127
128 duration btihasz downloaders* uploaders*
129 * = total, per btiha
130
131 QED 4-column-6-field row
132 */
133 const string metricf = gtitlelc + "-meta-collection-table.html";
134 std::ofstream ofs(metricf);
135 if (ofs.good())
136 {
137 const string thead = R"_delimiter_(
138 <thead>
139 <tr>
140 <th rowspan="2" width="25%">duration</th>
141 <th rowspan="2" width="25%">btiha size</th>
142 <th colspan="2" width="25%">downloaders</th>
143 <th colspan="2" width="25%">uploaders</th>
144 </tr>
145 <tr>
146 <th>total</th>
147 <th>per btiha</th>
148 <th>total</th>
149 <th>per btiha</th>
150 </tr>
151 </thead>
152 )_delimiter_";
153
154 ofs.imbue(std::locale(""));
155 ofs << std::fixed << std::setprecision(0);
156
157 ofs << "<table>" << svg::k::newline;
158 ofs << thead << svg::k::newline;
159 string tts(gtitlelc);
160 std::ranges::replace(tts, k::hyphen, k::space);
161 ofs << "<caption>" << tts << "</caption>" << svg::k::newline;
162 ofs << "<tbody>" << svg::k::newline;
163 ofs << "<tr>" << svg::k::newline;
164
165 ofs << "<td>" << sdur << "</td>" << svg::k::newline;
166 ofs << "<th>" << btihasz << "</th>" << svg::k::newline;
167 ofs << "<td>" << dl << "</td>" << svg::k::newline;
168 ofs << "<td>" << dl / btihasz << "</td>" << svg::k::newline;
169 ofs << "<td>" << ul << "</td>" << svg::k::newline;
170 ofs << "<td>" << ul / btihasz << "</td>" << svg::k::newline;
171
172 ofs << "</tr>" << svg::k::newline;
173 ofs << "</tbody>" << svg::k::newline;
174 ofs << "</table>" << svg::k::newline;
175 }
176}
177
178
179/// Serialize row of media object information.
180///
181/// media_object btihasz downloaders* uploaders* #weeks sample_dates
182/// * = total, per btiha
183string
184serialize_row_6c_8f(const string moname, const uint btihasz,
185 const uint dl, const uint ul,
186 const uint weeksn, const string sdates)
187{
188 ostringstream oss;
189 oss.imbue(std::locale(""));
190 oss << std::fixed << std::setprecision(0);
191
192 oss << "<tr>" << svg::k::newline;
193
194 // td elements == left
195 // th elements == center
196 oss << "<td>" << moname << "</td>" << svg::k::newline;
197 oss << "<td>" << weeksn << "</td>" << svg::k::newline;
198 oss << "<td>" << sdates << "</td>" << svg::k::newline;
199 oss << "<td>" << btihasz << "</td>" << svg::k::newline;
200 oss << "<td>" << dl << "</td>" << svg::k::newline;
201 oss << "<td>" << dl / btihasz << "</td>" << svg::k::newline;
202 oss << "<td>" << ul << "</td>" << svg::k::newline;
203 oss << "<td>" << ul / btihasz << "</td>" << svg::k::newline;
204
205 oss << "</tr>" << svg::k::newline;
206
207 return oss.str();
208}
209
210
211} // namespace svg
212
213#endif
constexpr char newline('\n')
string serialize_row_6c_8f(const string moname, const uint btihasz, const uint dl, const uint ul, const uint weeksn, const string sdates)
Serialize row of media object information.
void serialize_2_image_table(const string &gtitlelc)
Serialize borderless image table.
Definition izzi-tables.h:87
string iso_datestamp_string_to_html_time(const string ds)
Converts ISO datestamp to human-readable long form with month names.
Definition izzi-tables.h:28
string serialize_row_2c_4f(string_view img1src, string_view img1alt, string_view img2src, string_view img2alt)
Definition izzi-tables.h:67
unsigned int uint
Definition a60-svg.h:58
void serialize_meta_collection_table(const string &gtitlelc, const string sdur, const uint btihasz, const uint dl, const uint ul)
Serialize information about the analysis passes as an html table. Flatten all objects into one total ...