use std::io::BufRead;
use std::iter::Iterator;
-pub struct Config {}
+pub struct Config {
+ pub column_threshold: usize,
+}
-const HEADER: &str = "<!DOCTYPE html>
+const HEADER: &str = r#"<!DOCTYPE html>
<html>
<head>
- <meta charset=\"utf-8\">
- <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
+ <meta charset="utf-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1">
<style>
td { text-align: center; }
/* h/t https://wabain.github.io/2019/10/13/css-rotated-table-header.html */
tr.key > th > div { width: 1em; }
tr.key > th > div > div { width: 5em; transform-origin: bottom left; transform: translateX(1em) rotate(-65deg) }
td { border: thin solid gray; }
+ td.leftover { text-align: left; border: none; padding-left: .4em; }
td.yes { border: thin solid gray; background-color: #ddd; }
/* h/t https://stackoverflow.com/questions/5687035/css-bolding-some-text-without-changing-its-containers-size/46452396#46452396 */
.highlight { text-shadow: -0.06ex 0 black, 0.06ex 0 black; }
</style>
<script>
- function highlight(id) { const e = document.getElementById(id); if (e) { e.classList.add( \"highlight\"); } }
- function clear_highlight(id) { const e = document.getElementById(id); if (e) { e.classList.remove(\"highlight\"); } }
+ function highlight(id) { const e = document.getElementById(id); if (e) { e.classList.add( "highlight"); } }
+ function clear_highlight(id) { const e = document.getElementById(id); if (e) { e.classList.remove("highlight"); } }
function h2(a, b) { highlight(a); highlight(b); }
function ch2(a, b) { clear_highlight(a); clear_highlight(b); }
</script>
<body>
<table>
<tbody>
-";
+"#;
const FOOTER: &str = " </tbody>
</table>
</body>
counts.sort_unstable_by(|(an, acol), (bn, bcol)| bn.cmp(an).then(acol.cmp(bcol)));
counts
}
-fn column_order(rows: &[Row]) -> Vec<String> {
+fn column_order(config: &Config, rows: &[Row]) -> Vec<String> {
column_counts(rows)
.into_iter()
- .map(|(_, col)| col)
+ .filter_map(|(n, col)| (n >= config.column_threshold).then_some(col))
.collect()
}
-fn render_instance(instance: &Option<String>) -> HTML {
+fn render_one_instance(instance: &Option<String>) -> HTML {
match instance {
None => HTML::from("✓"),
Some(instance) => HTML::escape(instance.as_ref()),
}
}
-fn render_cell(col: &str, row: &mut Row) -> HTML {
- let row_label = HTML::escape(row.label.as_ref());
- let col_label = HTML::escape(col);
- let instances: Option<&Vec<Option<String>>> = row.entries.get(col);
- let class = HTML::from(if instances.is_none() { "" } else { "yes" });
- let all_empty = instances
- .iter()
- .flat_map(|is| is.iter())
- .all(Option::is_none);
- let contents = if instances.is_none() || (all_empty && instances.unwrap().len() == 1) {
+fn render_instances(instances: &[Option<String>]) -> HTML {
+ let all_empty = instances.iter().all(Option::is_none);
+ if all_empty && instances.len() == 1 {
HTML::from("")
} else if all_empty {
- HTML(format!("{}", instances.unwrap().len()))
+ HTML(format!("{}", instances.len()))
} else {
HTML(
instances
- .unwrap()
.iter()
- .map(render_instance)
+ .map(render_one_instance)
.map(|html| html.0) // Waiting for slice_concat_trait to stabilize
.collect::<Vec<_>>()
.join(" "),
)
+ }
+}
+
+fn render_cell(col: &str, row: &mut Row) -> HTML {
+ let row_label = HTML::escape(row.label.as_ref());
+ let col_label = HTML::escape(col);
+ let instances: Option<&Vec<Option<String>>> = row.entries.get(col);
+ let class = HTML::from(if instances.is_none() { "" } else { "yes" });
+ let contents = match instances {
+ None => HTML::from(""),
+ Some(is) => render_instances(is),
};
row.entries.remove(col);
- HTML(format!("<td class=\"{class}\" onmouseover=\"h2('{row_label}','{col_label}')\" onmouseout=\"ch2('{row_label}','{col_label}')\">{contents}</td>"))
+ HTML(format!(
+ r#"<td class="{class}" onmouseover="h2('{row_label}','{col_label}')" onmouseout="ch2('{row_label}','{col_label}')">{contents}</td>"#
+ ))
+}
+
+fn render_leftover(notcol: &str, instances: &[Option<String>]) -> HTML {
+ let label = HTML::escape(notcol);
+ let rest = render_instances(instances);
+ if rest == HTML::from("") {
+ HTML(format!("{label}"))
+ } else {
+ HTML(format!("{label}: {rest}"))
+ }
+}
+
+fn render_all_leftovers(row: &Row) -> HTML {
+ let mut order: Vec<_> = row.entries.keys().collect();
+ order.sort_unstable();
+ HTML(
+ order
+ .into_iter()
+ .map(|notcol| render_leftover(notcol, row.entries.get(notcol).expect("Key vanished?!")))
+ .map(|html| html.0) // Waiting for slice_concat_trait to stabilize
+ .collect::<Vec<_>>()
+ .join(", "),
+ )
}
fn render_row(columns: &[String], row: &mut Row) -> HTML {
let row_label = HTML::escape(row.label.as_ref());
+ let cells = columns
+ .iter()
+ .map(|col| render_cell(col, row))
+ .collect::<HTML>();
+ let leftovers = render_all_leftovers(row);
HTML(format!(
- "<tr><th id=\"{row_label}\">{row_label}</th>{}</tr>\n",
- &columns
- .iter()
- .map(|col| render_cell(col, row))
- .collect::<HTML>()
+ "<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"
))
}
fn render_column_headers(columns: &[String]) -> HTML {
HTML(
- String::from("<tr class=\"key\"><th></th>")
+ String::from(r#"<tr class="key"><th></th>"#)
+ &columns.iter().fold(String::new(), |mut acc, col| {
let col_header = HTML::escape(col.as_ref());
write!(
&mut acc,
- "<th id=\"{col_header}\"><div><div>{col_header}</div></div></th>"
+ r#"<th id="{col_header}"><div><div>{col_header}</div></div></th>"#
)
.unwrap();
acc
/// * an indented line with no preceding non-indented line
pub fn tablify(config: &Config, input: impl std::io::Read) -> Result<HTML, std::io::Error> {
let rows = read_rows(input).collect::<Result<Vec<_>, _>>()?;
- let columns = column_order(&rows);
+ let columns = column_order(config, &rows);
Ok(HTML(format!(
"{HEADER}{}{}{FOOTER}",
render_column_headers(&columns),
entries: HashMap::new(),
}
),
- HTML::from("<td class=\"\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\"></td>")
+ HTML::from(
+ r#"<td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"#
+ )
);
assert_eq!(
render_cell(
entries: HashMap::from([("bar".to_owned(), vec![None])]),
}
),
- HTML::from("<td class=\"\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\"></td>")
+ HTML::from(
+ r#"<td class="" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"#
+ )
);
assert_eq!(
render_cell(
entries: HashMap::from([("foo".to_owned(), vec![None])]),
}
),
- HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\"></td>")
+ HTML::from(
+ r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')"></td>"#
+ )
);
assert_eq!(
render_cell(
entries: HashMap::from([("foo".to_owned(), vec![None, None])]),
}
),
- HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\">2</td>")
+ HTML::from(
+ r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">2</td>"#
+ )
);
assert_eq!(
render_cell(
"foo",
&mut Row {
label: "nope".to_owned(),
- entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), Some("10".to_owned())])]),
+ entries: HashMap::from([(
+ "foo".to_owned(),
+ vec![Some("5".to_owned()), Some("10".to_owned())]
+ )]),
}
),
- HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\">5 10</td>")
+ HTML::from(
+ r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">5 10</td>"#
+ )
);
assert_eq!(
render_cell(
entries: HashMap::from([("foo".to_owned(), vec![Some("5".to_owned()), None])]),
}
),
- HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','foo')\" onmouseout=\"ch2('nope','foo')\">5 ✓</td>")
+ HTML::from(
+ r#"<td class="yes" onmouseover="h2('nope','foo')" onmouseout="ch2('nope','foo')">5 ✓</td>"#
+ )
);
assert_eq!(
render_cell(
entries: HashMap::from([("heart".to_owned(), vec![Some("<3".to_owned())])]),
}
),
- HTML::from("<td class=\"yes\" onmouseover=\"h2('nope','heart')\" onmouseout=\"ch2('nope','heart')\"><3</td>")
+ HTML::from(
+ r#"<td class="yes" onmouseover="h2('nope','heart')" onmouseout="ch2('nope','heart')"><3</td>"#
+ )
);
assert_eq!(
render_cell(
entries: HashMap::from([("foo".to_owned(), vec![None])]),
}
),
- HTML::from("<td class=\"yes\" onmouseover=\"h2('bob's','foo')\" onmouseout=\"ch2('bob's','foo')\"></td>")
+ HTML::from(
+ r#"<td class="yes" onmouseover="h2('bob's','foo')" onmouseout="ch2('bob's','foo')"></td>"#
+ )
);
let mut r = Row {
label: "nope".to_owned(),
render_cell("baz", &mut r);
assert_eq!(r.entries.len(), 0);
}
+
+ #[test]
+ fn test_render_leftovers() {
+ assert_eq!(
+ render_all_leftovers(&Row {
+ label: "nope".to_owned(),
+ entries: HashMap::from([("foo".to_owned(), vec![None])]),
+ }),
+ HTML::from("foo")
+ );
+ assert_eq!(
+ render_all_leftovers(&Row {
+ label: "nope".to_owned(),
+ entries: HashMap::from([
+ ("foo".to_owned(), vec![None]),
+ ("bar".to_owned(), vec![None])
+ ]),
+ }),
+ HTML::from("bar, foo")
+ );
+ assert_eq!(
+ render_all_leftovers(&Row {
+ label: "nope".to_owned(),
+ entries: HashMap::from([
+ ("foo".to_owned(), vec![None]),
+ ("bar".to_owned(), vec![None, None])
+ ]),
+ }),
+ HTML::from("bar: 2, foo")
+ );
+ }
+
+ #[test]
+ fn test_render_row() {
+ assert_eq!(
+ render_row(
+ &["foo".to_owned()],
+ &mut Row {
+ label: "nope".to_owned(),
+ entries: HashMap::from([("bar".to_owned(), vec![None])]),
+ }
+ ),
+ HTML::from(
+ 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>
+"#
+ )
+ );
+ }
}