Skip to content

Commit

Permalink
Fix lines not being hidden in indented code blocks (#915)
Browse files Browse the repository at this point in the history
  • Loading branch information
rparrett authored Feb 5, 2024
1 parent 22c3414 commit a5dc026
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 67 deletions.
45 changes: 45 additions & 0 deletions write-rustdoc-hide-lines/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions write-rustdoc-hide-lines/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
anyhow = "1.0"
regex = "1.5"

[dev-dependencies]
indoc = "1.0"
42 changes: 19 additions & 23 deletions write-rustdoc-hide-lines/src/code_block_definition.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::ops::Range;

use regex::Regex;

use crate::hidden_ranges::HiddenRanges;

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -74,27 +76,24 @@ pub struct CodeBlockDefinition {
hide_lines_idx: Option<usize>,
}

const RUST_CODE_BLOCK_LONG: &str = "```rust";
const RUST_CODE_BLOCK_SHORT: &str = "```rs";

impl CodeBlockDefinition {
pub fn new(line: &str) -> Option<CodeBlockDefinition> {
let tag: String;
let lang_re = Regex::new(r"(\s*)```(.+)").ok()?;
let captures = lang_re.captures(line)?;

if line.starts_with(RUST_CODE_BLOCK_LONG) {
tag = RUST_CODE_BLOCK_LONG.into();
} else if line.starts_with(RUST_CODE_BLOCK_SHORT) {
tag = RUST_CODE_BLOCK_SHORT.into();
} else {
let whitespace = captures.get(1).map(|mat| mat.as_str())?;
let lang = captures.get(2).map(|mat| mat.as_str())?;

let mut hide_lines_idx = None;

let mut parts = lang.split(',');
let tag = parts.next()?;

if tag != "rs" && tag != "rust" {
return None;
}

let mut hide_lines_idx = None;
let annotations = line
.get(tag.len()..)
.unwrap_or("")
.split(',')
.filter(|a| a.trim() != "")
let annotations = parts
.enumerate()
.map(|(idx, a)| {
let annotation = Annotation::from(a);
Expand All @@ -108,7 +107,7 @@ impl CodeBlockDefinition {
.collect();

Some(CodeBlockDefinition {
tag,
tag: format!("{}```{}", whitespace, tag),
annotations,
hide_lines_idx,
})
Expand Down Expand Up @@ -143,12 +142,9 @@ impl CodeBlockDefinition {
pub fn set_hidden_ranges(&mut self, hidden_ranges: HiddenRanges) {
if hidden_ranges.is_empty() {
// Remove
match self.hide_lines_idx {
Some(idx) => {
self.annotations.remove(idx);
self.hide_lines_idx = None;
}
None => (),
if let Some(idx) = self.hide_lines_idx {
self.annotations.remove(idx);
self.hide_lines_idx = None;
}
} else {
// Add
Expand All @@ -175,7 +171,7 @@ mod tests {

for case in cases {
let definition = CodeBlockDefinition::new(case);
assert_eq!(definition.is_none(), true);
assert_eq!(definition, None);
}
}

Expand Down
142 changes: 102 additions & 40 deletions write-rustdoc-hide-lines/src/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Result;
use regex::Regex;
use std::{
ffi::OsStr,
fmt::Write,
Expand All @@ -7,9 +8,7 @@ use std::{
path::Path,
};

use crate::{
code_block_definition::CodeBlockDefinition, hidden_ranges::get_hidden_ranges
};
use crate::{code_block_definition::CodeBlockDefinition, hidden_ranges::get_hidden_ranges};

pub fn run(dir: &Path) -> Result<()> {
visit_dir_md_files(dir, &|entry| {
Expand All @@ -19,8 +18,10 @@ pub fn run(dir: &Path) -> Result<()> {
let file = File::open(entry.path())?;
let file_size = file.metadata().unwrap().len().try_into().unwrap();
let contents = format_file(
io::BufReader::new(file).lines().map(|line| line.map_err(anyhow::Error::from)),
file_size
io::BufReader::new(file)
.lines()
.map(|line| line.map_err(anyhow::Error::from)),
file_size,
)?;

// Rewrite file
Expand Down Expand Up @@ -53,69 +54,83 @@ fn visit_dir_md_files(dir: &Path, cb: &dyn Fn(&DirEntry) -> Result<()>) -> Resul

fn format_file(reader: impl Iterator<Item = Result<String>>, file_size: usize) -> Result<String> {
let mut contents = String::with_capacity(file_size);
let mut is_inside_rust_code_block = false;
let mut rust_block: Vec<String> = vec![];
let mut is_rust = false;

let mut inside_code_block = false;

// Find a code block delimiter and optionally the first specified language
let code_block_delim = Regex::new(r"\s*```(\w*)")?;

for line in reader {
let line = line?;
let is_code_block_open = line.starts_with("```rust");
let is_code_block_close = line == "```";

if is_inside_rust_code_block && is_code_block_open {
panic!("Nested '```rust' code block not allowed");
} else if is_code_block_open {
is_inside_rust_code_block = true;
let code_block_delim_match = code_block_delim.captures(&line).and_then(|cap| cap.get(1));
let is_code_block_delim = code_block_delim_match.is_some();

if !inside_code_block && is_code_block_delim {
let lang = code_block_delim_match.unwrap().as_str();
if lang == "rust" || lang == "rs" {
is_rust = true;
}

inside_code_block = true;
} else if inside_code_block && is_code_block_delim {
inside_code_block = false;
}

// Skip the line, save it as is
if !is_inside_rust_code_block {
// Pass through non-rust code block contents and contents outside of code blocks.
if !is_rust {
writeln!(&mut contents, "{}", &line)?;
continue;
}

rust_block.push(line);

// Process the `rust` code block
if is_code_block_close {
let code = &rust_block[1..rust_block.len() - 1];
let real_hidden_ranges = get_hidden_ranges(code);
let mut definition = CodeBlockDefinition::new(&rust_block[0]).unwrap();

match definition.get_hidden_ranges() {
Some(annotation_hidden_ranges) => {
if *annotation_hidden_ranges != real_hidden_ranges {
definition.set_hidden_ranges(real_hidden_ranges);
}
if inside_code_block {
continue;
}

// Process the `rust `code block
let code = &rust_block[1..rust_block.len() - 1];
let real_hidden_ranges = get_hidden_ranges(code);
let mut definition = CodeBlockDefinition::new(&rust_block[0]).unwrap();

match definition.get_hidden_ranges() {
Some(annotation_hidden_ranges) => {
if *annotation_hidden_ranges != real_hidden_ranges {
definition.set_hidden_ranges(real_hidden_ranges);
}
None => {
if !real_hidden_ranges.is_empty() {
definition.set_hidden_ranges(real_hidden_ranges);
}
}
None => {
if !real_hidden_ranges.is_empty() {
definition.set_hidden_ranges(real_hidden_ranges);
}
}
}

// Rewrite code block Zola annotations
rust_block[0] = definition.into_string();
// Rewrite code block Zola annotations
rust_block[0] = definition.into_string();

// Write code block
writeln!(&mut contents, "{}", &rust_block.join("\n"))?;
// Write code block
writeln!(&mut contents, "{}", &rust_block.join("\n"))?;

// Reset state
is_inside_rust_code_block = false;
rust_block = vec![];
}
// Reset state
inside_code_block = false;
rust_block = vec![];
is_rust = false;
}

Ok(contents)
}

#[cfg(test)]
mod tests {
use indoc::indoc;
use super::*;
use indoc::indoc;

fn lines_iter(code: &str) -> impl Iterator<Item = Result<String>> + '_ {
code.split("\n").map(|line| Ok(String::from(line)))
code.split('\n').map(|line| Ok(String::from(line)))
}

#[test]
Expand All @@ -128,6 +143,10 @@ mod tests {
}
# test 3
#[derive(Component)]
struct A;
# #[derive(Component)]
struct B;
```
"#};

Expand All @@ -136,13 +155,17 @@ mod tests {
assert_eq!(
contents.unwrap(),
indoc! {r#"
```rust,hide_lines=1-2 6
```rust,hide_lines=1-2 6 9
# test
# test 2
fn not_hidden() {
}
# test 3
#[derive(Component)]
struct A;
# #[derive(Component)]
struct B;
```
"#}
Expand Down Expand Up @@ -204,4 +227,43 @@ mod tests {
"#}
);
}

#[test]
fn indented() {
let markdown = r#"
```rust
# test
# test 2
fn not_hidden() {
}
# test 3
#[derive(Component)]
struct A;
# #[derive(Component)]
struct B;
```
"#;

let contents = format_file(lines_iter(markdown), markdown.len());

assert_eq!(
contents.unwrap(),
r#"
```rust,hide_lines=1-2 6 9
# test
# test 2
fn not_hidden() {
}
# test 3
#[derive(Component)]
struct A;
# #[derive(Component)]
struct B;
```
"#
);
}
}
Loading

0 comments on commit a5dc026

Please sign in to comment.