use std::ops::Range;
use super::FormatRegistry;
use crate::markdown::{self, BlockKind};
pub(crate) fn read(text: &str) -> String {
markdown::reading(text).text
}
pub(crate) fn reads_prose(kind: BlockKind) -> bool {
matches!(
kind,
BlockKind::Prose
| BlockKind::BulletList
| BlockKind::NumberedList
| BlockKind::Table
| BlockKind::Blockquote
)
}
pub(crate) fn read_formats(formats: &FormatRegistry, read: fn(&str) -> String) -> FormatRegistry {
FormatRegistry {
entries: formats
.entries
.iter()
.map(|(name, (f, source))| (name.clone(), (f.read_with(&read), source.clone())))
.collect(),
types: formats.types.clone(),
}
}
pub(crate) fn read_row(text: &str) -> String {
row_reading(text).text
}
pub(crate) fn row_code_spans(text: &str) -> Vec<Range<usize>> {
let mut out = Vec::new();
let mut base = 0;
for (i, cell) in text.split(" | ").enumerate() {
if i > 0 {
base += 3;
}
out.extend(
markdown::code_spans_in(cell)
.into_iter()
.map(|r| r.start + base..r.end + base),
);
base += cell.len();
}
out
}
pub(crate) fn row_reading(text: &str) -> markdown::Reading {
let mut out = markdown::Reading::default();
let mut base = 0;
for (i, cell) in text.split(" | ").enumerate() {
if i > 0 {
out.map.extend(base..base + 3);
out.text.push_str(" | ");
base += 3;
}
let r = markdown::reading(cell);
out.map
.extend(r.map[..r.text.len()].iter().map(|o| base + o));
out.text.push_str(&r.text);
base += cell.len();
}
out.map.push(text.len());
out
}