Skip to content

Commit f5d4f6e

Browse files
committed
Add font-awesome access and docs for book content
1 parent a84723d commit f5d4f6e

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

guide/src/format/mdbook.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,18 @@ contents (sidebar) by including a `\{{#title ...}}` near the top of the page.
274274
```hbs
275275
\{{#title My Title}}
276276
```
277+
278+
## Font-Awesome icons
279+
280+
mdBook includes a copy of [Font Awesome Free's](https://fontawesome.com)
281+
MIT-licensed SVG files. It emulates the `<i class="fa">` syntax, but converts
282+
the results to inline SVG. Only the regular, solid, and brands icons are
283+
included; paid features like the light icons are not.
284+
285+
For example, given this HTML syntax:
286+
287+
```hbs
288+
The result looks like this: <i class="fas fa-print"></i>
289+
```
290+
291+
The result looks like this: <i class="fas fa-print"></i>

guide/src/format/theme/index-hbs.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,25 @@ Of course the inner html can be changed to your liking.
9999

100100
*If you would like other properties or helpers exposed, please [create a new
101101
issue](https://github.com/rust-lang/mdBook/issues)*
102+
103+
### 3. fa
104+
105+
mdBook includes a copy of [Font Awesome Free's](https://fontawesome.com)
106+
MIT-licensed SVG files. It accepts three positional arguments:
107+
108+
1. Type: one of "solid", "regular", and "brands" (light and duotone are not
109+
currently supported)
110+
2. Icon: anything chosen from the
111+
[free icon set](https://fontawesome.com/icons?d=gallery&m=free)
112+
3. ID (optional): if included, an HTML ID attribute will be added to the
113+
icon's wrapping `<span>` tag
114+
115+
For example, this handlebars syntax will become this HTML:
116+
117+
```handlebars
118+
{{fa "solid" "print" "print-button"}}
119+
```
120+
121+
```html
122+
<span class=fa-svg id="print-button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M448 192V77.25c0-8.49-3.37-16.62-9.37-22.63L393.37 9.37c-6-6-14.14-9.37-22.63-9.37H96C78.33 0 64 14.33 64 32v160c-35.35 0-64 28.65-64 64v112c0 8.84 7.16 16 16 16h48v96c0 17.67 14.33 32 32 32h320c17.67 0 32-14.33 32-32v-96h48c8.84 0 16-7.16 16-16V256c0-35.35-28.65-64-64-64zm-64 256H128v-96h256v96zm0-224H128V64h192v48c0 8.84 7.16 16 16 16h48v96zm48 72c-13.25 0-24-10.75-24-24 0-13.26 10.75-24 24-24s24 10.74 24 24c0 13.25-10.75 24-24 24z"/></svg></span>
123+
```

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ impl HtmlHandlebars {
197197
let rendered = build_header_links(&rendered);
198198
let rendered = fix_code_blocks(&rendered);
199199
let rendered = add_playground_pre(&rendered, playground_config, edition);
200+
let rendered = convert_fontawesome(&rendered);
200201

201202
rendered
202203
}
@@ -763,6 +764,54 @@ fn insert_link_into_header(
763764
)
764765
}
765766

767+
// Convert fontawesome `<i>` tags to inline SVG
768+
fn convert_fontawesome(html: &str) -> String {
769+
use font_awesome_as_a_crate as fa;
770+
771+
let regex = Regex::new(r##"<i([^>]+)class="([^"]+)"([^>]*)></i>"##).unwrap();
772+
regex
773+
.replace_all(html, |caps: &Captures<'_>| {
774+
let text = &caps[0];
775+
let before = &caps[1];
776+
let classes = &caps[2];
777+
let after = &caps[3];
778+
779+
let mut icon = String::new();
780+
let mut type_ = fa::Type::Regular;
781+
let mut other_classes = String::new();
782+
783+
for class in classes.split(" ") {
784+
if class.starts_with("fa-") {
785+
icon = class[3..].to_owned();
786+
} else if class == "fa" {
787+
type_ = fa::Type::Regular;
788+
} else if class == "fas" {
789+
type_ = fa::Type::Solid;
790+
} else if class == "fab" {
791+
type_ = fa::Type::Brands;
792+
} else {
793+
other_classes += " ";
794+
other_classes += class;
795+
}
796+
}
797+
798+
if icon == "" {
799+
text.to_owned()
800+
} else if let Ok(svg) = fa::svg(type_, &icon) {
801+
format!(
802+
r#"<span{before}class="fa-svg{other_classes}"{after}>{svg}</span>"#,
803+
before = before,
804+
other_classes = other_classes,
805+
after = after,
806+
svg = svg
807+
)
808+
} else {
809+
text.to_owned()
810+
}
811+
})
812+
.into_owned()
813+
}
814+
766815
// The rust book uses annotations for rustdoc to test code snippets,
767816
// like the following:
768817
// ```rust,should_panic

0 commit comments

Comments
 (0)