Skip to content

Commit 325be0f

Browse files
committed
Add font-awesome access and docs for book content
1 parent dec63d3 commit 325be0f

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

book-example/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>

book-example/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
}
@@ -724,6 +725,54 @@ fn insert_link_into_header(
724725
)
725726
}
726727

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

0 commit comments

Comments
 (0)