Skip to content

Commit

Permalink
fix html output for title-rows
Browse files Browse the repository at this point in the history
add boolean `title` flag to `Cell::print_html` to switch between `td`/`th`
  • Loading branch information
michaelsippel committed Jan 21, 2024
1 parent 121ca97 commit 54ac8f8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
11 changes: 6 additions & 5 deletions src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl Cell {
}

/// Print the cell in HTML format to `out`.
pub fn print_html<T: Write + ?Sized>(&self, out: &mut T) -> Result<usize, Error> {
pub fn print_html<T: Write + ?Sized>(&self, out: &mut T, title: bool) -> Result<usize, Error> {
/// Convert the color to a hex value useful in CSS
fn color2hex(color: color::Color) -> &'static str {
match color {
Expand Down Expand Up @@ -306,10 +306,11 @@ impl Cell {
let content = self.content.join("<br />");
out.write_all(
format!(
"<td{1} style=\"{2}\">{0}</td>",
"<{3}{1} style=\"{2}\">{0}</{3}>",
HtmlEscape(&content),
colspan,
styles
styles,
if title {"th"} else {"td"}
)
.as_bytes(),
)?;
Expand Down Expand Up @@ -437,7 +438,7 @@ mod tests {
assert_eq!(ascii_cell.get_width(), 5);

let mut out = StringWriter::new();
let _ = ascii_cell.print_html(&mut out);
let _ = ascii_cell.print_html(&mut out, false);
assert_eq!(
out.as_string(),
r#"<td style="text-align: left;">hello</td>"#
Expand All @@ -449,7 +450,7 @@ mod tests {
let ascii_cell = Cell::new("<abc\">&'");

let mut out = StringWriter::new();
let _ = ascii_cell.print_html(&mut out);
let _ = ascii_cell.print_html(&mut out, false);
assert_eq!(
out.as_string(),
r#"<td style="text-align: left;">&lt;abc&quot;&gt;&amp;&#39;</td>"#
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,14 @@ impl<'a> TableSlice<'a> {
out.write_all(b"<table>")?;
// Print titles / table header
if let Some(ref t) = *self.titles {
out.write_all(b"<th>")?;
t.print_html(out, column_num)?;
out.write_all(b"</th>")?;
out.write_all(b"<tr>")?;
t.print_html(out, column_num, true)?;
out.write_all(b"</tr>")?;
}
// Print rows
for r in self.rows {
out.write_all(b"<tr>")?;
r.print_html(out, column_num)?;
r.print_html(out, column_num, false)?;
out.write_all(b"</tr>")?;
}
out.write_all(b"</table>")?;
Expand Down
7 changes: 4 additions & 3 deletions src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,15 @@ impl Row {
/// Print the row in HTML format to `out`.
///
/// If the row is has fewer columns than `col_num`, the row is padded with empty cells.
pub fn print_html<T: Write + ?Sized>(&self, out: &mut T, col_num: usize) -> Result<(), Error> {
/// Parameter `title` tells, if the row is a title-row
pub fn print_html<T: Write + ?Sized>(&self, out: &mut T, col_num: usize, title: bool) -> Result<(), Error> {
let mut printed_columns = 0;
for cell in self.iter() {
printed_columns += cell.print_html(out)?;
printed_columns += cell.print_html(out, title)?;
}
// Pad with empty cells, if target width is not reached
for _ in 0..col_num - printed_columns {
Cell::default().print_html(out)?;
Cell::default().print_html(out, title)?;
}
Ok(())
}
Expand Down

0 comments on commit 54ac8f8

Please sign in to comment.