]> git.scottworley.com Git - tablify/blobdiff - src/lib.rs
Lighter gray background for active cells
[tablify] / src / lib.rs
index 9504c7f69928be110927531fadfe64dac0ed9a1b..1aee9782fcd4b477ba9205fc3badb924bd20765d 100644 (file)
@@ -1,4 +1,5 @@
 use std::collections::{HashMap, HashSet};
 use std::collections::{HashMap, HashSet};
+use std::fmt::Write;
 use std::io::BufRead;
 use std::iter::Iterator;
 
 use std::io::BufRead;
 use std::iter::Iterator;
 
@@ -17,7 +18,7 @@ const HEADER: &str = "<!DOCTYPE html>
     tr.key > th > div > div { width: 5em; transform-origin: bottom left; transform: translateX(1em) rotate(-65deg) }
     td { border: thin solid gray; }
     td.numeric { text-align: right; }
     tr.key > th > div > div { width: 5em; transform-origin: bottom left; transform: translateX(1em) rotate(-65deg) }
     td { border: thin solid gray; }
     td.numeric { text-align: right; }
-    td.yes { border: thin solid gray; background-color: gray; }
+    td.yes { border: thin solid gray; background-color: #ddd; }
     td.spacer { border: none; }
     /* 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; }
     td.spacer { border: none; }
     /* 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; }
@@ -32,7 +33,8 @@ const HEADER: &str = "<!DOCTYPE html>
 </head>
 <body>
   <table>
 </head>
 <body>
   <table>
-    <tbody>";
+    <tbody>
+";
 const FOOTER: &str = "    </tbody>
   </table>
 </body>
 const FOOTER: &str = "    </tbody>
   </table>
 </body>
@@ -186,6 +188,16 @@ fn render_row(columns: &[String], row: &RowInput) -> String {
     )
 }
 
     )
 }
 
+fn render_column_headers(columns: &[String]) -> String {
+    // TODO: Escape HTML special characters
+    String::from("<th></th>")
+        + &columns.iter().fold(String::new(), |mut acc, c| {
+            write!(&mut acc, "<th>{c}</th>").unwrap();
+            acc
+        })
+        + "\n"
+}
+
 /// # Errors
 ///
 /// Will return `Err` if
 /// # Errors
 ///
 /// Will return `Err` if
@@ -196,6 +208,7 @@ pub fn tablify(input: impl std::io::Read) -> Result<String, std::io::Error> {
     let rows = read_rows(input).collect::<Result<Vec<_>, _>>()?;
     let columns = column_order(&rows);
     Ok(String::from(HEADER)
     let rows = read_rows(input).collect::<Result<Vec<_>, _>>()?;
     let columns = column_order(&rows);
     Ok(String::from(HEADER)
+        + &render_column_headers(&columns)
         + &rows
             .into_iter()
             .map(|r| render_row(&columns, &r))
         + &rows
             .into_iter()
             .map(|r| render_row(&columns, &r))