Skip to content

Commit

Permalink
Support hidden lines in languages other than Rust
Browse files Browse the repository at this point in the history
Co-Authored-By: thecodewarrior <5467669+thecodewarrior@users.noreply.github.com>
  • Loading branch information
2 people authored and ehuss committed May 28, 2023
1 parent 3a51abf commit 7df1d8c
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 93 deletions.
3 changes: 3 additions & 0 deletions guide/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path
editable = true
line-numbers = true

[output.html.code.hidelines]
python = "~"

[output.html.search]
limit-results = 20
use-boolean-and = true
Expand Down
11 changes: 11 additions & 0 deletions guide/src/format/configuration/renderers.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,17 @@ runnable = true # displays a run button for rust code

[Ace]: https://ace.c9.io/

### `[output.html.code]`

The `[output.html.code]` table provides options for controlling code blocks.

```toml
[output.html.code]
# A prefix string per language (one or more chars).
# Any line starting with whitespace+prefix is hidden.
hidelines = { python = "~" }
```

### `[output.html.search]`

The `[output.html.search]` table provides options for controlling the built-in text [search].
Expand Down
41 changes: 40 additions & 1 deletion guide/src/format/mdbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

There is a feature in mdBook that lets you hide code lines by prepending them
with a `#` [like you would with Rustdoc][rustdoc-hide].
This currently only works with Rust language code blocks.

[rustdoc-hide]: https://doc.rust-lang.org/stable/rustdoc/documentation-tests.html#hiding-portions-of-the-example

Expand All @@ -30,6 +29,46 @@ Will render as

The code block has an eyeball icon (<i class="fa fa-eye"></i>) which will toggle the visibility of the hidden lines.

By default, this only works for code examples that are annotated with `rust`.
However, you can define custom prefixes for other languages by adding a new line-hiding prefix in your `book.toml` with the language name and prefix character(s):

```toml
[output.html.code.hidelines]
python = "~"
```

The prefix will hide any lines that begin with the given prefix. With the python prefix shown above, this:

```bash
~hidden()
nothidden():
~ hidden()
~hidden()
nothidden()
```

will render as

```python
~hidden()
nothidden():
~ hidden()
~hidden()
nothidden()
```

This behavior can be overridden locally with a different prefix. This has the same effect as above:

~~~bash
```python,hidelines=!!!
!!!hidden()
nothidden():
!!! hidden()
!!!hidden()
nothidden()
```
~~~

## Rust Playground

Rust language code blocks will automatically get a play button (<i class="fa fa-play"></i>) which will execute the code and display the output just below the code block.
Expand Down
32 changes: 0 additions & 32 deletions guide/src/format/theme/syntax-highlighting.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,38 +77,6 @@ the `theme` folder of your book.

Now your theme will be used instead of the default theme.

## Hiding code lines

There is a feature in mdBook that lets you hide code lines by prepending them
with a `#`.


```bash
# fn main() {
let x = 5;
let y = 6;

println!("{}", x + y);
# }
```

Will render as

```rust
# fn main() {
let x = 5;
let y = 7;

println!("{}", x + y);
# }
```

**At the moment, this only works for code examples that are annotated with
`rust`. Because it would collide with semantics of some programming languages.
In the future, we want to make this configurable through the `book.toml` so that
everyone can benefit from it.**


## Improve default theme

If you think the default theme doesn't look quite right for a specific language,
Expand Down
19 changes: 19 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ pub struct HtmlConfig {
/// Playground settings.
#[serde(alias = "playpen")]
pub playground: Playground,
/// Code settings.
pub code: Code,
/// Print settings.
pub print: Print,
/// Don't render section labels.
Expand Down Expand Up @@ -556,6 +558,7 @@ impl Default for HtmlConfig {
additional_js: Vec::new(),
fold: Fold::default(),
playground: Playground::default(),
code: Code::default(),
print: Print::default(),
no_section_label: false,
search: None,
Expand Down Expand Up @@ -642,6 +645,22 @@ impl Default for Playground {
}
}

/// Configuration for tweaking how the the HTML renderer handles code blocks.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct Code {
/// A prefix string to hide lines per language (one or more chars).
pub hidelines: HashMap<String, String>,
}

impl Default for Code {
fn default() -> Code {
Code {
hidelines: HashMap::new(),
}
}
}

/// Configuration of the search functionality of the HTML renderer.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
Expand Down
Loading

0 comments on commit 7df1d8c

Please sign in to comment.