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