Skip to content

Commit 3278f84

Browse files
committed
Move renderer types to mdbook-renderer
This sets up mdbook-renderer with the intent of being the core library that renderers use to implement the necessary interactions.
1 parent 12285f5 commit 3278f84

File tree

8 files changed

+93
-79
lines changed

8 files changed

+93
-79
lines changed

Cargo.lock

Lines changed: 4 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ anyhow = "1.0.98"
2525
log = "0.4.27"
2626
mdbook-core = { path = "crates/mdbook-core" }
2727
mdbook-preprocessor = { path = "crates/mdbook-preprocessor" }
28+
mdbook-renderer = { path = "crates/mdbook-renderer" }
2829
mdbook-summary = { path = "crates/mdbook-summary" }
2930
memchr = "2.7.5"
3031
pulldown-cmark = { version = "0.10.3", default-features = false, features = ["html"] } # Do not update, part of the public api.
@@ -63,6 +64,7 @@ hex = "0.4.3"
6364
log.workspace = true
6465
mdbook-core.workspace = true
6566
mdbook-preprocessor.workspace = true
67+
mdbook-renderer.workspace = true
6668
mdbook-summary.workspace = true
6769
memchr.workspace = true
6870
opener = "0.8.1"

crates/mdbook-renderer/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ repository.workspace = true
88
rust-version.workspace = true
99

1010
[dependencies]
11+
anyhow.workspace = true
1112
mdbook-core.workspace = true
13+
serde.workspace = true
14+
serde_json.workspace = true
1215

1316
[lints]
1417
workspace = true

crates/mdbook-renderer/src/lib.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,81 @@
11
//! Library to assist implementing an mdbook renderer.
22
3+
use anyhow::Context;
4+
use mdbook_core::book::Book;
5+
use mdbook_core::config::Config;
6+
use mdbook_core::errors::Result;
7+
use serde::{Deserialize, Serialize};
8+
use std::collections::HashMap;
9+
use std::io::Read;
10+
use std::path::PathBuf;
11+
312
pub use mdbook_core::MDBOOK_VERSION;
13+
pub use mdbook_core::book;
14+
pub use mdbook_core::config;
15+
pub use mdbook_core::errors;
16+
17+
/// An mdbook backend.
18+
pub trait Renderer {
19+
/// The `Renderer`'s name.
20+
fn name(&self) -> &str;
21+
22+
/// Invoke the `Renderer`, passing in all the necessary information for
23+
/// describing a book.
24+
fn render(&self, ctx: &RenderContext) -> Result<()>;
25+
}
26+
27+
/// The context provided to all renderers.
28+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
29+
pub struct RenderContext {
30+
/// Which version of `mdbook` did this come from (as written in `mdbook`'s
31+
/// `Cargo.toml`). Useful if you know the renderer is only compatible with
32+
/// certain versions of `mdbook`.
33+
pub version: String,
34+
/// The book's root directory.
35+
pub root: PathBuf,
36+
/// A loaded representation of the book itself.
37+
pub book: Book,
38+
/// The loaded configuration file.
39+
pub config: Config,
40+
/// Where the renderer *must* put any build artefacts generated. To allow
41+
/// renderers to cache intermediate results, this directory is not
42+
/// guaranteed to be empty or even exist.
43+
pub destination: PathBuf,
44+
/// Internal mapping of chapter titles.
45+
///
46+
/// This is used internally by mdbook to compute custom chapter titles.
47+
/// This should not be used outside of mdbook's internals.
48+
#[serde(skip)]
49+
pub chapter_titles: HashMap<PathBuf, String>,
50+
#[serde(skip)]
51+
__non_exhaustive: (),
52+
}
53+
54+
impl RenderContext {
55+
/// Create a new `RenderContext`.
56+
pub fn new<P, Q>(root: P, book: Book, config: Config, destination: Q) -> RenderContext
57+
where
58+
P: Into<PathBuf>,
59+
Q: Into<PathBuf>,
60+
{
61+
RenderContext {
62+
book,
63+
config,
64+
version: crate::MDBOOK_VERSION.to_string(),
65+
root: root.into(),
66+
destination: destination.into(),
67+
chapter_titles: HashMap::new(),
68+
__non_exhaustive: (),
69+
}
70+
}
71+
72+
/// Get the source directory's (absolute) path on disk.
73+
pub fn source_dir(&self) -> PathBuf {
74+
self.root.join(&self.config.book.src)
75+
}
76+
77+
/// Load a `RenderContext` from its JSON representation.
78+
pub fn from_json<R: Read>(reader: R) -> Result<RenderContext> {
79+
serde_json::from_reader(reader).with_context(|| "Unable to deserialize the `RenderContext`")
80+
}
81+
}

src/book/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub use mdbook_core::book::{Book, BookItem, BookItems, Chapter, SectionNumber};
1616
use mdbook_core::config::{Config, RustEdition};
1717
use mdbook_core::utils;
1818
use mdbook_preprocessor::{Preprocessor, PreprocessorContext};
19+
use mdbook_renderer::{RenderContext, Renderer};
1920
pub use mdbook_summary::{Link, Summary, SummaryItem, parse_summary};
2021
use std::ffi::OsString;
2122
use std::io::{IsTerminal, Write};
@@ -26,7 +27,7 @@ use toml::Value;
2627
use topological_sort::TopologicalSort;
2728

2829
use crate::preprocess::{CmdPreprocessor, IndexPreprocessor, LinkPreprocessor};
29-
use crate::renderer::{CmdRenderer, HtmlHandlebars, MarkdownRenderer, RenderContext, Renderer};
30+
use crate::renderer::{CmdRenderer, HtmlHandlebars, MarkdownRenderer};
3031

3132
/// The object used to manage and build a book.
3233
pub struct MDBook {

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
//! access to the various methods for working with the [`Config`].
7777
//!
7878
//! [user guide]: https://rust-lang.github.io/mdBook/
79-
//! [`RenderContext`]: renderer::RenderContext
79+
//! [`RenderContext`]: mdbook_renderer::RenderContext
8080
//! [relevant chapter]: https://rust-lang.github.io/mdBook/for_developers/backends.html
8181
//! [`Config`]: mdbook_core::config::Config
8282
@@ -88,6 +88,5 @@ pub mod theme;
8888

8989
pub use crate::book::BookItem;
9090
pub use crate::book::MDBook;
91-
pub use crate::renderer::Renderer;
9291
pub use mdbook_core::MDBOOK_VERSION;
9392
pub use mdbook_core::config::Config;

src/renderer/mod.rs

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,12 @@
1111
//! [For Developers]: https://rust-lang.github.io/mdBook/for_developers/index.html
1212
//! [RenderContext]: struct.RenderContext.html
1313
14-
use crate::book::Book;
1514
use anyhow::{Context, Result, bail};
1615
use log::{error, info, trace, warn};
17-
use mdbook_core::config::Config;
18-
use serde::{Deserialize, Serialize};
16+
use mdbook_renderer::{RenderContext, Renderer};
1917
use shlex::Shlex;
20-
use std::collections::HashMap;
2118
use std::fs;
22-
use std::io::{self, ErrorKind, Read};
19+
use std::io::{self, ErrorKind};
2320
use std::path::{Path, PathBuf};
2421
use std::process::{Command, Stdio};
2522
use toml::Value;
@@ -30,76 +27,6 @@ pub use self::markdown_renderer::MarkdownRenderer;
3027
mod html_handlebars;
3128
mod markdown_renderer;
3229

33-
/// An arbitrary `mdbook` backend.
34-
///
35-
/// Although it's quite possible for you to import `mdbook` as a library and
36-
/// provide your own renderer, there are two main renderer implementations that
37-
/// 99% of users will ever use:
38-
///
39-
/// - [`HtmlHandlebars`] - the built-in HTML renderer
40-
/// - [`CmdRenderer`] - a generic renderer which shells out to a program to do the
41-
/// actual rendering
42-
pub trait Renderer {
43-
/// The `Renderer`'s name.
44-
fn name(&self) -> &str;
45-
46-
/// Invoke the `Renderer`, passing in all the necessary information for
47-
/// describing a book.
48-
fn render(&self, ctx: &RenderContext) -> Result<()>;
49-
}
50-
51-
/// The context provided to all renderers.
52-
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
53-
pub struct RenderContext {
54-
/// Which version of `mdbook` did this come from (as written in `mdbook`'s
55-
/// `Cargo.toml`). Useful if you know the renderer is only compatible with
56-
/// certain versions of `mdbook`.
57-
pub version: String,
58-
/// The book's root directory.
59-
pub root: PathBuf,
60-
/// A loaded representation of the book itself.
61-
pub book: Book,
62-
/// The loaded configuration file.
63-
pub config: Config,
64-
/// Where the renderer *must* put any build artefacts generated. To allow
65-
/// renderers to cache intermediate results, this directory is not
66-
/// guaranteed to be empty or even exist.
67-
pub destination: PathBuf,
68-
#[serde(skip)]
69-
pub(crate) chapter_titles: HashMap<PathBuf, String>,
70-
#[serde(skip)]
71-
__non_exhaustive: (),
72-
}
73-
74-
impl RenderContext {
75-
/// Create a new `RenderContext`.
76-
pub fn new<P, Q>(root: P, book: Book, config: Config, destination: Q) -> RenderContext
77-
where
78-
P: Into<PathBuf>,
79-
Q: Into<PathBuf>,
80-
{
81-
RenderContext {
82-
book,
83-
config,
84-
version: crate::MDBOOK_VERSION.to_string(),
85-
root: root.into(),
86-
destination: destination.into(),
87-
chapter_titles: HashMap::new(),
88-
__non_exhaustive: (),
89-
}
90-
}
91-
92-
/// Get the source directory's (absolute) path on disk.
93-
pub fn source_dir(&self) -> PathBuf {
94-
self.root.join(&self.config.book.src)
95-
}
96-
97-
/// Load a `RenderContext` from its JSON representation.
98-
pub fn from_json<R: Read>(reader: R) -> Result<RenderContext> {
99-
serde_json::from_reader(reader).with_context(|| "Unable to deserialize the `RenderContext`")
100-
}
101-
}
102-
10330
/// A generic renderer which will shell out to an arbitrary executable.
10431
///
10532
/// # Rendering Protocol

tests/testsuite/renderer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::prelude::*;
44
use anyhow::Result;
5-
use mdbook::renderer::{RenderContext, Renderer};
5+
use mdbook_renderer::{RenderContext, Renderer};
66
use snapbox::IntoData;
77
use std::fs::File;
88
use std::sync::{Arc, Mutex};

0 commit comments

Comments
 (0)