Skip to content

Commit

Permalink
Support ignore flag
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisGorbachev committed Jul 21, 2024
1 parent 8ad0d43 commit 512349a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Links {
crate_ver.cloned(),
lib_name.clone()
);
let crate_ver = if crate_name == input.crate_name {
let crate_ver = if crate_name == input.crate_name {
None
} else {
crate_ver
Expand Down
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
//! ```

0 comments on commit 512349a

Please sign in to comment.