diff --git a/src/app/dune b/src/app/dune index f82e580e3..e48a6d160 100644 --- a/src/app/dune +++ b/src/app/dune @@ -3,10 +3,12 @@ (wrapped false) (flags :standard -warn-error -9-27-32) (modules Learnocaml_local_storage + Learnocaml_config Server_caller Learnocaml_common) (preprocess (per_module ((pps js_of_ocaml.ppx) + Learnocaml_config Learnocaml_local_storage Server_caller) ((pps ppx_ocplib_i18n js_of_ocaml.ppx) diff --git a/src/app/learnocaml_common.ml b/src/app/learnocaml_common.ml index 11cabbaab..2c983fd52 100644 --- a/src/app/learnocaml_common.ml +++ b/src/app/learnocaml_common.ml @@ -9,6 +9,7 @@ open Js_utils open Lwt.Infix open Learnocaml_data +open Learnocaml_config module H = Tyxml_js.Html @@ -171,7 +172,7 @@ let show_loading ?(id = "ocp-loading-layer") contents f = Manip.(removeClass elt "loaded") ; Manip.(addClass elt "loading") ; let chamo_src = - "/icons/tryocaml_loading_" ^ string_of_int (Random.int 9 + 1) ^ ".gif" in + api_server ^ "/icons/tryocaml_loading_" ^ string_of_int (Random.int 9 + 1) ^ ".gif" in Manip.replaceChildren elt H.[ div ~a: [ a_id "chamo" ] [ img ~alt: "loading" ~src: chamo_src () ] ; @@ -286,7 +287,7 @@ let button ~container ~theme ?group ?state ~icon lbl cb = | Some group -> group in let button = H.(button [ - img ~alt:"" ~src:("/icons/icon_" ^ icon ^ "_" ^ theme ^ ".svg") () ; + img ~alt:"" ~src:(api_server ^ "/icons/icon_" ^ icon ^ "_" ^ theme ^ ".svg") () ; pcdata " " ; span ~a:[ a_class [ "label" ] ] [ pcdata lbl ] ]) in @@ -537,13 +538,13 @@ let stars_div stars = let num = 5 * int_of_float (stars *. 2.) in let num = max (min num 40) 0 in let alt = Format.asprintf [%if"difficulty: %d / 40"] num in - let src = Format.asprintf "/icons/stars_%02d.svg" num in + let src = Format.asprintf "%s/icons/stars_%02d.svg" api_server num in H.img ~alt ~src () ] let exercise_text ex_meta exo = let mathjax_url = - "/js/mathjax/MathJax.js?delayStartupUntil=configured" + api_server ^ "/js/mathjax/MathJax.js?delayStartupUntil=configured" in let mathjax_config = "MathJax.Hub.Config({\n\ @@ -578,7 +579,7 @@ let exercise_text ex_meta exo = \ %s - exercise text\ \ - \ + \ \ \ @@ -588,6 +589,7 @@ let exercise_text ex_meta exo = \ " ex_meta.Exercise.Meta.title + api_server mathjax_config mathjax_url descr @@ -1023,7 +1025,7 @@ module Display_exercise = let num = 5 * int_of_float (ex_meta.Meta.stars *. 2.) in let num = max (min num 40) 0 in let alt = Format.asprintf [%if"difficulty: %d / 40"] num in - let src = Format.asprintf "/icons/stars_%02d.svg" num in + let src = Format.asprintf "%s/icons/stars_%02d.svg" api_server num in img ~alt ~src () in div ~a:[ a_class [ "stars" ] ] [ diff --git a/src/app/learnocaml_config.ml b/src/app/learnocaml_config.ml new file mode 100644 index 000000000..89914f87a --- /dev/null +++ b/src/app/learnocaml_config.ml @@ -0,0 +1,20 @@ +(* This file is part of Learn-OCaml + * + * Copyright (C) 2020 Alban Gruin. + * + * Learn-OCaml is distributed under the terms of the MIT license. See the + * included LICENSE file for details. *) + +class type learnocaml_config = object + method enableTryocaml: bool Js.optdef_prop + method enableLessons: bool Js.optdef_prop + method enableExercises: bool Js.optdef_prop + method enableToplevel: bool Js.optdef_prop + method enablePlayground: bool Js.optdef_prop + method txtLoginWelcome: Js.js_string Js.t Js.optdef_prop + method txtNickname: Js.js_string Js.t Js.optdef_prop + method root: Js.js_string Js.t Js.optdef_prop +end + +let config : learnocaml_config Js.t = Js.Unsafe.js_expr "learnocaml_config" +let api_server = Js.(to_string (Optdef.get config##.root (fun () -> string ""))) diff --git a/src/app/learnocaml_config.mli b/src/app/learnocaml_config.mli new file mode 100644 index 000000000..8950e1227 --- /dev/null +++ b/src/app/learnocaml_config.mli @@ -0,0 +1,24 @@ +(* This file is part of Learn-OCaml + * + * Copyright (C) 2020 Alban Gruin. + * + * Learn-OCaml is distributed under the terms of the MIT license. See the + * included LICENSE file for details. *) + +(* This is not transpiled to learnocaml-static.js, but is an interface + to the values stored in this file. It is "statically linked" with + learnocaml-common.ml. *) + +class type learnocaml_config = object + method enableTryocaml: bool Js.optdef_prop + method enableLessons: bool Js.optdef_prop + method enableExercises: bool Js.optdef_prop + method enableToplevel: bool Js.optdef_prop + method enablePlayground: bool Js.optdef_prop + method txtLoginWelcome: Js.js_string Js.t Js.optdef_prop + method txtNickname: Js.js_string Js.t Js.optdef_prop + method root: Js.js_string Js.t Js.optdef_prop +end + +val config : learnocaml_config Js.t +val api_server : string diff --git a/src/app/learnocaml_index_main.ml b/src/app/learnocaml_index_main.ml index 29ffd3fb4..fe289d576 100644 --- a/src/app/learnocaml_index_main.ml +++ b/src/app/learnocaml_index_main.ml @@ -10,6 +10,7 @@ open Js_utils open Lwt open Learnocaml_data open Learnocaml_common +open Learnocaml_config module H = Tyxml_js.Html5 @@ -63,7 +64,7 @@ let show_loading msg = show_loading ~id:El.loading_id H.[ul [li [pcdata msg]]] let get_url token dynamic_url static_url id = match token with | Some _ -> dynamic_url ^ Url.urlencode id ^ "/" - | None -> static_url ^ Url.urlencode id + | None -> api_server ^ "/" ^ static_url ^ Url.urlencode id let exercises_tab token _ _ () = show_loading [%i"Loading exercises"] @@ fun () -> @@ -111,7 +112,7 @@ let exercises_tab token _ _ () = | Some pct when pct >= 100 -> [ "stats" ; "success" ] | Some _ -> [ "stats" ; "partial" ]) pct_signal in - a ~a:[ a_href (get_url token "/exercises/" "/exercise.html#id=" exercise_id) ; + a ~a:[ a_href (get_url token "/exercises/" "exercise.html#id=" exercise_id) ; a_class [ "exercise" ] ] [ div ~a:[ a_class [ "descr" ] ] ( h1 [ pcdata title ] :: @@ -163,7 +164,7 @@ let playground_tab token _ _ () = let open Tyxml_js.Html5 in let title = pmeta.Playground.Meta.title in let short_description = pmeta.Playground.Meta.short_description in - a ~a:[ a_href (get_url token "/playground/" "/playground.html#id=" id) ; + a ~a:[ a_href (get_url token "/playground/" "playground.html#id=" id) ; a_class [ "exercise" ] ] [ div ~a:[ a_class [ "descr" ] ] ( h1 [ pcdata title ] :: @@ -606,18 +607,6 @@ let init_sync_token button_group = Lwt.return (Some token)) (fun _ -> Lwt.return None) -class type learnocaml_config = object - method enableTryocaml: bool Js.optdef_prop - method enableLessons: bool Js.optdef_prop - method enableExercises: bool Js.optdef_prop - method enableToplevel: bool Js.optdef_prop - method enablePlayground: bool Js.optdef_prop - method txtLoginWelcome: Js.js_string Js.t Js.optdef_prop - method txtNickname: Js.js_string Js.t Js.optdef_prop -end - -let config : learnocaml_config Js.t = Js.Unsafe.js_expr "learnocaml_config" - let set_string_translations () = let configured v s = Js.Optdef.case v (fun () -> s) Js.to_string in let translations = [ diff --git a/src/app/server_caller.ml b/src/app/server_caller.ml index 1a162d879..bb2a5ffb7 100644 --- a/src/app/server_caller.ml +++ b/src/app/server_caller.ml @@ -71,9 +71,8 @@ let () = (Printexc.to_string e)) | _ -> None -let urlpath = - let api_server = "" in - fun p -> String.concat "/" (api_server::p) +let urlpath p = + String.concat "/" (Learnocaml_config.api_server :: p) let request req = let do_req = function diff --git a/static/description.html b/static/description.html index ea5880bdd..bc56aab26 100644 --- a/static/description.html +++ b/static/description.html @@ -12,6 +12,7 @@ + diff --git a/static/exercise.html b/static/exercise.html index dd5024331..4f7fa580a 100644 --- a/static/exercise.html +++ b/static/exercise.html @@ -11,6 +11,7 @@ + @@ -32,7 +33,7 @@
diff --git a/static/partition-view.html b/static/partition-view.html index 688a87515..beda4f4d1 100644 --- a/static/partition-view.html +++ b/static/partition-view.html @@ -11,6 +11,7 @@ + diff --git a/static/playground.html b/static/playground.html index 059f95f45..fe7c7923c 100644 --- a/static/playground.html +++ b/static/playground.html @@ -10,6 +10,7 @@ + @@ -31,7 +32,7 @@
diff --git a/static/student-view.html b/static/student-view.html index 0ff06d6c5..e274a1172 100644 --- a/static/student-view.html +++ b/static/student-view.html @@ -11,6 +11,7 @@ +