mirror of
https://github.com/TheRealOwenRees/pgn_to_tex.git
synced 2026-11-18 12:52:06 +00:00
feat: add optional pgn_to_tex_js entry point
This commit is contained in:
@@ -111,17 +111,49 @@ game_tex goes here
|
|||||||
|
|
||||||
## Compile a JavaScript version of the library
|
## 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
|
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
|
## Dependencies
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
(executable
|
(executable
|
||||||
(name main)
|
(name pgn_to_tex_js)
|
||||||
(modes js)
|
(modes js)
|
||||||
|
(optional)
|
||||||
(libraries pgn_to_tex js_of_ocaml yojson)
|
(libraries pgn_to_tex js_of_ocaml yojson)
|
||||||
(preprocess
|
(preprocess
|
||||||
(pps js_of_ocaml-ppx))
|
(pps js_of_ocaml-ppx)))
|
||||||
(enabled_if
|
|
||||||
(= %{env:CI=false} "false")))
|
|
||||||
|
|||||||
@@ -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
|
let result = Pgn_to_tex.to_tex ~diagram_data ~clock:display_clock pgn in
|
||||||
Js.string result
|
Js.string result
|
||||||
|
|
||||||
(* Exporting the module to the global JavaScript scope *)
|
(* Exporting the module to the global JavaScript scope, including when the
|
||||||
let () = Js.export "to_tex" (Js.wrap_callback convert_js)
|
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
|
||||||
@@ -38,6 +38,12 @@
|
|||||||
(and
|
(and
|
||||||
:with-test
|
:with-test
|
||||||
(>= 2.2.7)))
|
(>= 2.2.7)))
|
||||||
|
(js_of_ocaml
|
||||||
|
(and
|
||||||
|
:with-test))
|
||||||
|
(js_of_ocaml-ppx
|
||||||
|
(and
|
||||||
|
:with-test))
|
||||||
("ocamlformat"
|
("ocamlformat"
|
||||||
(and :with-dev-setup)))
|
(and :with-dev-setup)))
|
||||||
(tags
|
(tags
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ depends: [
|
|||||||
"menhir" {>= "20260209"}
|
"menhir" {>= "20260209"}
|
||||||
"yojson" {>= "3.0"}
|
"yojson" {>= "3.0"}
|
||||||
"ounit2" {with-test & >= "2.2.7"}
|
"ounit2" {with-test & >= "2.2.7"}
|
||||||
|
"js_of_ocaml" {with-test}
|
||||||
|
"js_of_ocaml-ppx" {with-test}
|
||||||
"ocamlformat" {with-dev-setup}
|
"ocamlformat" {with-dev-setup}
|
||||||
"odoc" {with-doc}
|
"odoc" {with-doc}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -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");
|
||||||
Reference in New Issue
Block a user