Skip to content

Commit f2c5f66

Browse files
notriddleJoshua Nelson
authored and
Joshua Nelson
committed
Switch fontawesome from font files to embedded SVG
Fixes #790 The rationale for embedding the SVG files directly in the binary: * avoid file I/O occurring in your Tera Filter, which would likely have complex knock-on effects when combined with `async` * avoid complex error handling in your Tera Filter, which is the inevitable outcome of file I/O * avoid having to carefully construct filesystem paths, with all the terrible possible outcomes and failure modes that come with that (using the infrastructure that the existing static file server uses is not an option, because it's not an HTTP request) * I'm guessing it's faster to pull a string out of the binary's text section than it is to open a file for every request, though I have done no benchmarking to justify this wild speculation The rationale for doing it in its own crate: * we're not using any of docs.rs's file handling infrastructure anyway, so all the other stuff in its build.rs was just getting in the way while I coded up its codegen script * it should be useful in other Rust web projects, regardless of the web framework they're using * let's not force rustc to parse a 1.3MiB source file every time you change docs.rs; this seems like prime "separate translation unit" material
1 parent cc11a32 commit f2c5f66

File tree

1,652 files changed

+1836
-7546
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,652 files changed

+1836
-7546
lines changed

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ once_cell = { version = "1.4.0", features = ["parking_lot"] }
5050
base64 = "0.12.1"
5151
strum = { version = "0.18.0", features = ["derive"] }
5252
lol_html = "0.2"
53+
font-awesome-as-a-crate = { path = "crates/font-awesome-as-a-crate" }
5354

5455
# Async
5556
tokio = { version = "0.2.22", features = ["rt-threaded"] }

README.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,18 +220,9 @@ cargo run -- queue add <CRATE> <VERSION>
220220

221221
### Updating vendored sources
222222

223-
The instructions & links for updating Font Awesome can be found [on their website](https://fontawesome.com/how-to-use/on-the-web/using-with/sass). Similarly, Pure-CSS also [explains on theirs](https://purecss.io/start/).
223+
The instructions & links for updating Font Awesome can be found [on their website](https://fontawesome.com/how-to-use/on-the-web/advanced/svg-sprites). Similarly, Pure-CSS also [explains on theirs](https://purecss.io/start/).
224224

225225
When updating Font Awesome, make sure to change `$fa-font-path` in `scss/_variables.scss` (it should be at the top of the file) to `../-/static`. This will point font awesome at the correct path from which to request font and icon resources.
226-
<!--
227-
TODO: Whenever scss modules are avaliable, use [scss modules](https://sass-lang.com/documentation/at-rules/use#configuration)
228-
instead of manually editing the `_variables.scss` file. Something like this should work:
229-
```scss
230-
@use "fontawesome" with (
231-
$fa-font-path: "../-/static"
232-
);
233-
```
234-
-->
235226

236227
### Contact
237228

build.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ fn main() {
1818
println!("cargo:rerun-if-changed=templates/menu.js");
1919
println!("cargo:rerun-if-changed=templates/index.js");
2020
println!("cargo:rerun-if-changed=vendor/");
21-
println!("cargo:rerun-if-changed=vendor/fontawesome/scss/_variables.scss");
2221
// TODO: are these right?
2322
println!("cargo:rerun-if-changed=.git/HEAD");
2423
println!("cargo:rerun-if-changed=.git/index");
@@ -91,10 +90,7 @@ fn compile_sass() -> Result<(), Box<dyn Error>> {
9190
compile_sass_file(
9291
"vendored",
9392
"vendored",
94-
&[
95-
concat!(env!("CARGO_MANIFEST_DIR"), "/vendor/fontawesome/scss").to_owned(),
96-
concat!(env!("CARGO_MANIFEST_DIR"), "/vendor/pure-css/css").to_owned(),
97-
],
93+
&[concat!(env!("CARGO_MANIFEST_DIR"), "/vendor/pure-css/css").to_owned()],
9894
)?;
9995

10096
Ok(())
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
Cargo.lock
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "font-awesome-as-a-crate"
3+
version = "0.1.2"
4+
# https://github.com/FortAwesome/Font-Awesome/blob/master/composer.json
5+
authors = ["Michael Howell <michael@notriddle.com>", "Travis Chase", "Dave Gandy", "Rob Madole", "Jory Raphael", "Geremia Taglialatela", "Brian Talbot", "Mike Wilkerson", "Fonticons Inc <hello@fontawesome.com>"]
6+
edition = "2018"
7+
license = "CC-BY-4.0 AND MIT"
8+
description = "Font Awesome Free, packaged as a crate"
9+
repository = "https://github.com/rust-lang/docs.rs"
10+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# [Font Awesome Free](https://fontawesome.com/how-to-use/on-the-web/setup/hosting-font-awesome-yourself) SVG files as a crate
2+
3+
This is not officially supported by Fonticons, Inc.
4+
If you have problems, [contact us](https://github.com/rust-lang/docs.rs), not them.
5+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use std::{
2+
env,
3+
fs::{read_dir, File},
4+
io::{Read, Write},
5+
path::Path,
6+
};
7+
8+
fn main() {
9+
println!("cargo:rustc-cfg=font_awesome_out_dir");
10+
write_fontawesome_sprite();
11+
}
12+
13+
fn write_fontawesome_sprite() {
14+
let dest_path = Path::new(&env::var("OUT_DIR").unwrap()).join("fontawesome.rs");
15+
let mut dest_file = File::create(&dest_path).unwrap();
16+
dest_file
17+
.write_all(b"fn fontawesome_svg(dir:&str,file:&str)->&'static str{match(dir,file){")
18+
.expect("fontawesome fn write");
19+
for dirname in &["brands", "regular", "solid"] {
20+
let dir = read_dir(Path::new("fontawesome-free-5.14.0-web/svgs").join(dirname)).unwrap();
21+
let mut data = String::new();
22+
for file in dir {
23+
let file = file.expect("fontawesome directory access");
24+
let filename = file
25+
.file_name()
26+
.into_string()
27+
.expect("fontawesome filenames are unicode");
28+
let mut file = File::open(file.path()).expect("fontawesome file access");
29+
data.clear();
30+
file.read_to_string(&mut data)
31+
.expect("fontawesome file read");
32+
// if this assert goes off, add more hashes here and in the format! below
33+
assert!(
34+
data.find("###").is_none(),
35+
"file {} breaks raw string",
36+
filename,
37+
);
38+
dest_file
39+
.write_all(
40+
format!(
41+
r####"("{dirname}","{filename}")=>r###"{data}"###,"####,
42+
data = data,
43+
dirname = dirname,
44+
filename = filename.replace(".svg", ""),
45+
)
46+
.as_bytes(),
47+
)
48+
.expect("write fontawesome file");
49+
}
50+
}
51+
dest_file
52+
.write_all(b"_=>\"\"}}")
53+
.expect("fontawesome fn write");
54+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The svgs folder was downloaded from https://fontawesome.com/how-to-use/on-the-web/setup/hosting-font-awesome-yourself
2+
The rest of fontawesome was just deleted; this means that the font license and MIT license are not applicable, just the copyright license on the icon files themselves.
3+
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)