]> git.scottworley.com Git - tablify/blobdiff - src/lib.rs
Render column headers
[tablify] / src / lib.rs
index 9504c7f69928be110927531fadfe64dac0ed9a1b..5de1c121059ebbc4168a0b952d303fa195f1c4a1 100644 (file)
@@ -32,7 +32,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 +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
 /// # 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)
     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))