From acd74954b5dd0a10f683744d11635bb1b1b76076 Mon Sep 17 00:00:00 2001 From: Geo Date: Fri, 3 Jun 2022 00:40:52 +0100 Subject: [PATCH] feat: more flexible hooks system 1. Use more standard hook names: - head-start - head-end - body-end - footer-start - toc-start - toc-end 2. Enable multiple files per hook. For example, a starter template may have `hooks/head-end/tpl.html` and a user may have customizations in `hooks/head-end/user.html` custom_head.html -> hooks/head-end/custom.html custom_js.html -> hooks/body-end/custom.html Deprecates custom_head.html and custom_js.html but they remain supported for now. --- wowchemy/layouts/partials/book_layout.html | 4 +- wowchemy/layouts/partials/custom_head.html | 4 - wowchemy/layouts/partials/custom_js.html | 4 - .../layouts/partials/functions/get_hook.html | 18 + wowchemy/layouts/partials/hooks/footer.html | 2 - .../layouts/partials/hooks/page_toc_end.html | 0 .../partials/hooks/page_toc_start.html | 0 wowchemy/layouts/partials/site_footer.html | 2 +- wowchemy/layouts/partials/site_head.html | 11 +- wowchemy/layouts/partials/site_js.html | 395 +++++++++--------- 10 files changed, 231 insertions(+), 209 deletions(-) delete mode 100644 wowchemy/layouts/partials/custom_head.html delete mode 100644 wowchemy/layouts/partials/custom_js.html create mode 100644 wowchemy/layouts/partials/functions/get_hook.html delete mode 100644 wowchemy/layouts/partials/hooks/footer.html delete mode 100644 wowchemy/layouts/partials/hooks/page_toc_end.html delete mode 100644 wowchemy/layouts/partials/hooks/page_toc_start.html diff --git a/wowchemy/layouts/partials/book_layout.html b/wowchemy/layouts/partials/book_layout.html index 693fc438d..d73c1e8e4 100644 --- a/wowchemy/layouts/partials/book_layout.html +++ b/wowchemy/layouts/partials/book_layout.html @@ -12,7 +12,7 @@ {{/* Show ToC by default. */}} {{ if ne .Params.toc false }}
- {{ partial "hooks/page_toc_start" . }} + {{ partial "functions/get_hook" (dict "hook" "toc-start" "context" .) }}
{{ end }} diff --git a/wowchemy/layouts/partials/custom_head.html b/wowchemy/layouts/partials/custom_head.html deleted file mode 100644 index 7f262fae1..000000000 --- a/wowchemy/layouts/partials/custom_head.html +++ /dev/null @@ -1,4 +0,0 @@ -{{/* Do not directly modify this file! */}} -{{/* Instead, create a `layouts/partials/custom_head.html` file in your site and add your code to it. */}} - -{{/* This partial is included in `themes/academic/layouts/partials/site_head.html`. */}} diff --git a/wowchemy/layouts/partials/custom_js.html b/wowchemy/layouts/partials/custom_js.html deleted file mode 100644 index 8d081963f..000000000 --- a/wowchemy/layouts/partials/custom_js.html +++ /dev/null @@ -1,4 +0,0 @@ -{{/* Do not directly modify this file! */}} -{{/* Instead, create a `layouts/partials/custom_js.html` file in your site and add your code to it. */}} - -{{/* This partial is included in `themes/academic/layouts/partials/site_js.html`. */}} diff --git a/wowchemy/layouts/partials/functions/get_hook.html b/wowchemy/layouts/partials/functions/get_hook.html new file mode 100644 index 000000000..c1a0a0e41 --- /dev/null +++ b/wowchemy/layouts/partials/functions/get_hook.html @@ -0,0 +1,18 @@ +{{/* Function to inject custom code into layouts without overriding files. */}} +{{/* Input: hook folder name (str) */}} +{{/* Output: loaded (bool) */}} + +{{ $loaded := false }} +{{ $partial_dir := printf "hooks/%s/" .hook }} +{{ $hook_dir_path := path.Join "layouts/partials" $partial_dir }} +{{ if fileExists $hook_dir_path }} + {{ range os.ReadDir $hook_dir_path }} + {{ if not .IsDir }} + {{ $partial_path := path.Join $partial_dir .Name }} + {{ partial $partial_path . }} + {{ $loaded = true }} + {{ end }} + {{ end }} +{{ end }} +{{/* The return statement below is for debug purposes only and prevents the above partial(s) being included in the page */}} +{{/* return $loaded */}} diff --git a/wowchemy/layouts/partials/hooks/footer.html b/wowchemy/layouts/partials/hooks/footer.html deleted file mode 100644 index 9c47df60b..000000000 --- a/wowchemy/layouts/partials/hooks/footer.html +++ /dev/null @@ -1,2 +0,0 @@ -{{/* Override this file to add a custom footer to every page. */}} -{{/* This partial is included in `layouts/partials/site_footer.html`. */}} diff --git a/wowchemy/layouts/partials/hooks/page_toc_end.html b/wowchemy/layouts/partials/hooks/page_toc_end.html deleted file mode 100644 index e69de29bb..000000000 diff --git a/wowchemy/layouts/partials/hooks/page_toc_start.html b/wowchemy/layouts/partials/hooks/page_toc_start.html deleted file mode 100644 index e69de29bb..000000000 diff --git a/wowchemy/layouts/partials/site_footer.html b/wowchemy/layouts/partials/site_footer.html index 82a14b7ab..34c13e010 100644 --- a/wowchemy/layouts/partials/site_footer.html +++ b/wowchemy/layouts/partials/site_footer.html @@ -1,6 +1,6 @@