Skip to content

Commit 3dde776

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

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
@@ -192,3 +192,18 @@ Here is what a rendered code snippet looks like:
192192
{{#playground example.rs}}
193193

194194
[Rust Playground]: https://play.rust-lang.org/
195+
196+
## Font-Awesome icons
197+
198+
mdBook includes a copy of [Font Awesome Free's](https://fontawesome.com)
199+
MIT-licensed SVG files. It emulates the `<i class="fa">` syntax, but converts
200+
the results to inline SVG. Only the regular, solid, and brands icons are
201+
included; paid features like the light icons are not.
202+
203+
For example, given this HTML syntax:
204+
205+
```handlebars
206+
The result looks like this: <i class="fas fa-print"></i>
207+
```
208+
209+
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
@@ -169,6 +169,7 @@ impl HtmlHandlebars {
169169
let rendered = build_header_links(&rendered);
170170
let rendered = fix_code_blocks(&rendered);
171171
let rendered = add_playground_pre(&rendered, playground_config, edition);
172+
let rendered = convert_fontawesome(&rendered);
172173

173174
rendered
174175
}
@@ -729,6 +730,54 @@ fn insert_link_into_header(
729730
)
730731
}
731732

733+
// Convert fontawesome `<i>` tags to inline SVG
734+
fn convert_fontawesome(html: &str) -> String {
735+
use font_awesome_as_a_crate as fa;
736+
737+
let regex = Regex::new(r##"<i([^>]+)class="([^"]+)"([^>]*)></i>"##).unwrap();
738+
regex
739+
.replace_all(html, |caps: &Captures<'_>| {
740+
let text = &caps[0];
741+
let before = &caps[1];
742+
let classes = &caps[2];
743+
let after = &caps[3];
744+
745+
let mut icon = String::new();
746+
let mut type_ = fa::Type::Regular;
747+
let mut other_classes = String::new();
748+
749+
for class in classes.split(" ") {
750+
if class.starts_with("fa-") {
751+
icon = class[3..].to_owned();
752+
} else if class == "fa" {
753+
type_ = fa::Type::Regular;
754+
} else if class == "fas" {
755+
type_ = fa::Type::Solid;
756+
} else if class == "fab" {
757+
type_ = fa::Type::Brands;
758+
} else {
759+
other_classes += " ";
760+
other_classes += class;
761+
}
762+
}
763+
764+
if icon == "" {
765+
text.to_owned()
766+
} else if let Ok(svg) = fa::svg(type_, &icon) {
767+
format!(
768+
r#"<span{before}class="fa-svg{other_classes}"{after}>{svg}</span>"#,
769+
before = before,
770+
other_classes = other_classes,
771+
after = after,
772+
svg = svg
773+
)
774+
} else {
775+
text.to_owned()
776+
}
777+
})
778+
.into_owned()
779+
}
780+
732781
// The rust book uses annotations for rustdoc to test code snippets,
733782
// like the following:
734783
// ```rust,should_panic

0 commit comments

Comments
 (0)