use comrak::nodes::{AlertType, AstNode, NodeAlert};
use docx_rs::*;
use super::{Block, Ctx};
use crate::styles;
pub(super) fn build<'a>(node: &'a AstNode<'a>, alert: &NodeAlert, ctx: &mut Ctx) -> Table {
let (accent, fill) = palette(alert.alert_type);
let label = alert
.title
.clone()
.unwrap_or_else(|| alert.alert_type.default_title().to_string());
let mut cell = TableCell::new().shading(Shading::new().fill(fill));
cell = cell
.add_paragraph(Paragraph::new().add_run(Run::new().bold().color(accent).add_text(label)));
let mut blocks = Vec::new();
super::render_blocks(node, ctx, &mut blocks);
for block in blocks {
cell = match block {
Block::Body(p) | Block::Para(p) => cell.add_paragraph(p),
Block::Table(t) => cell.add_table(t),
Block::Gap => cell.add_paragraph(styles::block_gap_paragraph()),
};
}
let left = TableBorder::new(TableBorderPosition::Left)
.border_type(BorderType::Single)
.size(styles::ALERT_BORDER_SIZE)
.color(accent);
let borders = TableBorders::with_empty().set(left);
Table::new(vec![TableRow::new(vec![cell])])
.width(ctx.content_width_dxa, WidthType::Dxa)
.margins(styles::table_cell_margins())
.set_borders(borders)
}
fn palette(kind: AlertType) -> (&'static str, &'static str) {
match kind {
AlertType::Note => styles::ALERT_NOTE,
AlertType::Tip => styles::ALERT_TIP,
AlertType::Important => styles::ALERT_IMPORTANT,
AlertType::Warning => styles::ALERT_WARNING,
AlertType::Caution => styles::ALERT_CAUTION,
}
}