diff --git a/crates/formats/src/boxes.rs b/crates/formats/src/boxes.rs index 9173fd52..e7e04acc 100644 --- a/crates/formats/src/boxes.rs +++ b/crates/formats/src/boxes.rs @@ -303,7 +303,11 @@ fn styled_spans_in(block: &Block) -> usize { Block::Table { head, rows } => head.iter().chain(rows.iter().flatten()).collect(), Block::Code { .. } | Block::MathBlock(_) | Block::Rule | Block::Image { .. } => Vec::new(), }; - lines.iter().flat_map(|spans| spans.iter()).filter(|span| span.font_size.is_some() || span.font_family.is_some()).count() + lines + .iter() + .flat_map(|spans| spans.iter()) + .filter(|span| span.font_size.is_some() || span.font_family.is_some() || span.colour.is_some() || span.highlight.is_some()) + .count() } #[cfg(test)] diff --git a/crates/formats/src/html.rs b/crates/formats/src/html.rs index 602ab6f1..f79de27a 100644 --- a/crates/formats/src/html.rs +++ b/crates/formats/src/html.rs @@ -330,6 +330,15 @@ fn font_size(attrs: &str) -> Option { }) } +/// The text colour or highlight of an HTML span, when it is a colour NoteCraft writes (#260). +fn span_colour(attrs: &str, wanted: &str) -> Option { + let style = attr(attrs, "style")?; + crate::markdown::style_declarations(&style).into_iter().find_map(|declaration| { + let (property, value) = declaration.split_once(':')?; + property.trim().eq_ignore_ascii_case(wanted).then(|| crate::markdown::css_colour(value)).flatten() + }) +} + fn font_family(attrs: &str) -> Option { let style = attr(attrs, "style")?; crate::markdown::style_declarations(&style).into_iter().find_map(|declaration| { @@ -603,8 +612,11 @@ impl Writer { ("span", false) => { let size = font_size(attrs); let family = font_family(attrs); - self.span_sizes.push(size.is_some() || family.is_some()); - if size.is_some() || family.is_some() { + let colour = span_colour(attrs, "color"); + let highlight = span_colour(attrs, "background-color"); + let styled = size.is_some() || family.is_some() || colour.is_some() || highlight.is_some(); + self.span_sizes.push(styled); + if styled { let mut styles = Vec::new(); if let Some(family) = family { styles.push(format!("font-family: {}", escape(&crate::markdown::font_family_css(&family)))); @@ -612,6 +624,12 @@ impl Writer { if let Some(size) = size { styles.push(format!("font-size: {size}pt")); } + if let Some(colour) = colour { + styles.push(format!("color: {colour}")); + } + if let Some(colour) = highlight { + styles.push(format!("background-color: {colour}")); + } self.push(&format!("", styles.join("; "))); } if attrs.to_lowercase().contains("math") { diff --git a/crates/formats/src/markdown.rs b/crates/formats/src/markdown.rs index 42f8b7cf..7b850045 100644 --- a/crates/formats/src/markdown.rs +++ b/crates/formats/src/markdown.rs @@ -21,6 +21,10 @@ pub struct Span { /// Font size in points, carried by a NoteCraft inline ``. pub font_size: Option, pub font_family: Option, + /// Text colour, as `#rrggbb`, from a NoteCraft inline `` (#260). + pub colour: Option, + /// Highlight behind the text, as `#rrggbb`, from `background-color` (#260). + pub highlight: Option, /// Link target, if this span is part of a link. pub link: Option, /// Image source, if this span is an inline image (the text is the alt text). @@ -69,6 +73,8 @@ struct Style { script: Option