Debian Codegraph @

crates/mdbook-html/src/html_handlebars/helpers/fontawesome.rs

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 use font_awesome_as_a_crate as fa;
2 use handlebars::{
3 Context, Handlebars, Helper, Output, RenderContext, RenderError, RenderErrorReason,
4 };
5 use std::str::FromStr;
6 use tracing::trace;
7
8 pub(crate) fn fa_helper(
9 h: &Helper<'_>,
10 _r: &Handlebars<'_>,
11 _ctx: &Context,
12 _rc: &mut RenderContext<'_, '_>,
13 out: &mut dyn Output,
14 ) -> Result<(), RenderError> {
15 trace!("fa_helper (handlebars helper)");
16
17 let type_ = h
18 .param(0)
19 .and_then(|v| v.value().as_str())
20 .and_then(|v| fa::Type::from_str(v).ok())
21 .ok_or_else(|| {
22 RenderErrorReason::Other(
23 "Param 0 with String type is required for fontawesome helper.".to_owned(),
24 )
25 })?;
26
27 let name = h.param(1).and_then(|v| v.value().as_str()).ok_or_else(|| {
28 RenderErrorReason::Other(
29 "Param 1 with String type is required for fontawesome helper.".to_owned(),
30 )
31 })?;
32
33 trace!("fa_helper: {} {}", type_, name);
34
35 let name = name
36 .strip_prefix("fa-")
37 .or_else(|| name.strip_prefix("fab-"))
38 .or_else(|| name.strip_prefix("fas-"))
39 .unwrap_or(name);
40
41 if let Some(id) = h.param(2).and_then(|v| v.value().as_str()) {
42 out.write(&format!("<span class=fa-svg id=\"{}\">", id))?;
43 } else {
44 out.write("<span class=fa-svg>")?;
45 }
46 out.write(fa::svg(type_, name).map_err(|_| {
47 let valid_types = "fas (solid), fab (brands), or far (regular)";
48 RenderErrorReason::Other(format!(
49 "Unknown Font Awesome icon `{name}` for type `{type_}`. \
50 Hint: check the icon name and prefix ({valid_types}) at \
51 https://fontawesome.com/v6/search?m=free"
52 ))
53 })?)?;
54 out.write("</span>")?;
55
56 Ok(())
57 }