]> git.scottworley.com Git - tablify/blob - src/lib.rs
fbadb2a2bab44e937bf39f51b1a7836c85f32d81
[tablify] / src / lib.rs
1 use std::borrow::ToOwned;
2 use std::collections::HashMap;
3 use std::fmt::Write;
4 use std::io::BufRead;
5 use std::iter::Iterator;
6
7 pub struct Config {
8 pub column_threshold: usize,
9 }
10
11 const HEADER: &str = r#"<!DOCTYPE html>
12 <html>
13 <head>
14 <meta charset="utf-8">
15 <meta name="viewport" content="width=device-width, initial-scale=1">
16 <style>
17 td { text-align: center; }
18 /* h/t https://wabain.github.io/2019/10/13/css-rotated-table-header.html */
19 th, td { white-space: nowrap; }
20 th { text-align: left; font-weight: normal; }
21 th.spacer_row { height: .3em; }
22 table { border-collapse: collapse }
23 tr.key > th { height: 10em; vertical-align: bottom; line-height: 1 }
24 tr.key > th > div { width: 1em; }
25 tr.key > th > div > div { width: 5em; transform-origin: bottom left; transform: translateX(1em) rotate(-65deg) }
26 td { border: thin solid gray; }
27 td.leftover { text-align: left; border: none; padding-left: .4em; }
28 td.yes { border: thin solid gray; background-color: #ddd; }
29 /* h/t https://stackoverflow.com/questions/5687035/css-bolding-some-text-without-changing-its-containers-size/46452396#46452396 */
30 .highlight { text-shadow: -0.06ex 0 black, 0.06ex 0 black; }
31 </style>
32 <script>
33 function highlight(id) { const e = document.getElementById(id); if (e) { e.classList.add( "highlight"); } }
34 function clear_highlight(id) { const e = document.getElementById(id); if (e) { e.classList.remove("highlight"); } }
35 function h2(a, b) { highlight(a); highlight(b); }
36 function ch2(a, b) { clear_highlight(a); clear_highlight(b); }
37 </script>
38 </head>
39 <body>
40 <table>
41 <tbody>
42 "#;
43 const FOOTER: &str = " </tbody>
44 </table>
45 </body>
46 </html>";
47
48 #[derive(PartialEq, Eq, Debug)]
49 pub struct HTML(String);
50 impl HTML {
51 fn escape(value: &str) -> HTML {
52 let mut escaped: String = String::new();
53 for c in value.chars() {
54 match c {
55 '>' => escaped.push_str("&gt;"),
56 '<' => escaped.push_str("&lt;"),
57 '\'' => escaped.push_str("&#39;"),
58 '"' => escaped.push_str("&quot;"),
59 '&' => escaped.push_str("&amp;"),
60 ok_c => escaped.push(ok_c),
61 }
62 }
63 HTML(escaped)
64 }
65 }
66 impl From<&str> for HTML {
67 fn from(value: &str) -> HTML {
68 HTML(String::from(value))
69 }
70 }
71 impl FromIterator<HTML> for HTML {
72 fn from_iter<T>(iter: T) -> HTML
73 where
74 T: IntoIterator<Item = HTML>,
75 {
76 HTML(iter.into_iter().map(|html| html.0).collect::<String>())
77 }
78 }
79 impl std::fmt::Display for HTML {
80 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81 write!(f, "{}", self.0)
82 }
83 }
84
85 #[derive(Debug, PartialEq, Eq)]
86 enum InputLine<'a> {
87 Blank,
88 RowHeader(&'a str),
89 Entry(&'a str, Option<&'a str>),
90 }
91 impl<'a> From<&'a str> for InputLine<'a> {
92 fn from(value: &'a str) -> InputLine<'a> {
93 let trimmed = value.trim_end();
94 if trimmed.is_empty() {
95 InputLine::Blank
96 } else if !trimmed.starts_with(' ') {
97 InputLine::RowHeader(value.trim())
98 } else {
99 match value.split_once(':') {
100 None => InputLine::Entry(value.trim(), None),
101 Some((col, instance)) => InputLine::Entry(col.trim(), Some(instance.trim())),
102 }
103 }
104 }
105 }
106
107 #[derive(Debug, PartialEq, Eq)]
108 struct Row {
109 label: String,
110 entries: HashMap<String, Vec<Option<String>>>,
111 }
112
113 #[derive(Debug, PartialEq, Eq)]
114 enum Rowlike {
115 Row(Row),
116 Spacer,
117 }
118
119 struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> {
120 input: std::iter::Enumerate<Input>,
121 row: Option<Row>,
122 }
123 impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> {
124 fn new(input: Input) -> Self {
125 Self {
126 input: input.enumerate(),
127 row: None,
128 }
129 }
130 }
131 impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader<Input> {
132 type Item = Result<Rowlike, std::io::Error>;
133 fn next(&mut self) -> Option<Self::Item> {
134 loop {
135 match self.input.next() {
136 None => return Ok(std::mem::take(&mut self.row).map(Rowlike::Row)).transpose(),
137 Some((_, Err(e))) => return Some(Err(e)),
138 Some((n, Ok(line))) => match InputLine::from(line.as_ref()) {
139 InputLine::Blank if self.row.is_some() => {
140 return Ok(std::mem::take(&mut self.row).map(Rowlike::Row)).transpose()
141 }
142 InputLine::Blank => return Some(Ok(Rowlike::Spacer)),
143 InputLine::Entry(col, instance) => match &mut self.row {
144 None => {
145 return Some(Err(std::io::Error::other(format!(
146 "{}: Entry with no header",
147 n + 1
148 ))))
149 }
150 Some(ref mut row) => {
151 row.entries
152 .entry(col.to_owned())
153 .and_modify(|is| is.push(instance.map(ToOwned::to_owned)))
154 .or_insert_with(|| vec![instance.map(ToOwned::to_owned)]);
155 }
156 },
157 InputLine::RowHeader(row) => {
158 let prev = std::mem::take(&mut self.row);
159 self.row = Some(Row {
160 label: row.to_owned(),
161 entries: HashMap::new(),
162 });
163 if prev.is_some() {
164 return Ok(prev.map(Rowlike::Row)).transpose();
165 }
166 }
167 },
168 }
169 }
170 }
171 }
172
173 fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<Rowlike, std::io::Error>> {
174 Reader::new(std::io::BufReader::new(input).lines())
175 }
176
177 fn column_counts(rows: &[Rowlike]) -> Vec<(usize, String)> {
178 let empty = HashMap::new();
179 let mut counts: Vec<_> = rows
180 .iter()
181 .flat_map(|rl| match rl {
182 Rowlike::Row(r) => r.entries.keys(),
183 Rowlike::Spacer => empty.keys(),
184 })
185 .fold(HashMap::new(), |mut cs, col| {
186 cs.entry(col.to_owned())
187 .and_modify(|n| *n += 1)
188 .or_insert(1);
189 cs
190 })
191 .into_iter()
192 .map(|(col, n)| (n, col))
193 .collect();
194 counts.sort_unstable_by(|(an, acol), (bn, bcol)| bn.cmp(an).then(acol.cmp(bcol)));
195 counts
196 }
197 fn column_order(config: &Config, rows: &[Rowlike]) -> Vec<String> {
198 column_counts(rows)
199 .into_iter()
200 .filter_map(|(n, col)| (n >= config.column_threshold).then_some(col))
201 .collect()
202 }
203
204 fn render_one_instance(instance: &Option<String>) -> HTML {
205 match instance {
206 None => HTML::from("✓"),
207 Some(instance) => HTML::escape(instance.as_ref()),
208 }
209 }
210
211 fn render_instances(instances: &[Option<String>]) -> HTML {
212 let all_empty = instances.iter().all(Option::is_none);
213 if all_empty && instances.len() == 1 {
214 HTML::from("")
215 } else if all_empty {
216 HTML(format!("{}", instances.len()))
217 } else {
218 HTML(
219 instances
220 .iter()
221 .map(render_one_instance)
222 .map(|html| html.0) // Waiting for slice_concat_trait to stabilize
223 .collect::<Vec<_>>()
224 .join(" "),
225 )
226 }
227 }
228
229 fn render_cell(col: &str, row: &mut Row) -> HTML {
230 let row_label = HTML::escape(row.label.as_ref());
231 let col_label = HTML::escape(col);
232 let instances: Option<&Vec<Option<String>>> = row.entries.get(col);
233 let class = HTML::from(if instances.is_none() { "" } else { "yes" });
234 let contents = match instances {
235 None => HTML::from(""),
236 Some(is) => render_instances(is),
237 };
238 row.entries.remove(col);
239 HTML(format!(
240 r#"<td class="{class}" onmouseover="h2('{row_label}','{col_label}')" onmouseout="ch2('{row_label}','{col_label}')">{contents}</td>"#
241 ))
242 }
243
244 fn render_leftover(notcol: &str, instances: &[Option<String>]) -> HTML {
245 let label = HTML::escape(notcol);
246 let rest = render_instances(instances);
247 if rest == HTML::from("") {
248 HTML(format!("{label}"))
249 } else {
250 HTML(format!("{label}: {rest}"))
251 }
252 }
253
254 fn render_all_leftovers(row: &Row) -> HTML {
255 let mut order: Vec<_> = row.entries.keys().collect();
256 order.sort_unstable();
257 HTML(
258 order
259 .into_iter()
260 .map(|notcol| render_leftover(notcol, row.entries.get(notcol).expect("Key vanished?!")))
261 .map(|html| html.0) // Waiting for slice_concat_trait to stabilize
262 .collect::<Vec<_>>()
263 .join(", "),
264 )
265 }
266
267 fn render_row(columns: &[String], rowlike: &mut Rowlike) -> HTML {
268 match rowlike {
269 Rowlike::Spacer => HTML::from("<tr><th class=\"spacer_row\"></th></tr>\n"),
270 Rowlike::Row(row) => {
271 let row_label = HTML::escape(row.label.as_ref());
272 let cells = columns
273 .iter()
274 .map(|col| render_cell(col, row))
275 .collect::<HTML>();
276 let leftovers = render_all_leftovers(row);
277 HTML(format!(
278 "<tr><th id=\"{row_label}\">{row_label}</th>{cells}<td class=\"leftover\" onmouseover=\"highlight('{row_label}')\" onmouseout=\"clear_highlight('{row_label}')\">{leftovers}</td></tr>\n"
279 ))
280 }
281 }
282 }
283
284 fn render_column_headers(columns: &[String]) -> HTML {
285 HTML(
286 String::from(r#"<tr class="key"><th></th>"#)
287 + &columns.iter().fold(String::new(), |mut acc, col| {
288 let col_header = HTML::escape(col.as_ref());
289 write!(
290 &mut acc,
291 r#"<th id="{col_header}"><div><div>{col_header}</div></div></th>"#
292 )
293 .unwrap();
294 acc
295 })
296 + "</tr>\n",
297 )
298 }
299
300 /// # Errors
301 ///
302 /// Will return `Err` if
303 /// * there's an i/o error while reading `input`
304 /// * the log has invalid syntax:
305 /// * an indented line with no preceding non-indented line
306 pub fn tablify(config: &Config, input: impl std::io::Read) -> Result<HTML, std::io::Error> {
307 let rows = read_rows(input).collect::<Result<Vec<_>, _>>()?;
308 let columns = column_order(config, &rows);
309 Ok(HTML(format!(
310 "{HEADER}{}{}{FOOTER}",
311 render_column_headers(&columns),
312 rows.into_iter()
313 .map(|mut r| render_row(&columns, &mut r))
314 .collect::<HTML>()
315 )))
316 }
317
318 #[cfg(test)]
319 mod tests {
320 use super::*;
321
322 #[test]
323 fn test_parse_line() {
324 assert_eq!(InputLine::from(""), InputLine::Blank);
325 assert_eq!(InputLine::from(" "), InputLine::Blank);
326 assert_eq!(InputLine::from("foo"), InputLine::RowHeader("foo"));
327 assert_eq!(InputLine::from("foo "), InputLine::RowHeader("foo"));
328 assert_eq!(InputLine::from(" foo"), InputLine::Entry("foo", None));
329 assert_eq!(
330 InputLine::from(" foo:bar"),
331 InputLine::Entry("foo", Some("bar"))
332 );
333 assert_eq!(
334 InputLine::from(" foo: bar"),
335 InputLine::Entry("foo", Some("bar"))
336 );
337 assert_eq!(
338 InputLine::from(" foo: bar "),
339 InputLine::Entry("foo", Some("bar"))
340 );
341 assert_eq!(
342 InputLine::from(" foo: bar "),
343 InputLine::Entry("foo", Some("bar"))
344 );
345 assert_eq!(
346 InputLine::from(" foo : bar "),
347 InputLine::Entry("foo", Some("bar"))
348 );
349 }
350
351 #[test]
352 fn test_read_rows() {
353 assert_eq!(
354 read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(),
355 vec![Rowlike::Row(Row {
356 label: "foo".to_owned(),
357 entries: HashMap::new(),
358 })]
359 );
360 assert_eq!(
361 read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(),
362 vec![Rowlike::Row(Row {
363 label: "bar".to_owned(),
364 entries: HashMap::new(),
365 })]
366 );
367 assert_eq!(
368 read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(),
369 vec![
370 Rowlike::Row(Row {
371 label: "foo".to_owned(),
372 entries: HashMap::new(),
373 }),
374 Rowlike::Row(Row {
375 label: "bar".to_owned(),
376 entries: HashMap::new(),
377 })
378 ]
379 );
380 assert_eq!(
381 read_rows(&b"foo\n bar\n"[..]).flatten().collect::<Vec<_>>(),
382 vec![Rowlike::Row(Row {
383 label: "foo".to_owned(),
384 entries: HashMap::from([("bar".to_owned(), vec![None])]),
385 })]
386 );
387 assert_eq!(
388 read_rows(&b"foo\n bar\n baz\n"[..])
389 .flatten()
390 .collect::<Vec<_>>(),
391 vec![Rowlike::Row(Row {
392 label: "foo".to_owned(),
393 entries: HashMap::from([
394 ("bar".to_owned(), vec![None]),
395 ("baz".to_owned(), vec![None])
396 ]),
397 })]
398 );
399 assert_eq!(
400 read_rows(&b"foo\n\nbar\n"[..])
401 .flatten()
402 .collect::<Vec<_>>(),
403 vec![
404 Rowlike::Row(Row {
405 label: "foo".to_owned(),
406 entries: HashMap::new(),
407 }),
408 Rowlike::Row(Row {
409 label: "bar".to_owned(),
410 entries: HashMap::new(),
411 })
412 ]
413 );
414 assert_eq!(
415 read_rows(&b"foo\n\n\nbar\n"[..])
416 .flatten()
417 .collect::<Vec<_>>(),
418 vec![
419 Rowlike::Row(Row {
420 label: "foo".to_owned(),
421 entries: HashMap::new(),
422 }),
423 Rowlike::Spacer,
424 Rowlike::Row(Row {
425 label: "bar".to_owned(),
426 entries: HashMap::new(),
427 })
428 ]
429 );
430 assert_eq!(
431 read_rows(&b"foo\n \nbar\n"[..])
432 .flatten()
433 .collect::<Vec<_>>(),
434 vec![
435 Rowlike::Row(Row {
436 label: "foo".to_owned(),
437 entries: HashMap::new(),
438 }),
439 Rowlike::Row(Row {
440 label: "bar".to_owned(),
441 entries: HashMap::new(),
442 })
443 ]
444 );
445 assert_eq!(
446 read_rows(&b"foo \n bar \n"[..])
447 .flatten()
448 .collect::<Vec<_>>(),
449 vec![Rowlike::Row(Row {
450 label: "foo".to_owned(),
451 entries: HashMap::from([("bar".to_owned(), vec![None])]),
452 })]
453 );
454
455 let bad = read_rows(&b" foo"[..]).next().unwrap();
456 assert!(bad.is_err());
457 assert!(format!("{bad:?}").contains("1: Entry with no header"));
458
459 let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap();
460 assert!(bad2.is_err());
461 assert!(format!("{bad2:?}").contains("3: Entry with no header"));
462 }
463
464 #[test]
465 fn test_column_counts() {
466 assert_eq!(
467 column_counts(
468 &read_rows(&b"foo\n bar\n baz\n"[..])
469 .collect::<Result<Vec<_>, _>>()
470 .unwrap()
471 ),
472 vec![(1, String::from("bar")), (1, String::from("baz"))]
473 );
474 assert_eq!(
475 column_counts(
476 &read_rows(&b"foo\n bar\n baz\nquux\n baz"[..])
477 .collect::<Result<Vec<_>, _>>()
478 .unwrap()
479 ),
480 vec![(2, String::from("baz")), (1, String::from("bar"))]
481 );
482 assert_eq!(
483 column_counts(
484 &read_rows(&b"foo\n bar\n bar\n baz\n bar\nquux\n baz"[..])
485 .collect::<Result<Vec<_>, _>>()
486 .unwrap()
487 ),
488 vec![(2, String::from("baz")), (1, String::from("bar"))]
489 );
490 assert_eq!(
491 column_counts(
492 &read_rows(&b"foo\n bar: 1\n bar: 2\n baz\n bar\nquux\n baz"[..])
493 .collect::<Result<Vec<_>, _>>()
494 .unwrap()
495 ),
496 vec![(2, String::from("baz")), (1, String::from("bar"))]
497 );
498 }
499
500 #[test]
501 fn test_render_cell() {
502 assert_eq!(
503 render_cell(
504 "foo",
505 &mut Row {
506 label: "nope".to_owned(),
507 entries: HashMap::new(),
508 }
509 ),
510 HTML::from(
511 r#"<td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"#
512 )
513 );
514 assert_eq!(
515 render_cell(
516 "foo",
517 &mut Row {
518 label: "nope".to_owned(),
519 entries: HashMap::from([("bar".to_owned(), vec![None])]),
520 }
521 ),
522 HTML::from(
523 r#"<td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"#
524 )
525 );
526 assert_eq!(
527 render_cell(
528 "foo",
529 &mut Row {
530 label: "nope".to_owned(),
531 entries: HashMap::from([("foo".to_owned(), vec![None])]),
532 }
533 ),
534 HTML::from(
535 r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"#
536 )
537 );
538 assert_eq!(
539 render_cell(
540 "foo",
541 &mut Row {
542 label: "nope".to_owned(),
543 entries: HashMap::from([("foo".to_owned(), vec![None, None])]),
544 }
545 ),
546 HTML::from(
547 r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">2</td>"#
548 )
549 );
550 assert_eq!(
551 render_cell(
552 "foo",
553 &mut Row {
554 label: "nope".to_owned(),
555 entries: HashMap::from([(
556 "foo".to_owned(),
557 vec![Some("5".to_owned()), Some("10".to_owned())]
558 )]),
559 }
560 ),
561 HTML::from(
562 r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">5 10</td>"#
563 )
564 );
565 assert_eq!(
566 render_cell(
567 "foo",
568 &mut Row {
569 label: "nope".to_owned(),
570 entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), None])]),
571 }
572 ),
573 HTML::from(
574 r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">5 ✓</td>"#
575 )
576 );
577 assert_eq!(
578 render_cell(
579 "heart",
580 &mut Row {
581 label: "nope".to_owned(),
582 entries: HashMap::from([("heart".to_owned(), vec![Some("<3".to_owned())])]),
583 }
584 ),
585 HTML::from(
586 r#"<td class="yes" onmouseover="h2('nope','heart')" onmouseout="ch2('nope','heart')">&lt;3</td>"#
587 )
588 );
589 assert_eq!(
590 render_cell(
591 "foo",
592 &mut Row {
593 label: "bob's".to_owned(),
594 entries: HashMap::from([("foo".to_owned(), vec![None])]),
595 }
596 ),
597 HTML::from(
598 r#"<td class="yes" onmouseover="h2('bob&#39;s','foo')" onmouseout="ch2('bob&#39;s','foo')"></td>"#
599 )
600 );
601 let mut r = Row {
602 label: "nope".to_owned(),
603 entries: HashMap::from([
604 ("foo".to_owned(), vec![None]),
605 ("baz".to_owned(), vec![None]),
606 ]),
607 };
608 assert_eq!(r.entries.len(), 2);
609 render_cell("foo", &mut r);
610 assert_eq!(r.entries.len(), 1);
611 render_cell("bar", &mut r);
612 assert_eq!(r.entries.len(), 1);
613 render_cell("baz", &mut r);
614 assert_eq!(r.entries.len(), 0);
615 }
616
617 #[test]
618 fn test_render_leftovers() {
619 assert_eq!(
620 render_all_leftovers(&Row {
621 label: "nope".to_owned(),
622 entries: HashMap::from([("foo".to_owned(), vec![None])]),
623 }),
624 HTML::from("foo")
625 );
626 assert_eq!(
627 render_all_leftovers(&Row {
628 label: "nope".to_owned(),
629 entries: HashMap::from([
630 ("foo".to_owned(), vec![None]),
631 ("bar".to_owned(), vec![None])
632 ]),
633 }),
634 HTML::from("bar, foo")
635 );
636 assert_eq!(
637 render_all_leftovers(&Row {
638 label: "nope".to_owned(),
639 entries: HashMap::from([
640 ("foo".to_owned(), vec![None]),
641 ("bar".to_owned(), vec![None, None])
642 ]),
643 }),
644 HTML::from("bar: 2, foo")
645 );
646 }
647
648 #[test]
649 fn test_render_row() {
650 assert_eq!(
651 render_row(
652 &["foo".to_owned()],
653 &mut Rowlike::Row(Row {
654 label: "nope".to_owned(),
655 entries: HashMap::from([("bar".to_owned(), vec![None])]),
656 })
657 ),
658 HTML::from(
659 r#"<tr><th id="nope">nope</th><td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td><td class="leftover" onmouseover="highlight('nope')" onmouseout="clear_highlight('nope')">bar</td></tr>
660 "#
661 )
662 );
663 }
664 }