use std::{fmt::Write as _, ops::Range};
use serde::{Deserialize, Serialize};
use super::{Dialect, Marker, code, link_definitions};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Region {
pub target: String,
pub open_line: usize,
pub close_line: usize,
pub content: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DefinitionBlock {
pub line: usize,
pub entries: Vec<(String, String)>,
pub trailing: bool,
pub text: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Segments {
pub regions: Vec<Region>,
pub definition: Option<DefinitionBlock>,
pub unclosed: Option<usize>,
pub stray_closes: Vec<usize>,
}
impl Segments {
pub fn in_region(&self, line: usize) -> bool {
self.regions
.iter()
.any(|i| i.open_line < line && line < i.close_line)
|| self.unclosed.is_some_and(|open| line > open)
}
pub fn in_definition(&self, line: usize) -> bool {
self.definition.as_ref().is_some_and(|d| line >= d.line)
}
pub fn is_generated(&self, line: usize) -> bool {
self.in_region(line)
|| self.in_definition(line)
|| self
.regions
.iter()
.any(|i| i.open_line == line || i.close_line == line)
}
}
pub fn segments(body: &str, first_line: usize, dialect: &Dialect) -> Segments {
let mut out = Segments::default();
let lines: Vec<&str> = body.lines().collect();
let mut fences: Option<Vec<Range<usize>>> = None;
let mut open: Option<(String, usize, usize)> = None; let mut start = 0;
for (i, raw) in body.split_inclusive('\n').enumerate() {
let line = lines[i];
let file_line = first_line + i;
let at = start;
start += raw.len();
let marker = dialect.marker(line).filter(|_| {
let fences = fences.get_or_insert_with(|| code::fenced(body));
let k = fences.partition_point(|f| f.end <= at);
!fences.get(k).is_some_and(|f| f.contains(&at))
});
match marker {
Some(Marker::Open(target)) if open.is_none() => {
open = Some((target, file_line, i));
}
Some(Marker::Close) => match open.take() {
Some((target, open_line, open_index)) => {
let content: String =
lines[open_index + 1..i]
.iter()
.fold(String::new(), |mut out, l| {
let _ = writeln!(out, "{l}");
out
});
out.regions.push(Region {
target,
open_line,
close_line: file_line,
content,
});
}
None => out.stray_closes.push(file_line),
},
Some(Marker::Trailer) if open.is_none() => {
out.definition = Some(definition(&lines[i..], file_line));
return finish(out, open.as_ref());
}
Some(Marker::Open(_) | Marker::Trailer) | None => {}
}
}
finish(out, open.as_ref())
}
fn finish(mut out: Segments, open: Option<&(String, usize, usize)>) -> Segments {
if let Some(&(_, line, _)) = open {
out.unclosed = Some(line);
}
out
}
fn definition(lines: &[&str], line: usize) -> DefinitionBlock {
let after: Vec<&str> = lines[1..].iter().map(|l| l.trim_start()).collect();
let top: Vec<_> = link_definitions::read(&after)
.defs
.into_iter()
.filter(|d| after[d.first].starts_with('['))
.collect();
let taken = |i: usize| top.iter().any(|d| d.first <= i && i <= d.last);
let trailing = after
.iter()
.enumerate()
.any(|(i, l)| !l.trim().is_empty() && !taken(i));
let entries = top.into_iter().map(|d| (d.label, d.dest)).collect();
let text: String = lines.iter().fold(String::new(), |mut out, l| {
let _ = writeln!(out, "{l}");
out
});
DefinitionBlock {
line,
entries,
trailing,
text,
}
}
pub fn ref_def(line: &str) -> Option<(String, String)> {
let t = line.trim();
let rest = t.strip_prefix('[')?;
let end = rest.find("]:")?;
let label = &rest[..end];
if label.is_empty() || label.contains('[') {
return None;
}
let dest = rest[end + 2..].trim();
let dest = dest.split_whitespace().next()?;
Some((label.to_string(), dest.to_string()))
}
#[cfg(test)]
mod tests {
use super::*;
const HOST: Dialect = Dialect {
namespace: "host",
region: Some("include"),
trailer: Some("links"),
scheme: Some("host"),
};
fn segments(body: &str, first_line: usize) -> Segments {
super::segments(body, first_line, &HOST)
}
#[test]
fn finds_includes_and_the_definition_block() {
let body = "intro\n\n<!-- host:include a -->\n\nbody a\n\n<!-- /host:include -->\n\n```\n<!-- host:include fenced -->\n```\n\n<!-- host:links -->\n\n[host:a]: /k/a.md\n[host:b#c]: /k/b.md#c\n";
let s = segments(body, 10);
assert_eq!(s.regions.len(), 1);
let inc = &s.regions[0];
assert_eq!(inc.target, "a");
assert_eq!((inc.open_line, inc.close_line), (12, 16));
assert_eq!(inc.content, "\nbody a\n\n");
let def = s.definition.clone().unwrap();
assert_eq!(def.line, 22);
assert_eq!(
def.entries,
vec![
("host:a".to_string(), "/k/a.md".to_string()),
("host:b#c".to_string(), "/k/b.md#c".to_string())
]
);
assert!(!def.trailing);
assert!(def.text.starts_with("<!-- host:links -->\n"));
assert!(s.unclosed.is_none());
assert!(s.in_region(14));
assert!(!s.in_region(12));
assert!(s.in_definition(23));
assert!(s.is_generated(12));
assert!(!s.is_generated(11));
}
#[test]
fn unclosed_and_stray_markers() {
let s = segments(
"<!-- /host:include -->\n<!-- host:include a -->\nx\n<!-- host:include b -->\n",
1,
);
assert_eq!(s.stray_closes, vec![1]);
assert_eq!(s.unclosed, Some(2));
assert!(s.regions.is_empty(), "{:?}", s.regions);
assert!(s.in_region(3));
let s = segments(
"<!-- host:include a -->\n<!-- host:links -->\n<!-- /host:include -->\n",
1,
);
assert!(s.definition.is_none());
assert_eq!(s.regions.len(), 1);
}
#[test]
fn definition_block_with_trailing_content() {
let s = segments("x\n<!-- host:links -->\n[host:a]: /a.md\nnot a def\n", 1);
let def = s.definition.unwrap();
assert!(def.trailing);
assert_eq!(def.entries.len(), 1);
}
#[test]
fn a_wrapped_definition_of_the_trailer_is_one_entry() {
for after in [
"[host:a]: /k/a.md\n[host:b#c]: /k/b.md#c \"T\"\n",
"[host:a]:\n /k/a.md\n[host:b#c]:\n /k/b.md#c\n \"T\"\n",
"[host:a]: /k/a.md\n\n[host:b#c]: /k/b.md#c\n\"T\"\n",
" [host:a]: /k/a.md\n[host:b#c]: /k/b.md#c \"T\nU\"\n",
] {
let s = segments(&format!("x\n\n<!-- host:links -->\n\n{after}"), 1);
let def = s.definition.unwrap();
assert!(!def.trailing, "{after:?}");
assert_eq!(
def.entries,
vec![
("host:a".to_string(), "/k/a.md".to_string()),
("host:b#c".to_string(), "/k/b.md#c".to_string())
],
"{after:?}"
);
}
let s = segments("<!-- host:links -->\n[host:a]: <k a.md>\n", 1);
let def = s.definition.unwrap();
assert!(!def.trailing);
assert_eq!(
def.entries,
vec![("host:a".to_string(), "<k a.md>".to_string())]
);
for after in [
"[host:a]:\n",
"> [host:a]: /k/a.md\n",
"[host:a]: /k/a.md\n>\n",
"- [host:a]: /k/a.md\n",
"[host:a]: /k/a.md \"T\n",
] {
let s = segments(&format!("<!-- host:links -->\n{after}"), 1);
assert!(s.definition.unwrap().trailing, "{after:?}");
}
}
#[test]
fn fences_hide_markers_including_tildes_and_longer_closers() {
let body = "~~~\n<!-- host:links -->\n~~~\n````md\n```\n<!-- host:include a -->\n````\n- ```\n <!-- host:include a -->\n ```\n<!-- host:links -->\n";
let s = segments(body, 1);
assert!(s.regions.is_empty(), "{:?}", s.regions);
assert_eq!(s.definition.unwrap().line, 11);
}
fn targets(body: &str) -> Vec<String> {
segments(body, 1)
.regions
.into_iter()
.map(|r| r.target)
.collect()
}
#[test]
fn a_fence_in_a_footnote_hides_a_marker() {
let body = "Text[^1].\n\n[^1]: Note.\n\n ```\n <!-- host:include a -->\n <!-- /host:include -->\n ```\n";
let found = targets(body);
assert!(found.is_empty(), "{found:?}");
}
#[test]
fn a_fence_ends_with_the_quote_or_the_item_that_holds_it() {
let region = "\n<!-- host:include a -->\nx\n<!-- /host:include -->\n";
for fence in [
"> - ```\n> code\n",
"- a\n - b\n\n > ```\n > code\n",
">\t```\n>\tcode\n",
"- ```\n code\n",
] {
assert_eq!(targets(&format!("{fence}{region}")), ["a"], "{fence:?}");
}
}
#[test]
fn only_a_fence_hides_a_marker() {
for text in [" ```\n", "``` a ` b\n", "``\n"] {
let body = format!("{text}\n<!-- host:include a -->\nx\n<!-- /host:include -->\n");
assert_eq!(targets(&body), ["a"], "{text:?}");
}
}
#[test]
fn ref_defs() {
assert_eq!(ref_def("[a]: /x"), Some(("a".into(), "/x".into())));
assert_eq!(
ref_def("[a]: /x \"title\""),
Some(("a".into(), "/x".into()))
);
assert_eq!(ref_def("[a]:"), None);
assert_eq!(ref_def("[]: x"), None);
assert_eq!(ref_def("[a][b]: x"), None);
assert_eq!(ref_def("text"), None);
}
}