Skip to content

Commit e2d56e5

Browse files
committed
Add font-awesome access and docs for book content
1 parent 3e4eef0 commit e2d56e5

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
@@ -201,3 +201,18 @@ contents (sidebar) by including a `\{{#title ...}}` near the top of the page.
201201
```hbs
202202
\{{#title My Title}}
203203
```
204+
205+
## Font-Awesome icons
206+
207+
mdBook includes a copy of [Font Awesome Free's](https://fontawesome.com)
208+
MIT-licensed SVG files. It emulates the `<i class="fa">` syntax, but converts
209+
the results to inline SVG. Only the regular, solid, and brands icons are
210+
included; paid features like the light icons are not.
211+
212+
For example, given this HTML syntax:
213+
214+
```hbs
215+
The result looks like this: <i class="fas fa-print"></i>
216+
```
217+
218+
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
@@ -190,6 +190,7 @@ impl HtmlHandlebars {
190190
let rendered = build_header_links(&rendered);
191191
let rendered = fix_code_blocks(&rendered);
192192
let rendered = add_playground_pre(&rendered, playground_config, edition);
193+
let rendered = convert_fontawesome(&rendered);
193194

194195
rendered
195196
}
@@ -753,6 +754,54 @@ fn insert_link_into_header(
753754
)
754755
}
755756

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

0 commit comments

Comments
 (0)