Skip to content

Commit

Permalink
Add post-build hook
Browse files Browse the repository at this point in the history
  • Loading branch information
dmbaturin committed Sep 16, 2023
1 parent 4743e85 commit f24d816
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/hooks.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ let hook_types = [
"render";
"save";
"post-save";
"post-build";
]

(* Checks if the user didn't try to add hooks of non-existent types.
Expand Down Expand Up @@ -316,6 +317,35 @@ let run_post_save_hook soupault_state hook_config file_name lua_code env =
let () = soupault_state.global_data := (Plugin_api.extract_global_data lua_state) in
Ok ()

let run_post_build_hook soupault_state hooks =
let open Defaults in
let hook = Hashtbl.find_opt hooks "post-build" in
match hook with
| None -> Ok ()
| Some (file_name, source_code, hook_config) ->
let lua_str = I.Value.string in
let lua_state = I.mk () in
let settings = soupault_state.soupault_settings in
let () =
(* Set up the hook environment *)
I.register_globals [
"config", lua_of_toml hook_config;
"hook_config", lua_of_toml hook_config;
"soupault_config", lua_of_toml soupault_state.soupault_config;
"force", I.Value.bool.embed settings.force;
"build_dir", lua_str.embed settings.build_dir;
"site_dir", lua_str.embed settings.site_dir;
"global_data", lua_of_json !(soupault_state.global_data);
] lua_state
in
let () = Logs.info @@ fun m -> m "Running the post-build hook" in
(* Since this hook runs just before soupault finishes its work and exits,
there's no reason to update the global data variable --
there's no plugin code to run that could use it at that point.
*)
Plugin_api.run_lua lua_state file_name source_code


(* Lua index processors aren't actually hooks but their execution process is similar. *)

let run_lua_index_processor soupault_state index_view_config view_name file_name lua_code env soup =
Expand Down
2 changes: 2 additions & 0 deletions src/soupault.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,7 @@ let main cli_options =
let () = if new_pages <> [] then Logs.info @@ fun m -> m "Processing pages generated by indexer scripts" in
let state = {state with soupault_settings={state.soupault_settings with index=false}} in
let* () = Utils.iter_result (process_page state index index_hash widgets hooks) new_pages in
let* () = Hooks.run_post_build_hook state hooks in
(* Finally, dump the index file, if requested. *)
let* () = dump_index_json settings index in
Ok ()
Expand Down Expand Up @@ -1056,6 +1057,7 @@ let main cli_options =
let state = {state with soupault_settings={state.soupault_settings with index=false}} in
let () = if new_pages <> [] then Logs.info @@ fun m -> m "Processing pages generated by indexer scripts" in
let* () = Utils.iter_result (process_page state index index_hash widgets hooks) new_pages in
let* () = Hooks.run_post_build_hook state hooks in
(* Finally, dump the index file, if requested. *)
let* () = dump_index_json settings index in
Ok ()
Expand Down

0 comments on commit f24d816

Please sign in to comment.