requirements.references.markdown_literals
Requirements index · View definition
Requirement
In Markdown, escaped roles and role-looking text in inline code, fenced code, or raw HTML MUST remain literal and MUST NOT count as references. Raw HTML means an HTML block, an HTML comment, or an inline HTML tag including its attributes; text between inline tags is ordinary Markdown. A
{req}directive inside a fenced code example and a legacy inline{req}role MUST NOT define a requirement.
Rationale
I judge the tests adequate. They cover each obligation in requirements.references.markdown_literals, and their assertions check outcomes. I did not request implementation source. The test text was enough to map every clause.
- Escaped roles stay literal.
src/evidence.rs#mod[tests]/fn[every_markdown_extension_keeps_escaped_and_code_roles_literal]includes\{role}and asserts that no reference comes from any Markdown file.src/lib.rs#mod[tests]/fn[markdown_examples_and_escaped_references_stay_literal]runs the preprocessor and asserts that the escaped role leaves the content intact.- The same lib.rs test also covers backslash parity.
\\{marker}is not escaped,\\\{marker}is, androles.len()==1atstart==2pins this.
- Inline code stays literal.
- The evidence.rs test, the lib.rs preprocessor test and
src/terms.rs#mod[tests]/fn[term_dependencies_of_every_markdown_extension_skip_literal_roles]each include a role in backticks. src/lib.rs#mod[tests]/fn[code_examples_and_legacy_roles_remain_literal]uses a double-backtick span.
- The evidence.rs test, the lib.rs preprocessor test and
- Fenced code stays literal.
- Roles inside
```textfences are tested in evidence.rs, in the lib.rs preprocessor test and in terms.rs. - The evidence.rs test also puts a
:::{cite}directive fence inside a code fence. src/lib.rs#mod[tests]/fn[references_in_requirement_bodies_are_scanned_as_markdown]covers a fence inside a requirement body.
- Roles inside
- Raw HTML stays literal, and text between inline tags is ordinary Markdown.
src/lib.rs#mod[tests]/fn[roles_in_raw_html_stay_literal]covers an HTML block, a block comment, an inline comment and an attribute value. It asserts that only the role between<em>and</em>counts as a reference. It also asserts that 5 roles count in non-Markdown mode.src/fenced.rs#mod[tests]/fn[markdown_fences_in_raw_html_stay_literal]covers directive fences inside a block, a comment and a multi-line tag attribute. It asserts that only the outside fence counts, and that all fences count in plain-text mode.- The evidence.rs test, terms.rs and
tests/definitions.rs#fn[literal_term_roles_in_subject_and_requirements_inputs_need_no_definition]cover the attribute case for other role kinds.
- A
{req}directive in a fenced code example does not define a requirement.code_examples_and_legacy_roles_remain_literalhas an indented:::{req} blockinside a fence. It asserts that the text stays literal while the realvisibledirective is rendered as a requirement. - A legacy inline
{req}role does not define a requirement.code_examples_and_legacy_roles_remain_literalandsrc/lib.rs#mod[tests]/fn[colon_directives_render_markdown_bodies_and_legacy_roles_stay_literal]assert that{req}legacyand `{req}`inlinestay literal and produce nohabeas-req-requirements.inlineanchor. - Markdown versus non-Markdown files. The evidence.rs and terms.rs tests cover
.md,.markdown,.MDand.Markdown. Each uses.txtas a negative control, which shows the literal rules apply only to Markdown.
I found no named obligation or boundary without a test that asserts its outcome.
Inputs
Tests
Input 2: outline src/evidence.rs mod[tests]/fn[source_references_are_covered_by_evidence_inputs]
- File:
src/evidence.rs - Selector:
mod[tests]/fn[source_references_are_covered_by_evidence_inputs] - Kind:
fn src/evidence.rs:2738–2774
// {test}`requirements.references.evidence_coverage`
// {test}`requirements.references.markdown_literals`
#[test]
fn source_references_are_covered_by_evidence_inputs() {
let temp = fixture();
fs::write(
temp.path().join("md/reqs/foo/bar.md"),
"# Requirements\n\n:::{req} ready\nReady.\n:::\n",
)
.unwrap();
let implementation = "{impl}`foo.bar.ready`".to_owned();
let citation = "{cite}`foo.bar.ready`".to_owned();
let test = "{test}`foo.bar.ready`".to_owned();
let example = "{cite}`missing.example`".to_owned();
let source = format!("// {implementation}\n// {citation}\n");
let notes = format!("# Notes\n\n{test}\n\n`{example}`\n\n```text\n{example}\n```\n");
fs::write(temp.path().join("src/main.rs"), &source).unwrap();
fs::write(temp.path().join("md/notes.md"), ¬es).unwrap();
fs::write(
temp.path().join("evidence/foo.bar.ready.json"),
json!({
"requirement": "Ready.",
"determination": true,
"rationale": "The source and notes were examined.",
"inputs": [
outline(temp.path(), "src/main.rs", ""),
crate::inputs::outline_inputs_for_lines(temp.path(), "md/notes.md", 3, 3)
.unwrap()[0]
]
})
.to_string(),
)
.unwrap();
let errors = without_citation_reviews(validate_repository(temp.path()).unwrap().errors);
assert!(errors.is_empty(), "{errors:?}");
}
Input 3: outline src/evidence.rs mod[tests]/fn[every_markdown_extension_keeps_escaped_and_code_roles_literal]
- File:
src/evidence.rs - Selector:
mod[tests]/fn[every_markdown_extension_keeps_escaped_and_code_roles_literal] - Kind:
fn src/evidence.rs:2776–2809
// {test}`requirements.references.discovery`
// {test}`requirements.references.markdown_literals`
#[test]
fn every_markdown_extension_keeps_escaped_and_code_roles_literal() {
let temp = fixture();
let role = "{cite}`code.role`".to_owned();
let fence = ":::{cite} fenced.role\nBody.\n:::".to_owned();
let notes = format!(
"`{role}` and \\{role}\n\n```text\n{role}\n{fence}\n```\n\n<span title=\"{role}\"></span>\n"
);
fs::create_dir_all(temp.path().join("docs")).unwrap();
for file in [
"docs/a.md",
"docs/b.markdown",
"docs/C.MD",
"docs/d.Markdown",
] {
fs::write(temp.path().join(file), ¬es).unwrap();
}
// The same text in a plain-text file is not Markdown.
fs::write(temp.path().join("docs/e.txt"), ¬es).unwrap();
let mut errors = Vec::new();
let references = super::collect_references(temp.path(), &mut errors);
assert!(errors.is_empty(), "{errors:?}");
let files = references
.iter()
.map(|reference| super::display_path(temp.path(), &reference.file))
.collect::<std::collections::BTreeSet<_>>();
assert_eq!(
files.into_iter().collect::<Vec<_>>(),
["docs/e.txt"],
"{references:#?}"
);
}
Input 4: outline src/fenced.rs mod[tests]/fn[markdown_fences_in_raw_html_stay_literal]
- File:
src/fenced.rs - Selector:
mod[tests]/fn[markdown_fences_in_raw_html_stay_literal] - Kind:
fn src/fenced.rs:354–375
// {test}`requirements.references.markdown_literals`
#[test]
fn markdown_fences_in_raw_html_stay_literal() {
let content = "<div>\n:::{cite} in.block\nbody\n:::\n</div>\n\n<!--\n:::{cite} in.comment\nbody\n:::\n-->\n\nText <span title=\"\n:::{cite} in.tag\n:::\n\">tag</span>.\n\n:::{cite} outside.html\nbody\n:::\n";
let names = |markdown| {
markdown_fences(content, markdown)
.into_iter()
.map(|fence| fence.name)
.collect::<Vec<_>>()
};
// Only the fence outside raw HTML is a reference...
assert_eq!(names(true), ["outside.html"], "{content}");
assert_eq!(
crate::text_references(content, true),
[(ReferenceKind::Cite, "outside.html".to_owned())]
);
// ...while in plain text every fence counts.
assert_eq!(
names(false),
["in.block", "in.comment", "in.tag", "outside.html"]
);
}
Input 5: outline src/lib.rs mod[tests]/fn[code_examples_and_legacy_roles_remain_literal]
- File:
src/lib.rs - Selector:
mod[tests]/fn[code_examples_and_legacy_roles_remain_literal] - Kind:
fn src/lib.rs:1482–1491
// {test}`requirements.references.markdown_literals`
#[test]
fn code_examples_and_legacy_roles_remain_literal() {
let source = "`{req}`inline``\n\n```text\n :::{req} block\n```\n\n{req}`legacy`\n\n:::{req} visible\nVisible.\n:::";
let rendered = replace_definitions(source, "requirements", None, None);
assert!(rendered.contains("{req}`inline`"));
assert!(rendered.contains(":::{req} block"));
assert!(rendered.contains("{req}`legacy`"));
assert!(rendered.contains("alt=\"req requirements.visible\""));
}
Input 6: outline src/lib.rs mod[tests]/fn[markdown_examples_and_escaped_references_stay_literal]
- File:
src/lib.rs - Selector:
mod[tests]/fn[markdown_examples_and_escaped_references_stay_literal] - Kind:
fn src/lib.rs:1800–1828
// {test}`requirements.references.markdown_literals`
// {test}`requirements.references.discovery`
#[test]
fn markdown_examples_and_escaped_references_stay_literal() {
let marker = "{impl}`requirements.ready`".to_owned();
let content = format!("`{marker}`\n\n```text\n{marker}\n```\n\n\\{marker}");
let mut book = Book::new();
book.push_item(Chapter::new(
"Requirements",
":::{req} ready\nReady.\n:::".to_owned(),
"requirements.md",
Vec::new(),
));
book.push_item(Chapter::new("Guide", content, "guide.md", Vec::new()));
let root = book_root();
preprocess_book(&mut book, root.path()).unwrap();
let guide = book
.chapters()
.find(|chapter| chapter.name == "Guide")
.unwrap();
assert_eq!(guide.content.matches(&marker).count(), 3);
assert!(!guide.content.contains("habeas-req-ref-impl"));
// An escaped backslash does not escape the role.
let content = format!("\\\\{marker} \\\\\\{marker}");
let roles = reference_roles(&content, true);
assert_eq!(roles.len(), 1);
assert_eq!(roles[0].start, 2);
}
Input 7: outline src/lib.rs mod[tests]/fn[roles_in_raw_html_stay_literal]
- File:
src/lib.rs - Selector:
mod[tests]/fn[roles_in_raw_html_stay_literal] - Kind:
fn src/lib.rs:1830–1843
// {test}`requirements.references.markdown_literals`
#[test]
fn roles_in_raw_html_stay_literal() {
let marker = "{impl}`requirements.ready`".to_owned();
let content = format!(
"<div>\n{marker}\n</div>\n\n<!-- {marker} -->\n\nText <!-- {marker} --> and <span title=\"{marker}\">tag</span>.\n\nBetween <em>{marker}</em> tags.\n"
);
let references = reference_roles(&content, true);
// Only the role between inline tags is ordinary Markdown text.
assert_eq!(references.len(), 1, "{content}");
assert!(content[references[0].start..].starts_with(&format!("{marker}</em>")));
// Outside Markdown, HTML-looking text does not protect roles.
assert_eq!(reference_roles(&content, false).len(), 5);
}
Input 8: outline src/lib.rs mod[tests]/fn[colon_directives_render_markdown_bodies_and_legacy_roles_stay_literal]
- File:
src/lib.rs - Selector:
mod[tests]/fn[colon_directives_render_markdown_bodies_and_legacy_roles_stay_literal] - Kind:
fn src/lib.rs:1845–1858
// {test}`requirements.references.book_badges`
// {test}`requirements.references.markdown_literals`
#[test]
fn colon_directives_render_markdown_bodies_and_legacy_roles_stay_literal() {
let source = "An inline {req}`inline` role.\n\n:::{req} defined\nDefinition.\n\n## Details\n\n```text\nexample\n```\n:::\n\nAfter.";
let rendered = replace_definitions(source, "requirements", None, None);
assert!(rendered.contains("An inline {req}`inline` role."));
assert!(!rendered.contains("habeas-req-requirements.inline"));
assert!(rendered.contains("id=\"habeas-req-requirements.defined\""));
assert!(rendered.contains("## Details"));
assert!(rendered.contains("```text\nexample\n```"));
assert!(rendered.contains("After."));
assert!(!rendered.contains(":::{req} defined"));
}
Input 9: outline src/lib.rs mod[tests]/fn[references_in_requirement_bodies_are_scanned_as_markdown]
- File:
src/lib.rs - Selector:
mod[tests]/fn[references_in_requirement_bodies_are_scanned_as_markdown] - Kind:
fn src/lib.rs:1878–1888
// {test}`requirements.references.discovery`
// {test}`requirements.references.markdown_literals`
#[test]
fn references_in_requirement_bodies_are_scanned_as_markdown() {
let known = "{cite}`requirements.ready`".to_owned();
let example = "{cite}`missing.example`".to_owned();
let content = format!(":::{{req}} ready\nSee {known}.\n\n```text\n{example}\n```\n:::");
let references = reference_roles(&content, true);
assert_eq!(references.len(), 1);
assert_eq!(references[0].name, "requirements.ready");
}
Input 10: outline src/terms.rs mod[tests]/fn[term_dependencies_of_every_markdown_extension_skip_literal_roles]
- File:
src/terms.rs - Selector:
mod[tests]/fn[term_dependencies_of_every_markdown_extension_skip_literal_roles] - Kind:
fn src/terms.rs:233–280
// {test}`requirements.references.markdown_literals`
// {test}`requirements.definitions.dependencies`
#[test]
fn term_dependencies_of_every_markdown_extension_skip_literal_roles() {
let temp = tempfile::tempdir().unwrap();
let root = temp.path();
fs::create_dir_all(root.join("md")).unwrap();
fs::create_dir_all(root.join("docs")).unwrap();
fs::write(root.join("book.toml"), "[book]\nsrc = \"md\"\n").unwrap();
fs::write(
root.join("md/terms.md"),
":::{definition} unit\n:phrase: unit\nA unit.\n:::\n",
)
.unwrap();
let used = "{term}`terms.unit`".to_owned();
let missing = "{term}`terms.missing`".to_owned();
let notes = format!(
"Uses {used}.\n\n`{missing}` and \\{missing}\n\n```text\n{missing}\n```\n\n<span title=\"{missing}\"></span>\n"
);
let declarations = crate::evidence::load_definitions(root).unwrap();
let outlines = OutlineCache::default();
let dependencies = |file: &str| {
fs::write(root.join(file), ¬es).unwrap();
let record = json!({"inputs": [{"file": file, "item": ""}]});
dependencies(
root,
&declarations,
&outlines,
record.as_object().unwrap(),
None,
)
};
for file in [
"docs/a.md",
"docs/b.markdown",
"docs/C.MD",
"docs/d.Markdown",
] {
assert_eq!(
dependencies(file),
Ok(BTreeSet::from(["terms.unit".to_owned()])),
"{file}"
);
}
// The same text in a plain-text file is not Markdown.
let error = dependencies("docs/e.txt").unwrap_err();
assert!(error.contains("terms.missing"), "{error}");
}
Input 11: outline tests/definitions.rs fn[literal_term_roles_in_subject_and_requirements_inputs_need_no_definition]
- File:
tests/definitions.rs - Selector:
fn[literal_term_roles_in_subject_and_requirements_inputs_need_no_definition] - Kind:
fn tests/definitions.rs:812–872
// {test}`requirements.references.markdown_literals`
// {test}`requirements.definitions.dependencies`
#[test]
fn literal_term_roles_in_subject_and_requirements_inputs_need_no_definition() {
let temp = fixture();
let root = temp.path();
let glossary = root.join("md/glossary.md");
let mut text = fs::read_to_string(&glossary).unwrap();
text.push_str("\n:::{definition} c\n:phrase: gamma\n\nA third term.\n:::\n");
fs::write(&glossary, text).unwrap();
let literal = "Write `{term}`glossary.c`` as code, \\{term}`glossary.c` escaped, and <span title=\"{term}`glossary.c`\">html</span>.\n\n```text\n{term}`glossary.c`\n```\n";
let chapter = |extra: &str| {
format!(
":::{{req}} ready\nUse {{term}}`glossary.a`.\n:::\n\n:::{{req}} literal\n{literal}{extra}:::\n"
)
};
fs::write(root.join("md/rules.md"), chapter("")).unwrap();
let glossary_ab = || {
vec![
definition(root, "glossary.a"),
definition(root, "glossary.b"),
]
};
let write = |root: &Path| {
// The literal requirement as subject, and as a requirements input of
// another requirement.
approve(&record(
root,
"rules.literal",
vec![requirements(root, "rules.literal", &["rules.literal"])],
));
let mut inputs = vec![requirements(
root,
"rules.*",
&["rules.literal", "rules.ready"],
)];
inputs.extend(glossary_ab());
approve(&record(root, "rules.ready", inputs));
};
write(root);
let errors = validate_repository(root).unwrap().errors;
assert!(errors.is_empty(), "{errors:#?}");
// The same role written as a reference needs the definition in both.
fs::write(
root.join("md/rules.md"),
chapter("\nAnd {term}`glossary.c` itself.\n"),
)
.unwrap();
write(root);
let errors = validate_repository(root).unwrap().errors;
for record in ["rules.literal", "rules.ready"] {
assert!(
errors
.iter()
.any(|e| e.starts_with(&format!("evidence/{record}.json"))
&& e.contains("expected exactly one definition input for glossary.c, found 0")),
"{record}: {errors:#?}"
);
}
}
Other sources
Input 1: requirements selection requirements.references.markdown_literals
requirements.references.markdown_literals:md/requirements/references.md:433–435
:::{req} markdown_literals
In Markdown, escaped roles and role-looking text in inline code, fenced code, or raw HTML MUST remain literal and MUST NOT count as references. Raw HTML means an HTML block, an HTML comment, or an inline HTML tag including its attributes; text between inline tags is ordinary Markdown. A `{req}` directive inside a fenced code example and a legacy inline `{req}` role MUST NOT define a requirement.
:::