-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathresources.rs
More file actions
60 lines (54 loc) · 1.97 KB
/
Copy pathresources.rs
File metadata and controls
60 lines (54 loc) · 1.97 KB
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
use std::collections::HashMap;
use mdbook_core::utils;
use handlebars::{
Context, Handlebars, Helper, HelperDef, Output, RenderContext, RenderError, RenderErrorReason,
};
// Handlebars helper to find filenames with hashes in them
#[derive(Clone)]
pub(crate) struct ResourceHelper {
pub hash_map: HashMap<String, String>,
}
impl HelperDef for ResourceHelper {
fn call<'reg: 'rc, 'rc>(
&self,
h: &Helper<'rc>,
_r: &'reg Handlebars<'_>,
ctx: &'rc Context,
rc: &mut RenderContext<'reg, 'rc>,
out: &mut dyn Output,
) -> Result<(), RenderError> {
let param = h.param(0).and_then(|v| v.value().as_str()).ok_or_else(|| {
RenderErrorReason::Other(
"Param 0 with String type is required for resource helper.".to_owned(),
)
})?;
// Honor an explicit `path_to_root` from the render data when present
// (the `site-url` feature sets it to the absolute site root). Fall back
// to deriving it from the page path, which is the depth-relative prefix
// used for ordinary builds.
let path_to_root = match rc.evaluate(ctx, "@root/path_to_root") {
Ok(value) => value
.as_json()
.as_str()
.map(|s| s.replace('"', ""))
.unwrap_or_default(),
Err(_) => String::new(),
};
let path_to_root = if path_to_root.is_empty() {
let base_path = rc
.evaluate(ctx, "@root/path")?
.as_json()
.as_str()
.ok_or_else(|| {
RenderErrorReason::Other("Type error for `path`, string expected".to_owned())
})?
.replace("\"", "");
utils::fs::path_to_root(&base_path)
} else {
path_to_root
};
out.write(&path_to_root)?;
out.write(self.hash_map.get(param).map(|p| &p[..]).unwrap_or(¶m))?;
Ok(())
}
}