From 89ca25e8e7b06f3aeb1c8cefc9ebc5b2c769c63c Mon Sep 17 00:00:00 2001 From: Denis Gorbachev <829578+DenisGorbachev@users.noreply.github.com> Date: Sun, 21 Jul 2024 12:17:32 +0700 Subject: [PATCH] Support ignore flag --- src/output.rs | 12 ++++++++++-- tests/pass/code-block-with-hidden-lines/README.md | 5 +++++ tests/pass/code-block-with-hidden-lines/lib.rs | 5 +++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/output.rs b/src/output.rs index 64640a0..47bb95e 100644 --- a/src/output.rs +++ b/src/output.rs @@ -19,13 +19,14 @@ use syn::Path; use url::Url; const DEFAULT_CODEBLOCK_LANG: &str = "rust"; +const RUSTDOC_CODEBLOCK_IGNORE_FLAG: &str = "ignore"; /// List of codeblock flags that rustdoc allows const RUSTDOC_CODEBLOCK_FLAGS: &[&str] = &[ "compile_fail", "edition2015", "edition2018", "edition2021", - "ignore", + RUSTDOC_CODEBLOCK_IGNORE_FLAG, "no_run", "should_panic" ]; @@ -130,6 +131,7 @@ struct EventFilter<'a, I: Iterator>> { links: &'a mut BTreeMap, in_code_block: bool, + in_code_block_ignored: bool, block_quote_level: usize, inside_github_alert: bool, link_idx: usize @@ -142,6 +144,7 @@ impl<'a, I: Iterator>> EventFilter<'a, I> { links, in_code_block: false, + in_code_block_ignored: false, block_quote_level: 0, inside_github_alert: false, link_idx: 0 @@ -189,6 +192,8 @@ impl<'a, I: Iterator>> Iterator for EventFilter<'a, I> { CodeBlockKind::Indented => DEFAULT_CODEBLOCK_LANG.into(), CodeBlockKind::Fenced(lang) => { let mut lang: String = (*lang).to_owned(); + self.in_code_block_ignored = + lang.contains(RUSTDOC_CODEBLOCK_IGNORE_FLAG); for flag in RUSTDOC_CODEBLOCK_FLAGS { lang = lang.replace(flag, ""); } @@ -291,6 +296,7 @@ impl<'a, I: Iterator>> Iterator for EventFilter<'a, I> { "Ending non-started code block, wtf???" ); self.in_code_block = false; + self.in_code_block_ignored = false; TagEnd::CodeBlock }, TagEnd::BlockQuote => { @@ -309,7 +315,9 @@ impl<'a, I: Iterator>> Iterator for EventFilter<'a, I> { tag => tag }), - Event::Text(text) if self.in_code_block => { + Event::Text(text) + if self.in_code_block && !self.in_code_block_ignored => + { let mut filtered = text .lines() .filter(|line| !is_hidden_codeblock_line(line)) diff --git a/tests/pass/code-block-with-hidden-lines/README.md b/tests/pass/code-block-with-hidden-lines/README.md index 569c045..83aee34 100644 --- a/tests/pass/code-block-with-hidden-lines/README.md +++ b/tests/pass/code-block-with-hidden-lines/README.md @@ -11,3 +11,8 @@ Random lines with # will not be altered. // But not when those lines are actually relevant: #[no_std] ``` + +```rust +// If the code block has "ignore" flag, it is not processed, and the lines starting with "#" are not hidden +# // You can see me +``` diff --git a/tests/pass/code-block-with-hidden-lines/lib.rs b/tests/pass/code-block-with-hidden-lines/lib.rs index d523f26..0731ead 100644 --- a/tests/pass/code-block-with-hidden-lines/lib.rs +++ b/tests/pass/code-block-with-hidden-lines/lib.rs @@ -12,3 +12,8 @@ //! // But not when those lines are actually relevant: //! #[no_std] //! ``` +//! +//! ```ignore +//! // If the code block has "ignore" flag, it is not processed, and the lines starting with "#" are not hidden +//! # // You can see me +//! ```