update to mdbook 0.5

This commit is contained in:
Guillaume Desmottes
2026-05-26 15:56:09 +02:00
parent 8791feb495
commit f4cc4842ec
9 changed files with 174 additions and 1978 deletions

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)
}
}
@@ -60,9 +52,9 @@ fn process_book_item(item: &mut BookItem, renderer: &str, root_url: &str) {
let exercise_path = source_path.strip_suffix(".md").unwrap();
let link_section = format!(
"\n## Exercise\n\nThe exercise for this section is located in [`{exercise_path}`]({})\n",
format!("{}/{}", root_url, exercise_path)
);
"\n## Exercise\n\nThe exercise for this section is located in [`{exercise_path}`]({})\n",
format!("{}/{}", root_url, exercise_path)
);
chapter.content.push_str(&link_section);
if renderer == "pandoc" {

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 {