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

Add {{#shiftinclude}} command #2333

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions guide/src/format/mdbook.md
Original file line number Diff line number Diff line change
@@ -211,6 +211,25 @@ This is the full file.

Lines containing anchor patterns inside the included anchor are ignored.

## Including a file but changing its indentation

Sometimes it is useful to include a file or part of a file but with the indentation shifted,
using the following syntax:

```hbs
\{{#shiftinclude -2:file.rs}}
```

A positive number for the shift will prepend spaces to all lines; a negative number will remove
the corresponding number of whitespace characters from the beginning of each line (leaving
intact any non-whitespace).

The special `auto` value will remove common initial whitespace from all lines.

```hbs
\{{#shiftinclude auto:file.rs:indentedanchor}}
```

## Including a file but initially hiding all except specified lines

The `rustdoc_include` helper is for including code from external Rust files that contain complete
225 changes: 190 additions & 35 deletions src/preprocess/links.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -15,8 +15,8 @@ use std::fmt::Write;
use std::path::Path;

pub use self::string::{
take_anchored_lines, take_lines, take_rustdoc_include_anchored_lines,
take_rustdoc_include_lines,
take_anchored_lines, take_anchored_lines_with_shift, take_lines, take_lines_with_shift,
take_rustdoc_include_anchored_lines, take_rustdoc_include_lines, Shift,
};

/// Replaces multiple consecutive whitespace characters with a single space character.
374 changes: 362 additions & 12 deletions src/utils/string.rs

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions tests/dummy_book/src/first/nested-test-with-anchors.rs
Original file line number Diff line number Diff line change
@@ -9,3 +9,11 @@ assert!(!$TEST_STATUS);
// unique-string-for-anchor-test
assert!($TEST_STATUS);
// ANCHOR_END: myanchor

pub mod indent {
// ANCHOR: indentedanchor
pub fn indented_function() {
// This extra indent remains
}
// ANCHOR_END: indentedanchor
}
10 changes: 10 additions & 0 deletions tests/dummy_book/src/first/nested.md
Original file line number Diff line number Diff line change
@@ -18,6 +18,16 @@ assert!($TEST_STATUS);
{{#include nested-test-with-anchors.rs:myanchor}}
```

## Includes can be shifted

```rust
{{#shiftinclude +2:nested-test-with-anchors.rs:myanchor}}
```

```rust
{{#shiftinclude auto:nested-test-with-anchors.rs:indentedanchor}}
```

## Rustdoc include adds the rest of the file as hidden

```rust
1,358 changes: 870 additions & 488 deletions tests/searchindex_fixture.json

Large diffs are not rendered by default.