Skip to content

Commit

Permalink
Add a render hook (related to #36)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmbaturin committed Jan 10, 2022
1 parent a03f2c9 commit 941eb14
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
25 changes: 24 additions & 1 deletion src/hooks.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module I = Plugin_api.I

let lua_of_toml = Plugin_api.lua_of_toml

let hook_types = ["pre-parse"; "pre-process"; "post-index"; "save"]
let hook_types = ["pre-parse"; "pre-process"; "post-index"; "render"; "save"]

let hook_should_run settings hook_config hook_type page_file =
let disabled = Config.find_bool_or ~default:false hook_config ["disabled"] in
Expand All @@ -16,6 +16,29 @@ let hook_should_run settings hook_config hook_type page_file =
hook_type page_file
in false

let run_render_hook settings soupault_config hook_config file_name lua_code env soup =
let open Defaults in
let lua_str = I.Value.string in
let state = I.mk () in
let () =
(* Set up the hook environment *)
I.register_globals ["page", Plugin_api.lua_of_soup (Plugin_api.Html.SoupNode soup)] state;
I.register_globals ["site_index", Plugin_api.lua_of_json (Autoindex.json_of_entries env.site_index)] state;
I.register_globals ["target_file", lua_str.embed env.target_file] state;
I.register_globals ["target_dir", lua_str.embed env.target_dir] state;
I.register_globals ["config", lua_of_toml hook_config] state;
I.register_globals ["hook_config", lua_of_toml hook_config] state;
I.register_globals ["soupault_config", lua_of_toml soupault_config] state;
I.register_globals ["force", I.Value.bool.embed settings.force] state;
I.register_globals ["build_dir", lua_str.embed settings.build_dir] state;
I.register_globals ["site_dir", lua_str.embed settings.site_dir] state;
in
let (let*) = Result.bind in
let* () = Plugin_api.run_lua file_name state lua_code in
let res = I.getglobal state (I.Value.string.embed "page_source") in
if I.Value.string.is res then Ok (I.Value.string.project res)
else Error "render hook has not assigned a string to the page_source variable"

let run_save_hook settings soupault_config hook_config file_name lua_code env page_source =
let open Defaults in
let lua_str = I.Value.string in
Expand Down
24 changes: 22 additions & 2 deletions src/soupault.ml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,20 @@ let load_html settings soupault_config hooks page_file =
(* As of lambdasoup 0.7.2, Soup.parse never fails, only returns empty element trees. *)
Ok (Soup.parse page_source)

let render_html settings soup =
let run_render_hook settings config hooks env soup =
let hook = Hashtbl.find_opt hooks "render" in
match hook with
| Some (file_name, source_code, hook_config) ->
if not (Hooks.hook_should_run settings hook_config "render" env.page_file)
then Ok None
else
let () = Logs.info @@ fun m -> m "Running the \"render\" hook on page %s" env.page_file in
let* page_source = Hooks.run_render_hook
settings config hook_config file_name source_code env soup
in Ok (Some page_source)
| None -> Ok None

let render_html_builtin settings soup =
let print_html = if settings.pretty_print_html then Soup.pretty_print else Soup.to_string in
if settings.keep_doctype then
begin
Expand Down Expand Up @@ -175,6 +188,13 @@ let render_html settings soup =
print_html soup
end

let render_html settings config hooks env soup =
let res = run_render_hook settings config hooks env soup in
match res with
| Ok (Some page_source) -> Ok page_source
| Ok None -> Ok (render_html_builtin settings soup)
| Error _ as e -> e

let include_content action selector html content =
let element = Soup.select_one selector html in
match element with
Expand Down Expand Up @@ -383,7 +403,7 @@ let process_page page_file nav_path index widgets hooks config settings =
if settings.index_only then Ok index_entry else
let* () = process_widgets env settings after_index widget_hash config html in
let* () = mkdir target_dir in
let html_str = render_html settings html in
let* html_str = render_html settings config hooks env html in
let* () = save_html settings config hooks env html_str in
Ok index_entry

Expand Down

0 comments on commit 941eb14

Please sign in to comment.