use super::*;
pub fn inline_text(spans: &[InlineSpan]) -> String {
let mut out = String::new();
for span in spans {
append_inline_text(span, &mut out);
}
out
}
pub(super) fn append_inline_text(span: &InlineSpan, out: &mut String) {
match span {
InlineSpan::Text(text) | InlineSpan::Code(text) => out.push_str(text),
InlineSpan::Presented { spans, .. }
| InlineSpan::Emphasis(spans)
| InlineSpan::Strong(spans) => {
for inner in spans {
append_inline_text(inner, out);
}
},
InlineSpan::Link { spans, .. }
| InlineSpan::Submit { spans, .. }
| InlineSpan::InPage { spans, .. } => {
for inner in spans {
append_inline_text(inner, out);
}
},
InlineSpan::SoftBreak => out.push(' '),
InlineSpan::LineBreak => out.push('\n'),
}
}
pub(super) fn collect_block_spans<'a>(block: &'a Block, out: &mut Vec<&'a InlineSpan>) {
match block {
Block::Presented { block, .. } => collect_block_spans(block, out),
Block::Heading { spans, .. } | Block::Paragraph { spans } => {
for span in spans {
out.push(span);
}
},
Block::Quote { blocks } => {
for inner in blocks {
collect_block_spans(inner, out);
}
},
Block::List { items, .. } => {
for item in items {
for inner in item {
collect_block_spans(inner, out);
}
}
},
Block::Table { header, rows, .. } => {
for cell in header.iter().chain(rows.iter().flatten()) {
for span in cell {
out.push(span);
}
}
},
Block::Menu { rows } => {
for row in rows {
for span in &row.label {
out.push(span);
}
}
},
Block::CodeBlock { .. }
| Block::Image { .. }
| Block::Preformatted { .. }
| Block::Rule
| Block::FeedHeader { .. }
| Block::FeedEntry { .. }
| Block::MetadataRow { .. }
| Block::Badge { .. } => {},
}
}
pub(super) fn collect_block_link_urls<'a>(block: &'a Block, out: &mut Vec<&'a str>) {
match block {
Block::Presented { block, .. } => collect_block_link_urls(block, out),
Block::Heading { spans, .. } | Block::Paragraph { spans } => {
for span in spans {
collect_link_urls(span, out);
}
},
Block::Quote { blocks } => {
for inner in blocks {
collect_block_link_urls(inner, out);
}
},
Block::List { items, .. } => {
for item in items {
for inner in item {
collect_block_link_urls(inner, out);
}
}
},
Block::FeedHeader { source_url, .. } => {
if let Some(url) = source_url {
out.push(url.as_str());
}
},
Block::FeedEntry {
article_url,
source_url,
content_address,
..
} => {
if let Some(url) = article_url {
out.push(url.as_str());
}
if let Some(url) = source_url {
out.push(url.as_str());
}
if let Some(url) = content_address {
out.push(url.as_str());
}
},
Block::Table { header, rows, .. } => {
for cell in header.iter().chain(rows.iter().flatten()) {
for span in cell {
collect_link_urls(span, out);
}
}
},
Block::Menu { rows } => {
for row in rows {
if let Some(url) = &row.target {
out.push(url.as_str());
}
for span in &row.label {
collect_link_urls(span, out);
}
}
},
Block::CodeBlock { .. }
| Block::Image { .. }
| Block::Preformatted { .. }
| Block::Rule
| Block::MetadataRow { .. }
| Block::Badge { .. } => {},
}
}
pub(super) fn collect_link_urls<'a>(span: &'a InlineSpan, out: &mut Vec<&'a str>) {
match span {
InlineSpan::Link { url, spans, .. } => {
out.push(url.as_str());
for inner in spans {
collect_link_urls(inner, out);
}
},
InlineSpan::Presented { spans, .. }
| InlineSpan::Emphasis(spans)
| InlineSpan::Strong(spans)
| InlineSpan::Submit { spans, .. }
| InlineSpan::InPage { spans, .. } => {
for inner in spans {
collect_link_urls(inner, out);
}
},
InlineSpan::Text(_)
| InlineSpan::Code(_)
| InlineSpan::LineBreak
| InlineSpan::SoftBreak => {},
}
}