Skip to content

Commit 0cc58b3

Browse files
committed
In generator mode, parse page source files as HTML fragments (fixes #58)
1 parent 5ab0478 commit 0cc58b3

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/soupault.ml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,12 @@ let load_html state hooks page_file =
201201
else Ok page_source
202202
| None -> Ok page_source
203203

204-
let parse_html settings page_source =
204+
let parse_html ?(fragment=true) settings page_source =
205205
(* As of lambdasoup 0.7.2, Soup.parse never fails, only returns empty element trees,
206206
so there's no need to handle errors here.
207207
*)
208-
Ok (Html_utils.parse_html ~encoding:settings.page_character_encoding page_source)
208+
let context = if fragment then (`Fragment "body") else `Document in
209+
Ok (Html_utils.parse_html ~context:context ~encoding:settings.page_character_encoding page_source)
209210

210211
(* The built-in HTML rendering function that is used when the "render" hook is not configured. *)
211212
let render_html_builtin settings soup =
@@ -501,7 +502,11 @@ let process_page state page_data index index_hash widgets hooks =
501502
let () = Cache.refresh_page_cache settings page_file content in
502503
Ok content
503504
in
504-
let* content = parse_html settings page_source in
505+
(* If we are in generator mode, tell the parser to interpret pages
506+
as HTML fragments that will go inside <body>,
507+
otherwise treat them as documents. *)
508+
let fragment = if settings.generator_mode then true else false in
509+
let* content = parse_html ~fragment:fragment settings page_source in
505510
let page_name = FP.basename page_file |> FP.chop_extension in
506511
let orig_path = nav_path in
507512
let nav_path = fix_nav_path settings nav_path page_name in

0 commit comments

Comments
 (0)