use crate::ast::{Node, NodeKind, computed_style_to_text_style};
use crate::text::TextStyle;
pub fn collect_inline_segments(children: &[Node]) -> Vec<(String, TextStyle)> {
let mut segments = Vec::new();
for child in children {
match &child.kind {
NodeKind::Span { children: inner }
| NodeKind::Strong { children: inner }
| NodeKind::Emphasis { children: inner }
| NodeKind::Link {
children: inner, ..
}
| NodeKind::Delete { children: inner }
| NodeKind::Subscript { children: inner }
| NodeKind::Superscript { children: inner } => {
segments.extend(collect_inline_segments(inner));
}
NodeKind::Text { text } => {
if !text.is_empty() {
segments.push((text.clone(), computed_style_to_text_style(&child.style)));
}
}
NodeKind::InlineCode { code } => {
if !code.is_empty() {
segments.push((code.clone(), computed_style_to_text_style(&child.style)));
}
}
NodeKind::LineBreak => {
segments.push(("\n".to_string(), computed_style_to_text_style(&child.style)));
}
_ => {
let text = child.kind.text_content();
if !text.is_empty() {
segments.push((text, computed_style_to_text_style(&child.style)));
}
}
}
}
segments
}
pub fn annotate_runs_with_urls(
lines: &mut [crate::text::TextLine],
_total_text: &str,
segments: &[(String, TextStyle)],
) {
let mut seg_idx = 0;
let mut seg_char_consumed = 0_usize;
let seg_char_counts: Vec<usize> = segments.iter().map(|(s, _)| s.chars().count()).collect();
for line in lines.iter_mut() {
for run in line.runs.iter_mut() {
while seg_idx < seg_char_counts.len() && seg_char_consumed >= seg_char_counts[seg_idx] {
seg_idx += 1;
seg_char_consumed = 0;
}
if seg_idx < segments.len() {
let (_seg_text, seg_style) = &segments[seg_idx];
run.url = seg_style.url.clone();
run.decoration = seg_style.decoration;
seg_char_consumed += run.text.chars().count();
}
}
}
}
pub fn estimate_children_height(children: &[Node]) -> f32 {
let mut height = 0.0;
for child in children {
match &child.kind {
NodeKind::List {
children: items, ..
} => {
let item_count = items.len();
height += item_count as f32 * child.style.line_height_pt;
}
NodeKind::Blockquote { children: inner } => {
height += estimate_children_height(inner);
}
_ => {
height += child.style.line_height_pt;
}
}
}
height
}