From 786bad89561f6d2f25fafc47ccb0fb4604faa77a Mon Sep 17 00:00:00 2001 From: Owen Date: Fri, 23 Oct 2026 14:49:45 +0200 Subject: [PATCH] feat: add optional pgn_to_tex_js entry point --- README.md | 40 +++++++++++++++++++++++++++---- bin/dune | 7 +++--- bin/{main.ml => pgn_to_tex_js.ml} | 8 +++++-- dune-project | 6 +++++ pgn_to_tex.opam | 2 ++ test/js/smoke.js | 36 ++++++++++++++++++++++++++++ 6 files changed, 89 insertions(+), 10 deletions(-) rename bin/{main.ml => pgn_to_tex_js.ml} (63%) create mode 100644 test/js/smoke.js diff --git a/README.md b/README.md index 0ba2c69..f2ba197 100644 --- a/README.md +++ b/README.md @@ -111,17 +111,49 @@ game_tex goes here ## Compile a JavaScript version of the library -With the library installed, you can compile the code to JavaScript by running: +The JavaScript build is an optional target. Dune skips it when the JavaScript dependencies are not installed. Install them with: + +``` +opam install js_of_ocaml js_of_ocaml-ppx +``` + +Then compile the code to JavaScript by running: ``` make build ``` -This will generate the JavaScript file at `_build/default/bin/main.bc.js` +or build just the artifact: -This JavaScript file is minified and in CJS format, working in both Node and in the browser. +``` +dune build bin/pgn_to_tex_js.bc.js +``` -A single function `to_tex` is exposed, accepting the same arguments as the main library. +This generates the JavaScript file at `_build/default/bin/pgn_to_tex_js.bc.js`. + +This JavaScript file is minified and in CJS format, working in both Node and the browser. + +A single global `to_tex` function is exposed, accepting the same three arguments as the main library: + +```js +to_tex(pgnString, diagramJsonString, displayClock); +``` + +For example: + +```js +require("./_build/default/bin/pgn_to_tex_js.bc.js"); + +const gameTex = globalThis.to_tex("1. e4 e5 2. Nf3", "{}", false); +``` + +Pass `"{}"` (or an empty string) when there are no diagrams, and `false` to omit clock times. In the browser, the function is available as `window.to_tex` after loading the artifact. + +After building the artifact, you can run the Node smoke test with: + +``` +node test/js/smoke.js +``` ## Dependencies diff --git a/bin/dune b/bin/dune index cdc412a..5a480d9 100644 --- a/bin/dune +++ b/bin/dune @@ -1,8 +1,7 @@ (executable - (name main) + (name pgn_to_tex_js) (modes js) + (optional) (libraries pgn_to_tex js_of_ocaml yojson) (preprocess - (pps js_of_ocaml-ppx)) - (enabled_if - (= %{env:CI=false} "false"))) + (pps js_of_ocaml-ppx))) diff --git a/bin/main.ml b/bin/pgn_to_tex_js.ml similarity index 63% rename from bin/main.ml rename to bin/pgn_to_tex_js.ml index 5914644..ba7e40a 100644 --- a/bin/main.ml +++ b/bin/pgn_to_tex_js.ml @@ -15,5 +15,9 @@ let convert_js pgn_js diagram_json_js display_clock = let result = Pgn_to_tex.to_tex ~diagram_data ~clock:display_clock pgn in Js.string result -(* Exporting the module to the global JavaScript scope *) -let () = Js.export "to_tex" (Js.wrap_callback convert_js) +(* Exporting the module to the global JavaScript scope, including when the + artifact is loaded as a CommonJS module in Node. *) +let () = + let callback = Js.wrap_callback convert_js in + Js.export "to_tex" callback; + Js.Unsafe.set Js.Unsafe.global "to_tex" callback diff --git a/dune-project b/dune-project index 6ce7b7a..61ca3d0 100644 --- a/dune-project +++ b/dune-project @@ -38,6 +38,12 @@ (and :with-test (>= 2.2.7))) + (js_of_ocaml + (and + :with-test)) + (js_of_ocaml-ppx + (and + :with-test)) ("ocamlformat" (and :with-dev-setup))) (tags diff --git a/pgn_to_tex.opam b/pgn_to_tex.opam index a765cfd..ed5c124 100644 --- a/pgn_to_tex.opam +++ b/pgn_to_tex.opam @@ -18,6 +18,8 @@ depends: [ "menhir" {>= "20260209"} "yojson" {>= "3.0"} "ounit2" {with-test & >= "2.2.7"} + "js_of_ocaml" {with-test} + "js_of_ocaml-ppx" {with-test} "ocamlformat" {with-dev-setup} "odoc" {with-doc} ] diff --git a/test/js/smoke.js b/test/js/smoke.js new file mode 100644 index 0000000..f3ee4c6 --- /dev/null +++ b/test/js/smoke.js @@ -0,0 +1,36 @@ +"use strict"; + +const assert = require("node:assert"); +const path = require("node:path"); + +const artifact = path.resolve( + __dirname, + "../../_build/default/bin/pgn_to_tex_js.bc.js", +); + +require(artifact); + +const toTex = globalThis.to_tex; +assert.strictEqual( + typeof toTex, + "function", + "the generated artifact should export a global to_tex function", +); + +const latex = toTex("1. e4 e5 2. Nf3", "{}", false); + +assert.strictEqual(typeof latex, "string", "to_tex should return a string"); +assert.ok( + latex.includes("\\textbf{1.}"), + "rendered LaTeX should contain the first move number", +); +assert.ok( + latex.includes("\\textbf{e4}"), + "rendered LaTeX should contain the first move", +); +assert.ok( + latex.includes("\\textbf{Nf3}"), + "rendered LaTeX should contain the third move", +); + +console.log("js smoke test passed");