//! `rumdl check --fix` for MD065 must not turn text or raw HTML into a rule's //! neighbours: a run of `*`, `-` or `_` that the parser reads as paragraph text, //! a line of an HTML block or a line of a footnote paragraph is not a thematic //! break, however the containers around it are written. //! //! The oracle parses the document before and after the fix with pulldown-cmark //! (footnotes on) and lists every leaf block with the containers around it, HTML //! blocks and footnote paragraphs included, so a fix that splits a paragraph, a //! raw HTML block or a footnote body shows as a different list. use pulldown_cmark::{Event, Options, Parser, Tag, TagEnd}; use std::fs; use std::process::Command; use tempfile::tempdir; /// Every leaf block as `containers > kind: text`, in document order. Tight and /// loose lists are not told apart. fn skeleton(content: &str) -> Vec { let mut path: Vec<&'static str> = Vec::new(); let mut blocks = Vec::new(); let mut text = String::new(); let mut open: Option<&'static str> = None; for event in Parser::new_ext(content, Options::ENABLE_FOOTNOTES) { let boundary = matches!( event, Event::Rule | Event::Start( Tag::List(_) | Tag::Item | Tag::BlockQuote(_) | Tag::FootnoteDefinition(_) | Tag::Heading { .. } | Tag::CodeBlock(_) | Tag::HtmlBlock ) | Event::End(TagEnd::List(_) | TagEnd::Item | TagEnd::BlockQuote(_) | TagEnd::FootnoteDefinition) ); if boundary && !text.is_empty() { blocks.push(format!("{} > text: {}", path.join(" "), text.trim())); text.clear(); } match event { Event::Start(Tag::List(_)) => path.push("list"), Event::Start(Tag::Item) => path.push("item"), Event::Start(Tag::BlockQuote(_)) => path.push("quote"), Event::Start(Tag::FootnoteDefinition(_)) => path.push("footnote"), Event::End(TagEnd::List(_) | TagEnd::Item | TagEnd::BlockQuote(_) | TagEnd::FootnoteDefinition) => { path.pop(); } Event::Rule => blocks.push(format!("{} > rule", path.join(" "))), Event::Start(Tag::Heading { .. }) => open = Some("heading"), Event::Start(Tag::CodeBlock(_)) => open = Some("code"), Event::Start(Tag::HtmlBlock) => open = Some("html"), Event::Text(t) | Event::Code(t) | Event::Html(t) | Event::InlineHtml(t) => text.push_str(&t), Event::SoftBreak => text.push(' '), Event::End(TagEnd::Heading(_) | TagEnd::CodeBlock | TagEnd::HtmlBlock) => { if let Some(kind) = open.take() { blocks.push(format!("{} > {kind}: {}", path.join(" "), text.trim())); text.clear(); } } Event::End(TagEnd::Paragraph) => { blocks.push(format!("{} > text: {}", path.join(" "), text.trim())); text.clear(); } _ => {} } } blocks } /// The document after `rumdl check --fix`, with the given extra arguments. fn fix(content: &str, extra: &[&str]) -> String { let dir = tempdir().unwrap(); let path = dir.path().join("doc.md"); fs::write(&path, content).unwrap(); // The exit status reports what is left unfixed, which is not under test. Command::new(env!("CARGO_BIN_EXE_rumdl")) .args(["check", "--isolated", "--no-cache", "--fix"]) .args(extra) .arg(&path) .output() .expect("run rumdl check --fix"); fs::read_to_string(&path).unwrap() } /// The number of MD065 warnings `rumdl check` prints for `content`. fn md065_warnings(content: &str) -> usize { let dir = tempdir().unwrap(); let path = dir.path().join("doc.md"); fs::write(&path, content).unwrap(); let output = Command::new(env!("CARGO_BIN_EXE_rumdl")) .args(["check", "--isolated", "--no-cache", "--enable", "MD065"]) .arg(&path) .output() .expect("run rumdl check"); String::from_utf8_lossy(&output.stdout).matches("[MD065]").count() } const ONLY_MD065: &[&str] = &["--enable", "MD065"]; /// Text the parser reads as part of a paragraph or an HTML block, where MD065 has /// no rule to surround. const NOT_RULES: &[&str] = &[ // A tag alone on a line cannot interrupt a paragraph. "> o\n> \n> ***\n", "> o\n> \n> ***\n", "> o\n> \n> ***\n", "> o\n> \n> ***\n", "> o\n> \n> ***\n", "- o\n \n ***\n", "> o\n>