]> git.scottworley.com Git - tablify/commitdiff
Render column headers
authorScott Worley <scottworley@scottworley.com>
Mon, 19 Aug 2024 20:46:51 +0000 (13:46 -0700)
committerScott Worley <scottworley@scottworley.com>
Mon, 19 Aug 2024 20:54:27 +0000 (13:54 -0700)
src/lib.rs

index 9504c7f69928be110927531fadfe64dac0ed9a1b..5de1c121059ebbc4168a0b952d303fa195f1c4a1 100644 (file)
@@ -32,7 +32,8 @@ const HEADER: &str = "<!DOCTYPE html>
 </head>
 <body>
   <table>
-    <tbody>";
+    <tbody>
+";
 const FOOTER: &str = "    </tbody>
   </table>
 </body>
@@ -186,6 +187,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()
+            .map(|c| format!("<th>{c}</th>"))
+            .collect::<String>()
+        + "\n"
+}
+
 /// # Errors
 ///
 /// Will return `Err` if
@@ -196,6 +207,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)
+        + &render_column_headers(&columns)
         + &rows
             .into_iter()
             .map(|r| render_row(&columns, &r))