max-heller / mdbook-pandoc

A mdbook backend powered by Pandoc.
Apache License 2.0
97 stars 7 forks source link

Handle hidden portions of examples #73

Closed simonsan closed 6 months ago

simonsan commented 6 months ago

https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.html#hiding-portions-of-the-example

Sometimes you want to make an example more clear/visual appealing while still providing a lot of functionality. E.g.:

# use std::io;
# use std::fs;

# fn main() -> Result<(), Box<dyn std::error::Error>> {
# let arg = "-";

// These must live longer than `readable`, and thus are declared first:
let (mut stdin_read, mut file_read);

// We need to describe the type to get dynamic dispatch.
let readable: &mut dyn io::Read = if arg == "-" {
    stdin_read = io::stdin();
    &mut stdin_read
} else {
    file_read = fs::File::open(arg)?;
    &mut file_read
};

// Read from `readable` here.

# Ok(())
# }

But the marker # for hidden portions of the example is being rendered into the PDF.

The question arises, how to handle these cases. In an interactive example in a mdbook you can click on the top right to show hidden lines. In a PDF, you can't do that, so we need to make a tradeoff here.

Do we want to show the full examples with the # stripped at the beginning, or do we want to hide these lines? It would be good, to have an option for opting in to any of both behaviours.