Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ignore flag #468

Merged
merged 1 commit into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
];
Expand Down Expand Up @@ -130,6 +131,7 @@ struct EventFilter<'a, I: Iterator<Item = Event<'a>>> {
links: &'a mut BTreeMap<String, String>,

in_code_block: bool,
in_code_block_ignored: bool,
block_quote_level: usize,
inside_github_alert: bool,
link_idx: usize
Expand All @@ -142,6 +144,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> EventFilter<'a, I> {
links,

in_code_block: false,
in_code_block_ignored: false,
block_quote_level: 0,
inside_github_alert: false,
link_idx: 0
Expand Down Expand Up @@ -189,6 +192,8 @@ impl<'a, I: Iterator<Item = Event<'a>>> 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, "");
}
Expand Down Expand Up @@ -291,6 +296,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> 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 => {
Expand All @@ -309,7 +315,9 @@ impl<'a, I: Iterator<Item = Event<'a>>> 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))
Expand Down
5 changes: 5 additions & 0 deletions tests/pass/code-block-with-hidden-lines/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
5 changes: 5 additions & 0 deletions tests/pass/code-block-with-hidden-lines/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//! ```