@@ -47,6 +47,15 @@ module Args = struct
4747 " Directory where the app should be generated for the $(i,build) command, \
4848 and from where it is served by the $(i,serve) command."
4949
50+ let base_url =
51+ value & opt string " " &
52+ info [" base-url" ] ~docv: " BASE_URL" ~env: (Arg. env_var " LEARNOCAML_BASE_URL" ) ~doc:
53+ " Set the base URL of the website. \
54+ Should not end with a trailing slash. \
55+ Currently, this has no effect on the backend - '$(b,learn-ocaml serve)'. \
56+ Mandatory for '$(b,learn-ocaml build)' if the site is not hosted in path '/', \
57+ which typically occurs for static deployment."
58+
5059 module Grader = struct
5160 let info = info ~docs: " GRADER OPTIONS"
5261
@@ -180,27 +189,22 @@ module Args = struct
180189 value & opt int 1 & info [" jobs" ;" j" ] ~docv: " INT" ~doc:
181190 " Number of building jobs to run in parallel"
182191
183- let root =
184- value & opt string " " & info [" root" ] ~docv: " ROOT" ~doc:
185- " Set the root of all documents. Use only for static deployment.\
186- Should not end with a trailing slash."
187-
188192 type t = {
189193 contents_dir : string ;
190194 try_ocaml : bool option ;
191195 lessons : bool option ;
192196 exercises : bool option ;
193197 playground : bool option ;
194198 toplevel : bool option ;
195- root : string
199+ base_url : string
196200 }
197201
198202 let builder_conf =
199203 let apply
200- contents_dir try_ocaml lessons exercises playground toplevel root
201- = { contents_dir; try_ocaml; lessons; exercises; playground; toplevel; root }
204+ contents_dir try_ocaml lessons exercises playground toplevel base_url
205+ = { contents_dir; try_ocaml; lessons; exercises; playground; toplevel; base_url }
202206 in
203- Term. (const apply $ contents_dir $ try_ocaml $ lessons $ exercises $ playground $ toplevel $ root )
207+ Term. (const apply $ contents_dir $ try_ocaml $ lessons $ exercises $ playground $ toplevel $ base_url )
204208
205209 let repo_conf =
206210 let apply repo_dir exercises_filtered jobs =
@@ -241,16 +245,16 @@ module Args = struct
241245 { commands; app_dir; repo_dir; grader; builder; server }
242246 in
243247 Term. (const apply $ commands $ app_dir $ repo_dir
244- $ Grader. term $ Builder. term $ Server. term app_dir)
248+ $ Grader. term $ Builder. term $ Server. term app_dir base_url )
245249end
246250
247251open Args
248252
249- let process_html_file orig_file dest_file root =
253+ let process_html_file orig_file dest_file base_url =
250254 let transform_tag e tag attrs attr =
251255 let attr_pair = (" " , attr) in
252256 match List. assoc_opt attr_pair attrs with
253- | Some url -> `Start_element ((e, tag), (attr_pair, root ^ url) :: (List. remove_assoc attr_pair attrs))
257+ | Some url -> `Start_element ((e, tag), (attr_pair, base_url ^ url) :: (List. remove_assoc attr_pair attrs))
254258 | None -> `Start_element ((e, tag), attrs) in
255259 Lwt_io. open_file ~mode: Lwt_io. Input orig_file >> = fun ofile ->
256260 Lwt_io. open_file ~mode: Lwt_io. Output dest_file >> = fun wfile ->
@@ -322,11 +326,13 @@ let main o =
322326 let json_config = ServerData. build_config preconfig in
323327 Learnocaml_store. write_to_file ServerData. config_enc json_config www_server_config
324328 >> = fun () ->
329+ if o.builder.Builder. base_url <> " " then
330+ Printf. printf " Base URL: %s\n %!" o.builder.Builder. base_url;
325331 Lwt_unix. files_of_directory o.builder.Builder. contents_dir
326332 |> Lwt_stream. iter_s (fun file ->
327333 if Filename. extension file = " .html" then
328334 process_html_file (o.builder.Builder. contents_dir/ file)
329- (o.app_dir/ file) o.builder.Builder. root
335+ (o.app_dir/ file) o.builder.Builder. base_url
330336 else
331337 Lwt. return_unit) >> = fun () ->
332338 let if_enabled opt dir f = (match opt with
@@ -363,14 +369,14 @@ let main o =
363369 \ enableLessons: %b,\n \
364370 \ enableExercises: %b,\n \
365371 \ enableToplevel: %b,\n \
366- \ root : \" %s\"\n \
372+ \ baseUrl : \" %s\"\n \
367373 }\n "
368374 (tutorials_ret <> None )
369375 (playground_ret <> None )
370376 (lessons_ret <> None )
371377 (exercises_ret <> None )
372378 (o.builder.Builder. toplevel <> Some false )
373- o.builder.Builder. root >> = fun () ->
379+ o.builder.Builder. base_url >> = fun () ->
374380 Lwt. return (tutorials_ret <> Some false && exercises_ret <> Some false )))
375381 else
376382 Lwt. return true
@@ -383,14 +389,18 @@ let main o =
383389 let open Server in
384390 (" --app-dir=" ^ o.app_dir) ::
385391 (" --sync-dir=" ^ o.server.sync_dir) ::
392+ (" --base-url=" ^ o.builder.Builder. base_url) ::
386393 (" --port=" ^ string_of_int o.server.port) ::
387394 (match o.server.cert with None -> [] | Some c -> [" --cert=" ^ c])
388395 in
389396 Unix. execv native_server (Array. of_list (native_server::server_args))
390- else
391- Printf. printf " Starting server on port %d\n %!"
392- ! Learnocaml_server. port;
393- Learnocaml_server. launch ()
397+ else begin
398+ Printf. printf " Starting server on port %d\n %!"
399+ ! Learnocaml_server. port;
400+ if o.builder.Builder. base_url <> " " then
401+ Printf. printf " Base URL: %s\n %!" o.builder.Builder. base_url;
402+ Learnocaml_server. launch ()
403+ end
394404 else
395405 Lwt. return true
396406 in
0 commit comments