diff --git a/Cargo.lock b/Cargo.lock index 7730a42..687656f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -44,6 +44,21 @@ version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "0.6.18" @@ -160,6 +175,16 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bstr" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40723b8fb387abc38f4f4a37c09073622e41dd12327033091ef8950659e6dc0c" +dependencies = [ + "memchr", + "serde", +] + [[package]] name = "bumpalo" version = "3.16.0" @@ -199,7 +224,32 @@ version = "0.4.38" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" dependencies = [ + "android-tzdata", + "iana-time-zone", "num-traits", + "windows-targets 0.52.6", +] + +[[package]] +name = "chrono-tz" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93698b29de5e97ad0ae26447b344c482a7284c737d9ddc5f9e52b74a336671bb" +dependencies = [ + "chrono", + "chrono-tz-build", + "phf 0.11.2", +] + +[[package]] +name = "chrono-tz-build" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c088aee841df9c3041febbb73934cfc39708749bf96dc827e3359cd39ef11b1" +dependencies = [ + "parse-zoneinfo", + "phf 0.11.2", + "phf_codegen 0.11.2", ] [[package]] @@ -274,6 +324,12 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + [[package]] name = "cpufeatures" version = "0.2.14" @@ -449,6 +505,12 @@ dependencies = [ "syn 2.0.87", ] +[[package]] +name = "deunicode" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "339544cc9e2c4dc3fc7149fd630c5f22263a4fdf18a98afd0075784968b5cf00" + [[package]] name = "digest" version = "0.10.7" @@ -720,6 +782,30 @@ version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +[[package]] +name = "globset" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15f1ce686646e7f1e19bf7d5533fe443a45dbfb990e00629110797578b42fb19" +dependencies = [ + "aho-corasick", + "bstr", + "log", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "globwalk" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf760ebf69878d9fd8f110c89703d90ce35095324d1f1edcb595c63945ee757" +dependencies = [ + "bitflags 2.6.0", + "ignore", + "walkdir", +] + [[package]] name = "grass" version = "0.13.4" @@ -762,21 +848,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "handlebars" -version = "6.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd4ccde012831f9a071a637b0d4e31df31c0f6c525784b35ae76a9ac6bc1e315" -dependencies = [ - "log", - "num-order", - "pest", - "pest_derive", - "serde", - "serde_json", - "thiserror", -] - [[package]] name = "hashbrown" version = "0.13.2" @@ -888,6 +959,15 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "humansize" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6cb51c9a029ddc91b07a787f1d86b53ccfa49b0e86688c946ebe8d3555685dd7" +dependencies = [ + "libm", +] + [[package]] name = "hyper" version = "0.14.31" @@ -912,6 +992,29 @@ dependencies = [ "want", ] +[[package]] +name = "iana-time-zone" +version = "0.1.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "icu_collections" version = "1.5.0" @@ -1057,6 +1160,22 @@ dependencies = [ "icu_properties", ] +[[package]] +name = "ignore" +version = "0.4.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d89fd380afde86567dfba715db065673989d6253f42b88179abd3eae47bda4b" +dependencies = [ + "crossbeam-deque", + "globset", + "log", + "memchr", + "regex-automata", + "same-file", + "walkdir", + "winapi-util", +] + [[package]] name = "indenter" version = "0.3.3" @@ -1176,6 +1295,12 @@ version = "0.2.161" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" +[[package]] +name = "libm" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8355be11b20d696c8f18f6cc018c4e372165b1fa8126cef092399c9951984ffa" + [[package]] name = "libredox" version = "0.1.3" @@ -1377,21 +1502,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" -[[package]] -name = "num-modular" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17bb261bf36fa7d83f4c294f834e91256769097b3cb505d44831e0a179ac647f" - -[[package]] -name = "num-order" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "537b596b97c40fcf8056d153049eb22f481c17ebce72a513ec9286e4986d1bb6" -dependencies = [ - "num-modular", -] - [[package]] name = "num-traits" version = "0.2.19" @@ -1445,6 +1555,15 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "parse-zoneinfo" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f2a05b18d44e2957b88f96ba460715e295bc1d7510468a2f3d3b44535d26c24" +dependencies = [ + "regex", +] + [[package]] name = "percent-encoding" version = "2.3.1" @@ -1527,6 +1646,16 @@ dependencies = [ "phf_shared 0.8.0", ] +[[package]] +name = "phf_codegen" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8d39688d359e6b34654d328e262234662d16cc0f60ec8dcbe5e718709342a5a" +dependencies = [ + "phf_generator 0.11.2", + "phf_shared 0.11.2", +] + [[package]] name = "phf_generator" version = "0.8.0" @@ -1906,7 +2035,7 @@ dependencies = [ "log", "matches", "phf 0.8.0", - "phf_codegen", + "phf_codegen 0.8.0", "precomputed-hash", "servo_arc", "smallvec", @@ -2040,6 +2169,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "slug" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "882a80f72ee45de3cc9a5afeb2da0331d58df69e4e7d8eeb5d3c7784ae67e724" +dependencies = [ + "deunicode", + "wasm-bindgen", +] + [[package]] name = "smallvec" version = "1.13.2" @@ -2107,6 +2246,28 @@ dependencies = [ "syn 2.0.87", ] +[[package]] +name = "tera" +version = "1.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab9d851b45e865f178319da0abdbfe6acbc4328759ff18dafc3a41c16b4cd2ee" +dependencies = [ + "chrono", + "chrono-tz", + "globwalk", + "humansize", + "lazy_static", + "percent-encoding", + "pest", + "pest_derive", + "rand 0.8.5", + "regex", + "serde", + "serde_json", + "slug", + "unic-segment", +] + [[package]] name = "thin-slice" version = "0.1.1" @@ -2321,6 +2482,56 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2896d95c02a80c6d6a5d6e953d479f5ddf2dfdb6a244441010e373ac0fb88971" +[[package]] +name = "unic-char-property" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8c57a407d9b6fa02b4795eb81c5b6652060a15a7903ea981f3d723e6c0be221" +dependencies = [ + "unic-char-range", +] + +[[package]] +name = "unic-char-range" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0398022d5f700414f6b899e10b8348231abf9173fa93144cbc1a43b9793c1fbc" + +[[package]] +name = "unic-common" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80d7ff825a6a654ee85a63e80f92f054f904f21e7d12da4e22f9834a4aaa35bc" + +[[package]] +name = "unic-segment" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4ed5d26be57f84f176157270c112ef57b86debac9cd21daaabbe56db0f88f23" +dependencies = [ + "unic-ucd-segment", +] + +[[package]] +name = "unic-ucd-segment" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2079c122a62205b421f499da10f3ee0f7697f012f55b675e002483c73ea34700" +dependencies = [ + "unic-char-property", + "unic-char-range", + "unic-ucd-version", +] + +[[package]] +name = "unic-ucd-version" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96bd2f2237fe450fcd0a1d2f5f4e91711124f7857ba2e964247776ebeeb7b0c4" +dependencies = [ + "unic-common", +] + [[package]] name = "unicase" version = "2.8.0" @@ -2505,6 +2716,15 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-sys" version = "0.48.0" @@ -2763,7 +2983,6 @@ dependencies = [ "fs_extra", "futures", "grass", - "handlebars", "hotwatch", "itertools", "lol_html", @@ -2774,6 +2993,7 @@ dependencies = [ "rss", "serde", "serde_yml", + "tera", "time", "tokio", "url", diff --git a/Cargo.toml b/Cargo.toml index 2af6611..e7dc79d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,6 @@ eyre = "0.6" fs_extra = "1.2" futures = {version = "0.3", optional = true} grass = {version = "0.13", default-features = false} -handlebars = "6" hotwatch = {version = "0.5", optional = true} itertools = "0.13" lol_html = "2" @@ -24,6 +23,7 @@ rayon = "1" rss = {version = "2", features = ["validation"]} serde = {version = "1", features = ["derive"]} serde_yml = "0.0.12" +tera = "1" time = {version = "0.3", features = ["serde-human-readable"]} tokio = {version = "1.10", features = [ "macros", diff --git a/site/config.yaml b/site/config.yaml index 889045e..da44ff7 100644 --- a/site/config.yaml +++ b/site/config.yaml @@ -9,9 +9,9 @@ resources: source_path: blog output_path_short: blog output_path_long: blog - resource_template: blog-post - resource_list_template: blog-list - rss_template: rss/blog-post + resource_template: blog-post.tera + resource_list_template: blog-list.tera + rss_template: rss/blog-post.tera rss_title: zyl's blog rss_description: feed of recent blog posts on zyl's website. list_title: blog @@ -22,9 +22,9 @@ resources: source_path: images output_path_short: i output_path_long: images - resource_template: image - resource_list_template: images - rss_template: rss/image + resource_template: image.tera + resource_list_template: images.tera + rss_template: rss/image.tera rss_title: zyl's images rss_description: feed of newly uploaded images from zyl's website. list_title: images diff --git a/site/pages/click.md b/site/pages/click.md index 4997b3c..6046d53 100644 --- a/site/pages/click.md +++ b/site/pages/click.md @@ -8,5 +8,5 @@ embed: description: click click click extra: name: basic - template: extras/click + template: extras/click.tera --- diff --git a/site/pages/index.md b/site/pages/index.md index d9b37a8..386ba35 100644 --- a/site/pages/index.md +++ b/site/pages/index.md @@ -1,7 +1,7 @@ --- extra: name: resource-list-outside - template: extras/index-injection + template: extras/index-injection.tera resource: blog count: 3 --- diff --git a/site/templates/base.hbs b/site/templates/base.tera similarity index 67% rename from site/templates/base.hbs rename to site/templates/base.tera index 63997ba..14cd3c0 100644 --- a/site/templates/base.hbs +++ b/site/templates/base.tera @@ -1,3 +1,7 @@ +{% macro badge(badge, url, alt) %} +{{alt}} +{% endmacro badge %} + @@ -5,15 +9,15 @@ - {{title}} + {{ title }} - {{{head}}} - {{#each scripts}} - - {{/each}} - {{#each styles}} - - {{/each}} + {{ head | safe }} + {% for script in scripts %} + + {% endfor %} + {% for style in styles %} + + {% endfor %} @@ -32,7 +36,7 @@
- {{{page}}} + {% block content %}{{ page | safe }}{% endblock content %}
@@ -42,10 +46,7 @@ bark bark awruff :3
- {{#*inline "badge"}} - {{alt}} - {{/inline}} - {{> badge badge="transbian.png" url="https://badge.les.bi" alt="transgender and lesbian flags"}} + {{ self::badge(badge="transbian.png", url="https://badge.les.bi", alt="transgender and lesbian flags") }}
diff --git a/site/templates/basic-link-list.hbs b/site/templates/basic-link-list.hbs deleted file mode 100644 index 88607ca..0000000 --- a/site/templates/basic-link-list.hbs +++ /dev/null @@ -1,8 +0,0 @@ -

{{title}}

- diff --git a/site/templates/basic-link-list.tera b/site/templates/basic-link-list.tera new file mode 100644 index 0000000..a07f12c --- /dev/null +++ b/site/templates/basic-link-list.tera @@ -0,0 +1,8 @@ +

{{ title }}

+ diff --git a/site/templates/blog-list.hbs b/site/templates/blog-list.tera similarity index 53% rename from site/templates/blog-list.hbs rename to site/templates/blog-list.tera index 0904a3d..0836220 100644 --- a/site/templates/blog-list.hbs +++ b/site/templates/blog-list.tera @@ -1,24 +1,24 @@ -{{#if tag}} +{% if tag %}

blog posts tagged {{tag}}

View all blog posts

-{{else}} +{% else %}

blog Posts

view blog tags

rss feed

-{{/if}} +{% endif %}

Page {{page}}/{{page_max}}

-{{#if previous}} +{% if previous %} previous page -{{/if}} -{{#if next}} +{% endif %} +{% if next %} next page -{{/if}} +{% endif %}
- {{#each resources}} + {% for resource in resources %}
-

{{title}}

-

{{timestamp}}

-

{{desc}}

+

{{resource.title}}

+

{{resource.timestamp}}

+

{{resource.desc}}

- {{/each}} + {% endfor %}
diff --git a/site/templates/blog-post.hbs b/site/templates/blog-post.tera similarity index 71% rename from site/templates/blog-post.hbs rename to site/templates/blog-post.tera index cc9dff1..e9a4453 100644 --- a/site/templates/blog-post.hbs +++ b/site/templates/blog-post.tera @@ -1,22 +1,22 @@

{{title}}

published {{timestamp}} - {{#if draft}} + {% if draft %}

DRAFT

- {{/if}} + {% endif %}

{{desc}}

{{header_image_alt}}
- {{{content}}} + {{ content | safe }}

tags

- {{#each tags}} - {{this}}{{#unless @last}},{{/unless}} - {{/each}} + {% for tag in tags %} + {{tag}}{% if not loop.last %},{% endif %} + {% endfor %}
diff --git a/site/templates/extras/click.hbs b/site/templates/extras/click.hbs deleted file mode 100644 index c727c7e..0000000 --- a/site/templates/extras/click.hbs +++ /dev/null @@ -1,30 +0,0 @@ -
-

WARNING: no save mechanic is implemented yet!!

-

click

- -
- {{#*inline "resource"}} - {{name}}0 (0/s) - {{/inline}} - {{> resource id="pets" name="pets"}} - {{> resource id="barks" name="barks"}} - {{> resource id="kisses" name="kisses"}} -
- -
- {{#*inline "tool"}} -
-

{{name}} (0, lvl 1)

-

{{description}}

- -
- {{/inline}} - {{> tool id="hand" name="hand" description="don't bite the hand that pets you"}} - {{> tool id="puppy" name="puppy" description="arf arf wruff :3"}} - {{> tool id="foodBowl" name="food bowl" description="more food for more barking"}} - {{> tool id="kisser" name="kisser wow" description="someone to kiss all those poor puppies,,"}} -
-
diff --git a/site/templates/extras/click.tera b/site/templates/extras/click.tera new file mode 100644 index 0000000..0084ae1 --- /dev/null +++ b/site/templates/extras/click.tera @@ -0,0 +1,31 @@ +{% macro resource(id, name) %} +{{name}}0 (0/s) +{% endmacro resource %} + +{% macro tool(id, name, description) %} +
+

{{name}} (0, lvl 1)

+

{{description}}

+ +
+{% endmacro tool %} + +
+

WARNING: no save mechanic is implemented yet!!

+

click

+ +
+ {{ self::resource(id="pets", name="pets") }} + {{ self::resource(id="barks", name="barks") }} + {{ self::resource(id="kisses", name="kisses") }} +
+ +
+ {{ self::tool(id="hand", name="hand", description="don't bite the hand that pets you") }} + {{ self::tool(id="puppy", name="puppy", description="arf arf wruff :3") }} + {{ self::tool(id="foodBowl", name="food bowl", description="more food for more barking") }} + {{ self::tool(id="kisser", name="kisser wow", description="someone to kiss all those poor puppies,,") }} +
+
diff --git a/site/templates/extras/index-injection.hbs b/site/templates/extras/index-injection.hbs deleted file mode 100644 index d8b3047..0000000 --- a/site/templates/extras/index-injection.hbs +++ /dev/null @@ -1,13 +0,0 @@ -
-
-

most recent blog posts

-
- {{#each resources}} -
-

{{title}}

-

{{timestamp}}

-

{{desc}}

-
- {{/each}} -
-
diff --git a/site/templates/extras/index-injection.tera b/site/templates/extras/index-injection.tera new file mode 100644 index 0000000..ef3d60d --- /dev/null +++ b/site/templates/extras/index-injection.tera @@ -0,0 +1,13 @@ +
+
+

most recent blog posts

+
+ {% for resource in resources %} +
+

{{resource.title}}

+

{{resource.timestamp}}

+

{{resource.desc}}

+
+ {% endfor %} +
+
diff --git a/site/templates/image.hbs b/site/templates/image.tera similarity index 67% rename from site/templates/image.hbs rename to site/templates/image.tera index 0d5c672..516882d 100644 --- a/site/templates/image.hbs +++ b/site/templates/image.tera @@ -2,12 +2,12 @@

{{title}}

published {{timestamp}} {{alt}} - {{{content}}} + {{ content | safe }}

view full size image

tags

- {{#each tags}} - {{this}}{{#unless @last}},{{/unless}} - {{/each}} + {% for tag in tags %} + {{tag}}{% if not loop.last %},{% endif %} + {% endfor %}
diff --git a/site/templates/images.hbs b/site/templates/images.tera similarity index 51% rename from site/templates/images.hbs rename to site/templates/images.tera index 062121a..66a4545 100644 --- a/site/templates/images.hbs +++ b/site/templates/images.tera @@ -1,23 +1,23 @@ -{{#if tag}} +{% if tag %}

images tagged {{tag}}

view all images

-{{else}} +{% else %}

images

view image tags

rss feed

-{{/if}} +{% endif %}

page {{page}}/{{page_max}}

-{{#if previous}} +{% if previous %} previous page -{{/if}} -{{#if next}} +{% endif %} +{% if next %} next page -{{/if}} +{% endif %}
- {{#each resources}} - - {{alt}} - {{title}} + {% for resource in resources %} + + {{resource.alt}} + {{resource.title}} - {{/each}} + {% endfor %}
diff --git a/site/templates/rss/blog-post.hbs b/site/templates/rss/blog-post.hbs deleted file mode 100644 index ee20e88..0000000 --- a/site/templates/rss/blog-post.hbs +++ /dev/null @@ -1 +0,0 @@ -
{{{source}}}
diff --git a/site/templates/rss/blog-post.tera b/site/templates/rss/blog-post.tera new file mode 100644 index 0000000..f82a8fc --- /dev/null +++ b/site/templates/rss/blog-post.tera @@ -0,0 +1 @@ +
{{ desc | safe }}
diff --git a/site/templates/rss/image.hbs b/site/templates/rss/image.hbs deleted file mode 100644 index b520216..0000000 --- a/site/templates/rss/image.hbs +++ /dev/null @@ -1 +0,0 @@ -{{alt}} diff --git a/site/templates/rss/image.tera b/site/templates/rss/image.tera new file mode 100644 index 0000000..28864b2 --- /dev/null +++ b/site/templates/rss/image.tera @@ -0,0 +1 @@ +{{alt}} diff --git a/src/builder.rs b/src/builder.rs index b81b2a0..ea1491e 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -3,10 +3,10 @@ use std::{collections::HashMap, path::PathBuf}; use eyre::{eyre, Context, OptionExt}; -use handlebars::Handlebars; use lol_html::{element, html_content::ContentType, HtmlRewriter, Settings}; use pulldown_cmark::{Options, Parser}; use serde::Serialize; +use tera::Tera; use url::Url; use crate::{resource::ResourceBuilder, util, PageMetadata, Site, ROOT_PATH, SASS_PATH}; @@ -30,9 +30,9 @@ struct TemplateData<'a, T> { } /// Struct used to build the site. -pub struct SiteBuilder<'a> { +pub struct SiteBuilder { /// The Handlebars registry used to render templates. - pub(crate) reg: Handlebars<'a>, + pub(crate) tera: Tera, /// The site info used to build the site. pub site: Site, /// The path to the build directory. @@ -44,7 +44,7 @@ pub struct SiteBuilder<'a> { pub resource_builders: HashMap, } -impl<'a> SiteBuilder<'a> { +impl SiteBuilder { /// Creates a new site builder. pub fn new(site: Site, serving: bool) -> Self { let mut build_path = match &site.config.build { @@ -55,8 +55,17 @@ impl<'a> SiteBuilder<'a> { build_path = site.site_path.join("build"); } + let mut tera = Tera::new( + site.site_path + .join("templates/**/*.tera") + .to_str() + .expect("failed to convert path to string"), + ) + .expect("failed to create tera instance"); + tera.autoescape_on(vec![".tera"]); + Self { - reg: Handlebars::new(), + tera, resource_builders: HashMap::new(), site, build_path, @@ -66,6 +75,7 @@ impl<'a> SiteBuilder<'a> { /// Prepares the site builder for use and sets up the build directory. pub fn prepare(mut self) -> eyre::Result { + self.tera.full_reload()?; if self.build_path.exists() { for entry in self.build_path.read_dir()? { let path = &entry?.path(); @@ -82,12 +92,6 @@ impl<'a> SiteBuilder<'a> { std::fs::create_dir(&self.build_path).wrap_err("Failed to create build directory")?; } - for (template_name, template_path) in &self.site.template_index { - self.reg - .register_template_file(template_name, template_path) - .wrap_err("Failed to register template file")?; - } - let root_path = self.site.site_path.join(ROOT_PATH); if root_path.exists() { for entry in walkdir::WalkDir::new(&root_path) { @@ -272,16 +276,18 @@ impl<'a> SiteBuilder<'a> { embed.build() }); - let out = self.reg.render( - &page_metadata.template.unwrap_or_else(|| "base".to_string()), - &TemplateData { + let out = self.tera.render( + &page_metadata + .template + .unwrap_or_else(|| "base.tera".to_string()), + &tera::Context::from_serialize(TemplateData { page: page_html, title: &title, head, scripts: &page_metadata.scripts, styles: &page_metadata.styles, extra_data, - }, + })?, )?; // Modify HTML output diff --git a/src/extras.rs b/src/extras.rs index be5611c..93ac9e6 100644 --- a/src/extras.rs +++ b/src/extras.rs @@ -30,7 +30,7 @@ impl Extra { match self { Self::Basic => { let data: BasicData = serde_yml::from_value(data.inner.clone())?; - let content = builder.reg.render(&data.template, &())?; + let content = builder.tera.render(&data.template, &tera::Context::new())?; append_to(&page, &content, "main.page") } Self::HtmlModification(f) => (f)(page, builder, data), @@ -91,9 +91,9 @@ fn resource_list_outside( let data: ResourceListData = serde_yml::from_value(data.inner.clone())?; - let resource_list = builder.reg.render( + let resource_list = builder.tera.render( &data.template, - &ResourceListTemplateData { + &tera::Context::from_serialize(ResourceListTemplateData { resources: builder .resource_builders .get(&data.resource) @@ -107,7 +107,7 @@ fn resource_list_outside( timestamp: v.timestamp, }) .collect(), - }, + })?, )?; append_to(&page, &resource_list, "#content") diff --git a/src/link_list.rs b/src/link_list.rs index a6e0d97..5d927cb 100644 --- a/src/link_list.rs +++ b/src/link_list.rs @@ -29,7 +29,10 @@ pub fn render_basic_link_list( title: &str, ) -> eyre::Result { let data = LinkTemplateData { links, title }; - let out = builder.reg.render("basic-link-list", &data)?; + let out = builder.tera.render( + "basic-link-list.tera", + &tera::Context::from_serialize(data)?, + )?; let out = builder.build_page_raw( PageMetadata { title: Some(title.to_owned()), diff --git a/src/resource.rs b/src/resource.rs index a4797d5..64e005b 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -258,7 +258,10 @@ impl ResourceBuilder { id, timestamp: resource.timestamp, }; - builder.reg.render(&self.config.resource_template, &data)? + builder.tera.render( + &self.config.resource_template, + &tera::Context::from_serialize(data)?, + )? }; let out = builder.build_page_raw_with_extra_data( @@ -341,7 +344,10 @@ impl ResourceBuilder { previous, next, }; - let out = builder.reg.render(&config.resource_list_template, &data)?; + let out = builder.tera.render( + &config.resource_list_template, + &tera::Context::from_serialize(data)?, + )?; let out = builder.build_page_raw( PageMetadata { title: Some(title.to_owned()), @@ -437,9 +443,10 @@ impl ResourceBuilder { )) .description(resource.resource.desc.clone()) .pub_date(Some(resource.timestamp.format(&Rfc2822)?)) - .content(Some( - builder.reg.render(&self.config.rss_template, &resource)?, - )) + .content(Some(builder.tera.render( + &self.config.rss_template, + &tera::Context::from_serialize(resource)?, + )?)) .build(), ) } diff --git a/src/serving.rs b/src/serving.rs index c19796d..ea274ab 100644 --- a/src/serving.rs +++ b/src/serving.rs @@ -79,9 +79,8 @@ fn create( if build { builder.build_page(&page_name_str)?; } - } else if let Ok(template_path) = relative_path.strip_prefix(TEMPLATES_PATH) { - let (_template_name, template_name_str) = get_name(template_path); - builder.refresh_template(&template_name_str, path)?; + } else if let Ok(_template_path) = relative_path.strip_prefix(TEMPLATES_PATH) { + builder.tera.full_reload()?; if build { builder.site.build_all_pages(builder)?; builder.build_all_resources()?; @@ -118,7 +117,7 @@ fn remove(builder: &mut SiteBuilder, path: &Path, relative_path: &Path) -> eyre: } else if let Ok(template_path) = relative_path.strip_prefix(TEMPLATES_PATH) { let (_template_name, template_name_str) = get_name(template_path); builder.site.template_index.remove(&template_name_str); - builder.reg.unregister_template(&template_name_str); + builder.tera.full_reload()?; builder .site .build_all_pages(builder) @@ -324,17 +323,3 @@ impl Site { Ok(()) } } - -impl<'a> SiteBuilder<'a> { - /// Refreshes a template to ensure it's up to date. - pub fn refresh_template( - &mut self, - template_name: &str, - template_path: &Path, - ) -> eyre::Result<()> { - self.reg - .register_template_file(template_name, template_path)?; - - Ok(()) - } -}