Skip to content

Commit 8a66c54

Browse files
committed
fix html output for title-rows
- adds `CellType` for each `Cell` with corresponding getter/setter - in `Cell::print_html()` choose `<th>` or `<td>` based on `cell_type` - add `Row::set_cell_type()` to set type for all cells in a row - always set title-row to `CellType::Head`
1 parent 121ca97 commit 8a66c54

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

src/cell.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ use std::io::{Error, Write};
77
use std::str::FromStr;
88
use std::string::ToString;
99

10+
11+
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
12+
pub enum CellType {
13+
Head,
14+
Data
15+
}
16+
1017
/// Represent a table cell containing a string.
1118
///
1219
/// Once created, a cell's content cannot be modified.
@@ -18,6 +25,7 @@ pub struct Cell {
1825
align: Alignment,
1926
style: Vec<Attr>,
2027
hspan: usize,
28+
cell_type: CellType
2129
}
2230

2331
impl Cell {
@@ -38,6 +46,7 @@ impl Cell {
3846
align,
3947
style: Vec::new(),
4048
hspan: 1,
49+
cell_type: CellType::Data
4150
}
4251
}
4352

@@ -69,6 +78,12 @@ impl Cell {
6978
self
7079
}
7180

81+
/// Set cell type (head or data)
82+
pub fn with_cell_type(mut self, cell_type: CellType) -> Cell {
83+
self.set_cell_type(cell_type);
84+
self
85+
}
86+
7287
/// Remove all style attributes and reset alignment to default (LEFT)
7388
pub fn reset_style(&mut self) {
7489
self.style.clear();
@@ -197,6 +212,16 @@ impl Cell {
197212
self.hspan
198213
}
199214

215+
/// Set type for this cell (head or data)
216+
pub fn set_cell_type(&mut self, cell_type: CellType) {
217+
self.cell_type = cell_type;
218+
}
219+
220+
/// Get type of this cell (head or data)
221+
pub fn get_cell_type(&self) -> CellType {
222+
self.cell_type.clone()
223+
}
224+
200225
/// Return a copy of the full string contained in the cell
201226
pub fn get_content(&self) -> String {
202227
self.content.join("\n")
@@ -306,10 +331,14 @@ impl Cell {
306331
let content = self.content.join("<br />");
307332
out.write_all(
308333
format!(
309-
"<td{1} style=\"{2}\">{0}</td>",
334+
"<{3}{1} style=\"{2}\">{0}</{3}>",
310335
HtmlEscape(&content),
311336
colspan,
312-
styles
337+
styles,
338+
match self.cell_type {
339+
CellType::Head => "th",
340+
CellType::Data => "td",
341+
}
313342
)
314343
.as_bytes(),
315344
)?;
@@ -345,6 +374,7 @@ impl Default for Cell {
345374
align: Alignment::LEFT,
346375
style: Vec::new(),
347376
hspan: 1,
377+
cell_type: CellType::Data
348378
}
349379
}
350380
}

src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub mod csv;
2929
#[cfg(feature = "evcxr")]
3030
pub mod evcxr;
3131

32-
pub use cell::Cell;
32+
pub use cell::{Cell, CellType};
3333
use format::{consts, LinePosition, TableFormat};
3434
pub use row::Row;
3535
use utils::StringWriter;
@@ -218,9 +218,9 @@ impl<'a> TableSlice<'a> {
218218
out.write_all(b"<table>")?;
219219
// Print titles / table header
220220
if let Some(ref t) = *self.titles {
221-
out.write_all(b"<th>")?;
221+
out.write_all(b"<tr>")?;
222222
t.print_html(out, column_num)?;
223-
out.write_all(b"</th>")?;
223+
out.write_all(b"</tr>")?;
224224
}
225225
// Print rows
226226
for r in self.rows {
@@ -285,7 +285,8 @@ impl Table {
285285
}
286286

287287
/// Set the optional title lines
288-
pub fn set_titles(&mut self, titles: Row) {
288+
pub fn set_titles(&mut self, mut titles: Row) {
289+
titles.set_cell_type(CellType::Head);
289290
*self.titles = Some(titles);
290291
}
291292

src/row.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use super::Terminal;
99

1010
use super::format::{ColumnPosition, TableFormat};
1111
use super::utils::NEWLINE;
12-
use super::Cell;
12+
use super::{Cell, CellType};
1313

1414
/// Represent a table row made of cells
1515
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
@@ -140,6 +140,13 @@ impl Row {
140140
self.cells.iter_mut()
141141
}
142142

143+
/// Set cell type for all cells in this row
144+
pub fn set_cell_type(&mut self, cell_type: CellType) {
145+
for c in self.iter_mut() {
146+
c.set_cell_type(cell_type.clone());
147+
}
148+
}
149+
143150
/// Internal only
144151
fn __print<T: Write + ?Sized, F>(
145152
&self,
@@ -221,6 +228,7 @@ impl Row {
221228
/// Print the row in HTML format to `out`.
222229
///
223230
/// If the row is has fewer columns than `col_num`, the row is padded with empty cells.
231+
/// Parameter `title` tells, if the row is a title-row
224232
pub fn print_html<T: Write + ?Sized>(&self, out: &mut T, col_num: usize) -> Result<(), Error> {
225233
let mut printed_columns = 0;
226234
for cell in self.iter() {

0 commit comments

Comments
 (0)