html2md/scraper/tables.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
use super::StructuredPrinter;
use super::TagHandler;
use super::{clean_markdown, walk};
use std::sync::Arc;
use std::{cmp, collections::HashMap};
use html5ever::LocalName;
use markup5ever_rcdom::{Handle, NodeData};
use url::Url;
#[derive(Default)]
pub struct TableHandler {
commonmark: bool,
url: Option<Arc<Url>>,
}
const TD: LocalName = html5ever::local_name!("td");
const TH: LocalName = html5ever::local_name!("th");
const TABLE_LIMIT: usize = 1000;
impl TableHandler {
/// A new table handler.
pub fn new(commonmark: bool, url: Option<std::sync::Arc<Url>>) -> Self {
TableHandler { commonmark, url }
}
}
impl TagHandler for TableHandler {
fn handle(&mut self, tag: &Handle, printer: &mut StructuredPrinter) {
let mut table_markup = String::new();
let any_matcher = |cell: &Handle| match cell.data {
NodeData::Element { ref name, .. } => name.local == TD || name.local == TH,
_ => false,
};
// detect cell width, counts
let mut column_widths: Vec<usize> = Vec::new();
let rows = find_children(tag, "tr");
// detect row count
let most_big_row = rows.iter().max_by(|left, right| {
collect_children(left, any_matcher)
.len()
.cmp(&collect_children(right, any_matcher).len())
});
if most_big_row.is_some() {
let column_count = match most_big_row {
Some(tag) => collect_children(tag, any_matcher).len(),
_ => 0,
};
column_widths = vec![3; column_count];
// detect max column width
for (idx, row) in rows.iter().enumerate() {
if idx >= TABLE_LIMIT {
break;
}
let cells = collect_children(row, any_matcher);
for index in 0..column_count {
// from regular rows
if let Some(cell) = cells.get(index) {
let text = to_text(cell, self.commonmark, &self.url);
column_widths[index] = cmp::max(column_widths[index], text.chars().count());
}
}
}
// header row must always be present
for (idx, row) in rows.iter().enumerate() {
if idx >= TABLE_LIMIT {
break;
}
table_markup.push('|');
let cells = collect_children(row, any_matcher);
for index in 0..column_count {
// we need to fill all cells in a column, even if some rows don't have enough
let padded_cell_text = pad_cell_text(
&cells.get(index),
column_widths[index],
self.commonmark,
&self.url,
);
table_markup.push_str(&padded_cell_text);
table_markup.push('|');
}
table_markup.push('\n');
if idx == 0 {
// first row is a header row
// add header-body divider row
table_markup.push('|');
for index in 0..column_count {
let width = column_widths[index];
if width < 3 {
// no point in aligning, just post as-is
table_markup.push_str(&"-".repeat(width.max(2)));
table_markup.push('|');
continue;
}
// // try to detect alignment
// let mut alignment = String::new();
// if let Some(header_cell) = cells.get(index) {
// // we have a header, try to extract alignment from it
// alignment = match header_cell.data {
// NodeData::Element { ref attrs, .. } => {
// let attrs = attrs.borrow();
// let align_attr = attrs
// .iter()
// .find(|attr| attr.name.local.to_string() == "align");
// align_attr
// .map(|attr| attr.value.to_string())
// .unwrap_or_default()
// }
// _ => String::new(),
// };
// }
// push lines according to alignment, fallback to default behaviour
// match alignment.as_ref() {
// "left" => {
// table_markup.push(':');
// table_markup.push_str(&"-".repeat(width - 1));
// }
// "center" => {
// table_markup.push(':');
// table_markup.push_str(&"-".repeat(width - 2));
// table_markup.push(':');
// }
// "right" => {
// table_markup.push_str(&"-".repeat(width - 1));
// table_markup.push(':');
// }
// _ => table_markup.push_str(&"-".repeat(width)),
// }
table_markup.push('|');
}
table_markup.push('\n');
}
}
// remove the children to prevent infinite loops
tag.children.take();
printer.insert_newline();
printer.insert_newline();
printer.append_str(&table_markup);
}
}
fn after_handle(&mut self, _printer: &mut StructuredPrinter) {}
fn skip_descendants(&self) -> bool {
true
}
}
/// Pads cell text from right and left so it looks centered inside the table cell
/// ### Arguments
/// `tag` - optional reference to currently processed handle, text is extracted from here
///
/// `column_width` - precomputed column width to compute padding length from
fn pad_cell_text(
tag: &Option<&Handle>,
column_width: usize,
commonmark: bool,
url: &Option<Arc<Url>>,
) -> String {
let mut result = String::new();
if let Some(cell) = tag {
// have header at specified position
let text = to_text(cell, commonmark, url);
// compute difference between width and text length
let len_diff = column_width
.checked_sub(text.chars().count())
.unwrap_or_default();
if len_diff > 0 {
// should pad
if len_diff > 1 {
result.push(' ');
result.push_str(&text);
result.push(' ');
} else {
// it's just one space, add at the end
result.push_str(&text);
result.push(' ');
}
} else {
// shouldn't pad, text fills whole cell
result.push_str(&text);
}
} else {
// no text in this cell, fill cell with spaces
result.push(' ');
}
result
}
/// Extracts tag name from passed tag
/// Returns empty string if it's not an html element
fn tag_name(tag: &Handle) -> String {
match tag.data {
NodeData::Element { ref name, .. } => name.local.to_string(),
_ => String::new(),
}
}
/// Find descendants of this tag with tag name `name`
/// This includes both direct children and descendants
fn find_children(tag: &Handle, name: &str) -> Vec<Handle> {
let children = tag.children.borrow();
let mut result: Vec<Handle> = vec![];
for child in children.iter() {
if tag_name(child) == name {
result.push(child.clone());
}
let mut descendants = find_children(child, name);
result.append(&mut descendants);
}
result
}
/// Collect direct children that satisfy the predicate
/// This doesn't include descendants
fn collect_children<P>(tag: &Handle, predicate: P) -> Vec<Handle>
where
P: Fn(&Handle) -> bool,
{
let children = tag.children.borrow();
let mut result: Vec<Handle> = Vec::with_capacity(children.len());
for child in children.iter() {
if predicate(child) {
result.push(child.clone());
}
}
result
}
/// Convert html tag to text. This collects all tag children in correct order where they're observed
/// and concatenates their text, recursively.
fn to_text(tag: &Handle, commonmark: bool, url: &Option<Arc<Url>>) -> String {
let mut printer = StructuredPrinter::default();
walk(
&tag,
&mut printer,
&HashMap::default(),
commonmark,
&url,
true,
);
clean_markdown(&printer.data)
}