Skip to content

Commit

Permalink
Make the index_entry variable available to all hooks after render
Browse files Browse the repository at this point in the history
  • Loading branch information
dmbaturin committed Nov 25, 2023
1 parent d9696e4 commit e7f233c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# 4.8.0 (2023-11-04)
# 4.8.0 (TBD)

## New features and improvements

* `site_index` is now available to the post-build hook.
* `site_index` variable is now available to the post-build hook.
* `index_entry` variable (the site index entry for the current page) is now available to save and post-save hooks and to Lua index processors.
* New options for ignoring certain paths in the sire dir: `settings.ignore_path_regexes` and `settings.ignore_directories`.

### New plugin API functions
Expand Down
25 changes: 16 additions & 9 deletions src/hooks.ml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ let get_hooks config =

(* Hook functions *)

(* A helper function to get the index entry for the current page from the index hash *)
let get_index_entry_json env =
let index_entry = Hashtbl.find_opt env.site_index_hash env.page_file in
match index_entry with
| None -> `Null
| Some ie -> Utils.json_of_index_entry ie

(* pre-parse hook runs just after the page source is loaded from a file or received from a preprocessor
and before it's parsed into an HTML element tree.
Expand Down Expand Up @@ -229,15 +236,7 @@ let run_render_hook soupault_state hook_config file_name lua_code env soup =
let lua_str = I.Value.string in
let lua_state = I.mk () in
let settings = soupault_state.soupault_settings in
(* Get the index entry for the current page from the index hash *)
let index_entry_json =
begin
let index_entry = Hashtbl.find_opt env.site_index_hash env.page_file in
match index_entry with
| None -> `Null
| Some ie -> Utils.json_of_index_entry ie
end
in
let index_entry_json = get_index_entry_json env in
let () =
(* Set up the hook environment *)
I.register_globals [
Expand Down Expand Up @@ -270,13 +269,16 @@ let run_save_hook soupault_state hook_config file_name lua_code env page_source
let open Defaults in
let lua_str = I.Value.string in
let lua_state = I.mk () in
let index_entry_json = get_index_entry_json env in
let settings = soupault_state.soupault_settings in
let () =
(* Set up the hook environment *)
I.register_globals [
"page_source", lua_str.embed page_source;
"page_file", lua_str.embed env.page_file;
"page_url", lua_str.embed env.page_url;
"site_index", Plugin_api.lua_of_json (Utils.json_of_index_entries env.site_index);
"index_entry", Plugin_api.lua_of_json index_entry_json;
"target_file", lua_str.embed env.target_file;
"target_dir", lua_str.embed env.target_dir;
"config", lua_of_toml hook_config;
Expand All @@ -299,12 +301,15 @@ let run_post_save_hook soupault_state hook_config file_name lua_code env =
let open Defaults in
let lua_str = I.Value.string in
let lua_state = I.mk () in
let index_entry_json = get_index_entry_json env in
let settings = soupault_state.soupault_settings in
let () =
(* Set up the hook environment *)
I.register_globals [
"page_file", lua_str.embed env.page_file;
"page_url", lua_str.embed env.page_url;
"site_index", Plugin_api.lua_of_json (Utils.json_of_index_entries env.site_index);
"index_entry", Plugin_api.lua_of_json index_entry_json;
"target_file", lua_str.embed env.target_file;
"target_dir", lua_str.embed env.target_dir;
"config", lua_of_toml hook_config;
Expand Down Expand Up @@ -370,13 +375,15 @@ let run_lua_index_processor soupault_state index_view_config view_name file_name
let lua_str = I.Value.string in
let table_list = I.Value.list I.Value.table in
let lua_state = I.mk () in
let index_entry_json = get_index_entry_json env in
let settings = soupault_state.soupault_settings in
let () =
(* Set up index processor environment *)
I.register_globals [
"page", Plugin_api.lua_of_soup (Plugin_api.Html.SoupNode soup);
"page_url", lua_str.embed env.page_url;
"site_index", Plugin_api.lua_of_json (Utils.json_of_index_entries env.site_index);
"index_entry", Plugin_api.lua_of_json index_entry_json;
"page_file", lua_str.embed env.page_file;
"target_file", lua_str.embed env.target_file;
"target_dir", lua_str.embed env.target_dir;
Expand Down

0 comments on commit e7f233c

Please sign in to comment.