From 31a16531dd9745567c14aa2804ff35c1fa686d0c Mon Sep 17 00:00:00 2001 From: Lewin Probst Date: Thu, 5 May 2022 15:25:38 +0200 Subject: [PATCH 1/6] feat: Added copy-custom-fonts parameter to HtmlConfig Only applies, when copy-fonts parameter is set to true as well. When set to true, the fonts/fonts.css file is scanned for @font-face rules that contain a src: url(font-file.name) attribute. The files that have been found are copied into the book folder on build. --- Cargo.lock | 525 ++++++++++++++++-- Cargo.toml | 1 + src/config.rs | 6 + src/renderer/html_handlebars/hbs_renderer.rs | 11 + .../html_handlebars/helpers/custom_fonts.rs | 42 ++ src/renderer/html_handlebars/helpers/mod.rs | 1 + 6 files changed, 537 insertions(+), 49 deletions(-) create mode 100644 src/renderer/html_handlebars/helpers/custom_fonts.rs diff --git a/Cargo.lock b/Cargo.lock index 9dae5361b8..d05258a7b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -68,9 +68,18 @@ dependencies = [ [[package]] name = "autocfg" -version = "1.0.1" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +checksum = "0dde43e75fd43e8a1bf86103336bc699aa8d17ad1be60c76c0bdfd4828e19b78" +dependencies = [ + "autocfg 1.1.0", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "base64" @@ -208,6 +217,21 @@ dependencies = [ "clap", ] +[[package]] +name = "cloudabi" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +dependencies = [ + "bitflags", +] + +[[package]] +name = "convert_case" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" + [[package]] name = "cpufeatures" version = "0.1.5" @@ -217,14 +241,54 @@ dependencies = [ "libc", ] +[[package]] +name = "cssparser" +version = "0.29.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31542adad356e4fd6e9df55221485a22eb69c470141e110e7dbb4a8052e45fa6" +dependencies = [ + "cssparser-macros", + "dtoa-short", + "itoa", + "matches", + "phf 0.10.1", + "proc-macro2 1.0.28", + "quote 1.0.9", + "smallvec", + "syn 1.0.75", +] + +[[package]] +name = "cssparser-macros" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfae75de57f2b2e85e8768c3ea840fd159c8f33e2b6522c7835b7abac81be16e" +dependencies = [ + "quote 1.0.9", + "syn 1.0.75", +] + [[package]] name = "ctor" version = "0.1.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e98e2ad1a782e33928b96fc3948e7c355e5af34ba4de7670fe8bac2a3b2006d" dependencies = [ - "quote", - "syn", + "quote 1.0.9", + "syn 1.0.75", +] + +[[package]] +name = "derive_more" +version = "0.99.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" +dependencies = [ + "convert_case", + "proc-macro2 1.0.28", + "quote 1.0.9", + "rustc_version", + "syn 1.0.75", ] [[package]] @@ -263,6 +327,21 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" +[[package]] +name = "dtoa" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0" + +[[package]] +name = "dtoa-short" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bde03329ae10e79ede66c9ce4dc930aa8599043b0743008548680f25b91502d6" +dependencies = [ + "dtoa", +] + [[package]] name = "either" version = "1.6.1" @@ -359,6 +438,12 @@ dependencies = [ "libc", ] +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + [[package]] name = "fuchsia-zircon" version = "0.3.3" @@ -427,11 +512,11 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c54913bae956fb8df7f4dc6fc90362aa72e69148e3f39041fbe8742d21e0ac57" dependencies = [ - "autocfg", + "autocfg 1.1.0", "proc-macro-hack", - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.28", + "quote 1.0.9", + "syn 1.0.75", ] [[package]] @@ -452,7 +537,7 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67eb846bfd58e44a8481a00049e82c43e0ccb5d61f8dc071057cb19249dd4d78" dependencies = [ - "autocfg", + "autocfg 1.1.0", "futures-core", "futures-macro", "futures-sink", @@ -464,6 +549,15 @@ dependencies = [ "slab", ] +[[package]] +name = "fxhash" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" +dependencies = [ + "byteorder", +] + [[package]] name = "generic-array" version = "0.12.4" @@ -611,9 +705,9 @@ dependencies = [ "log", "mac", "markup5ever", - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.28", + "quote 1.0.9", + "syn 1.0.75", ] [[package]] @@ -700,7 +794,7 @@ version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc633605454125dec4b66843673f01c7df2b89479b32e0ed634e43a91cff62a5" dependencies = [ - "autocfg", + "autocfg 1.1.0", "hashbrown", ] @@ -779,6 +873,44 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" +[[package]] +name = "lewp-css" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39d5f56e217205b30a01e1ec42c608f8b14aade46c1355c7e031ac8c161dde14" +dependencies = [ + "bitflags", + "cssparser", + "either", + "lewp-selectors", + "ordermap", + "phf 0.7.24", + "phf_codegen 0.7.24", + "phf_macros 0.7.24", + "precomputed-hash", + "quick-error 1.2.3", + "servo_arc", + "smallvec", +] + +[[package]] +name = "lewp-selectors" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08b1dc58ea0886a660510a1be6a62677aee0f6e9892932e43d3257e2b1a58886" +dependencies = [ + "bitflags", + "cssparser", + "derive_more", + "fxhash", + "log", + "phf 0.8.0", + "phf_codegen 0.8.0", + "precomputed-hash", + "servo_arc", + "smallvec", +] + [[package]] name = "libc" version = "0.2.100" @@ -813,8 +945,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a24f40fb03852d1cdd84330cddcaf98e9ec08a7b7768e952fad3b4cf048ec8fd" dependencies = [ "log", - "phf", - "phf_codegen", + "phf 0.8.0", + "phf_codegen 0.8.0", "string_cache", "string_cache_codegen", "tendril", @@ -854,6 +986,7 @@ dependencies = [ "gitignore", "handlebars", "lazy_static", + "lewp-css", "log", "memchr", "notify", @@ -980,6 +1113,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" +[[package]] +name = "nodrop" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" + [[package]] name = "normalize-line-endings" version = "0.3.0" @@ -1019,7 +1158,7 @@ version = "0.1.44" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" dependencies = [ - "autocfg", + "autocfg 1.1.0", "num-traits", ] @@ -1029,7 +1168,7 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" dependencies = [ - "autocfg", + "autocfg 1.1.0", ] [[package]] @@ -1064,6 +1203,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "ordermap" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b81cf3b8cb96aa0e73bbedfcdc9708d09fec2854ba8d474be4e6f666d7379e8b" + [[package]] name = "os_str_bytes" version = "6.0.0" @@ -1115,9 +1260,9 @@ checksum = "99b8db626e31e5b81787b9783425769681b347011cc59471e33ea46d2ea0cf55" dependencies = [ "pest", "pest_meta", - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.28", + "quote 1.0.9", + "syn 1.0.75", ] [[package]] @@ -1131,13 +1276,43 @@ dependencies = [ "sha-1 0.8.2", ] +[[package]] +name = "phf" +version = "0.7.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3da44b85f8e8dfaec21adae67f95d93244b2ecf6ad2a692320598dcc8e6dd18" +dependencies = [ + "phf_shared 0.7.24", +] + [[package]] name = "phf" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dfb61232e34fcb633f43d12c58f83c1df82962dcdfa565a4e866ffc17dafe12" dependencies = [ - "phf_shared", + "phf_shared 0.8.0", +] + +[[package]] +name = "phf" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fabbf1ead8a5bcbc20f5f8b939ee3f5b0f6f281b6ad3468b84656b658b455259" +dependencies = [ + "phf_macros 0.10.0", + "phf_shared 0.10.0", + "proc-macro-hack", +] + +[[package]] +name = "phf_codegen" +version = "0.7.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b03e85129e324ad4166b06b2c7491ae27fe3ec353af72e72cd1654c7225d517e" +dependencies = [ + "phf_generator 0.7.24", + "phf_shared 0.7.24", ] [[package]] @@ -1146,8 +1321,18 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cbffee61585b0411840d3ece935cce9cb6321f01c45477d30066498cd5e1a815" dependencies = [ - "phf_generator", - "phf_shared", + "phf_generator 0.8.0", + "phf_shared 0.8.0", +] + +[[package]] +name = "phf_generator" +version = "0.7.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09364cc93c159b8b06b1f4dd8a4398984503483891b0c26b867cf431fb132662" +dependencies = [ + "phf_shared 0.7.24", + "rand 0.6.5", ] [[package]] @@ -1156,17 +1341,72 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "17367f0cc86f2d25802b2c26ee58a7b23faeccf78a396094c13dced0d0182526" dependencies = [ - "phf_shared", + "phf_shared 0.8.0", "rand 0.7.3", ] +[[package]] +name = "phf_generator" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d5285893bb5eb82e6aaf5d59ee909a06a16737a8970984dd7746ba9283498d6" +dependencies = [ + "phf_shared 0.10.0", + "rand 0.8.4", +] + +[[package]] +name = "phf_macros" +version = "0.7.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdb45e833315153371697760dad1831da99ce41884162320305e4f123ca3fe37" +dependencies = [ + "phf_generator 0.7.24", + "phf_shared 0.7.24", + "proc-macro2 0.4.30", + "quote 0.6.13", + "syn 0.15.44", +] + +[[package]] +name = "phf_macros" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58fdf3184dd560f160dd73922bea2d5cd6e8f064bf4b13110abd81b03697b4e0" +dependencies = [ + "phf_generator 0.10.0", + "phf_shared 0.10.0", + "proc-macro-hack", + "proc-macro2 1.0.28", + "quote 1.0.9", + "syn 1.0.75", +] + +[[package]] +name = "phf_shared" +version = "0.7.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "234f71a15de2288bcb7e3b6515828d22af7ec8598ee6d24c3b526fa0a80b67a0" +dependencies = [ + "siphasher 0.2.3", +] + [[package]] name = "phf_shared" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c00cf8b9eafe68dde5e9eaa2cef8ee84a9336a47d566ec55ca16589633b65af7" dependencies = [ - "siphasher", + "siphasher 0.3.6", +] + +[[package]] +name = "phf_shared" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6796ad771acdc0123d2a88dc428b5e38ef24456743ddb1744ed628f9815c096" +dependencies = [ + "siphasher 0.3.6", ] [[package]] @@ -1184,9 +1424,9 @@ version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e8fe8163d14ce7f0cdac2e040116f22eac817edabff0be91e8aff7e9accf389" dependencies = [ - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.28", + "quote 1.0.9", + "syn 1.0.75", ] [[package]] @@ -1267,13 +1507,22 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc881b2c22681370c6a780e47af9840ef841837bc98118431d4e1868bd0c1086" +[[package]] +name = "proc-macro2" +version = "0.4.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" +dependencies = [ + "unicode-xid 0.1.0", +] + [[package]] name = "proc-macro2" version = "1.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c7ed8b8c7b886ea3ed7dde405212185f423ab44682667c8c6dd14aa1d9f6612" dependencies = [ - "unicode-xid", + "unicode-xid 0.2.2", ] [[package]] @@ -1299,13 +1548,41 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3" +[[package]] +name = "quote" +version = "0.6.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1" +dependencies = [ + "proc-macro2 0.4.30", +] + [[package]] name = "quote" version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" dependencies = [ - "proc-macro2", + "proc-macro2 1.0.28", +] + +[[package]] +name = "rand" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" +dependencies = [ + "autocfg 0.1.8", + "libc", + "rand_chacha 0.1.1", + "rand_core 0.4.2", + "rand_hc 0.1.0", + "rand_isaac", + "rand_jitter", + "rand_os", + "rand_pcg 0.1.2", + "rand_xorshift", + "winapi 0.3.9", ] [[package]] @@ -1319,7 +1596,7 @@ dependencies = [ "rand_chacha 0.2.2", "rand_core 0.5.1", "rand_hc 0.2.0", - "rand_pcg", + "rand_pcg 0.2.1", ] [[package]] @@ -1334,6 +1611,16 @@ dependencies = [ "rand_hc 0.3.1", ] +[[package]] +name = "rand_chacha" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef" +dependencies = [ + "autocfg 0.1.8", + "rand_core 0.3.1", +] + [[package]] name = "rand_chacha" version = "0.2.2" @@ -1354,6 +1641,21 @@ dependencies = [ "rand_core 0.6.3", ] +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +dependencies = [ + "rand_core 0.4.2", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" + [[package]] name = "rand_core" version = "0.5.1" @@ -1372,6 +1674,15 @@ dependencies = [ "getrandom 0.2.3", ] +[[package]] +name = "rand_hc" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b40677c7be09ae76218dc623efbf7b18e34bced3f38883af07bb75630a21bc4" +dependencies = [ + "rand_core 0.3.1", +] + [[package]] name = "rand_hc" version = "0.2.0" @@ -1390,6 +1701,50 @@ dependencies = [ "rand_core 0.6.3", ] +[[package]] +name = "rand_isaac" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08" +dependencies = [ + "rand_core 0.3.1", +] + +[[package]] +name = "rand_jitter" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b" +dependencies = [ + "libc", + "rand_core 0.4.2", + "winapi 0.3.9", +] + +[[package]] +name = "rand_os" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" +dependencies = [ + "cloudabi", + "fuchsia-cprng", + "libc", + "rand_core 0.4.2", + "rdrand", + "winapi 0.3.9", +] + +[[package]] +name = "rand_pcg" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44" +dependencies = [ + "autocfg 0.1.8", + "rand_core 0.4.2", +] + [[package]] name = "rand_pcg" version = "0.2.1" @@ -1399,6 +1754,24 @@ dependencies = [ "rand_core 0.5.1", ] +[[package]] +name = "rand_xorshift" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" +dependencies = [ + "rand_core 0.3.1", +] + +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.1", +] + [[package]] name = "redox_syscall" version = "0.2.10" @@ -1440,6 +1813,15 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + [[package]] name = "ryu" version = "1.0.5" @@ -1490,9 +1872,9 @@ version = "1.0.129" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e57ae87ad533d9a56427558b516d0adac283614e347abf85b0dc0cbbf0a249f3" dependencies = [ - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.28", + "quote 1.0.9", + "syn 1.0.75", ] [[package]] @@ -1518,6 +1900,16 @@ dependencies = [ "serde", ] +[[package]] +name = "servo_arc" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d98238b800e0d1576d8b6e3de32827c2d74bee68bb97748dcf5071fb53965432" +dependencies = [ + "nodrop", + "stable_deref_trait", +] + [[package]] name = "sha-1" version = "0.8.2" @@ -1549,6 +1941,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42a568c8f2cd051a4d283bd6eb0343ac214c1b0f1ac19f93e1175b2dee38c73d" +[[package]] +name = "siphasher" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac" + [[package]] name = "siphasher" version = "0.3.6" @@ -1561,6 +1959,12 @@ version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c307a32c1c5c437f38c7fd45d753050587732ba8628319fbdf12a7e289ccc590" +[[package]] +name = "smallvec" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83" + [[package]] name = "socket2" version = "0.4.1" @@ -1571,6 +1975,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + [[package]] name = "string_cache" version = "0.8.1" @@ -1579,7 +1989,7 @@ checksum = "8ddb1139b5353f96e429e1a5e19fbaf663bddedaa06d1dbd49f82e352601209a" dependencies = [ "lazy_static", "new_debug_unreachable", - "phf_shared", + "phf_shared 0.8.0", "precomputed-hash", "serde", ] @@ -1590,10 +2000,10 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f24c8e5e19d22a726626f1a5e16fe15b132dcf21d10177fa5a45ce7962996b97" dependencies = [ - "phf_generator", - "phf_shared", - "proc-macro2", - "quote", + "phf_generator 0.8.0", + "phf_shared 0.8.0", + "proc-macro2 1.0.28", + "quote 1.0.9", ] [[package]] @@ -1615,9 +2025,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d06aaeeee809dbc59eb4556183dd927df67db1540de5be8d3ec0b6636358a5ec" dependencies = [ "heck", - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.28", + "quote 1.0.9", + "syn 1.0.75", +] + +[[package]] +name = "syn" +version = "0.15.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" +dependencies = [ + "proc-macro2 0.4.30", + "quote 0.6.13", + "unicode-xid 0.1.0", ] [[package]] @@ -1626,9 +2047,9 @@ version = "1.0.75" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7f58f7e8eaa0009c5fec437aabf511bd9933e4b2d7407bd05273c01a8906ea7" dependencies = [ - "proc-macro2", - "quote", - "unicode-xid", + "proc-macro2 1.0.28", + "quote 1.0.9", + "unicode-xid 0.2.2", ] [[package]] @@ -1702,7 +2123,7 @@ version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "01cf844b23c6131f624accf65ce0e4e9956a8bb329400ea5bcc26ae3a5c20b0b" dependencies = [ - "autocfg", + "autocfg 1.1.0", "bytes", "libc", "memchr", @@ -1719,9 +2140,9 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "54473be61f4ebe4efd09cec9bd5d16fa51d70ea0192213d754d2d500457db110" dependencies = [ - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.28", + "quote 1.0.9", + "syn 1.0.75", ] [[package]] @@ -1877,6 +2298,12 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b" +[[package]] +name = "unicode-xid" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" + [[package]] name = "unicode-xid" version = "0.2.2" diff --git a/Cargo.toml b/Cargo.toml index 6b2404fa8f..84e5fa53b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ shlex = "1" tempfile = "3.0" toml = "0.5.1" topological-sort = "0.1.0" +lewp-css = "0.2" # Watch feature notify = { version = "4.0", optional = true } diff --git a/src/config.rs b/src/config.rs index 951957bd42..8a717ba64c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -495,6 +495,11 @@ pub struct HtmlConfig { pub mathjax_support: bool, /// Whether to fonts.css and respective font files to the output directory. pub copy_fonts: bool, + /// If true, find custom font files by parsing the fonts/fonts.css and copy + /// them to the output directory. + /// + /// ***Is only applied when [copy_fonts] equals to `true`.*** + pub copy_custom_fonts: bool, /// An optional google analytics code. pub google_analytics: Option, /// Additional CSS stylesheets to include in the rendered page's ``. @@ -555,6 +560,7 @@ impl Default for HtmlConfig { curly_quotes: false, mathjax_support: false, copy_fonts: true, + copy_custom_fonts: false, google_analytics: None, additional_css: Vec::new(), additional_js: Vec::new(), diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index f477c43ef8..c81a4b1d81 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -286,6 +286,17 @@ impl HtmlHandlebars { theme::fonts::SOURCE_CODE_PRO.1, )?; } + if html_config.copy_custom_fonts && html_config.copy_fonts { + let custom_font_file_names = + helpers::custom_fonts::find_custom_font_files(Path::new("fonts/fonts.css"))?; + for font_file in custom_font_file_names { + write_file( + &Path::new(destination).join("fonts"), + &font_file, + &std::fs::read(&Path::new("fonts").join(&font_file))?, + )?; + } + } let playground_config = &html_config.playground; diff --git a/src/renderer/html_handlebars/helpers/custom_fonts.rs b/src/renderer/html_handlebars/helpers/custom_fonts.rs new file mode 100644 index 0000000000..ed47f94010 --- /dev/null +++ b/src/renderer/html_handlebars/helpers/custom_fonts.rs @@ -0,0 +1,42 @@ +use crate::errors::Result; +use lewp_css::{ + domain::{ + at_rules::font_face::{FontFaceAtRule, FontUrlSource, Source}, + CssRule, + }, + Stylesheet, +}; +use std::path::{Path, PathBuf}; + +/// Parses the given file_name and extracts the given font file names. +pub fn find_custom_font_files(css_file_name: &Path) -> Result> { + let stylesheet = std::fs::read_to_string(css_file_name)?; + let stylesheet = Stylesheet::parse(&stylesheet).unwrap_or_else(|_| { + panic!( + "Stylesheet: \"{}\" could not be parsed!", + css_file_name.display() + ) + }); + let css_rules = stylesheet.rules.0; + let mut file_names = vec![]; + for rule in css_rules { + match rule { + CssRule::FontFace(font) => { + if let Some(f) = extract_font_file_name(font) { + file_names.push(f); + } + } + _ => continue, + } + } + Ok(file_names) +} + +/// Extracts the first file name given in an URL statement from the rule. +pub fn extract_font_file_name(at_rule: FontFaceAtRule) -> Option { + at_rule.sources.as_ref()?; + match at_rule.sources.unwrap().get(0) { + Some(Source::Url(FontUrlSource { url, .. })) => Some(PathBuf::from(url.0.clone())), + _ => None, + } +} diff --git a/src/renderer/html_handlebars/helpers/mod.rs b/src/renderer/html_handlebars/helpers/mod.rs index 52be6d204b..d8d1c62c67 100644 --- a/src/renderer/html_handlebars/helpers/mod.rs +++ b/src/renderer/html_handlebars/helpers/mod.rs @@ -1,3 +1,4 @@ +pub mod custom_fonts; pub mod navigation; pub mod theme; pub mod toc; From 25e323ecb26287a52017ce81b7cd18113dcc0cf2 Mon Sep 17 00:00:00 2001 From: Lewin Probst Date: Thu, 5 May 2022 15:48:27 +0200 Subject: [PATCH 2/6] chore: Updated msrv from 1.54.0 to 1.56.0 because lewp-css requires Rust 2021 --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index bfd114cac5..d5f1add2e5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -32,7 +32,7 @@ jobs: - build: msrv os: ubuntu-latest # sync MSRV with docs: guide/src/guide/installation.md - rust: 1.54.0 + rust: 1.56.0 steps: - uses: actions/checkout@master - name: Install Rust From bb7abf4b5adabed2d71898670511c62b10265a7e Mon Sep 17 00:00:00 2001 From: Lewin Probst Date: Thu, 5 May 2022 16:03:42 +0200 Subject: [PATCH 3/6] fix: Compiler deprecation warnings on install --- src/cmd/build.rs | 6 +++--- src/cmd/clean.rs | 6 +++--- src/cmd/init.rs | 6 +++--- src/cmd/serve.rs | 6 +++--- src/cmd/test.rs | 10 +++++----- src/cmd/watch.rs | 6 +++--- src/main.rs | 12 ++++++------ 7 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 0091d4827a..1fa8f5fb69 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -1,11 +1,11 @@ use crate::{get_book_dir, open}; -use clap::{arg, App, Arg, ArgMatches}; +use clap::{arg, Arg, ArgMatches, Command}; use mdbook::errors::Result; use mdbook::MDBook; // Create clap subcommand arguments -pub fn make_subcommand<'help>() -> App<'help> { - App::new("build") +pub fn make_subcommand<'help>() -> Command<'help> { + Command::new("build") .about("Builds a book from its markdown files") .arg( Arg::new("dest-dir") diff --git a/src/cmd/clean.rs b/src/cmd/clean.rs index 0569726e1b..51dce46a06 100644 --- a/src/cmd/clean.rs +++ b/src/cmd/clean.rs @@ -1,12 +1,12 @@ use crate::get_book_dir; use anyhow::Context; -use clap::{arg, App, Arg, ArgMatches}; +use clap::{arg, Arg, ArgMatches, Command}; use mdbook::MDBook; use std::fs; // Create clap subcommand arguments -pub fn make_subcommand<'help>() -> App<'help> { - App::new("clean") +pub fn make_subcommand<'help>() -> Command<'help> { + Command::new("clean") .about("Deletes a built book") .arg( Arg::new("dest-dir") diff --git a/src/cmd/init.rs b/src/cmd/init.rs index 1ee5ff2185..e046ab22eb 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -1,5 +1,5 @@ use crate::get_book_dir; -use clap::{arg, App, Arg, ArgMatches}; +use clap::{arg, Arg, ArgMatches, Command as ClapCommand}; use mdbook::config; use mdbook::errors::Result; use mdbook::MDBook; @@ -8,8 +8,8 @@ use std::io::Write; use std::process::Command; // Create clap subcommand arguments -pub fn make_subcommand<'help>() -> App<'help> { - App::new("init") +pub fn make_subcommand<'help>() -> ClapCommand<'help> { + ClapCommand::new("init") .about("Creates the boilerplate structure and files for a new book") // the {n} denotes a newline which will properly aligned in all help messages .arg(arg!([dir] diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index bafbfd52ea..7505d27a99 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -1,7 +1,7 @@ #[cfg(feature = "watch")] use super::watch; use crate::{get_book_dir, open}; -use clap::{arg, App, Arg, ArgMatches}; +use clap::{arg, Arg, ArgMatches, Command}; use futures_util::sink::SinkExt; use futures_util::StreamExt; use mdbook::errors::*; @@ -18,8 +18,8 @@ use warp::Filter; const LIVE_RELOAD_ENDPOINT: &str = "__livereload"; // Create clap subcommand arguments -pub fn make_subcommand<'help>() -> App<'help> { - App::new("serve") +pub fn make_subcommand<'help>() -> Command<'help> { + Command::new("serve") .about("Serves a book at http://localhost:3000, and rebuilds it on changes") .arg( Arg::new("dest-dir") diff --git a/src/cmd/test.rs b/src/cmd/test.rs index 02f982a498..fd1ddb08f3 100644 --- a/src/cmd/test.rs +++ b/src/cmd/test.rs @@ -1,11 +1,11 @@ use crate::get_book_dir; -use clap::{arg, App, Arg, ArgMatches}; +use clap::{arg, Arg, ArgMatches, Command}; use mdbook::errors::Result; use mdbook::MDBook; // Create clap subcommand arguments -pub fn make_subcommand<'help>() -> App<'help> { - App::new("test") +pub fn make_subcommand<'help>() -> Command<'help> { + Command::new("test") .about("Tests that a book's Rust code samples compile") .arg( Arg::new("dest-dir") @@ -27,8 +27,8 @@ pub fn make_subcommand<'help>() -> App<'help> { .long("library-path") .value_name("dir") .takes_value(true) - .use_delimiter(true) - .require_delimiter(true) + .use_value_delimiter(true) + .require_value_delimiter(true) .multiple_values(true) .multiple_occurrences(true) .forbid_empty_values(true) diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index 78ae196880..63aa6d2750 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -1,5 +1,5 @@ use crate::{get_book_dir, open}; -use clap::{arg, App, Arg, ArgMatches}; +use clap::{arg, Arg, ArgMatches, Command}; use mdbook::errors::Result; use mdbook::utils; use mdbook::MDBook; @@ -10,8 +10,8 @@ use std::thread::sleep; use std::time::Duration; // Create clap subcommand arguments -pub fn make_subcommand<'help>() -> App<'help> { - App::new("watch") +pub fn make_subcommand<'help>() -> Command<'help> { + Command::new("watch") .about("Watches a book's files and rebuilds it on changes") .arg( Arg::new("dest-dir") diff --git a/src/main.rs b/src/main.rs index 35562e64bb..82c5739729 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ extern crate log; use anyhow::anyhow; use chrono::Local; -use clap::{App, AppSettings, Arg, ArgMatches}; +use clap::{Arg, ArgMatches, Command}; use clap_complete::Shell; use env_logger::Builder; use log::LevelFilter; @@ -61,13 +61,13 @@ fn main() { } /// Create a list of valid arguments and sub-commands -fn create_clap_app() -> App<'static> { - let app = App::new(crate_name!()) +fn create_clap_app() -> Command<'static> { + let app = Command::new(crate_name!()) .about(crate_description!()) .author("Mathieu David ") .version(VERSION) - .setting(AppSettings::PropagateVersion) - .setting(AppSettings::ArgRequiredElseHelp) + .propagate_version(true) + .arg_required_else_help(true) .after_help( "For more information about a specific command, try `mdbook --help`\n\ The source code for mdBook is available at: https://github.com/rust-lang/mdBook", @@ -77,7 +77,7 @@ fn create_clap_app() -> App<'static> { .subcommand(cmd::test::make_subcommand()) .subcommand(cmd::clean::make_subcommand()) .subcommand( - App::new("completions") + Command::new("completions") .about("Generate shell completions for your shell to stdout") .arg( Arg::new("shell") From 8ee75ac3125e9074dc7bafdf2e586bdaf2b7bc77 Mon Sep 17 00:00:00 2001 From: Lewin Probst Date: Thu, 5 May 2022 16:58:19 +0200 Subject: [PATCH 4/6] fix: Updated clap dependency --- Cargo.lock | 22 ++++++++++++++-------- Cargo.toml | 2 +- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d05258a7b9..a132d2bae2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -194,15 +194,15 @@ dependencies = [ [[package]] name = "clap" -version = "3.0.10" +version = "3.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a30c3bf9ff12dfe5dae53f0a96e0febcd18420d1c0e7fad77796d9d5c4b5375" +checksum = "85a35a599b11c089a7f49105658d089b8f2cf0882993c17daf6de15285c2c35d" dependencies = [ "atty", "bitflags", + "clap_lex", "indexmap", "lazy_static", - "os_str_bytes", "strsim", "termcolor", "textwrap", @@ -217,6 +217,15 @@ dependencies = [ "clap", ] +[[package]] +name = "clap_lex" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a37c35f1112dad5e6e0b1adaff798507497a18fceeb30cceb3bae7d1427b9213" +dependencies = [ + "os_str_bytes", +] + [[package]] name = "cloudabi" version = "0.0.3" @@ -1214,9 +1223,6 @@ name = "os_str_bytes" version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64" -dependencies = [ - "memchr", -] [[package]] name = "output_vt100" @@ -2088,9 +2094,9 @@ dependencies = [ [[package]] name = "textwrap" -version = "0.14.2" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80" +checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" [[package]] name = "time" diff --git a/Cargo.toml b/Cargo.toml index 84e5fa53b7..34471b6e85 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ description = "Creates a book from markdown files" [dependencies] anyhow = "1.0.28" chrono = "0.4" -clap = { version = "3.0", features = ["cargo"] } +clap = { version = "3.1.15", features = ["cargo"] } clap_complete = "3.0" env_logger = "0.7.1" handlebars = "4.0" From 239a9f5ff99eea0ae4574d9575d0c7bba393d131 Mon Sep 17 00:00:00 2001 From: Lewin Probst Date: Thu, 5 May 2022 17:00:25 +0200 Subject: [PATCH 5/6] feat: The copy-custom-fonts parameter is now decoupled from copy_fonts If both are enabled, the build in fonts.css is extended by the custom one. Also both, the build in as well as the custom files are copied to the output folder. --- src/config.rs | 2 -- src/renderer/html_handlebars/hbs_renderer.rs | 13 ++++++++++++- src/theme/index.hbs | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index 8a717ba64c..0ce2f6c628 100644 --- a/src/config.rs +++ b/src/config.rs @@ -497,8 +497,6 @@ pub struct HtmlConfig { pub copy_fonts: bool, /// If true, find custom font files by parsing the fonts/fonts.css and copy /// them to the output directory. - /// - /// ***Is only applied when [copy_fonts] equals to `true`.*** pub copy_custom_fonts: bool, /// An optional google analytics code. pub google_analytics: Option, diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index c81a4b1d81..7b810cf067 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -286,7 +286,14 @@ impl HtmlHandlebars { theme::fonts::SOURCE_CODE_PRO.1, )?; } - if html_config.copy_custom_fonts && html_config.copy_fonts { + if html_config.copy_custom_fonts { + let mut custom_fonts_css = std::fs::read(&Path::new("fonts/fonts.css"))?; + if html_config.copy_fonts { + // we need to extend the custom fonts file by the build in one + // to have the build in ones still available + custom_fonts_css.extend(theme::fonts::CSS); + } + write_file(destination, "fonts/fonts.css", &custom_fonts_css)?; let custom_font_file_names = helpers::custom_fonts::find_custom_font_files(Path::new("fonts/fonts.css"))?; for font_file in custom_font_file_names { @@ -665,6 +672,10 @@ fn make_data( data.insert("copy_fonts".to_owned(), json!(true)); } + if html_config.copy_custom_fonts { + data.insert("copy_custom_fonts".to_owned(), json!(true)); + } + // Add check to see if there is an additional style if !html_config.additional_css.is_empty() { let mut css = Vec::new(); diff --git a/src/theme/index.hbs b/src/theme/index.hbs index 18d984a2b7..12d25952aa 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -35,7 +35,7 @@ - {{#if copy_fonts}} + {{#if (or copy_fonts copy_custom_fonts)}} {{/if}} From 02b9e429d37b8296b6225bdc5c90d2ce02ebd92f Mon Sep 17 00:00:00 2001 From: Lewin Probst Date: Fri, 6 May 2022 09:08:17 +0200 Subject: [PATCH 6/6] docs: Added custom fonts section to the documentation guide --- guide/src/SUMMARY.md | 1 + guide/src/format/custom-fonts.md | 59 ++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 guide/src/format/custom-fonts.md diff --git a/guide/src/SUMMARY.md b/guide/src/SUMMARY.md index 974d65fae7..73133affb1 100644 --- a/guide/src/SUMMARY.md +++ b/guide/src/SUMMARY.md @@ -30,6 +30,7 @@ - [index.hbs](format/theme/index-hbs.md) - [Syntax highlighting](format/theme/syntax-highlighting.md) - [Editor](format/theme/editor.md) + - [Custom fonts](format/custom-fonts.md) - [MathJax Support](format/mathjax.md) - [mdBook-specific features](format/mdbook.md) - [Markdown](format/markdown.md) diff --git a/guide/src/format/custom-fonts.md b/guide/src/format/custom-fonts.md new file mode 100644 index 0000000000..281606c30d --- /dev/null +++ b/guide/src/format/custom-fonts.md @@ -0,0 +1,59 @@ +# Custom fonts + +It is possible to use custom fonts by using a combination of the parameters +`copy-fonts` and `copy-custom-fonts`. `copy-fonts` is enabled by default. + +The fonts are specified by the `fonts/fonts.css` file, relative to the root directory +of your book project. + +## The copy-fonts option + +If `copy-fonts` is enabled, the build in `fonts/fonts.css` file is being copied +to the output directory. In addition to that, the default fonts are copied to +the output directory. + +## The copy-custom-fonts option + +If `copy-custom-fonts` is enabled, the `fonts/fonts.css` file that you have created +is copied to the output directory. Also, the file gets parsed and searched for +`@font-face` rules having a `src: url(CUSTOM-FONT)` definition. These `CUSTOM-FONT` +files are copied to the output directory. + +## Enabling both options + +It is also possible to use both options in parallel. Then, the build in fonts +are copied, as well as the custom font files that have been found. The build in +and the custom definition of `fonts/fonts.css` are combined and copied to the +output directory as well. + +## Example with both options enabled + +Create a `fonts/` folder in your book root. Then copy the fonts you want to use +into this folder (in this example we are assuming `Lato-Regular.ttf`) + +Create a custom fonts file `fonts/fonts.css` + +```css +@font-face { + font-family: "Lato"; + font-style: normal; + font-weight: normal; + src: url('Lato-Regular.ttf'); +} +``` + +Setup your `book.toml` that it contains the following parameters: + +```toml +[output.html] +copy-fonts = true +copy-custom-fonts = true +``` + +Adjust your `theme/css/general.css` file according to your needs, for example: + +```css +html { + font-family: "Lato", sans-serif; +} +```