1 use std::borrow::ToOwned;
2 use std::collections::HashMap;
5 use std::iter::Iterator;
7 const HEADER: &str = "<!DOCTYPE html>
10 <meta charset=\"utf-8\">
11 <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
13 td { text-align: center; }
14 /* h/t https://wabain.github.io/2019/10/13/css-rotated-table-header.html */
15 th, td { white-space: nowrap; }
16 th { text-align: left; font-weight: normal; }
17 table { border-collapse: collapse }
18 tr.key > th { height: 10em; vertical-align: bottom; line-height: 1 }
19 tr.key > th > div { width: 1em; }
20 tr.key > th > div > div { width: 5em; transform-origin: bottom left; transform: translateX(1em) rotate(-65deg) }
21 td { border: thin solid gray; }
22 td.yes { border: thin solid gray; background-color: #ddd; }
23 /* h/t https://stackoverflow.com/questions/5687035/css-bolding-some-text-without-changing-its-containers-size/46452396#46452396 */
24 .highlight { text-shadow: -0.06ex 0 black, 0.06ex 0 black; }
27 function highlight(id) { const e = document.getElementById(id); if (e) { e.classList.add( \"highlight\"); } }
28 function clear_highlight(id) { const e = document.getElementById(id); if (e) { e.classList.remove(\"highlight\"); } }
29 function h2(a, b) { highlight(a); highlight(b); }
30 function ch2(a, b) { clear_highlight(a); clear_highlight(b); }
37 const FOOTER: &str = " </tbody>
42 #[derive(PartialEq, Eq, Debug)]
43 pub struct HTML(String);
45 fn escape(value: &str) -> HTML {
46 let mut escaped: String = String::new();
47 for c in value.chars() {
49 '>' => escaped.push_str(">"),
50 '<' => escaped.push_str("<"),
51 '\'' => escaped.push_str("'"),
52 '"' => escaped.push_str("""),
53 '&' => escaped.push_str("&"),
54 ok_c => escaped.push(ok_c),
60 impl From<&str> for HTML {
61 fn from(value: &str) -> HTML {
62 HTML(String::from(value))
65 impl FromIterator<HTML> for HTML {
66 fn from_iter<T>(iter: T) -> HTML
68 T: IntoIterator<Item = HTML>,
70 HTML(iter.into_iter().map(|html| html.0).collect::<String>())
73 impl std::fmt::Display for HTML {
74 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75 write!(f, "{}", self.0)
79 #[derive(Debug, PartialEq, Eq)]
83 Entry(&'a str, Option<&'a str>),
85 impl<'a> From<&'a str> for InputLine<'a> {
86 fn from(value: &'a str) -> InputLine<'a> {
87 let trimmed = value.trim_end();
88 if trimmed.is_empty() {
90 } else if !trimmed.starts_with(' ') {
91 InputLine::RowHeader(value.trim())
93 match value.split_once(':') {
94 None => InputLine::Entry(value.trim(), None),
95 Some((col, instance)) => InputLine::Entry(col.trim(), Some(instance.trim())),
101 #[derive(Debug, PartialEq, Eq)]
104 entries: HashMap<String, Vec<Option<String>>>,
107 struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> {
108 input: std::iter::Enumerate<Input>,
111 impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> {
112 fn new(input: Input) -> Self {
114 input: input.enumerate(),
119 impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader<Input> {
120 type Item = Result<Row, std::io::Error>;
121 fn next(&mut self) -> Option<Self::Item> {
123 match self.input.next() {
124 None => return Ok(std::mem::take(&mut self.row)).transpose(),
125 Some((_, Err(e))) => return Some(Err(e)),
126 Some((n, Ok(line))) => match InputLine::from(line.as_ref()) {
127 InputLine::Blank if self.row.is_some() => {
128 return Ok(std::mem::take(&mut self.row)).transpose()
130 InputLine::Blank => {}
131 InputLine::Entry(col, instance) => match &mut self.row {
133 return Some(Err(std::io::Error::other(format!(
134 "{}: Entry with no header",
138 Some(ref mut row) => {
140 .entry(col.to_owned())
141 .and_modify(|is| is.push(instance.map(ToOwned::to_owned)))
142 .or_insert_with(|| vec![instance.map(ToOwned::to_owned)]);
145 InputLine::RowHeader(row) => {
146 let prev = std::mem::take(&mut self.row);
147 self.row = Some(Row {
148 label: row.to_owned(),
149 entries: HashMap::new(),
152 return Ok(prev).transpose();
161 fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<Row, std::io::Error>> {
162 Reader::new(std::io::BufReader::new(input).lines())
165 fn column_counts(rows: &[Row]) -> Vec<(usize, String)> {
166 let mut counts: Vec<_> = rows
168 .flat_map(|r| r.entries.keys())
169 .fold(HashMap::new(), |mut cs, col| {
170 cs.entry(col.to_owned())
171 .and_modify(|n| *n += 1)
176 .map(|(col, n)| (n, col))
178 counts.sort_unstable_by(|(an, acol), (bn, bcol)| bn.cmp(an).then(acol.cmp(bcol)));
181 fn column_order(rows: &[Row]) -> Vec<String> {
188 fn render_instance(instance: &Option<String>) -> HTML {
190 None => HTML::from("✓"),
191 Some(instance) => HTML::escape(instance.as_ref()),
195 fn render_cell(col: &str, row: &Row) -> HTML {
196 let row_label = HTML::escape(row.label.as_ref());
197 let col_label = HTML::escape(col);
198 let instances: Option<&Vec<Option<String>>> = row.entries.get(col);
199 let class = HTML::from(if instances.is_none() { "" } else { "yes" });
200 let all_empty = instances
202 .flat_map(|is| is.iter())
203 .all(Option::is_none);
204 let contents = if instances.is_none() || (all_empty && instances.unwrap().len() == 1) {
206 } else if all_empty {
207 HTML(format!("{}", instances.unwrap().len()))
213 .map(render_instance)
214 .map(|html| html.0) // Waiting for slice_concat_trait to stabilize
219 HTML(format!("<td class=\"{class}\" onmouseover=\"h2('{row_label}','{col_label}')\" onmouseout=\"ch2('{row_label}','{col_label}')\">{contents}</td>"))
222 fn render_row(columns: &[String], row: &Row) -> HTML {
223 let row_label = HTML::escape(row.label.as_ref());
225 "<tr><th id=\"{row_label}\">{row_label}</th>{}</tr>\n",
228 .map(|col| render_cell(col, row))
233 fn render_column_headers(columns: &[String]) -> HTML {
235 String::from("<tr class=\"key\"><th></th>")
236 + &columns.iter().fold(String::new(), |mut acc, col| {
237 let col_header = HTML::escape(col.as_ref());
240 "<th id=\"{col_header}\"><div><div>{col_header}</div></div></th>"
251 /// Will return `Err` if
252 /// * there's an i/o error while reading `input`
253 /// * the log has invalid syntax:
254 /// * an indented line with no preceding non-indented line
255 pub fn tablify(input: impl std::io::Read) -> Result<HTML, std::io::Error> {
256 let rows = read_rows(input).collect::<Result<Vec<_>, _>>()?;
257 let columns = column_order(&rows);
259 "{HEADER}{}{}{FOOTER}",
260 render_column_headers(&columns),
262 .map(|r| render_row(&columns, &r))
272 fn test_parse_line() {
273 assert_eq!(InputLine::from(""), InputLine::Blank);
274 assert_eq!(InputLine::from(" "), InputLine::Blank);
275 assert_eq!(InputLine::from("foo"), InputLine::RowHeader("foo"));
276 assert_eq!(InputLine::from("foo "), InputLine::RowHeader("foo"));
277 assert_eq!(InputLine::from(" foo"), InputLine::Entry("foo", None));
279 InputLine::from(" foo:bar"),
280 InputLine::Entry("foo", Some("bar"))
283 InputLine::from(" foo: bar"),
284 InputLine::Entry("foo", Some("bar"))
287 InputLine::from(" foo: bar "),
288 InputLine::Entry("foo", Some("bar"))
291 InputLine::from(" foo: bar "),
292 InputLine::Entry("foo", Some("bar"))
295 InputLine::from(" foo : bar "),
296 InputLine::Entry("foo", Some("bar"))
301 fn test_read_rows() {
303 read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(),
305 label: "foo".to_owned(),
306 entries: HashMap::new(),
310 read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(),
312 label: "bar".to_owned(),
313 entries: HashMap::new(),
317 read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(),
320 label: "foo".to_owned(),
321 entries: HashMap::new(),
324 label: "bar".to_owned(),
325 entries: HashMap::new(),
330 read_rows(&b"foo\n bar\n"[..]).flatten().collect::<Vec<_>>(),
332 label: "foo".to_owned(),
333 entries: HashMap::from([("bar".to_owned(), vec![None])]),
337 read_rows(&b"foo\n bar\n baz\n"[..])
339 .collect::<Vec<_>>(),
341 label: "foo".to_owned(),
342 entries: HashMap::from([
343 ("bar".to_owned(), vec![None]),
344 ("baz".to_owned(), vec![None])
349 read_rows(&b"foo\n\nbar\n"[..])
351 .collect::<Vec<_>>(),
354 label: "foo".to_owned(),
355 entries: HashMap::new(),
358 label: "bar".to_owned(),
359 entries: HashMap::new(),
364 read_rows(&b"foo\n \nbar\n"[..])
366 .collect::<Vec<_>>(),
369 label: "foo".to_owned(),
370 entries: HashMap::new(),
373 label: "bar".to_owned(),
374 entries: HashMap::new(),
379 read_rows(&b"foo \n bar \n"[..])
381 .collect::<Vec<_>>(),
383 label: "foo".to_owned(),
384 entries: HashMap::from([("bar".to_owned(), vec![None])]),
388 let bad = read_rows(&b" foo"[..]).next().unwrap();
389 assert!(bad.is_err());
390 assert!(format!("{bad:?}").contains("1: Entry with no header"));
392 let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap();
393 assert!(bad2.is_err());
394 assert!(format!("{bad2:?}").contains("3: Entry with no header"));
398 fn test_column_counts() {
401 &read_rows(&b"foo\n bar\n baz\n"[..])
402 .collect::<Result<Vec<_>, _>>()
405 vec![(1, String::from("bar")), (1, String::from("baz"))]
409 &read_rows(&b"foo\n bar\n baz\nquux\n baz"[..])
410 .collect::<Result<Vec<_>, _>>()
413 vec![(2, String::from("baz")), (1, String::from("bar"))]
417 &read_rows(&b"foo\n bar\n bar\n baz\n bar\nquux\n baz"[..])
418 .collect::<Result<Vec<_>, _>>()
421 vec![(2, String::from("baz")), (1, String::from("bar"))]
425 &read_rows(&b"foo\n bar: 1\n bar: 2\n baz\n bar\nquux\n baz"[..])
426 .collect::<Result<Vec<_>, _>>()
429 vec![(2, String::from("baz")), (1, String::from("bar"))]
434 fn test_render_cell() {
439 label: "nope".to_owned(),
440 entries: HashMap::new(),
443 HTML::from("<td class=\"\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\"></td>")
449 label: "nope".to_owned(),
450 entries: HashMap::from([("bar".to_owned(), vec![None])]),
453 HTML::from("<td class=\"\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\"></td>")
459 label: "nope".to_owned(),
460 entries: HashMap::from([("foo".to_owned(), vec![None])]),
463 HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\"></td>")
469 label: "nope".to_owned(),
470 entries: HashMap::from([("foo".to_owned(), vec![None, None])]),
473 HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\">2</td>")
479 label: "nope".to_owned(),
480 entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), Some("10".to_owned())])]),
483 HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\">5 10</td>")
489 label: "nope".to_owned(),
490 entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), None])]),
493 HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\">5 ✓</td>")
499 label: "nope".to_owned(),
500 entries: HashMap::from([("heart".to_owned(), vec![Some("<3".to_owned())])]),
503 HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','heart')\" onmouseout=\"ch2('nope','heart')\"><3</td>")
509 label: "bob's".to_owned(),
510 entries: HashMap::from([("foo".to_owned(), vec![None])]),
513 HTML::from("<td class=\"yes\" onmouseover=\"h2('bob's','foo')\" onmouseout=\"ch2('bob's','foo')\"></td>")