mdbookkit 3.0.0

Support library for mdBook preprocessors in the mdbookkit project
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use std::{
    borrow::{Borrow, Cow},
    hash::Hash,
    io::{Read, Write},
    path::{Path, PathBuf},
    str::FromStr,
};

use anyhow::{Context, Result, anyhow, bail};
use mdbook_markdown::pulldown_cmark::Options as MarkdownOptions;
use mdbook_preprocessor::{
    PreprocessorContext,
    book::{Book, BookItem, Chapter},
    config::{self, HtmlConfig},
};
use serde::{Deserialize, Deserializer, de::IntoDeserializer};
use serde_json::{Value, json};
use tap::Pipe;
use tracing::warn;
use url::Url;

use crate::{
    config::FeatureGated, emit_debug, error::WithDebugContext, markdown::default_markdown_options,
    url::UrlFromPath,
};

pub fn string_from_stdin() -> Result<String> {
    Ok(Vec::new()
        .pipe(|mut buf| std::io::stdin().read_to_end(&mut buf).and(Ok(buf)))?
        .pipe(String::from_utf8)?)
}

/// This uses [`serde_json::from_str`] whereas [`mdbook_preprocessor::parse_input`] uses
/// [`serde_json::from_reader`], which could be slow.
pub fn book_from_stdin(doc_url: &'static str) -> Result<(PreprocessorContext, Book)> {
    let input = string_from_stdin()?;
    let error = match serde_json::from_str(&input) {
        Ok(book) => return Ok(book),
        Err(err) => err,
    };
    let error = if error.is_data() {
        match patch_mdbook_input(input, error) {
            Ok(book) => return Ok(book),
            Err(err) => err,
        }
    } else {
        error.into()
    };
    let error =
        anyhow!("https://rust-lang.github.io/mdBook/format/configuration/preprocessors.html")
            .context(doc_url)
            .context("help: for more information, please visit the following links:")
            .context("help: this program is meant to be invoked by mdBook")
            .context(error)
            .context("error reading book content from stdin");
    Err(error)
}

#[allow(clippy::result_unit_err)]
pub trait PreprocessorHelper {
    fn book_toml(&self) -> BookToml<'_>;

    fn markdown_options(&self) -> MarkdownOptions;

    fn book_dir(&self) -> Result<PathBuf>;

    fn page_dir(&self) -> Result<PathBuf>;

    fn for_each_page<'a, F, E>(&self, book: &'a Book, func: F) -> Result<(), E>
    where
        F: FnMut(Url, &'a str) -> Result<(), E>;

    fn for_each_page_mut<F, E>(&self, book: &mut Book, func: F) -> Result<(), E>
    where
        F: FnMut(Url, &mut String) -> Result<(), E>;

    fn print(&self, book: Book) -> Result<()>;
}

impl PreprocessorHelper for PreprocessorContext {
    fn book_toml(&self) -> BookToml<'_> {
        BookToml::from_ctx(self)
    }

    fn markdown_options(&self) -> MarkdownOptions {
        let HtmlConfig {
            smart_punctuation,
            definition_lists,
            admonitions,
            ..
        } = self
            .config
            .get::<HtmlConfig>("output.html")
            .unwrap_or_default()
            .unwrap_or_default();
        let mut options = default_markdown_options();
        if admonitions {
            options.insert(MarkdownOptions::ENABLE_GFM);
        }
        if smart_punctuation {
            options.insert(MarkdownOptions::ENABLE_SMART_PUNCTUATION);
        }
        if definition_lists {
            options.insert(MarkdownOptions::ENABLE_DEFINITION_LIST);
        }
        options
    }

    fn book_dir(&self) -> Result<PathBuf> {
        let path = &self.root;
        let path = (path.canonicalize())
            .with_path_debug(path)
            .context("could not access the root directory of the book")?;
        Ok(path)
    }

    fn page_dir(&self) -> Result<PathBuf> {
        Ok(self.book_dir()?.join(&self.config.book.src))
    }

    fn for_each_page<'a, F, E>(&self, book: &'a Book, mut func: F) -> Result<(), E>
    where
        F: FnMut(Url, &'a str) -> Result<(), E>,
    {
        for item in book.iter() {
            let BookItem::Chapter(ch) = item else {
                continue;
            };
            let Some(path) = &ch.source_path else {
                continue;
            };
            func(page_url(self, path), &ch.content)?;
        }
        Ok(())
    }

    fn for_each_page_mut<F, E>(&self, book: &mut Book, mut func: F) -> Result<(), E>
    where
        F: FnMut(Url, &mut String) -> Result<(), E>,
    {
        let mut result = Ok(());
        book.for_each_chapter_mut(|ch| {
            if result.is_err() {
                return;
            }
            let &mut Chapter {
                source_path: Some(ref path),
                ref mut content,
                ..
            } = ch
            else {
                return;
            };
            result = func(page_url(self, path), content);
        });
        result
    }

    fn print(&self, book: Book) -> Result<()> {
        let output = if self.mdbook_version.starts_with("0.4.") {
            patch_mdbook_output_0_4(book)
        } else {
            serde_json::to_string(&book).map_err(Into::into)
        }
        .context("failed to serialize mdBook output")?;
        std::io::stdout()
            .write_all(output.as_bytes())
            .context("failed to write mdBook output")
    }
}

fn page_dir(ctx: &PreprocessorContext) -> PathBuf {
    ctx.root.join(&ctx.config.book.src)
}

fn page_url(ctx: &PreprocessorContext, path: &Path) -> Url {
    let base = page_dir(ctx);
    let path = base.join(path);
    path.file_to_url()
}

#[derive(Debug)]
pub struct BookToml<'a> {
    config: Cow<'a, config::Config>,
    source: BookTomlSource,
}

#[derive(Debug)]
enum BookTomlSource {
    Read(String),
    Path(PathBuf),
}

macro_rules! preprocessor_table {
    (preprocessor.$name:expr) => {
        format!("preprocessor.{}", preprocessor_table!($name))
    };
    ($name:expr) => {
        $name.strip_prefix("mdbook-").unwrap_or($name)
    };
}

impl<'a> BookToml<'a> {
    fn from_ctx(ctx: &'a PreprocessorContext) -> Self {
        Self {
            config: Cow::Borrowed(&ctx.config),
            source: BookTomlSource::Path(ctx.root.join("book.toml")),
        }
    }

    fn load_source(&mut self) -> Result<&str> {
        let path = match self.source {
            BookTomlSource::Path(ref path) => path,
            BookTomlSource::Read(ref source) => return Ok(source),
        };

        let source = std::fs::read_to_string(path)
            .with_path_debug(path)
            .context("failed to reload config from file")?;

        let mut config = config::Config::from_str(&source)
            .with_path_debug(path)
            .context("failed to reload config from file")?;

        config
            .update_from_env()
            .context("failed to update config from environment variables")?;

        self.config = Cow::Owned(config);
        self.source = BookTomlSource::Read(source);

        self.load_source()
    }

    fn read_by_path<T>(&mut self, path: &str) -> Result<Option<T>>
    where
        T: for<'de> Deserialize<'de>,
    {
        let deserializer = match self.config.get::<toml::Value>(path)? {
            None => return Ok(None),
            Some(value) => value,
        }
        .toml_deserializer(path);

        let error = match deserializer.deserialize() {
            Ok(config) => return Ok(Some(config)),
            Err(error) => error,
        };

        if let Ok(source) = self.load_source().or_else(emit_debug!())
            && let Ok(error) = recover_toml_error::<T>(source, path).or_else(emit_debug!())
        {
            Err(error)?
        } else {
            Err(error)?
        }
    }

    pub fn inner(&self) -> &config::Config {
        &self.config
    }

    pub fn with_source(mut self) -> Self {
        self.load_source().or_else(emit_debug!()).ok();
        self
    }

    pub fn preprocessor<T>(&mut self, names: &[&str]) -> Result<Option<T>>
    where
        T: for<'de> Deserialize<'de>,
    {
        for (idx, name) in names.iter().enumerate() {
            let name = preprocessor_table!(preprocessor.name);
            if let Some(value) = self.read_by_path(&name)? {
                if idx != 0 {
                    warn! {
                        "the book.toml section [{deprecated}] is deprecated, \
                        use [{recommended}] instead.",
                        deprecated  = name,
                        recommended = preprocessor_table!(preprocessor.names[0])
                    };
                }
                return Ok(value);
            }
        }
        Ok(None)
    }

    pub fn html_config<T>(&mut self, key: &str) -> Result<Option<T>>
    where
        T: for<'de> Deserialize<'de>,
    {
        self.read_by_path::<T>(&format!("output.html.{key}"))
    }
}

impl FromStr for BookToml<'static> {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self {
            config: Cow::Owned(s.parse()?),
            source: BookTomlSource::Read(s.to_owned()),
        })
    }
}

fn recover_toml_error<T>(source: &str, path: &str) -> Result<toml::de::Error>
where
    T: for<'de> Deserialize<'de>,
{
    let table = toml::de::DeTable::parse(source)?;
    let table = toml::Spanned::new(table.span(), toml::de::DeValue::Table(table.into_inner()));
    let deserializer = path
        .split('.')
        .try_fold(table, |parent, key| {
            if let toml::de::DeValue::Table(mut table) = parent.into_inner() {
                table.remove(key)
            } else {
                None
            }
        })
        .context("value not defined in source")?
        .toml_deserializer(path);

    if let Err(mut error) = deserializer.deserialize::<T>() {
        error.set_input(Some(source));
        Ok(error)
    } else {
        bail!("parsing from TOML source did not error")
    }
}

trait BookTomlDeserializer<'de> {
    type Deserializer: Deserializer<'de, Error = toml::de::Error>;
    fn toml_deserializer(self, path: &str) -> FeatureGated<Self::Deserializer>;
}

impl<'de> BookTomlDeserializer<'de> for toml::Value {
    type Deserializer = toml::Value;

    fn toml_deserializer(mut self, path: &str) -> FeatureGated<Self::Deserializer> {
        let mut unstable = None;
        if path.starts_with("preprocessor.")
            && let toml::Value::Table(ref mut table) = self
        {
            if let Some(flag) = table.remove("unstable-features") {
                unstable = Some(flag);
            }
            remove_builtin_options(table);
        }
        FeatureGated {
            unstable,
            deserializer: self.into_deserializer(),
        }
    }
}

impl<'de> BookTomlDeserializer<'de> for toml::Spanned<toml::de::DeValue<'de>> {
    type Deserializer = toml::de::ValueDeserializer<'de>;

    fn toml_deserializer(mut self, path: &str) -> FeatureGated<Self::Deserializer> {
        let mut unstable = None;
        if path.starts_with("preprocessor.")
            && let toml::de::DeValue::Table(table) = self.as_mut()
        {
            if let Some(flag) = table.remove("unstable-features") {
                unstable = Some(flag.into_deserializer());
            }
            remove_builtin_options(table);
        }
        FeatureGated {
            unstable,
            deserializer: self.into_deserializer(),
        }
    }
}

/// Remove mdbook's builtin preprocessor options from table before deserializing
/// so that they don't interfere with `deny_unknown_fields`. Keep in-sync with:
///
/// - <https://rust-lang.github.io/mdBook/format/configuration/preprocessors.html>
/// - <https://github.com/rust-lang/mdBook/blob/v0.5.2/crates/mdbook-driver/src/mdbook.rs#L434-L443>
fn remove_builtin_options<K, V>(table: &mut toml::map::Map<K, V>)
where
    K: Borrow<str> + Ord + Hash,
{
    table.remove("command");
    table.remove("before");
    table.remove("after");
    table.remove("optional");
    table.remove("renderers");
}

fn patch_mdbook_input(
    input: String,
    error: serde_json::Error,
) -> Result<(PreprocessorContext, Book)> {
    let (mut ctx, mut book): (Value, Value) = serde_json::from_str(&input)?;

    match ctx.get("mdbook_version") {
        Some(Value::String(version)) => {
            if !version.starts_with("0.4.") && !version.starts_with("0.5.") {
                bail! { "unsupported mdBook version {version}; \
                supported versions are 0.4, 0.5" }
            }
        }
        _ => return Err(error)?,
    }

    // 0.4 -> 0.5
    if let Some(conf) = ctx
        .pointer_mut("/config/book")
        .and_then(|val| val.as_object_mut())
    {
        conf.remove("multilingual");
    }

    // 0.4 -> 0.5
    if let Some(book) = book.as_object_mut()
        && let Some(sections) = book.remove("sections")
    {
        book.insert("items".into(), sections);
    }

    Ok(serde_json::from_value(json!([ctx, book]))?)
}

fn patch_mdbook_output_0_4(book: Book) -> Result<String> {
    let mut book = serde_json::to_value(book)?;

    if let Some(book) = book.as_object_mut() {
        if let Some(sections) = book.remove("items") {
            book.insert("sections".into(), sections);
        }
        book.insert("__non_exhaustive".into(), Value::Null);
    }

    Ok(serde_json::to_string(&book)?)
}