Skip to content

Commit 8f46ad8

Browse files
authored
Merge pull request #2826 from ehuss/guide-version
Add the mdbook version to the guide
2 parents f3bb6ce + 6dd8be2 commit 8f46ad8

File tree

8 files changed

+139
-2
lines changed

8 files changed

+139
-2
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
members = [
33
".",
44
"crates/*",
5-
"examples/remove-emphasis/mdbook-remove-emphasis",
5+
"examples/remove-emphasis/mdbook-remove-emphasis", "guide/guide-helper",
66
]
77

88
[workspace.lints.clippy]

guide/book.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@ heading-split-level = 2
3333

3434
[output.html.redirect]
3535
"/format/config.html" = "configuration/index.html"
36+
37+
[preprocessor.guide-helper]
38+
command = "cargo run --quiet --manifest-path guide-helper/Cargo.toml"
39+
40+
[build]
41+
extra-watch-dirs = ["guide-helper/src"]

guide/guide-helper/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "guide-helper"
3+
publish = false
4+
edition.workspace = true
5+
license.workspace = true
6+
repository.workspace = true
7+
rust-version.workspace = true
8+
9+
[dependencies]
10+
mdbook-preprocessor.workspace = true
11+
semver.workspace = true
12+
serde_json.workspace = true
13+
toml.workspace = true
14+
15+
[lints]
16+
workspace = true

guide/guide-helper/src/lib.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//! Preprocessor for the mdBook guide.
2+
3+
use mdbook_preprocessor::book::{Book, BookItem};
4+
use mdbook_preprocessor::errors::Result;
5+
use mdbook_preprocessor::{Preprocessor, PreprocessorContext};
6+
use semver::{Version, VersionReq};
7+
use std::io;
8+
9+
/// Preprocessing entry point.
10+
pub fn handle_preprocessing() -> Result<()> {
11+
let pre = GuideHelper;
12+
let (ctx, book) = mdbook_preprocessor::parse_input(io::stdin())?;
13+
14+
let book_version = Version::parse(&ctx.mdbook_version)?;
15+
let version_req = VersionReq::parse(mdbook_preprocessor::MDBOOK_VERSION)?;
16+
17+
if !version_req.matches(&book_version) {
18+
eprintln!(
19+
"warning: The {} plugin was built against version {} of mdbook, \
20+
but we're being called from version {}",
21+
pre.name(),
22+
mdbook_preprocessor::MDBOOK_VERSION,
23+
ctx.mdbook_version
24+
);
25+
}
26+
27+
let processed_book = pre.run(&ctx, book)?;
28+
serde_json::to_writer(io::stdout(), &processed_book)?;
29+
30+
Ok(())
31+
}
32+
33+
struct GuideHelper;
34+
35+
impl Preprocessor for GuideHelper {
36+
fn name(&self) -> &str {
37+
"guide-helper"
38+
}
39+
40+
fn run(&self, _ctx: &PreprocessorContext, mut book: Book) -> Result<Book> {
41+
insert_version(&mut book);
42+
Ok(book)
43+
}
44+
}
45+
46+
fn insert_version(book: &mut Book) {
47+
let path = std::env::current_dir()
48+
.unwrap()
49+
.parent()
50+
.unwrap()
51+
.join("Cargo.toml");
52+
let manifest_contents = std::fs::read_to_string(&path).unwrap();
53+
let manifest: toml::Value = toml::from_str(&manifest_contents).unwrap();
54+
let version = manifest["package"]["version"].as_str().unwrap();
55+
const MARKER: &str = "{{ mdbook-version }}";
56+
book.for_each_mut(|item| {
57+
let BookItem::Chapter(ch) = item else {
58+
return;
59+
};
60+
if ch.is_draft_chapter() {
61+
return;
62+
}
63+
if ch.content.contains(MARKER) {
64+
ch.content = ch.content.replace(MARKER, version);
65+
}
66+
});
67+
}

guide/guide-helper/src/main.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//! Preprocessor for the mdBook guide.
2+
3+
fn main() {
4+
let mut args = std::env::args().skip(1);
5+
match args.next().as_deref() {
6+
Some("supports") => {
7+
// Supports all renderers.
8+
return;
9+
}
10+
Some(arg) => {
11+
eprintln!("unknown argument: {arg}");
12+
std::process::exit(1);
13+
}
14+
None => {}
15+
}
16+
17+
if let Err(e) = guide_helper::handle_preprocessing() {
18+
eprintln!("{e:?}");
19+
std::process::exit(1);
20+
}
21+
}

guide/src/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
# Introduction
22

3+
<style>
4+
.mdbook-version {
5+
position: absolute;
6+
right: 20px;
7+
top: 60px;
8+
background-color: var(--theme-popup-bg);
9+
border-radius: 8px;
10+
padding: 2px 5px 2px 5px;
11+
border: 1px solid var(--theme-popup-border);
12+
font-size: 0.9em;
13+
}
14+
</style>
15+
16+
<div class="mdbook-version">
17+
Version: {{ mdbook-version }}
18+
</div>
19+
320
**mdBook** is a command line tool to create books with Markdown.
421
It is ideal for creating product or API documentation, tutorials, course materials or anything that requires a clean,
522
easily navigable and customizable presentation.

guide/src/continuous-integration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ A simple approach would be to use the popular `curl` CLI tool to download the ex
2121

2222
```sh
2323
mkdir bin
24-
curl -sSL https://github.com/rust-lang/mdBook/releases/download/v0.4.52/mdbook-v0.4.52-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=bin
24+
curl -sSL https://github.com/rust-lang/mdBook/releases/download/{{ mdbook-version }}/mdbook-{{ mdbook-version }}-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=bin
2525
bin/mdbook build
2626
```
2727

0 commit comments

Comments
 (0)