Skip to content

Commit

Permalink
Add startup hook
Browse files Browse the repository at this point in the history
  • Loading branch information
dmbaturin committed Mar 18, 2024
1 parent 6ff50d2 commit 72d58a9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/hooks.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ let lua_of_json = Plugin_api.lua_of_json
(* Auxilliary functions *)

let hook_types = [
"startup";
"pre-parse";
"pre-process";
"post-index";
Expand Down Expand Up @@ -337,6 +338,43 @@ 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 ()

(** These two hooks are executed only once rather than for every page:
before the first page is processed and after the last page is processed. *)

(* 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.
It only has access to the page source, not an element tree.
It is free to modify the [page_source] variable that contains the page source.
*)

let run_startup_hook soupault_state hooks =
let open Defaults in
let hook = Hashtbl.find_opt hooks "startup" 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;
"site_dir", lua_str.embed settings.site_dir;
"soupault_pass", I.Value.int.embed soupault_state.soupault_pass;
"global_data", lua_of_json !(soupault_state.global_data);
] lua_state
in
let (let*) = Result.bind in
let () = Logs.info @@ fun m -> m "Running the startup hook" in
let* () = Plugin_api.run_lua lua_state file_name source_code in
let () = soupault_state.global_data := (Plugin_api.extract_global_data lua_state) in
Ok()

let run_post_build_hook soupault_state site_index hooks =
let open Defaults in
let hook = Hashtbl.find_opt hooks "post-build" in
Expand Down
1 change: 1 addition & 0 deletions src/soupault.ml
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,7 @@ let main cli_options =
then Logs.info @@ fun m -> m "Running with build profiles: %s" (String.concat ", " settings.build_profiles)
in
let* () = make_build_dir settings.build_dir in
let* () = Hooks.run_startup_hook state hooks in
let (page_files, index_files, asset_files) = Site_dir.get_site_files settings in
(* If settings.process_pages_first is set, extract those pages and move them to the head of the list.
For an empty list it would return the original list, but it would require traversing that list twice,
Expand Down

0 comments on commit 72d58a9

Please sign in to comment.