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