"""What each parsing pattern matches, using lines from the books themselves. uv run pytest """ import pytest from ingest.parse_books import ( CALLOUT, FENCE, HEADING, HIDDEN_LINE, LEGACY_ANCHOR, LINK_DEFINITION, LISTING_OPEN, QUOTE, RULE_MARKER, strip_emphasis, strip_links, ) from rag.config import SOURCES_DIR def test_quote(): assert QUOTE.match("> NOTE: There's a `transparent_unions` nightly feature") # nomicon/other-reprs.md assert QUOTE.match(">> nested") assert not QUOTE.match("a > b") def test_fence(): assert FENCE.match("```rust") # nomicon/other-reprs.md assert FENCE.match("```rust,ignore") # nomicon/aliasing.md assert FENCE.match("```rust,compile_fail") # nomicon/dropck.md assert FENCE.match("```") tagged = FENCE.match("```rust,ignore") assert tagged and tagged.group("info") == "rust,ignore" assert not FENCE.match("```rust inline``` text") def test_heading(): assert HEADING.match("# Working With Uninitialized Memory") # nomicon/uninitialized.md assert HEADING.match("## repr(C)") # nomicon/other-reprs.md subsection = HEADING.match("## repr(C)") assert subsection and subsection.group("hashes") == "##" assert not HEADING.match("#no space") def test_hidden_line_keeps_rust_attributes(): """Only ever applied inside a Rust fence, since it also matches an h1.""" assert HIDDEN_LINE.match("# use std::mem::size_of;") # nomicon/other-reprs.md assert HIDDEN_LINE.match("# use libc::{c_int, size_t};") # nomicon/ffi.md assert HIDDEN_LINE.match("#") assert not HIDDEN_LINE.match("#[derive(Debug)]") assert not HIDDEN_LINE.match("#![allow(dead_code)]") assert HIDDEN_LINE.match("# Alternative representations") def test_rule_marker(): assert RULE_MARKER.match("r[macro.proc]") # reference/procedural-macros.md assert RULE_MARKER.match("r[macro.proc.intro]") # reference/procedural-macros.md assert not RULE_MARKER.match("r[macro.proc] and then prose") def test_link_definition(): assert LINK_DEFINITION.match("[drop flags]: drop-flags.html") # nomicon/other-reprs.md assert LINK_DEFINITION.match("[ub loads]: https://github.com/rust-lang/rust/issues/27060") assert not LINK_DEFINITION.match("[^1]: a footnote") assert not LINK_DEFINITION.match("see [drop flags]: inline") def test_legacy_anchor(): assert LEGACY_ANCHOR.match('') # book/ch10-03 assert not LEGACY_ANCHOR.match('text') def test_callout_is_given_a_blockquote_stripped_line(): """The books write these as `> [!NOTE]`, and the parser strips the prefix first.""" assert CALLOUT.match(QUOTE.sub("", "> [!NOTE]")) # reference/procedural-macros.md assert CALLOUT.match(QUOTE.sub("", "> [!EXAMPLE]")) assert not CALLOUT.match("[!NOTE] with trailing prose") def test_listing_open(): assert LISTING_OPEN.search('