Merge pull request #339 from mainmatter/push-xrzmzzmtzywz

update to mdbook 0.5
This commit is contained in:
Guillaume Desmottes
2026-05-26 16:28:22 +02:00
committed by GitHub
9 changed files with 174 additions and 1978 deletions

View File

@@ -47,7 +47,7 @@ jobs:
run: cargo install --path helpers/mdbook-link-shortener
- name: Install mdbook-pandoc, calibre, pdftk and related dependencies
run: |
cargo install mdbook-pandoc --locked --version 0.7.1
cargo install mdbook-pandoc --locked --version 0.11.0
sudo apt-get update
sudo apt-get install -y fonts-noto fonts-open-sans calibre pdftk
sudo fc-cache -f -v

2039
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,6 @@
[book]
authors = ["Luca Palmieri"]
language = "en"
multilingual = false
src = "src"
title = "100 Exercises To Learn Rust"

View File

@@ -6,6 +6,6 @@ edition = "2021"
[dependencies]
anyhow = "1.0.100"
clap = "4.5.50"
mdbook = "0.4.52"
mdbook-preprocessor = "0.5.3"
semver = "1.0.27"
serde_json = "1.0.145"

View File

@@ -1,7 +1,6 @@
use anyhow::{Context, Error};
use mdbook::book::Book;
use mdbook::preprocess::{Preprocessor, PreprocessorContext};
use mdbook::BookItem;
use mdbook_preprocessor::book::{Book, BookItem};
use mdbook_preprocessor::{Preprocessor, PreprocessorContext};
pub struct ExerciseLinker;
@@ -17,27 +16,20 @@ impl Preprocessor for ExerciseLinker {
}
fn run(&self, ctx: &PreprocessorContext, mut book: Book) -> Result<Book, Error> {
let config = ctx
let root_url: String = ctx
.config
.get_preprocessor(self.name())
.context("Failed to get preprocessor configuration")?;
let key = String::from("exercise_root_url");
let root_url = config
.get(&key)
.context("Failed to get `exercise_root_url`")?;
let root_url = root_url
.as_str()
.context("`exercise_root_url` is not a string")?
.to_owned();
.get("preprocessor.exercise-linker.exercise_root_url")
.context("Failed to get `exercise_root_url`")?
.context("`exercise_root_url` is not set")?;
book.sections
book.items
.iter_mut()
.for_each(|i| process_book_item(i, &ctx.renderer, &root_url));
Ok(book)
}
fn supports_renderer(&self, _renderer: &str) -> bool {
true
fn supports_renderer(&self, _renderer: &str) -> mdbook_preprocessor::errors::Result<bool> {
Ok(true)
}
}

View File

@@ -2,8 +2,7 @@ use std::io;
use std::process;
use clap::{Arg, ArgMatches, Command};
use mdbook::errors::Error;
use mdbook::preprocess::{CmdPreprocessor, Preprocessor};
use mdbook_preprocessor::{Preprocessor, MDBOOK_VERSION};
use semver::{Version, VersionReq};
use mdbook_exercise_linker::ExerciseLinker;
@@ -30,18 +29,18 @@ fn main() {
}
}
fn handle_preprocessing(pre: &dyn Preprocessor) -> Result<(), Error> {
let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())?;
fn handle_preprocessing(pre: &dyn Preprocessor) -> anyhow::Result<()> {
let (ctx, book) = mdbook_preprocessor::parse_input(io::stdin())?;
let book_version = Version::parse(&ctx.mdbook_version)?;
let version_req = VersionReq::parse(mdbook::MDBOOK_VERSION)?;
let version_req = VersionReq::parse(MDBOOK_VERSION)?;
if !version_req.matches(&book_version) {
eprintln!(
"Warning: The {} plugin was built against version {} of mdbook, \
but we're being called from version {}",
pre.name(),
mdbook::MDBOOK_VERSION,
MDBOOK_VERSION,
ctx.mdbook_version
);
}
@@ -56,7 +55,9 @@ fn handle_supports(pre: &dyn Preprocessor, sub_args: &ArgMatches) -> ! {
let renderer = sub_args
.get_one::<String>("renderer")
.expect("Required argument");
let supported = pre.supports_renderer(renderer);
let supported = pre
.supports_renderer(renderer)
.expect("Failed to check renderer support");
// Signal whether the renderer is supported by exiting with 1 or 0.
if supported {

View File

@@ -8,7 +8,7 @@ anyhow = "1.0.100"
bimap = { version = "0.6.3", features = ["serde"] }
clap = { version = "4.5.50", features = ["derive"] }
itertools = "0.13.0"
mdbook = "0.4.52"
mdbook-preprocessor = "0.5.3"
pulldown-cmark = "0.11.3"
pulldown-cmark-to-cmark = "15"
semver = "1.0.27"

View File

@@ -1,9 +1,8 @@
use anyhow::{Context, Error};
use bimap::BiHashMap;
use itertools::Itertools;
use mdbook::book::{Book, Chapter};
use mdbook::preprocess::{Preprocessor, PreprocessorContext};
use mdbook::BookItem;
use mdbook_preprocessor::book::{Book, BookItem, Chapter};
use mdbook_preprocessor::{Preprocessor, PreprocessorContext};
use std::collections::{BTreeMap, BTreeSet};
use std::fs::File;
use std::path::PathBuf;
@@ -65,23 +64,17 @@ impl Preprocessor for LinkShortener {
}
fn run(&self, ctx: &PreprocessorContext, mut book: Book) -> Result<Book, Error> {
let config = ctx
let root_url: String = ctx
.config
.get_preprocessor(self.name())
.context("Failed to get preprocessor configuration")?;
let root_url = {
let root_url = config.get("base_url").context("Failed to get `base_url`")?;
root_url
.as_str()
.context("`base_url` is not a string")?
.to_owned()
};
.get("preprocessor.link-shortener.base_url")
.context("Failed to get `base_url`")?
.context("`base_url` is not set")?;
let mapping = {
let mapping = config.get("mapping").context("Failed to get `mapping`")?;
let mapping = mapping
.as_str()
.context("`mapping` is not a string")?
.to_owned();
let mapping: String = ctx
.config
.get("preprocessor.link-shortener.mapping")
.context("Failed to get `mapping`")?
.context("`mapping` is not set")?;
PathBuf::from_str(&mapping).context("Failed to parse `mapping` as a path")?
};
let mut link2alias = {
@@ -98,11 +91,11 @@ impl Preprocessor for LinkShortener {
}
}
};
let verify = config
.get("verify")
let verify: bool = ctx
.config
.get("preprocessor.link-shortener.verify")
.context("Failed to get `verify`")?
.as_bool()
.context("`verify` is not a boolean")?;
.context("`verify` is not set")?;
// Env var overrides config
let verify = std::env::var("LINK_SHORTENER_VERIFY")
.map(|v| v == "true")
@@ -110,7 +103,7 @@ impl Preprocessor for LinkShortener {
let mut alias_gen = AliasGenerator::new();
book.sections.iter_mut().for_each(|i| {
book.items.iter_mut().for_each(|i| {
if let BookItem::Chapter(c) = i {
c.content = replace_anchors(c, &root_url, &mut alias_gen, &mut link2alias, verify)
.expect("Error converting links for chapter");
@@ -139,8 +132,8 @@ impl Preprocessor for LinkShortener {
Ok(book)
}
fn supports_renderer(&self, _renderer: &str) -> bool {
true
fn supports_renderer(&self, _renderer: &str) -> mdbook_preprocessor::errors::Result<bool> {
Ok(true)
}
}

View File

@@ -1,7 +1,6 @@
use clap::Parser;
use mdbook::errors::Error;
use mdbook::preprocess::{CmdPreprocessor, Preprocessor};
use mdbook_link_shortener::LinkShortener;
use mdbook_preprocessor::{Preprocessor, MDBOOK_VERSION};
use semver::{Version, VersionReq};
use std::io;
use std::process;
@@ -30,7 +29,10 @@ fn main() -> Result<(), anyhow::Error> {
let preprocessor = LinkShortener::new();
if let Some(SubCommand::Supports(Supports { renderer })) = cli.sub {
let code = if preprocessor.supports_renderer(&renderer) {
let code = if preprocessor
.supports_renderer(&renderer)
.expect("Failed to check renderer support")
{
0
} else {
1
@@ -43,18 +45,18 @@ fn main() -> Result<(), anyhow::Error> {
Ok(())
}
fn handle_preprocessing(pre: &dyn Preprocessor) -> Result<(), Error> {
let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())?;
fn handle_preprocessing(pre: &dyn Preprocessor) -> anyhow::Result<()> {
let (ctx, book) = mdbook_preprocessor::parse_input(io::stdin())?;
let book_version = Version::parse(&ctx.mdbook_version)?;
let version_req = VersionReq::parse(mdbook::MDBOOK_VERSION)?;
let version_req = VersionReq::parse(MDBOOK_VERSION)?;
if !version_req.matches(&book_version) {
eprintln!(
"Warning: The {} plugin was built against version {} of mdbook, \
but we're being called from version {}",
pre.name(),
mdbook::MDBOOK_VERSION,
MDBOOK_VERSION,
ctx.mdbook_version
);
}