use crate::helpers::re;
use regex::Captures;
type Cell = String;
type Row = Vec<Cell>;
type Table = Vec<Row>;
type Thead = Table;
type Tbody = Table;
type Tfoot = Table;
fn section_to_html(section: Table, name: &str) -> String {
let mut html: String = format!(" <t{name}>\n");
for row in section {
html.push_str(" <tr>\n");
for cell in row {
let format: &str = if name == "head" || name == "foot" { "th" } else { "td" };
html.push_str(&format!(" <{format}>{}</{format}>\n", cell));
}
html.push_str(" </tr>\n");
}
html.push_str(&format!(" </t{name}>\n"));
html
}
pub fn default(html: &mut String) {
re::parse(html, re::from(re::TABLE), | capture: Captures | {
let table: Table = capture[0].trim().lines()
.map(| mut line: &str | {
line = &line.trim();
if line.starts_with("|") {
line = &line.trim()[1..];
}
if line.ends_with("|") {
line = &line[0..(line.len() - 1)];
}
line.trim().split("|")
.map(| cell: &str | cell.replace(":", "").replace("-", "").trim().to_string())
.collect::<Row>()
})
.map(| mut row: Row | if row.join("").trim().is_empty() { row.clear(); row } else { row })
.collect::<Table>();
let mut max: usize = 0;
let (mut i, table_length): (usize, usize) = (0, table.len());
let (mut table_header, mut table_body, mut table_footer): (Thead, Tbody, Tfoot) = (vec![], vec![], vec![]);
let mut current_section: &str = "header";
for row in table {
let row_length: usize = row.len();
if max < row_length {
max = row_length;
}
if !((i == 0 && row_length < 1) || (i == (table_length - 1) && row_length < 1)) {
if row_length < 1 {
current_section = match current_section { "header" => "body", "body" => "footer", _ => "footer" };
}
else {
match current_section {
"header" => table_header.push(row),
"body" => table_body.push(row),
_ => table_footer.push(row)
}
}
}
i += 1;
}
let mut body: Tbody;
let (mut header, mut footer): (Thead, Tfoot) = (vec![], vec![]);
if table_header.is_empty() && table_body.is_empty() && table_footer.is_empty() {
return String::new()
}
else if !table_header.is_empty() && table_body.is_empty() && table_footer.is_empty() {
body = table_header;
}
else if !table_header.is_empty() && !table_body.is_empty() && table_footer.is_empty() {
(header, body) = (table_header, table_body);
}
else if !table_header.is_empty() && !table_body.is_empty() && !table_footer.is_empty() {
(header, body, footer) = (table_header, table_body, table_footer);
}
else if table_header.is_empty() && table_body.is_empty() && !table_footer.is_empty() {
body = table_footer;
}
else if table_header.is_empty() && !table_body.is_empty() && !table_footer.is_empty() {
(body, footer) = (table_body, table_footer);
}
else if !table_header.is_empty() && table_body.is_empty() && !table_footer.is_empty() {
(header, body) = (table_header, table_footer);
}
else if !table_header.is_empty() && table_body.is_empty() && !table_footer.is_empty() {
body = table_body;
}
else {
(header, body, footer) = (table_header, table_body, table_footer);
}
while header.len() > 1 {
body.insert(0, header.pop().unwrap());
}
while footer.len() > 1 {
body.insert(body.len(), footer.remove(0));
}
format!(
"\n<table>\n{}{}{}</table>\n",
section_to_html(header, "head"),
section_to_html(body, "body"),
section_to_html(footer, "foot")
)
});
}