18 Commits
Author SHA1 Message Date
owenrees dfa7d5bc7e chore: configure agent skills and issue tracker (#15) 2026-10-23 16:48:39 +02:00
owenrees c654beb5a8 ci: disable opam cache for reliable setup 2026-10-23 16:38:24 +02:00
owenrees 4d20084f7e ci: avoid duplicate push and pull request runs 2026-10-23 16:38:24 +02:00
owenrees 16fcaa3b13 fix: address code review findings for public API 2026-10-23 16:38:24 +02:00
owenrees 4f50ff020e docs: prepare 0.1.0 release metadata and migration 2026-10-23 16:38:24 +02:00
owenrees 2bf11e92a8 ci: add CI and GitHub Pages documentation workflows 2026-10-23 16:38:24 +02:00
owenrees 786bad8956 feat: add optional pgn_to_tex_js entry point 2026-10-23 16:38:24 +02:00
owenrees 135491e915 cover real-world headers, NAGs, and fixtures 2026-10-23 16:38:24 +02:00
owenrees 9cd7878ad8 accept path-only diagram entries 2026-10-23 16:38:24 +02:00
owenrees 548f98f303 feat: add public Pgn_to_tex entry point
Move parsing and rendering internals into the private
pgn_to_tex_internal library and rename pgn2tex to converter. Expose only
Pgn_to_tex with default to_tex arguments, abstract diagram_data,
empty_diagrams, and parse_diagrams_json. Update tests, JS bridge, and
README to the new names.
2026-10-23 16:38:24 +02:00
owenrees 8df30f1746 run each OUnit suite only from test_suite 2026-10-23 16:38:24 +02:00
owenrees 79eda1beab add directive to skip or accept failures in ocaml versions < 5.1.0 2026-10-18 21:05:11 +02:00
owenrees c500b1625d update readme to account for variations 2026-10-18 20:26:00 +02:00
owenrees fe9eb8ba45 Feature/variations (#5)
* support diagrams in variations.

* Diagram data now carries an optional `path` (the SAN move sequence from the root) alongside the legacy `ply`; diagrams whose `path` resolves inside a variation are rendered inline within the variation. `parse_diagrams_json` returns a `diagram_data` record with `by_ply` and `by_path` maps.

* make variation boards slightly smaller than the mainline boards

* update changelog v0.0.2
2026-10-18 20:06:43 +02:00
owenrees 5831ddfb5e update release info and docs link 2026-06-29 21:26:20 +02:00
owenrees 5f0521430d update release info and docs link 2026-06-29 21:23:34 +02:00
owenrees a58a6504b4 V0.0.1 (#4)
* update documentation link

* remove superfluous tests

* add game example with subtitle custom header

* remove commented out code

* fix subtitle by making it a new line of smaller text within the title

* update changelog with v0.0.1
2026-06-29 21:18:10 +02:00
owenrees b4816553c7 update rc.2 version in meta data 2026-05-28 22:31:49 +02:00
47 changed files with 1500 additions and 380 deletions
+52
View File
@@ -0,0 +1,52 @@
name: CI
on:
push:
branches:
- main
pull_request:
permissions:
contents: read
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true
jobs:
build:
name: Format, build, test, and document
runs-on: ubuntu-latest
steps:
- name: Check out the repository
uses: actions/checkout@v4
- name: Set up OCaml
uses: ocaml/setup-ocaml@v3
with:
ocaml-compiler: 5.4.x
cache: false
- name: Install dependencies
run: opam install . --deps-only --with-test --with-doc --with-dev-setup
- name: Check formatting
run: opam exec -- dune build @fmt
- name: Build
run: opam exec -- dune build @install
- name: Run tests
run: opam exec -- dune runtest
- name: Build documentation
run: opam exec -- dune build @doc
- name: Build the JavaScript artifact
run: opam exec -- dune build bin/pgn_to_tex_js.bc.js
- name: Run the Node smoke test
run: node test/js/smoke.js
- name: Lint the opam metadata
run: opam lint pgn_to_tex.opam
+59
View File
@@ -0,0 +1,59 @@
name: Documentation
on:
push:
branches:
- main
workflow_dispatch:
permissions:
contents: read
pages: write
id-token: write
concurrency:
group: pages
cancel-in-progress: false
jobs:
build:
name: Build odoc
runs-on: ubuntu-latest
steps:
- name: Check out the repository
uses: actions/checkout@v4
- name: Set up OCaml
uses: ocaml/setup-ocaml@v3
with:
ocaml-compiler: 5.4.x
cache: false
- name: Install documentation dependencies
run: opam install . --deps-only --with-doc
- name: Build documentation
run: opam exec -- dune build @doc
- name: Disable Jekyll processing
run: touch _build/default/_doc/_html/.nojekyll
- name: Configure GitHub Pages
uses: actions/configure-pages@v5
- name: Upload the documentation artifact
uses: actions/upload-pages-artifact@v3
with:
path: _build/default/_doc/_html
deploy:
name: Deploy to GitHub Pages
needs: build
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
+1 -1
View File
@@ -1,2 +1,2 @@
profile = default
version = 0.28.1
version = 0.29.0
+13
View File
@@ -0,0 +1,13 @@
## Agent skills
### Issue tracker
Issues live in GitHub Issues for TheRealOwenRees/pgn_to_tex and are managed with `gh`. See `docs/agents/issue-tracker.md`.
### Triage labels
The five canonical triage roles map directly to `needs-triage`, `needs-info`, `ready-for-agent`, `ready-for-human`, and `wontfix`. See `docs/agents/triage-labels.md`.
### Domain docs
Single-context: `GLOSSARY.md` and `docs/adr/` at the repo root. See `docs/agents/domain.md`.
+78
View File
@@ -1,3 +1,81 @@
## 0.1.0
### Breaking changes
- Renamed the public entry point from `Pgn_logic` / `Pgn2tex` to
`Pgn_to_tex`. The old modules are removed with no compatibility aliases.
- `Pgn_to_tex.to_tex` takes optional `~diagram_data` and `~clock` arguments
before the PGN string, and defaults to no diagrams and no clock output.
- Removed the internal parser, AST, LaTeX, and NAG modules from the public
surface. `diagram_data` is now an abstract type.
- Renamed the optional JavaScript artifact from `main.bc.js` to
`pgn_to_tex_js.bc.js`.
### Added
- New public API: `Pgn_to_tex.to_tex`, `Pgn_to_tex.parse_diagrams_json`,
`Pgn_to_tex.empty_diagrams`, and the abstract `diagram_data` type.
- Diagram JSON accepts `path`, `ply`, or both. Paths identify mainline and
variation positions; path lookup takes precedence over the legacy ply
fallback, and path-only variation entries are supported.
- Diagrams inside variations render inline at the smaller `smallboard` size.
- Generated odoc API documentation, a package landing page, and a GitHub Pages
deployment workflow.
- GitHub Actions CI covering formatting, build, tests, docs, the JavaScript
build, the Node smoke test, and opam lint.
- A self-contained README covering installation, the API, diagram JSON,
clocks, variations, headers, NAGs, errors, the LaTeX preamble, and JavaScript
usage.
- `js_of_ocaml` and `js_of_ocaml-ppx` are declared as test-only dependencies.
### Fixed
- A mixed `Author` and player header no longer produces an empty author line.
- A `Title` without a `Subtitle` no longer emits an empty subtitle block.
### Changed
- The package version is now `0.1.0`.
- Documentation now points at
<https://therealowenrees.github.io/pgn_to_tex/>.
- Removed stale CI-acceptance metadata for unsupported OCaml versions.
### Migration from 0.0.x
Replace the old import and module prefix:
```ocaml
(* 0.0.x *)
open Pgn_logic
let diagrams = Pgn2tex.parse_diagrams_json json
let tex = Pgn2tex.to_tex pgn ~diagram_data:diagrams ~clock:false
(* 0.1.0 *)
let diagrams = Pgn_to_tex.parse_diagrams_json json
let tex = Pgn_to_tex.to_tex ~diagram_data:diagrams ~clock:false pgn
```
Use `Pgn_to_tex.empty_diagrams` when a game has no diagrams. Diagram data is
now abstract, so replace any hand-built `MoveMap.t` with
`Pgn_to_tex.parse_diagrams_json`. The JavaScript `to_tex` function keeps the
same three arguments; only the artifact name changed.
## 0.0.2
- support diagrams in variations. Diagram data now carries an optional `path` (the SAN move sequence from the root) alongside the legacy `ply`; diagrams whose `path` resolves inside a variation are rendered inline within the variation. `parse_diagrams_json` returns a `diagram_data` record with `by_ply` and `by_path` maps.
- make variation board diagrams slightly smaller than mainline board diagrams
## v0.0.1
- fix bug whereby a `subtitle` custom PGN header was rendering on a separate page. It now appears under the title
- remove old tests and commented out code
## v0.0.1-rc.2
- add correct versioning metadata
## v0.0.1-rc.1
- Update readme and add license file
+59 -4
View File
@@ -1,13 +1,68 @@
# Contributing
When contributing to this repository, please first discuss the change you wish to make via the repository's issues.
When contributing to this repository, please first discuss the change you wish
to make via the repository's issues.
If you are wanting to tackle one of the pre-existing issues, please comment on that issue so that you can be assigned to it.
If you are wanting to tackle one of the pre-existing issues, please comment on
that issue so that you can be assigned to it.
Alternatively, just send a PR and I will check it asap. Please bear in mind that I may not be reactive if I was not expecting a PR.
Alternatively, just send a PR and I will check it asap. Please bear in mind that
I may not be reactive if I was not expecting a PR.
## Guidelines
- We use the [GitHub Flow](https://www.gitkraken.com/learn/git/best-practices/git-branch-strategy#github-flow-considerations) branching strategy. Add your code into a feature branch such as `feature/feature-name`.
- Write good commit messages. 'Fix bug' is not a good commit message. 'Fix bug with comments before moves' is a good commit message.
- Explain the changes in the PR comments
- Explain the changes in the PR comments.
## Development setup
The package requires OCaml 5.4 or newer. From a checkout, install all build,
test, documentation, and development dependencies:
```sh
opam install . --deps-only --with-test --with-doc --with-dev-setup
```
The JavaScript build is an optional target. Its dependencies are part of the
`--with-test` set (`js_of_ocaml` and `js_of_ocaml-ppx`), so the command above
also prepares the Node smoke test.
## Verification commands
Run these before opening or updating a pull request.
Check formatting, build the package, run the test suite, and build the API
documentation:
```sh
dune build @fmt @install @runtest @doc
```
`@fmt` checks formatting with `ocamlformat` 0.29.0. If it reports a diff, apply
the formatting with `dune fmt` (or `dune build @fmt --auto-promote`).
Build the optional JavaScript artifact:
```sh
dune build bin/pgn_to_tex_js.bc.js
```
Run the Node smoke test against that artifact:
```sh
node test/js/smoke.js
```
Lint the generated opam metadata:
```sh
opam lint pgn_to_tex.opam
```
The opam file is generated from `dune-project`, so change dependencies and
package metadata there rather than editing `pgn_to_tex.opam` by hand.
The test suite has one OUnit runner, `test/test_suite.ml`. Add a suite to that
runner instead of starting a second test executable. Bundled PGN fixtures live
in `test/pgn_examples/` and are exercised by the real-world tests.
+290 -29
View File
@@ -1,55 +1,316 @@
# Pgn to Tex
# Pgn_to_tex
## Install
`Pgn_to_tex` converts chess games written in PGN to LaTeX. It renders move
numbers, moves, comments, variations, NAG annotations, clock times, and
optional chessboard diagrams of selected positions.
- Install the Opam package with:
`opam install pgn_to_tex`
- Reference `pgn_to_tex` in the libraries stanza in the relevant dune file
- [API documentation](https://therealowenrees.github.io/pgn_to_tex/)
- [Changelog](CHANGELOG.md)
- [Contributing](CONTRIBUTING.md)
## Usage
## Requirements
- OCaml 5.4 or newer
- dune
- A LaTeX installation with `xskak` and `multicol` (see
[LaTeX setup](#latex-setup))
## Installation
Install the released package with opam:
```sh
opam install pgn_to_tex
```
To use the current repository instead, clone it and pin the checkout:
```sh
git clone https://github.com/TheRealOwenRees/pgn_to_tex.git
cd pgn_to_tex
opam install .
```
Then add the library to the relevant `dune` file:
```lisp
(libraries pgn_to_tex)
```
## Quick start
Open `Pgn_to_tex` and call `to_tex` with a PGN string:
```ocaml
open Pgn_logic
let pgn = "1. e4 e5 2. Nf3 Nc6 3. Bb5"
let diagrams_json_stringified = {| [{"ply":1,"fen":"rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b KQkq - 0 1"},{"ply":2,"fen":"rnbqkbnr/ppp1pppp/8/3p4/3P4/8/PPP1PPPP/RNBQKBNR w KQkq - 0 2"}] |}
let diagram_data = Pgn2tex.parse_diagrams_json diagrams_json_stringified
let game_tex = Pgn2tex.to_tex pgn ~diagram_data ~clock:false
let tex = Pgn_to_tex.to_tex pgn
```
This will return a LaTex markdown representation of the PGN file along with chess board diagrams at the specified ply. Clock times can be rendered for both sides if you pass `~clock:true` to the `to_tex` function.
`to_tex` returns a LaTeX fragment without a preamble or `\begin{document}`.
`~diagram_data` defaults to no diagrams and `~clock` defaults to `false`, so
this basic call needs no other arguments.
Latex preample and document setup markdown is not included in the output. You will need to add something similar to below around the `game_tex` output to create a valid document:
To render diagrams, describe the selected positions as JSON and parse it with
`parse_diagrams_json`:
```ocaml
let diagrams =
Pgn_to_tex.parse_diagrams_json
{| [
{"fen": "rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b KQkq - 0 1",
"path": ["d4"]},
{"fen": "rnbqkbnr/ppp1pppp/8/3p4/3P4/8/PPP1PPPP/RNBQKBNR w KQkq - 0 2",
"ply": 2}
] |}
let tex = Pgn_to_tex.to_tex ~diagram_data:diagrams pgn
```
To explicitly request a game with no diagrams, pass
`~diagram_data:Pgn_to_tex.empty_diagrams`.
`diagram_data` is abstract. Construct it with `parse_diagrams_json` or
`empty_diagrams` instead of building the internal representation directly.
## Diagram JSON schema
`parse_diagrams_json` accepts a JSON array. Each entry is an object:
```json
[
{"fen": "8/8/8/8/8/8/4K3/7k w - - 0 1", "path": ["d4", "d5", "e4", "d6"]},
{"fen": "8/8/8/8/8/8/4K3/7k w - - 0 1", "ply": 5},
{"fen": "8/8/8/8/8/8/4K3/7k w - - 0 1", "ply": 9, "path": ["e4", "e5", "Nf3", "Nc6"]}
]
```
| Field | Type | Required | Description |
| ------ | --------------- | --------------- | ----------- |
| `fen` | string | yes | The board position to render. |
| `path` | array of string | one of path/ply | The SAN move sequence from the start of the game, including moves inside a variation (for example `["d4","d5","e4","d6"]`). It uniquely identifies mainline and variation positions. |
| `ply` | integer | one of path/ply | Legacy half-move count from the start of the game: 1 after White's first move. It is used for mainline positions. |
Resolution rules:
- An entry may carry `path`, `ply`, or both.
- A matching `path` takes precedence over a matching `ply`.
- If no `path` matches, mainline positions fall back to a matching legacy `ply`.
- Variation diagrams require `path`; a `ply`-only entry only matches mainline
positions.
- Missing both `path` and `ply` leaves the entry with no position to match.
Diagrams on the mainline use the default board size. Diagrams inside a
variation are rendered inline and use the smaller `smallboard` size.
### Malformed diagram JSON
If the JSON is invalid, or any entry is malformed, `parse_diagrams_json`
returns empty diagram data instead of raising. Conversion then proceeds
without diagrams.
## Clocks
Clock annotations are opt-in:
```ocaml
let tex = Pgn_to_tex.to_tex ~clock:true ~diagram_data:diagrams pgn
```
With `~clock:true`, the most recent clock values for both players are shown
above and below each rendered diagram. Clock values come from PGN clock
annotations such as `{[%clk 0:10:00]}` or an inline `[%clk 0:10:00]`.
Clocks are displayed as part of diagram output. When no diagram is selected
for a position, its clock values are not emitted.
## Variations
Parenthesised variations are rendered inline inside the mainline, for example
`( 2... d6 )`. Mainline moves and move numbers are bold; variation moves are
not. Comments and NAGs inside a variation are rendered there too. Variation
diagrams are looked up by their full `path` and use `smallboard` so they do not
dominate the subline.
## Supported headers
The following PGN headers are rendered:
- `Event` is used as the title when `Title` is absent.
- `Title` is the document title. When `Subtitle` is also present, the subtitle
is rendered below the title. A title without a subtitle renders no subtitle
block.
- `Author` is the author line when present. Otherwise the author line is built
from `White` and `Black`, including `WhiteElo` and `BlackElo` when supplied.
- `Date` and `Site` are combined into the date line. An empty date with a site
renders the site; an empty date and site render an empty date.
- `Result` is rendered in bold at the end of the game.
All other headers are ignored.
## NAG annotations
The following Numeric Annotation Glyphs are rendered:
| NAG | LaTeX |
| ------ | ----------------------- |
| `$1` | `!` |
| `$2` | `?` |
| `$3` | `!!` |
| `$4` | `??` |
| `$5` | `!?` |
| `$6` | `?!` |
| `$10` | `=` |
| `$13` | `\infty` |
| `$14` | `\wbetter` |
| `$15` | `\bbetter` |
| `$16` | `\ensuremath{\pm}` |
| `$17` | `\ensuremath{\mp}` |
| `$18` | `+-` |
| `$19` | `-+` |
| `$22` | `\zugzwang` |
| `$32` | `\development` |
| `$36` | `\initiative` |
| `$40` | `\attack` |
| `$44` | `\compensation` |
| `$132` | `\counterplay` |
Unmapped NAGs are ignored.
## Error behavior
- `to_tex` raises `Failure "Parse error"` when the PGN cannot be parsed.
- `parse_diagrams_json` never raises for malformed JSON; it returns empty
diagram data, as described above.
## LaTeX setup
The generated string is a fragment: it does not include a document class,
preamble, or `\begin{document}`. A complete document using `xskak` (which
loads `chessboard`, `skak`, and the chess symbol fonts), `multicol` for the
column layout, and the default diagram commands looks like this:
```latex
\documentclass{article}
\usepackage{xskak}
\usepackage{multicol}
\usepackage{amsmath,amssymb}
% NAG $13 renders \infty, a math-mode symbol, in text mode.
\let\pgninfinity\infty
\renewcommand{\infty}{\ensuremath{\pgninfinity}}
\begin{document}
\begin{multicols}{2}
game_tex goes here
% Paste the string returned by Pgn_to_tex.to_tex here.
\end{multicols}
\end{document}
```
## Compile a JavaScript version of the library
The relevant packages are:
With the library installed, you can compile the code to JavaScript by running:
- `xskak` for `\newchessgame`, `\chessboard`, and the NAG symbol commands.
- `multicol` for the two-column environment shown above.
- `amsmath` and `amssymb` for math symbols.
```
make build
## JavaScript build
The JavaScript target is optional. Dune skips it when the JavaScript
dependencies are not installed. Install them with:
```sh
opam install js_of_ocaml js_of_ocaml-ppx
```
This will generate the JavaScript file at `_build/default/bin/main.bc.js`
Build the artifact with:
This JavaScript file is minified and in CJS format, working in both Node and in the browser.
```sh
dune build bin/pgn_to_tex_js.bc.js
```
A single function `to_tex` is exposed, accepting the same arguments as the main library.
The generated CommonJS file is
`_build/default/bin/pgn_to_tex_js.bc.js`. It works in Node and in the browser.
The build exposes a single global function with the same three inputs as the
JavaScript entry point:
```js
to_tex(pgnString, diagramJsonString, displayClock);
```
- `pgnString` is the PGN text.
- `diagramJsonString` is the diagram JSON. Pass `""` or `"{}"` when there are
no diagrams.
- `displayClock` is a boolean controlling clock rendering.
- The return value is the LaTeX fragment as a string.
Node example:
```js
require("./_build/default/bin/pgn_to_tex_js.bc.js");
const gameTex = globalThis.to_tex("1. e4 e5 2. Nf3", "{}", false);
```
In the browser, the function is available as `window.to_tex` after the
artifact is loaded.
Run the Node smoke test after building the artifact:
```sh
node test/js/smoke.js
```
## Migration from 0.0.x
Version `0.1.0` is a breaking release. The old `Pgn_logic` library name and the
`Pgn2tex` module are gone.
| 0.0.x | 0.1.0 |
| ----- | ----- |
| `open Pgn_logic` | `open Pgn_to_tex` or use `Pgn_to_tex.` |
| `Pgn2tex.to_tex` | `Pgn_to_tex.to_tex` |
| `Pgn2tex.parse_diagrams_json` | `Pgn_to_tex.parse_diagrams_json` |
| Exposed `MoveMap.t` diagram data | Abstract `Pgn_to_tex.diagram_data` |
| `_build/default/bin/main.bc.js` | `_build/default/bin/pgn_to_tex_js.bc.js` |
The optional arguments now come before the PGN string:
```ocaml
(* 0.0.x *)
let tex = Pgn2tex.to_tex pgn ~diagram_data ~clock:false
(* 0.1.0 *)
let tex = Pgn_to_tex.to_tex ~diagram_data ~clock:false pgn
```
Replace a hand-built diagram map with `parse_diagrams_json`, or use
`empty_diagrams` for a game without diagrams. Path-only diagram entries are
supported; see [Diagram JSON schema](#diagram-json-schema). The JavaScript
`to_tex(pgn, diagramJson, displayClock)` contract is unchanged apart from the
artifact name.
## Dependencies
- dune
- sedlex
- menhir
- yojson
- js_of_ocaml-compiler
- ocamlformat
- ocaml-lsp-server
- ounit2
Runtime and build dependencies:
- `ocaml` >= 5.4
- `dune` >= 3.21
- `sedlex` >= 3.7
- `menhir` >= 20260209
- `yojson` >= 3.0
Test-only dependencies:
- `ounit2` >= 2.2.7
- `js_of_ocaml`
- `js_of_ocaml-ppx`
Development and documentation dependencies:
- `ocamlformat` 0.29.0 (development setup)
- `odoc` (documentation build)
## License
AGPL-3.0-or-later. See [LICENSE](LICENSE).
+3 -4
View File
@@ -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)))
-35
View File
@@ -1,35 +0,0 @@
open Js_of_ocaml
open Pgn_logic
open Ast
let parse_diagram_json json_str =
let json = Yojson.Basic.from_string json_str in
let json_list = Yojson.Basic.Util.to_list json in
List.fold_left
(fun acc item ->
let move_int =
item |> Yojson.Basic.Util.member "ply" |> Yojson.Basic.Util.to_int
in
let fen_str =
item |> Yojson.Basic.Util.member "fen" |> Yojson.Basic.Util.to_string
in
Pgn2tex.MoveMap.add move_int fen_str acc)
Pgn2tex.MoveMap.empty json_list
let convert_js pgn_js diagram_json_js display_clock =
let pgn = Js.to_string pgn_js in
(* convert JS string into OCaml string *)
let json_str = Js.to_string diagram_json_js in
let diagram_data =
if json_str = "" || json_str = "{}" then Pgn2tex.MoveMap.empty
else parse_diagram_json json_str
in
let result = Pgn2tex.to_tex pgn ~diagram_data ~clock:display_clock in
Js.string result
(* Exporting the module to the global JavaScript scope *)
let () = Js.export "to_tex" (Js.wrap_callback convert_js)
+20
View File
@@ -0,0 +1,20 @@
open Js_of_ocaml
open Pgn_to_tex
let convert_js pgn_js diagram_json_js display_clock =
let pgn = Js.to_string pgn_js in
(* convert JS string into OCaml string *)
let json_str = Js.to_string diagram_json_js in
let diagram_data =
if json_str = "" || json_str = "{}" then Pgn_to_tex.empty_diagrams
else Pgn_to_tex.parse_diagrams_json json_str
in
let result = Pgn_to_tex.to_tex ~diagram_data ~clock:display_clock pgn in
Js.string result
(* The documented Node and browser contract is a single global [to_tex]
function, so setting it on the global object is the only export needed. *)
let () = Js.Unsafe.set Js.Unsafe.global "to_tex" (Js.wrap_callback convert_js)
+3
View File
@@ -0,0 +1,3 @@
(documentation
(package pgn_to_tex)
(mld_files index))
+21
View File
@@ -0,0 +1,21 @@
{0 Pgn_to_tex}
[Pgn_to_tex] converts chess games written in PGN to LaTeX fragments. Move
numbers, moves, comments, variations, NAGs, and optional diagrams of selected
positions are rendered into text suitable for inclusion in a larger LaTeX
document.
{1 Quick start}
{[
let tex = Pgn_to_tex.to_tex "1. e4 e5 2. Nf3"
]}
Pass [~clock:true] to render clock annotations. Pass [~diagram_data] to select
positions for diagrams; build a selection with
{!Pgn_to_tex.parse_diagrams_json} or use {!Pgn_to_tex.empty_diagrams} to
explicitly request a game with no diagrams.
{1 API}
{!modules:Pgn_to_tex}
+35
View File
@@ -0,0 +1,35 @@
# Domain Docs
How the engineering skills should consume this repo's domain documentation when exploring the codebase.
## Before exploring, read these
- **`GLOSSARY.md`** at the repo root.
- **`docs/adr/`**: read ADRs that touch the area you're about to work in.
If any of these files don't exist, **proceed silently**. Don't flag their absence; don't suggest creating them upfront. The `/domain-modeling` skill (reached via `/grill-with-docs` and `/improve-codebase-architecture`) creates them lazily when terms or decisions actually get resolved.
## File structure
Single-context repo (most repos):
```
/
├── GLOSSARY.md
├── docs/adr/
│ ├── 0001-event-sourced-orders.md
│ └── 0002-postgres-for-write-model.md
└── src/
```
## Use the glossary's vocabulary
When your output names a domain concept (in an issue title, a refactor proposal, a hypothesis, a test name), use the term as defined in `GLOSSARY.md`. Don't drift to synonyms the glossary explicitly avoids.
If the concept you need isn't in the glossary yet, that's a signal: either you're inventing language the project doesn't use (reconsider) or there's a real gap (note it for `/domain-modeling`).
## Flag ADR conflicts
If your output contradicts an existing ADR, surface it explicitly rather than silently overriding:
> _Contradicts ADR-0007 (event-sourced orders), but worth reopening because…_
+46
View File
@@ -0,0 +1,46 @@
# Issue tracker: GitHub
Issues and specs for this repo live as GitHub issues. Use the `gh` CLI for all operations.
## Conventions
- **Create an issue**: `gh issue create --title "..." --body "..."`. Use a heredoc for multi-line bodies.
- **Read an issue**: `gh issue view <number> --json number,title,body,labels,comments`.
- **List issues**: `gh issue list --state open --json number,title,body,labels,comments --jq '[.[] | {number, title, body, labels: [.labels[].name], comments: [.comments[].body]}]'` with appropriate `--label` and `--state` filters.
- **Make an issue a sub-issue of a parent**: `gh issue create --parent <parent> ...`, or `gh issue edit <parent> --add-sub-issue <child>` afterwards (`gh` 2.94+). Older `gh`: `gh api --method POST repos/<owner>/<repo>/issues/<parent>/sub_issues -F sub_issue_id=<child-db-id>` (database id, as in **Blocking** below). Without sub-issues, put `Part of #<parent>` at the top of the child body.
- **Comment on an issue**: `gh issue comment <number> --body "..."`
- **Apply / remove labels**: `gh issue edit <number> --add-label "..."` / `--remove-label "..."`
- **Close**: `gh issue close <number> --comment "..."`
Infer the repo from `git remote -v`; `gh` does this automatically when run inside a clone.
## Pull requests as a triage surface
**PRs as a request surface: no.** _(Set to `yes` if this repo treats external PRs as feature requests; `/triage` reads this flag.)_
When set to `yes`, PRs run through the same labels and states as issues, using the `gh pr` equivalents:
- **Read a PR**: `gh pr view <number> --comments` and `gh pr diff <number>` for the diff.
- **List external PRs for triage**: `gh api --paginate 'repos/{owner}/{repo}/pulls?state=open' --jq '.[] | select(.author_association | IN("OWNER","MEMBER","COLLABORATOR") | not) | {number, title, author: .user.login, author_association, labels: [.labels[].name]}'`.
- **Comment / label / close**: `gh pr comment`, `gh pr edit --add-label`/`--remove-label`, `gh pr close`.
GitHub shares one number space across issues and PRs, so a bare `#42` may be either: resolve with `gh pr view 42` and fall back to `gh issue view 42`.
## When a skill says "publish to the issue tracker"
Create a GitHub issue.
## When a skill says "fetch the relevant ticket"
Read it as in **Read an issue** above.
## Wayfinding operations
Used by `/wayfinder`. The **map** is a single issue with **child** issues as tickets.
- **Map**: a single issue labelled `wayfinder:map`, holding the Notes / Decisions-so-far / Fog body. `gh issue create --label wayfinder:map`.
- **Child ticket**: an issue linked to the map as a GitHub sub-issue (see **Make an issue a sub-issue of a parent**). Where sub-issues aren't enabled, add the child to a task list in the map body and put `Part of #<map>` at the top of the child body. Labels: `wayfinder:<type>` (`research`/`prototype`/`grilling`/`task`). Once claimed, the ticket is assigned to the driving dev.
- **Blocking**: GitHub's **native issue dependencies**, the canonical, UI-visible representation. Add an edge with `gh api --method POST repos/<owner>/<repo>/issues/<child>/dependencies/blocked_by -F issue_id=<blocker-db-id>`, where `<blocker-db-id>` is the blocker's numeric **database id** (`gh api repos/<owner>/<repo>/issues/<n> --jq .id`, _not_ the `#number` or `node_id`). GitHub reports `issue_dependencies_summary.blocked_by` (open blockers only, the live gate). Where dependencies aren't available, fall back to a `Blocked by: #<n>, #<n>` line at the top of the child body. A ticket is unblocked when every blocker is closed.
- **Frontier query**: list the map's open children (`gh issue list --state open`, scoped to the map's sub-issues / task list), drop any with an open blocker (`issue_dependencies_summary.blocked_by > 0`, or an open issue in the `Blocked by` line) or an assignee; first in map order wins.
- **Claim**: `gh issue edit <n> --add-assignee @me`, the session's first write.
- **Resolve**: `gh issue comment <n> --body "<answer>"`, then `gh issue close <n>`, then append a context pointer (gist + link) to the map's Decisions-so-far.
+15
View File
@@ -0,0 +1,15 @@
# Triage Labels
The skills speak in terms of five canonical triage roles. This file maps those roles to the actual label strings used in this repo's issue tracker.
| Label in mattpocock/skills | Label in our tracker | Meaning |
| -------------------------- | -------------------- | ---------------------------------------- |
| `needs-triage` | `needs-triage` | Maintainer needs to evaluate this issue |
| `needs-info` | `needs-info` | Waiting on reporter for more information |
| `ready-for-agent` | `ready-for-agent` | Fully specified, ready for an AFK agent |
| `ready-for-human` | `ready-for-human` | Requires human implementation |
| `wontfix` | `wontfix` | Will not be actioned |
When a skill mentions a role (e.g. "apply the AFK-ready triage label"), use the corresponding label string from this table.
Edit the "Label in our tracker" column to match whatever vocabulary you actually use.
+12 -3
View File
@@ -15,9 +15,10 @@
(license AGPL-3.0-or-later)
(documentation https://github.com/TheRealOwenRees/pgn_to_tex/README.md)
(documentation
https://therealowenrees.github.io/pgn_to_tex/)
(version 0.0.1)
(version 0.1.0)
(package
(name pgn_to_tex)
@@ -37,8 +38,16 @@
(and
:with-test
(>= 2.2.7)))
(js_of_ocaml
(and
:with-test))
(js_of_ocaml-ppx
(and
:with-test))
("ocamlformat"
(and :with-dev-setup)))
(and
:with-dev-setup
(= 0.29.0))))
(tags
("pgn" "latex" "chess" "parser")))
-14
View File
@@ -1,14 +0,0 @@
module MoveMap = Map.Make (Int)
type tag = { key : string; value : string }
type item =
| Move of string
| Number of string
| Clock of string
| Nag of string
| Comment of string
| Result of string
| Variation of item list
type game = { tags : tag list; content : item list; result : string option }
+3 -8
View File
@@ -1,10 +1,5 @@
(library
(name pgn_logic)
(name pgn_to_tex)
(public_name pgn_to_tex)
(libraries sedlex menhirLib yojson)
(modules ast parser lexer parsing latex latex_helpers pgn2tex nag)
(preprocess
(pps sedlex.ppx)))
(menhir
(modules parser))
(modules pgn_to_tex)
(libraries pgn_to_tex_internal))
+23
View File
@@ -0,0 +1,23 @@
module MoveMap = Map.Make (Int)
module PathMap = Map.Make (String)
type tag = { key : string; value : string }
(* Diagrams selected by the client. [by_ply] keeps the legacy mainline lookup
keyed on the ply number; [by_path] keys on the SAN move sequence from the
root (space separated), which uniquely identifies mainline and variation
positions alike. *)
type diagram_data = { by_ply : string MoveMap.t; by_path : string PathMap.t }
let empty_diagrams = { by_ply = MoveMap.empty; by_path = PathMap.empty }
type item =
| Move of string
| Number of string
| Clock of string
| Nag of string
| Comment of string
| Result of string
| Variation of item list
type game = { tags : tag list; content : item list; result : string option }
+38
View File
@@ -0,0 +1,38 @@
module MoveMap = Ast.MoveMap
module PathMap = Ast.PathMap
open Yojson.Safe.Util
let path_key path_moves = String.concat " " path_moves
let parse_diagrams_json diagrams_json_str =
let fail () = raise Exit in
try
let json = Yojson.Safe.from_string diagrams_json_str in
let diagrams_list = to_list json in
List.fold_left
(fun acc item ->
let fields = match item with `Assoc fields -> fields | _ -> fail () in
let fen =
match List.assoc_opt "fen" fields with
| Some (`String fen) -> fen
| _ -> fail ()
in
let acc =
match List.assoc_opt "ply" fields with
| None -> acc
| Some (`Int ply) ->
{ acc with Ast.by_ply = MoveMap.add ply fen acc.Ast.by_ply }
| Some _ -> fail ()
in
match List.assoc_opt "path" fields with
| None -> acc
| Some (`List path_items) ->
let key = path_items |> List.map to_string |> path_key in
{ acc with Ast.by_path = PathMap.add key fen acc.Ast.by_path }
| Some _ -> fail ())
Ast.empty_diagrams diagrams_list
with _ -> Ast.empty_diagrams
let to_tex pgn ~diagram_data ~clock =
let game = Parsing.parse_pgn pgn in
Latex.game_to_tex game ~diagram_data ~clock
+10
View File
@@ -0,0 +1,10 @@
(library
(name pgn_to_tex_internal)
(package pgn_to_tex)
(libraries sedlex menhirLib yojson)
(modules ast parser lexer parsing latex latex_helpers converter nag)
(preprocess
(pps sedlex.ppx)))
(menhir
(modules parser))
+40 -15
View File
@@ -27,7 +27,10 @@ let tags_to_tex tags =
^ build_date_site_string date site
^ "\\maketitle\n\\newchessgame"
let rec item_to_tex is_mainline ply ?(diagram_data = MoveMap.empty) = function
let path_key path_moves = String.concat " " path_moves
let rec item_to_tex is_mainline ply ?(diagram_data = Ast.empty_diagrams)
?(branch_path = []) = function
| Number n -> if is_mainline then "\\textbf{" ^ n ^ "}" else n
| Move m ->
if is_mainline then "\\textbf{" ^ escape_tex m ^ "}" else escape_tex m
@@ -35,40 +38,53 @@ let rec item_to_tex is_mainline ply ?(diagram_data = MoveMap.empty) = function
if is_mainline then "\\newline " ^ escape_tex c ^ "\\par"
else escape_tex c
| Result r -> "\\textbf{" ^ r ^ "}"
| Variation v -> "( " ^ render_game false ~diagram_data v ^ " )"
| Variation v ->
"( " ^ render_game false ~diagram_data ~path:branch_path v ^ " )"
| Nag g -> " " ^ Nag.nag_to_tex g ^ "{}"
| Clock c -> c
and render_game is_mainline ?(diagram_data = MoveMap.empty) ?(clock = false)
items =
let rec aux ply interrupted ?(white_time = "0:00") ?(black_time = "0:00") =
and render_game is_mainline ?(diagram_data = Ast.empty_diagrams)
?(clock = false) ?(path = []) items =
let rec aux ply interrupted ~white_time ~black_time ~path ~branch_path =
function
| [] -> ""
| Comment c :: Move m :: tail when is_mainline && ply mod 2 != 0 ->
let next_ply = ply + 1 in
let next_path = path @ [ m ] in
let rendered_comment = "\\newline " ^ escape_tex c ^ "\\par" in
let rendered_move = "\\textbf{\\ldots{}" ^ escape_tex m ^ "}" in
let diag_str =
match get_diagram next_ply diagram_data with
match
get_diagram ~path:(path_key next_path) ~ply:next_ply diagram_data
with
| Some s -> s
| None -> ""
in
let has_diag = diag_str <> "" in
rendered_comment ^ " " ^ rendered_move ^ diag_str ^ " "
^ aux next_ply has_diag tail
^ aux next_ply has_diag ~white_time ~black_time ~path:next_path
~branch_path:path tail
| head :: tail -> (
match head with
| Clock c ->
if ply mod 2 = 0 then
aux ply interrupted ~white_time ~black_time:c tail
else aux ply interrupted ~white_time:c ~black_time tail
aux ply interrupted ~white_time ~black_time:c ~path ~branch_path
tail
else
aux ply interrupted ~white_time:c ~black_time ~path ~branch_path
tail
| _ ->
let is_move = match head with Move _ -> true | _ -> false in
let move_san = match head with Move m -> Some m | _ -> None in
let is_move = Option.is_some move_san in
let next_ply =
match head with Move _ when is_mainline -> ply + 1 | _ -> ply
in
let next_path =
match move_san with Some m -> path @ [ m ] | None -> path
in
let prefix =
if is_move && is_mainline && next_ply mod 2 == 0 && interrupted
then "\\ldots{}"
@@ -77,7 +93,7 @@ and render_game is_mainline ?(diagram_data = MoveMap.empty) ?(clock = false)
let rendered_head =
let raw_head =
item_to_tex is_mainline next_ply ~diagram_data head
item_to_tex is_mainline next_ply ~diagram_data ~branch_path head
in
if prefix <> "" && raw_head <> "" then
"\\textbf{" ^ prefix ^ "}" ^ raw_head
@@ -85,16 +101,23 @@ and render_game is_mainline ?(diagram_data = MoveMap.empty) ?(clock = false)
in
let diag_str =
if is_move && is_mainline then
get_diagram next_ply diagram_data ~clock ~white_time ~black_time
if is_move then
if is_mainline then
get_diagram ~path:(path_key next_path) ~ply:next_ply ~clock
~white_time ~black_time diagram_data
else
get_diagram_by_path ~path:(path_key next_path) ~small:true
diagram_data
else None
in
let diag_output = match diag_str with Some s -> s | None -> "" in
let is_comment = match head with Comment _ -> true | _ -> false in
let next_interrupted = diag_output <> "" || is_comment in
let next_branch_path = if is_move then path else branch_path in
let rest =
aux next_ply next_interrupted ~white_time ~black_time tail
aux next_ply next_interrupted ~white_time ~black_time
~path:next_path ~branch_path:next_branch_path tail
in
if rendered_head = "" then rest
@@ -102,7 +125,9 @@ and render_game is_mainline ?(diagram_data = MoveMap.empty) ?(clock = false)
rendered_head ^ diag_output ^ if rest = "" then "" else " " ^ rest
)
in
String.trim (aux 0 false items)
String.trim
(aux 0 false ~white_time:"0:00" ~black_time:"0:00" ~path ~branch_path:path
items)
let game_to_tex game ~diagram_data ~clock =
let header_tex = tags_to_tex game.tags in
+94
View File
@@ -0,0 +1,94 @@
module MoveMap = Ast.MoveMap
module PathMap = Ast.PathMap
(* escape strings for LaTeX *)
let escape_tex s =
let b = Buffer.create (String.length s) in
String.iter
(fun c ->
match c with
| '#' -> Buffer.add_string b "\\#"
| '%' -> Buffer.add_string b "\\%"
| '{' -> Buffer.add_string b "\\{"
| '}' -> Buffer.add_string b "\\}"
| '_' -> Buffer.add_string b "\\_"
| '&' -> Buffer.add_string b "\\&"
| _ -> Buffer.add_char b c)
s;
Buffer.contents b
(* ELO string builder *)
let build_elo_string elo = if String.length elo > 0 then "(" ^ elo ^ ")" else ""
(* Author string builder *)
let build_author_string author white black white_elo black_elo =
if author <> "" then "\\author{" ^ escape_tex author ^ "}"
else
match (white, black) with
| "", "" -> "\\author{}"
| white, black ->
"\\author{" ^ escape_tex white ^ " " ^ build_elo_string white_elo
^ "\\\\" ^ escape_tex black ^ " " ^ build_elo_string black_elo ^ "}"
(* Title string builder *)
let build_title_string event title subtitle =
match (event, title, subtitle) with
| event, "", "" -> "\\title{" ^ event ^ "}"
| _event, "", subtitle -> "\\title{" ^ subtitle ^ "}"
| _event, title, "" -> "\\title{" ^ title ^ "}"
| _event, title, subtitle ->
"\\title{" ^ title ^ "\\\\[2ex]\\large{" ^ subtitle ^ "}}"
(* Date and Site string builder *)
let build_date_site_string date site =
match (date, site) with
| "", "" -> "\\date{}"
| "", s -> "\\date{" ^ s ^ "}"
| d, "" -> "\\date{" ^ d ^ "}"
| d, s -> "\\date{" ^ d ^ ", " ^ s ^ "}"
(* board size options: mainline diagrams use the default size, variation
diagrams use the smaller "smallboard" so they don't dominate the subline *)
let board_options small = if small then ", smallboard" else ""
(* emit a diagram with or without clock times *)
let diagram_tex ?(clock = false) ?(white_time = "0:00") ?(black_time = "0:00")
?(small = false) fen =
let size = board_options small in
if not clock then
"\n\\par\\nobreak\\medskip\\chessboard[setfen=" ^ fen ^ ", vmargin=false"
^ size ^ "]\\par\\medskip\n"
else
"\\par\\medskip\\noindent" ^ "\\begin{minipage}{\\linewidth}" ^ black_time
^ "\\par\\nopagebreak\\smallskip\n" ^ "\\chessboard[setfen=" ^ fen
^ ", vmargin=false" ^ size ^ "]\\par\\nopagebreak\\vspace{1em}\n"
^ white_time ^ "\\end{minipage}\\par\\medskip"
(* look up a diagram by its SAN path (root -> position, space separated) *)
let get_diagram_by_path ?(clock = false) ?(white_time = "0:00")
?(black_time = "0:00") ?(small = false) ~path diagram_data =
match PathMap.find_opt path diagram_data.Ast.by_path with
| None -> None
| Some fen -> Some (diagram_tex ~clock ~white_time ~black_time ~small fen)
(* look up a diagram by ply (legacy mainline keying) *)
let get_diagram_by_ply ?(clock = false) ?(white_time = "0:00")
?(black_time = "0:00") ply diagram_data =
match MoveMap.find_opt ply diagram_data.Ast.by_ply with
| None -> None
| Some fen -> Some (diagram_tex ~clock ~white_time ~black_time fen)
(* Prefer the path lookup, falling back to ply for older payloads that only
carry a mainline ply. *)
let get_diagram ?(clock = false) ?(white_time = "0:00") ?(black_time = "0:00")
?(small = false) ?path ~ply diagram_data =
let by_path =
match path with
| Some path ->
get_diagram_by_path ~clock ~white_time ~black_time ~small ~path
diagram_data
| None -> None
in
match by_path with
| Some _ as diagram -> diagram
| None -> get_diagram_by_ply ~clock ~white_time ~black_time ply diagram_data
+2 -2
View File
@@ -76,8 +76,8 @@ and read_comment_clock buf =
| clock_val ->
let time = Utf8.lexeme buf in
begin match%sedlex buf with
| ']' -> begin
match%sedlex buf with
| ']' ->
begin match%sedlex buf with
| Star white_space, '}' -> CLOCK time
| _ -> failwith "Expected closing '}' after clock comment"
end
View File
@@ -28,15 +28,3 @@ let parse_pgn s =
in
try MenhirLib.Convert.Simplified.traditional2revised Parser.main provider
with _ -> failwith "Parse error"
(* let parse_diagram_json json_str =
let json = Yojson.Basic.from_string json_str in
let json_assoc = Yojson.Basic.Util.to_assoc json in
List.fold_left
(fun acc (key_str, json_value) ->
let value_string = Yojson.Basic.Util.to_string json_value in
match int_of_string_opt key_str with
| Some k -> Pgn2tex.MoveMap.add k value_string acc
| None -> acc)
Pgn2tex.MoveMap.empty json_assoc *)
-63
View File
@@ -1,63 +0,0 @@
module MoveMap = Ast.MoveMap
(* escape strings for LaTeX *)
let escape_tex s =
let b = Buffer.create (String.length s) in
String.iter
(fun c ->
match c with
| '#' -> Buffer.add_string b "\\#"
| '%' -> Buffer.add_string b "\\%"
| '{' -> Buffer.add_string b "\\{"
| '}' -> Buffer.add_string b "\\}"
| '_' -> Buffer.add_string b "\\_"
| '&' -> Buffer.add_string b "\\&"
| _ -> Buffer.add_char b c)
s;
Buffer.contents b
(* ELO string builder *)
let build_elo_string elo = if String.length elo > 0 then "(" ^ elo ^ ")" else ""
(* Author string builder *)
let build_author_string author white black white_elo black_elo =
match (author, white, black) with
| "", "", "" -> "\\author{}"
| "", white, black ->
"\\author{" ^ escape_tex white ^ " " ^ build_elo_string white_elo ^ "\\\\"
^ escape_tex black ^ " " ^ build_elo_string black_elo ^ "}"
| author, "", "" -> "\\author{" ^ escape_tex author ^ "}"
| _ -> ""
(* Title string builder *)
let build_title_string event title subtitle =
match (event, title, subtitle) with
| event, "", "" -> "\\title{" ^ escape_tex event ^ "}"
| _event, title, subtitle ->
"\\title{" ^ title ^ "}\\\\[2ex]\\large{" ^ subtitle ^ "}"
(* Date and Site string builder *)
let build_date_site_string date site =
match (date, site) with
| "", "" -> "\\date{}"
| "", s -> "\\date{" ^ s ^ "}"
| d, "" -> "\\date{" ^ d ^ "}"
| d, s -> "\\date{" ^ d ^ ", " ^ s ^ "}"
(* generate a diagram with or without clock times from the passed in Map (JSON in JS) *)
let get_diagram ?(clock = false) ?(white_time = "0:00") ?(black_time = "0:00")
ply diagram_data =
match MoveMap.find_opt ply diagram_data with
| None -> None
| Some fen ->
if not clock then
Some
("\n\\par\\nobreak\\medskip\\chessboard[setfen=" ^ fen
^ ", vmargin=false]\\par\\medskip\n")
else
Some
("\\par\\medskip\\noindent" ^ "\\begin{minipage}{\\linewidth}"
^ black_time ^ "\\par\\nopagebreak\\smallskip\n"
^ "\\chessboard[setfen=" ^ fen
^ ", vmargin=false]\\par\\nopagebreak\\vspace{1em}\n" ^ white_time
^ "\\end{minipage}\\par\\medskip")
-18
View File
@@ -1,18 +0,0 @@
module MoveMap = Ast.MoveMap
open Yojson.Safe.Util
let parse_diagrams_json diagrams_json_str =
try
let json = Yojson.Safe.from_string diagrams_json_str in
let diagrams_list = to_list json in
List.fold_left
(fun acc_map item ->
let ply = item |> member "ply" |> to_int in
let fen = item |> member "fen" |> to_string in
MoveMap.add ply fen acc_map)
MoveMap.empty diagrams_list
with _ -> MoveMap.empty
let to_tex pgn ~diagram_data ~clock =
let game = Parsing.parse_pgn pgn in
Latex.game_to_tex game ~diagram_data ~clock
+7
View File
@@ -0,0 +1,7 @@
type diagram_data = Pgn_to_tex_internal.Ast.diagram_data
let empty_diagrams = Pgn_to_tex_internal.Ast.empty_diagrams
let parse_diagrams_json = Pgn_to_tex_internal.Converter.parse_diagrams_json
let to_tex ?(diagram_data = empty_diagrams) ?(clock = false) pgn =
Pgn_to_tex_internal.Converter.to_tex pgn ~diagram_data ~clock
+40
View File
@@ -0,0 +1,40 @@
(** Convert chess PGN games to LaTeX.
[Pgn_to_tex] is the public entry point for the library. Open it to convert a
PGN string without referring to any internal parsing or rendering modules:
{[
let tex = Pgn_to_tex.to_tex pgn
]}
Diagrams are optional and selected by passing {!diagram_data} built with
{!empty_diagrams} or {!parse_diagrams_json}. *)
type diagram_data
(** Diagram selection data.
The representation is intentionally abstract; construct values with
{!empty_diagrams} or {!parse_diagrams_json}. *)
val empty_diagrams : diagram_data
(** [empty_diagrams] selects no diagrams. It is the default used by {!to_tex}.
*)
val parse_diagrams_json : string -> diagram_data
(** [parse_diagrams_json json] parses diagram selections from a JSON string.
The input is expected to be a JSON array of objects. Each object contains a
[fen] string and either a legacy [ply] number, a [path] array of SAN moves,
or both. A matching [path] takes precedence; [ply] is the fallback for
mainline positions. Malformed JSON yields {!empty_diagrams} rather than
raising. *)
val to_tex : ?diagram_data:diagram_data -> ?clock:bool -> string -> string
(** [to_tex pgn] converts the PGN game in [pgn] to a LaTeX fragment.
@param diagram_data
diagram selections to render; defaults to {!empty_diagrams}.
@param clock
when [true], render clock times alongside diagrams; defaults to [false].
Raises [Failure "Parse error"] if [pgn] is not a valid PGN game. *)
+5 -3
View File
@@ -1,6 +1,6 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
version: "0.0.1"
version: "0.1.0"
synopsis: "A chess PGN to TeX conversion tool"
description:
"Convert PGN files to LaTeX markdown, including rendered images of selected positions"
@@ -9,7 +9,7 @@ authors: ["Owen Rees <owenrees.dev@gmail.com>"]
license: "AGPL-3.0-or-later"
tags: ["pgn" "latex" "chess" "parser"]
homepage: "https://github.com/therealowenrees/pgn_to_tex"
doc: "https://github.com/TheRealOwenRees/pgn_to_tex/README.md"
doc: "https://therealowenrees.github.io/pgn_to_tex/"
bug-reports: "https://github.com/therealowenrees/pgn_to_tex/issues"
depends: [
"dune" {>= "3.21"}
@@ -18,7 +18,9 @@ depends: [
"menhir" {>= "20260209"}
"yojson" {>= "3.0"}
"ounit2" {with-test & >= "2.2.7"}
"ocamlformat" {with-dev-setup}
"js_of_ocaml" {with-test}
"js_of_ocaml-ppx" {with-test}
"ocamlformat" {with-dev-setup & = "0.29.0"}
"odoc" {with-doc}
]
build: [
+1 -1
View File
@@ -1,6 +1,6 @@
(test
(name test_suite)
(modules (:standard))
(libraries ounit2 sedlex sedlex.ppx pgn_to_tex helpers)
(libraries ounit2 sedlex sedlex.ppx pgn_to_tex pgn_to_tex_internal helpers)
(deps
(source_tree pgn_examples)))
+2 -1
View File
@@ -1,3 +1,4 @@
(library
(name helpers)
(modules latex_helper))
(modules latex_helper text_helper)
(libraries pgn_to_tex_internal))
+7 -2
View File
@@ -1,6 +1,9 @@
module MoveMap = Map.Make (Int)
open Pgn_to_tex_internal
open Ast
let diagram_data =
{
by_ply =
MoveMap.empty
|> MoveMap.add 5
"rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3"
@@ -9,4 +12,6 @@ let diagram_data =
|> MoveMap.add 10
"r1bqkbnr/pp1ppppp/2n5/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 3 5"
|> MoveMap.add 15
"r1bqkb1r/pp1ppppp/2n5/2p5/4P3/2N2N2/PPPP1PPP/R1BQKB1R b KQkq - 5 7"
"r1bqkb1r/pp1ppppp/2n5/2p5/4P3/2N2N2/PPPP1PPP/R1BQKB1R b KQkq - 5 7";
by_path = PathMap.empty;
}
+8
View File
@@ -0,0 +1,8 @@
let contains ~needle haystack =
let n = String.length needle and h = String.length haystack in
let rec loop i =
if i + n > h then false
else if String.sub haystack i n = needle then true
else loop (i + 1)
in
n = 0 || loop 0
+36
View File
@@ -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");
+7
View File
@@ -0,0 +1,7 @@
[Result "*"]
[Title "Some Title"]
[Subtitle "Some Subtitle"]
[Date "2025.12.23"]
[Author "Some Author"]
1. d4 d5 2. c4 dxc4 3. e4 Nf6 4. e5 Nd5 5. Bxc4 Nb6 6. Bb3 e6 (6... Nc6) 7. Nf3 c5 8. Be3 N8d7 9. O-O c4 10. Bc2 Nd5 11. Nc3 N7b6 12. Be4 Bb4 13. Bxd5 Nxd5? (13... Bxc3) 14. Nxd5 exd5 15. Qa4+ Bd7 16. Qxb4 Qb6 17. Qxb6 axb6 18. Rfc1 O-O 19. Nd2 Bf5 20. b3 b5 21. a4 cxb3 22. Nxb3 bxa4 23. Nc5 b5 24. Bd2 Rfe8 25. Bb4 Rac8 26. Nb7 Rxc1+ 27. Rxc1 Rc8 28. Rxc8+ Bxc8 29. Nd6 Bd7 30. f4 g6 31. Kf2 Kg7 32. h3 *
+132
View File
@@ -0,0 +1,132 @@
open OUnit2
let contains = Helpers.Text_helper.contains
let path_only_variation_json =
{| [{"fen":"8/8/8/8/8/8/4K3/7k w - - 0 1","path":["e4","e5","Nf3","d6"]}] |}
let test_path_only_variation_diagram _ctxt =
let pgn = "1. e4 e5 2. Nf3 Nc6 (2... d6) 3. Bb5" in
let diagrams = Pgn_to_tex.parse_diagrams_json path_only_variation_json in
let rendered = Pgn_to_tex.to_tex ~diagram_data:diagrams pgn in
assert_bool
(Printf.sprintf "path-only variation diagram missing from:\n%s" rendered)
(contains ~needle:"setfen=8/8/8/8/8/8/4K3/7k w - - 0 1" rendered);
assert_bool "path-only variation diagram should use smallboard"
(contains ~needle:"smallboard" rendered)
let path_only_mainline_json =
{| [{"fen":"8/8/8/8/8/8/4K3/7k w - - 0 1","path":["e4","e5"]}] |}
let test_path_only_mainline_diagram _ctxt =
let pgn = "1. e4 e5 2. Nf3" in
let diagrams = Pgn_to_tex.parse_diagrams_json path_only_mainline_json in
let rendered = Pgn_to_tex.to_tex ~diagram_data:diagrams pgn in
assert_bool
(Printf.sprintf "path-only mainline diagram missing from:\n%s" rendered)
(contains ~needle:"setfen=8/8/8/8/8/8/4K3/7k w - - 0 1" rendered)
let test_path_takes_precedence_over_ply _ctxt =
let pgn = "1. e4 e5 2. Nf3" in
let path_fen = "8/8/8/8/8/8/4K3/7k w - - 0 1" in
let ply_fen = "7k/8/8/8/8/8/8/4K3 b - - 0 1" in
let json =
Printf.sprintf {| [{"fen":"%s","path":["e4","e5"]},{"ply":2,"fen":"%s"}] |}
path_fen ply_fen
in
let diagrams = Pgn_to_tex.parse_diagrams_json json in
let rendered = Pgn_to_tex.to_tex ~diagram_data:diagrams pgn in
assert_bool "path diagram should win over the ply fallback"
(contains ~needle:("setfen=" ^ path_fen) rendered);
assert_bool "ply diagram should not replace the matching path diagram"
(not (contains ~needle:("setfen=" ^ ply_fen) rendered))
let test_entry_with_path_and_ply _ctxt =
let pgn = "1. e4 e5" in
let both_fen = "8/8/8/8/8/8/4K3/7k w - - 0 1" in
let path_fen = "7k/8/8/8/8/8/8/4K3 b - - 0 1" in
let json =
Printf.sprintf
{| [{"ply":9,"fen":"%s","path":["e4"]},{"fen":"%s","path":["e4","e5"]}] |}
both_fen path_fen
in
let diagrams = Pgn_to_tex.parse_diagrams_json json in
let rendered = Pgn_to_tex.to_tex ~diagram_data:diagrams pgn in
assert_bool "entry carrying both path and ply should still resolve by path"
(contains ~needle:("setfen=" ^ both_fen) rendered);
assert_bool "second path-only entry should resolve by path"
(contains ~needle:("setfen=" ^ path_fen) rendered)
let test_ply_only_entry_falls_back _ctxt =
let pgn = "1. e4 e5 2. Nf3" in
let ply_fen = "7k/8/8/8/8/8/8/4K3 b - - 0 1" in
let json = Printf.sprintf {| [{"ply":2,"fen":"%s"}] |} ply_fen in
let diagrams = Pgn_to_tex.parse_diagrams_json json in
let rendered = Pgn_to_tex.to_tex ~diagram_data:diagrams pgn in
assert_bool "legacy ply-only diagram should still resolve by ply"
(contains ~needle:("setfen=" ^ ply_fen) rendered)
let test_malformed_json_yields_empty_diagrams _ctxt =
let pgn = "1. e4 e5" in
let expected = Pgn_to_tex.to_tex pgn in
let malformed =
[
"not json at all";
{| {"ply":1} |};
{| [{"path":["e4"]}] |};
{| [{"fen":"8/8/8/8/8/8/4K3/7k w - - 0 1","ply":"not-a-number"}] |};
]
in
List.iter
(fun json ->
let actual =
Pgn_to_tex.to_tex
~diagram_data:(Pgn_to_tex.parse_diagrams_json json)
pgn
in
assert_equal
~printer:(fun x -> x)
~msg:("malformed JSON: " ^ json)
expected actual)
malformed
let test_mixed_valid_and_invalid_entries_yield_empty _ctxt =
let pgn = "1. e4 e5 2. Nf3" in
let expected = Pgn_to_tex.to_tex pgn in
let valid = {|{"ply":2,"fen":"8/8/8/8/8/8/4K3/7k w - - 0 1"}|} in
let invalid_entries =
[
{|{"ply":3}|};
{|{"ply":"not-a-number","fen":"8/8/8/8/8/8/4K3/7k w - - 0 1"}|};
{|{"fen":"8/8/8/8/8/8/4K3/7k w - - 0 1","path":[1,2]}|};
{|{"fen":42}|};
]
in
List.iter
(fun invalid ->
let json = Printf.sprintf "[%s,%s]" valid invalid in
let actual =
Pgn_to_tex.to_tex
~diagram_data:(Pgn_to_tex.parse_diagrams_json json)
pgn
in
assert_equal
~printer:(fun x -> x)
~msg:("mixed valid/invalid JSON: " ^ json)
expected actual)
invalid_entries
let suite =
"diagram resolution tests"
>::: [
"path_only_variation_diagram" >:: test_path_only_variation_diagram;
"path_only_mainline_diagram" >:: test_path_only_mainline_diagram;
"path_takes_precedence_over_ply"
>:: test_path_takes_precedence_over_ply;
"entry_with_path_and_ply" >:: test_entry_with_path_and_ply;
"ply_only_entry_falls_back" >:: test_ply_only_entry_falls_back;
"malformed_json_yields_empty_diagrams"
>:: test_malformed_json_yields_empty_diagrams;
"mixed_valid_and_invalid_entries_yield_empty"
>:: test_mixed_valid_and_invalid_entries_yield_empty;
]
+25 -8
View File
@@ -1,5 +1,5 @@
open OUnit2
open Pgn_logic
open Pgn_to_tex_internal
open Ast
open Helpers
@@ -105,15 +105,28 @@ let test_date_site =
ae "\\date{}" (Latex_helpers.build_date_site_string "" "") );
]
let test_escape_tex _ctxt =
ae "\\#\\%\\{\\}\\_\\&" (Latex_helpers.escape_tex "#%{}_&");
ae "plain text" (Latex_helpers.escape_tex "plain text")
let test_title =
[
( "Event only" >:: fun _ ->
ae "\\title{some event}"
(Latex_helpers.build_title_string "some event" "" "") );
( "Title and subtitle" >:: fun _ ->
ae "\\title{Some title}\\\\[2ex]\\large{Some subtitle}"
ae "\\title{Some title\\\\[2ex]\\large{Some subtitle}}"
(Latex_helpers.build_title_string "irrelevant" "Some title"
"Some subtitle") );
( "Title without subtitle" >:: fun _ ->
ae "\\title{Some title}"
(Latex_helpers.build_title_string "irrelevant" "Some title" "") );
( "Subtitle without title" >:: fun _ ->
ae "\\title{Some subtitle}"
(Latex_helpers.build_title_string "irrelevant" "" "Some subtitle") );
( "Title is not escaped" >:: fun _ ->
ae "\\title{100% & more}"
(Latex_helpers.build_title_string "" "100% & more" "") );
]
let test_author =
@@ -121,12 +134,15 @@ let test_author =
( "Author only" >:: fun _ ->
ae "\\author{Some author}"
(Latex_helpers.build_author_string "Some author" "" "" "" "") );
( "Author wins over players" >:: fun _ ->
ae "\\author{Carol}"
(Latex_helpers.build_author_string "Carol" "Alice" "Bob" "1" "2") );
( "Nothing" >:: fun _ ->
ae "\\author{}" (Latex_helpers.build_author_string "" "" "" "" "") );
]
let test_diagram _ctxt =
match Latex_helpers.get_diagram 5 Helpers.Latex_helper.diagram_data with
match Latex_helpers.get_diagram ~ply:5 Helpers.Latex_helper.diagram_data with
| Some actual ->
let expected =
"\n\\par\\nobreak\\medskip\\chessboard[setfen="
@@ -138,8 +154,8 @@ let test_diagram _ctxt =
let test_diagram_clock _ctxt =
match
Latex_helpers.get_diagram 5 Helpers.Latex_helper.diagram_data ~clock:true
~white_time:"1:00" ~black_time:"0:21"
Latex_helpers.get_diagram ~ply:5 Helpers.Latex_helper.diagram_data
~clock:true ~white_time:"1:00" ~black_time:"0:21"
with
| Some actual ->
let expected =
@@ -151,7 +167,9 @@ let test_diagram_clock _ctxt =
| None -> assert_failure "Diagram for ply 5 was not found"
let test_no_diagram _ctxt =
let result = Latex_helpers.get_diagram 99 Helpers.Latex_helper.diagram_data in
let result =
Latex_helpers.get_diagram ~ply:99 Helpers.Latex_helper.diagram_data
in
match result with
| None -> ()
@@ -172,11 +190,10 @@ let suite =
"diagram_placement" >:: test_diagram_placement;
"elo tests" >::: test_player_elo;
"date & site tests" >::: test_date_site;
"escape tex" >:: test_escape_tex;
"title tests" >::: test_title;
"author tests" >::: test_author;
"diagram_test" >:: test_diagram;
"diagram_clock" >:: test_diagram_clock;
"no_diagram" >:: test_no_diagram;
]
let () = run_test_tt_main suite
+25 -3
View File
@@ -1,5 +1,5 @@
open OUnit2
open Pgn_logic
open Pgn_to_tex_internal
open Parser
let lex_all ?(is_header = false) s =
@@ -170,9 +170,33 @@ let test_clock_in_comment _ctxt =
in
assert_tokens ~ctxt:_ctxt ~input ~is_header:false ~expected
let test_header_unterminated_string_raises _ctxt =
let input = {|Event "oops|} in
let lexbuf = Sedlexing.Utf8.from_string input in
ignore (Lexer.tokenize_header lexbuf);
let exn_opt =
try
ignore (Lexer.tokenize_header lexbuf);
None
with exn -> Some exn
in
assert_bool "Expected exception on unterminated header string"
(Option.is_some exn_opt)
let test_unknown_characters_are_skipped _ctxt =
let input = "1. e4 ?! e5 @ 2. Nf3" in
let expected =
[ NUMBER "1."; MOVE "e4"; MOVE "e5"; NUMBER "2."; MOVE "Nf3"; EOF ]
in
assert_tokens ~ctxt:_ctxt ~input ~is_header:false ~expected
let suite =
"lexer tests"
>::: [
"header_unterminated_string_raises"
>:: test_header_unterminated_string_raises;
"unknown_characters_are_skipped"
>:: test_unknown_characters_are_skipped;
"header_simple" >:: test_header_simple;
"header_escaped_string" >:: test_header_escaped_string;
"numbers_and_moves" >:: test_numbers_and_moves;
@@ -187,5 +211,3 @@ let suite =
"clock_inline" >:: test_clock_inline;
"clock_in_comment" >:: test_clock_in_comment;
]
let () = run_test_tt_main suite
+4 -1
View File
@@ -1,5 +1,5 @@
open OUnit2
open Pgn_logic
open Pgn_to_tex_internal
open Ast
open Nag
@@ -19,12 +19,15 @@ let test_direct_nags =
("$15" >:: fun _ -> ae "\\bbetter" (nag_to_tex "$15"));
("$16" >:: fun _ -> ae "\\ensuremath{\\pm}" (nag_to_tex "$16"));
("$17" >:: fun _ -> ae "\\ensuremath{\\mp}" (nag_to_tex "$17"));
("$18" >:: fun _ -> ae "+-" (nag_to_tex "$18"));
("$19" >:: fun _ -> ae "-+" (nag_to_tex "$19"));
("$22" >:: fun _ -> ae "\\zugzwang" (nag_to_tex "$22"));
("$32" >:: fun _ -> ae "\\development" (nag_to_tex "$32"));
("$36" >:: fun _ -> ae "\\initiative" (nag_to_tex "$36"));
("$40" >:: fun _ -> ae "\\attack" (nag_to_tex "$40"));
("$44" >:: fun _ -> ae "\\compensation" (nag_to_tex "$44"));
("$132" >:: fun _ -> ae "\\counterplay" (nag_to_tex "$132"));
("unknown fallback" >:: fun _ -> ae "" (nag_to_tex "$999"));
]
let suite = "nag test" >::: [ "direct nags" >::: test_direct_nags ]
+51
View File
@@ -0,0 +1,51 @@
open OUnit2
open Pgn_to_tex_internal
open Ast
let ae expected actual = assert_equal expected actual
let test_empty_input _ctxt =
ae { tags = []; content = []; result = None } (Parsing.parse_pgn "")
let test_result_only_game _ctxt =
ae
{ tags = []; content = [ Result "1-0" ]; result = None }
(Parsing.parse_pgn "1-0")
let test_nested_variations _ctxt =
let pgn = "1. e4 (1. d4 (1... d5) 1... e5) 1... c5" in
let expected =
{
tags = [];
content =
[
Number "1.";
Move "e4";
Variation
[
Number "1.";
Move "d4";
Variation [ Number "1..."; Move "d5" ];
Number "1...";
Move "e5";
];
Number "1...";
Move "c5";
];
result = None;
}
in
ae expected (Parsing.parse_pgn pgn)
let test_malformed_input_raises _ctxt =
assert_raises (Failure "Parse error") (fun () ->
ignore (Parsing.parse_pgn "1. e4 e5 (2. Nf3"))
let suite =
"parser tests"
>::: [
"empty_input" >:: test_empty_input;
"result_only_game" >:: test_result_only_game;
"nested_variations" >:: test_nested_variations;
"malformed_input_raises" >:: test_malformed_input_raises;
]
-143
View File
@@ -1,143 +0,0 @@
open OUnit2
open Pgn_logic
open Ast
module MoveMap = Pgn2tex.MoveMap
let ae expected actual = assert_equal ~printer:(fun x -> x) expected actual
let diagram_json_str =
{| [{"ply":1,"fen":"rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b KQkq - 0 1"},{"ply":2,"fen":"rnbqkbnr/ppp1pppp/8/3p4/3P4/8/PPP1PPPP/RNBQKBNR w KQkq - 0 2"}] |}
let diagram_data =
MoveMap.empty
|> MoveMap.add 5
"rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3"
|> MoveMap.add 6
"rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 3"
|> MoveMap.add 10
"r1bqkbnr/pp1ppppp/2n5/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 3 5"
|> MoveMap.add 15
"r1bqkb1r/pp1ppppp/2n5/2p5/4P3/2N2N2/PPPP1PPP/R1BQKB1R b KQkq - 5 7"
let test_json_str_to_map _ctxt =
let result = Pgn2tex.parse_diagrams_json diagram_json_str in
let actual_bindings = MoveMap.bindings result in
let expected_bindings =
[
(1, "rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b KQkq - 0 1");
(2, "rnbqkbnr/ppp1pppp/8/3p4/3P4/8/PPP1PPPP/RNBQKBNR w KQkq - 0 2");
]
in
let string_of_bindings pairs =
let string_of_pair (ply, fen) = Printf.sprintf "(%d, %S)" ply fen in
"[" ^ String.concat "; " (List.map string_of_pair pairs) ^ "]"
in
assert_equal expected_bindings actual_bindings ~printer:string_of_bindings
let test_basic_pgn_to_tex _ctxt =
let filename = "pgn_examples/1.pgn" in
let ic = In_channel.open_text filename in
let input_string = In_channel.input_all ic in
let actual = Pgn2tex.to_tex input_string ~diagram_data ~clock:false in
let expected =
{|\title{URS-chJ}
\author{Ibragimov, Ildar (2455)\\Kramnik, Vladimir (2480)}
\date{1991.??.??, Kherson}\maketitle
\newchessgame
\textbf{1.} \textbf{d4} \newline A88: Dutch Defence: Leningrad System: 5 Nf3 0-0 6 0-0 d6 7 Nc3 c6\par \textbf{\ldots{}d6} \textbf{2.} \textbf{c4} \textbf{f5} \textbf{3.} \textbf{Nf3}
\par\nobreak\medskip\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip
\textbf{\ldots{}}\textbf{Nf6}
\par\nobreak\medskip\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 3, vmargin=false]\par\medskip
\textbf{4.} \textbf{g3} \textbf{g6} \textbf{5.} \textbf{Bg2} \textbf{Bg7}
\par\nobreak\medskip\chessboard[setfen=r1bqkbnr/pp1ppppp/2n5/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 3 5, vmargin=false]\par\medskip
\textbf{6.} \textbf{O-O} \textbf{O-O} \textbf{7.} \textbf{Nc3} \textbf{Qe8} \textbf{8.} \textbf{b3}
\par\nobreak\medskip\chessboard[setfen=r1bqkb1r/pp1ppppp/2n5/2p5/4P3/2N2N2/PPPP1PPP/R1BQKB1R b KQkq - 5 7, vmargin=false]\par\medskip
\textbf{\ldots{}}\textbf{Na6} \textbf{9.} \textbf{Ba3} \textbf{c6} \textbf{10.} \textbf{Qd3} \textbf{Rb8} \textbf{11.} \textbf{e4} \textbf{fxe4} \textbf{12.} \textbf{Nxe4} \textbf{Bf5} \textbf{13.} \textbf{Nxf6+} \textbf{Bxf6} \textbf{14.} \textbf{Qd2} \textbf{Nc7} \textbf{15.} \textbf{Rae1} \textbf{Qd7} \textbf{16.} \textbf{h4} \textbf{b5} \textbf{17.} \textbf{Re3} \textbf{bxc4} \textbf{18.} \textbf{bxc4} \textbf{Bh3} \textbf{19.} \textbf{Rfe1} \textbf{Bxg2} \newline last book move\par \textbf{20.} \textbf{Kxg2} \textbf{Qf5} \textbf{21.} \textbf{Re4} \textbf{Rbe8} \textbf{22.} \textbf{Rf4} \textbf{Qc8} \textbf{23.} \textbf{Qa5} \textbf{d5} \textbf{24.} \textbf{cxd5} \textbf{Nxd5} \textbf{25.} \textbf{Rfe4} \newline e7 seems the pivot of the position\par \textbf{\ldots{}Qf5} \textbf{26.} \textbf{Qd2} ( 26. Qxa7 ??{} taking the pawn is naive Qxf3+ Annihilates a defender: f3 27. Kxf3 Bxd4+ 28. Kg4 Bxa7 29. Bxe7 Rxf2 -+{} ( 29... Rxe7 30. Rxe7 Bxf2 31. a4 -+{} ) ( 29... Nxe7 ?!{} 30. Rxe7 Rxe7 31. Rxe7 Bxf2 32. a4 -+{} ) ) \textbf{Bg7} \textbf{27.} \textbf{Nh2} \textbf{Rf7} \textbf{28.} \textbf{Bc5} \newline The white bishop on an outpost\par \textbf{\ldots{}Qd7} \textbf{29.} \textbf{a4} \textbf{Nf6} \textbf{30.} \textbf{Re5} \textbf{Nd5} \textbf{31.} \textbf{R5e4} ( 31. R5e2 Rb8 \wbetter{} ) \textbf{Nf6} {} \textbf{32.} \textbf{Re5} \textbf{Kh8} ( 32... Nd5 33. R5e2 \wbetter{} ) \textbf{33.} \textbf{Kg1} \textbf{Nd5} \newline A valuable piece\par \textbf{34.} \textbf{R5e2} \textbf{a6} \textbf{35.} \textbf{Qd3} \textbf{Qh3} ( 35... Ra8 \wbetter{} ) \textbf{36.} \textbf{Nf3} ( 36. Qxa6 Ref8 37. Qa5 \wbetter{} ( 37. Qxc6 Rxf2 38. Rxf2 Qxg3+ 39. Kh1 Rxf2 ( 39... Qxf2 ?!{} 40. Rf1 Qa2 41. Rxf8+ Bxf8 42. Kg1 {} ) 40. Qa8+ Bf8 41. Qxf8+ Rxf8 42. Re2 Rf2 43. Rxf2 Qxf2 44. Ng4 Qf1+ 45. Kh2 Nf4 46. Ne3 Qf2+ 47. Kh1 Qxh4+ 48. Kg1 Qg3+ 49. Kf1 Qxe3 50. Bb6 Qe2+ 51. Kg1 Qg2\# ) ( 37. Bxe7 ??{} White will choke on that pawn Rxf2 ( 37... Nxe7 ?!{} 38. Rd1 -+{} ) 38. Rxf2 Qxg3+ 39. Kh1 Rxf2 ( 39... Qxf2 40. Re2 Qxd4 41. Bxf8 Qd1+ 42. Kg2 Nf4+ 43. Kf3 {} ) 40. Qc8+ Bf8 41. Bf6+ Nxf6 42. Qxf8+ Ng8 43. Qxf2 Qxf2 -+{} ) ) \textbf{Nf4} \bbetter{} \newline Do you see the mate threat?\par \textbf{37.} \textbf{gxf4} \textbf{Rxf4} \newline The mate threat is Rg4\par \textbf{38.} \textbf{Ne5} ( 38. Re4 !?{} is worthy of consideration Qg4+ 39. Kf1 Rxf3 40. Rxg4 Rxd3 41. Re6 \bbetter{} ) \textbf{Rg4+} !{} \ensuremath{\mp}{} \newline keeping the advantage\par \textbf{39.} \textbf{Nxg4} \newline Theme: Deflection from d3\par \textbf{\ldots{}Qxd3} \textbf{40.} \textbf{Re4} \textbf{Qf3} \textbf{41.} \textbf{Nh2} \textbf{Qf5} \textbf{42.} \textbf{Bxe7} ( 42. Kg2 Rf8 43. f3 Bf6 -+{} ) \textbf{Kg8} \textbf{43.} \textbf{f3} ( 43. R1e3 Qd5 -+{} ) \textbf{Qh3} -+{} \textbf{44.} \textbf{R1e2} ( 44. Kh1 -+{} ) \textbf{Qg3+} \textbf{45.} \textbf{Rg2} \textbf{Bxd4+} !{} \newline a devastating blow\par \textbf{46.} \textbf{Kh1} ( 46. Rxd4 A deflection Qe1+ Theme: Double Attack ) \textbf{Qh3} \textbf{47.} \textbf{Rgg4} ( 47. a5 Bc3 -+{} ) \textbf{c5} \textbf{48.} \textbf{h5} ( 48. Re1 does not improve anything Bf6 49. Rge4 Bxe7 50. Rxe7 Rxe7 51. Rxe7 Qxh4 -+{} ) \textbf{Rb8} \textbf{49.} \textbf{Re1} \textbf{Be5} \textbf{50.} \textbf{Rh4} \textbf{Qf5} \textbf{51.} \textbf{hxg6} \textbf{hxg6} \textbf{52.} \textbf{Re2} ( 52. Bxc5 doesn't change the outcome of the game Bg3 53. Rb4 Rc8 -+{} ( 53... Bxe1 ?!{} is clearly worse 54. Rxb8+ Kh7 55. Rb7+ Kh8 56. Rb8+ Kg7 57. Rb7+ Kf6 58. Ng4+ Kg5 59. Be3+ Kh4 60. Kg2 Qc2+ 61. Nf2 \bbetter{} ) ( 53... Qxc5 ?!{} is much worse 54. Rxb8+ Bxb8 55. Re8+ Kf7 56. Rxb8 -+{} ) ) \textbf{Rb1+} ( 52... Qb1+ !?{} keeps an even firmer grip 53. Kg2 Rb2 54. Rxb2 Qxb2+ 55. Kg1 -+{} ) \textbf{53.} \textbf{Kg2} \textbf{Bd4} \textbf{54.} \textbf{Ng4} ( 54. Rxd4 no good, but what else? cxd4 55. Ng4 -+{} ) \textbf{Rg1+} \textbf{55.} \textbf{Kh2} ( 55. Kh3 doesn't do any good Qxf3+ 56. Kh2 Rh1\# ) \textbf{Qf4+} ( 55... Qf4+ 56. Kh3 Qg3\# ) \textbf{0-1}|}
in
ae expected actual;
close_in ic
let test_pgn_with_clock_to_tex _ctxt =
let filename = "pgn_examples/2.pgn" in
let ic = In_channel.open_text filename in
let input_string = In_channel.input_all ic in
let actual = Pgn2tex.to_tex input_string ~diagram_data ~clock:true in
let expected =
{|\title{Casual Rapid game}
\author{montillo2015 (1919)\\reassessyourchess (1869)}
\date{2023.12.03, https://lichess.org/9Z0taHcG}\maketitle
\newchessgame
\textbf{1.} \textbf{d4} \textbf{1...} \textbf{Nf6} \textbf{2.} \textbf{Nf3} \textbf{2...} \textbf{d5} \textbf{3.} \textbf{e3}\par\medskip\noindent\begin{minipage}{\linewidth}0:10:01\par\nopagebreak\smallskip
\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\nopagebreak\vspace{1em}
0:10:00\end{minipage}\par\medskip \textbf{3...} \textbf{e6}\par\medskip\noindent\begin{minipage}{\linewidth}0:10:01\par\nopagebreak\smallskip
\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 3, vmargin=false]\par\nopagebreak\vspace{1em}
0:10:01\end{minipage}\par\medskip \newline D05 Queen's Pawn Game: Colle System\par \textbf{4.} \textbf{a3} \textbf{4...} \textbf{c5} \textbf{5.} \textbf{c4} \textbf{5...} \textbf{Nc6}\par\medskip\noindent\begin{minipage}{\linewidth}0:10:02\par\nopagebreak\smallskip
\chessboard[setfen=r1bqkbnr/pp1ppppp/2n5/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 3 5, vmargin=false]\par\nopagebreak\vspace{1em}
0:10:02\end{minipage}\par\medskip \textbf{6.} \textbf{dxc5} \textbf{6...} \textbf{Bxc5} \textbf{7.} \textbf{b4} \textbf{7...} \textbf{Be7} \textbf{8.} \textbf{Bb2}\par\medskip\noindent\begin{minipage}{\linewidth}0:10:00\par\nopagebreak\smallskip
\chessboard[setfen=r1bqkb1r/pp1ppppp/2n5/2p5/4P3/2N2N2/PPPP1PPP/R1BQKB1R b KQkq - 5 7, vmargin=false]\par\nopagebreak\vspace{1em}
0:10:03\end{minipage}\par\medskip \textbf{8...} \textbf{dxc4} \textbf{9.} \textbf{Qxd8+} \textbf{9...} \textbf{Nxd8} \textbf{10.} \textbf{Bxc4} \textbf{10...} \textbf{O-O} \textbf{11.} \textbf{O-O} \textbf{11...} \textbf{a6} \textbf{12.} \textbf{Nc3} \textbf{12...} \textbf{b5} \textbf{13.} \textbf{Be2} \textbf{13...} \textbf{Bb7} \textbf{14.} \textbf{Nd4} \textbf{14...} \textbf{Ne4} \textbf{15.} \textbf{Nxe4} \textbf{15...} \textbf{Bxe4} \textbf{16.} \textbf{Bf3} \textbf{16...} \textbf{Bxf3} \textbf{17.} \textbf{Nxf3} \textbf{17...} \textbf{Nc6} \textbf{18.} \textbf{Rac1} \textbf{18...} \textbf{Rac8} \textbf{19.} \textbf{Rfd1} \textbf{19...} \textbf{Rfd8} \textbf{20.} \textbf{Rxd8+} \textbf{20...} \textbf{Rxd8} \textbf{21.} \textbf{g3} \textbf{21...} \textbf{Nb8} \textbf{22.} \textbf{Rc7} \textbf{22...} \textbf{Bd6} \textbf{23.} \textbf{Rc2} \textbf{23...} \textbf{f6} \textbf{24.} \textbf{Kg2} \textbf{24...} \textbf{e5} \textbf{25.} \textbf{h4} \textbf{25...} \textbf{e4} \textbf{26.} \textbf{Nd4} \textbf{26...} \textbf{Kf7} \textbf{27.} \textbf{Nf5} \textbf{27...} \textbf{Nd7} \textbf{28.} \textbf{Nxd6+} \newline Black resigns.\par \textbf{1-0}|}
in
ae expected actual;
close_in ic
let test_pgn_with_unsupported_text _ctxt =
let filename = "pgn_examples/4.pgn" in
let ic = In_channel.open_text filename in
let input_string = In_channel.input_all ic in
let actual = Pgn2tex.to_tex input_string ~diagram_data ~clock:true in
let expected =
{|\title{Casual Rapid game}
\author{montillo2015 (1919) vs. reassessyourchess (1869)}
\date{2023.12.03, https://lichess.org/9Z0taHcG}\maketitle
\newchessgame
\textbf{1.} \textbf{d4} \textbf{1...} \textbf{Nf6} \textbf{2.} \textbf{Nf3} \textbf{2...} \textbf{d5} \textbf{3.} \textbf{e3}\par\nobreak 0:10:01\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:00\par \textbf{3...} \textbf{e6}\par\nobreak 0:10:01\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:01\par \newline D05 Queen's Pawn Game: Colle System\par \textbf{4.} \textbf{a3} \textbf{4...} \textbf{c5} \textbf{5.} \textbf{c4} \textbf{5...} \textbf{Nc6}\par\nobreak 0:10:02\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:02\par \textbf{6.} \textbf{dxc5} \textbf{6...} \textbf{Bxc5} \textbf{7.} \textbf{b4} \textbf{7...} \textbf{Be7} \textbf{8.} \textbf{Bb2}\par\nobreak 0:10:00\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:03\par \textbf{8...} \textbf{dxc4} \textbf{9.} \textbf{Qxd8+} \textbf{9...} \textbf{Nxd8} \textbf{10.} \textbf{Bxc4} \textbf{10...} \textbf{O-O} \textbf{11.} \textbf{O-O} \textbf{11...} \textbf{a6} \textbf{12.} \textbf{Nc3} \textbf{12...} \textbf{b5} \textbf{13.} \textbf{Be2} \textbf{13...} \textbf{Bb7} \textbf{14.} \textbf{Nd4} \textbf{14...} \textbf{Ne4} \textbf{15.} \textbf{Nxe4} \textbf{15...} \textbf{Bxe4} \textbf{16.} \textbf{Bf3} \textbf{16...} \textbf{Bxf3} \textbf{17.} \textbf{Nxf3} \textbf{17...} \textbf{Nc6} \textbf{18.} \textbf{Rac1} \textbf{18...} \textbf{Rac8} \textbf{19.} \textbf{Rfd1} \textbf{19...} \textbf{Rfd8} \textbf{20.} \textbf{Rxd8+} \textbf{20...} \textbf{Rxd8} \textbf{21.} \textbf{g3} \textbf{21...} \textbf{Nb8} \textbf{22.} \textbf{Rc7} \textbf{22...} \textbf{Bd6} \textbf{23.} \textbf{Rc2} \textbf{23...} \textbf{f6} \textbf{24.} \textbf{Kg2} \textbf{24...} \textbf{e5} \textbf{25.} \textbf{h4} \textbf{25...} \textbf{e4} \textbf{26.} \textbf{Nd4} \textbf{26...} \textbf{Kf7} \textbf{27.} \textbf{Nf5} \textbf{27...} \textbf{Nd7} \textbf{28.} \textbf{Nxd6+} \newline Black resigns.\par \textbf{1-0}|}
in
ae expected actual;
close_in ic
let test_pgn_with_custom_headers _ctxt =
let filename = "pgn_examples/7.pgn" in
let ic = In_channel.open_text filename in
let input_string = In_channel.input_all ic in
let actual = Pgn2tex.to_tex input_string ~diagram_data ~clock:false in
let expected =
{|\title{Casual Rapid game}
\author{montillo2015 (1919) vs. reassessyourchess (1869)}
\date{2023.12.03, https://lichess.org/9Z0taHcG}\maketitle
\newchessgame
\textbf{1.} \textbf{d4} \textbf{1...} \textbf{Nf6} \textbf{2.} \textbf{Nf3} \textbf{2...} \textbf{d5} \textbf{3.} \textbf{e3}\par\nobreak 0:10:01\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:00\par \textbf{3...} \textbf{e6}\par\nobreak 0:10:01\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:01\par \newline D05 Queen's Pawn Game: Colle System\par \textbf{4.} \textbf{a3} \textbf{4...} \textbf{c5} \textbf{5.} \textbf{c4} \textbf{5...} \textbf{Nc6}\par\nobreak 0:10:02\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:02\par \textbf{6.} \textbf{dxc5} \textbf{6...} \textbf{Bxc5} \textbf{7.} \textbf{b4} \textbf{7...} \textbf{Be7} \textbf{8.} \textbf{Bb2}\par\nobreak 0:10:00\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:03\par \textbf{8...} \textbf{dxc4} \textbf{9.} \textbf{Qxd8+} \textbf{9...} \textbf{Nxd8} \textbf{10.} \textbf{Bxc4} \textbf{10...} \textbf{O-O} \textbf{11.} \textbf{O-O} \textbf{11...} \textbf{a6} \textbf{12.} \textbf{Nc3} \textbf{12...} \textbf{b5} \textbf{13.} \textbf{Be2} \textbf{13...} \textbf{Bb7} \textbf{14.} \textbf{Nd4} \textbf{14...} \textbf{Ne4} \textbf{15.} \textbf{Nxe4} \textbf{15...} \textbf{Bxe4} \textbf{16.} \textbf{Bf3} \textbf{16...} \textbf{Bxf3} \textbf{17.} \textbf{Nxf3} \textbf{17...} \textbf{Nc6} \textbf{18.} \textbf{Rac1} \textbf{18...} \textbf{Rac8} \textbf{19.} \textbf{Rfd1} \textbf{19...} \textbf{Rfd8} \textbf{20.} \textbf{Rxd8+} \textbf{20...} \textbf{Rxd8} \textbf{21.} \textbf{g3} \textbf{21...} \textbf{Nb8} \textbf{22.} \textbf{Rc7} \textbf{22...} \textbf{Bd6} \textbf{23.} \textbf{Rc2} \textbf{23...} \textbf{f6} \textbf{24.} \textbf{Kg2} \textbf{24...} \textbf{e5} \textbf{25.} \textbf{h4} \textbf{25...} \textbf{e4} \textbf{26.} \textbf{Nd4} \textbf{26...} \textbf{Kf7} \textbf{27.} \textbf{Nf5} \textbf{27...} \textbf{Nd7} \textbf{28.} \textbf{Nxd6+} \newline Black resigns.\par \textbf{1-0}|}
in
ae expected actual;
close_in ic
let test_pgn_with_different_unsupported_text _ctxt =
let filename = "pgn_examples/10.pgn" in
let ic = In_channel.open_text filename in
let input_string = In_channel.input_all ic in
let actual = Pgn2tex.to_tex input_string ~diagram_data ~clock:false in
let expected =
{|\title{Casual Rapid game}
\author{montillo2015 (1919) vs. reassessyourchess (1869)}
\date{2023.12.03, https://lichess.org/9Z0taHcG}\maketitle
\newchessgame
\textbf{1.} \textbf{d4} \textbf{1...} \textbf{Nf6} \textbf{2.} \textbf{Nf3} \textbf{2...} \textbf{d5} \textbf{3.} \textbf{e3}\par\nobreak 0:10:01\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:00\par \textbf{3...} \textbf{e6}\par\nobreak 0:10:01\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:01\par \newline D05 Queen's Pawn Game: Colle System\par \textbf{4.} \textbf{a3} \textbf{4...} \textbf{c5} \textbf{5.} \textbf{c4} \textbf{5...} \textbf{Nc6}\par\nobreak 0:10:02\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:02\par \textbf{6.} \textbf{dxc5} \textbf{6...} \textbf{Bxc5} \textbf{7.} \textbf{b4} \textbf{7...} \textbf{Be7} \textbf{8.} \textbf{Bb2}\par\nobreak 0:10:00\par\nobreak\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip\nobreak\ 0:10:03\par \textbf{8...} \textbf{dxc4} \textbf{9.} \textbf{Qxd8+} \textbf{9...} \textbf{Nxd8} \textbf{10.} \textbf{Bxc4} \textbf{10...} \textbf{O-O} \textbf{11.} \textbf{O-O} \textbf{11...} \textbf{a6} \textbf{12.} \textbf{Nc3} \textbf{12...} \textbf{b5} \textbf{13.} \textbf{Be2} \textbf{13...} \textbf{Bb7} \textbf{14.} \textbf{Nd4} \textbf{14...} \textbf{Ne4} \textbf{15.} \textbf{Nxe4} \textbf{15...} \textbf{Bxe4} \textbf{16.} \textbf{Bf3} \textbf{16...} \textbf{Bxf3} \textbf{17.} \textbf{Nxf3} \textbf{17...} \textbf{Nc6} \textbf{18.} \textbf{Rac1} \textbf{18...} \textbf{Rac8} \textbf{19.} \textbf{Rfd1} \textbf{19...} \textbf{Rfd8} \textbf{20.} \textbf{Rxd8+} \textbf{20...} \textbf{Rxd8} \textbf{21.} \textbf{g3} \textbf{21...} \textbf{Nb8} \textbf{22.} \textbf{Rc7} \textbf{22...} \textbf{Bd6} \textbf{23.} \textbf{Rc2} \textbf{23...} \textbf{f6} \textbf{24.} \textbf{Kg2} \textbf{24...} \textbf{e5} \textbf{25.} \textbf{h4} \textbf{25...} \textbf{e4} \textbf{26.} \textbf{Nd4} \textbf{26...} \textbf{Kf7} \textbf{27.} \textbf{Nf5} \textbf{27...} \textbf{Nd7} \textbf{28.} \textbf{Nxd6+} \newline Black resigns.\par \textbf{1-0}|}
in
ae expected actual;
close_in ic
let suite =
"pgn2tex tests"
>::: [
"json_str_to_map" >:: test_json_str_to_map;
"basic_pgn_to_tex" >:: test_basic_pgn_to_tex;
"pgn_with_clock_to_tex" >:: test_pgn_with_clock_to_tex;
(* "pgn_with_unsupported_text" >:: test_pgn_with_unsupported_text; *)
(* "pgn_with_custom_headers" >:: test_pgn_with_custom_headers; *)
(* "pgn_with_different_unsupported_text"
>:: test_pgn_with_different_unsupported_text; *)
]
let () = run_test_tt_main suite
+101
View File
@@ -0,0 +1,101 @@
open OUnit2
let ae expected actual = assert_equal ~printer:(fun x -> x) expected actual
let contains = Helpers.Text_helper.contains
let game_diagram_json_str =
{| [
{"ply":5,"fen":"rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3"},
{"ply":6,"fen":"rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 3"},
{"ply":10,"fen":"r1bqkbnr/pp1ppppp/2n5/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 3 5"},
{"ply":15,"fen":"r1bqkb1r/pp1ppppp/2n5/2p5/4P3/2N2N2/PPPP1PPP/R1BQKB1R b KQkq - 5 7"}
] |}
let diagram_data = Pgn_to_tex.parse_diagrams_json game_diagram_json_str
let variation_diagram_json_str =
{| [{"ply":4,"fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1","path":["e4","e5","Nf3","d6"]}] |}
let test_variation_diagram_to_tex _ctxt =
let pgn = "1. e4 e5 2. Nf3 Nc6 (2... d6) 3. Bb5" in
let diagrams = Pgn_to_tex.parse_diagrams_json variation_diagram_json_str in
let rendered = Pgn_to_tex.to_tex ~diagram_data:diagrams ~clock:false pgn in
let fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" in
assert_bool
(Printf.sprintf "variation diagram misplaced in:\n%s" rendered)
(contains
~needle:("2... d6\n\\par\\nobreak\\medskip\\chessboard[setfen=" ^ fen)
rendered);
assert_bool "variation diagram should use smallboard"
(contains ~needle:"smallboard" rendered)
let test_basic_pgn_to_tex _ctxt =
let filename = "pgn_examples/1.pgn" in
let ic = In_channel.open_text filename in
let input_string = In_channel.input_all ic in
let actual = Pgn_to_tex.to_tex ~diagram_data ~clock:false input_string in
let expected =
{|\title{URS-chJ}
\author{Ibragimov, Ildar (2455)\\Kramnik, Vladimir (2480)}
\date{1991.??.??, Kherson}\maketitle
\newchessgame
\textbf{1.} \textbf{d4} \newline A88: Dutch Defence: Leningrad System: 5 Nf3 0-0 6 0-0 d6 7 Nc3 c6\par \textbf{\ldots{}d6} \textbf{2.} \textbf{c4} \textbf{f5} \textbf{3.} \textbf{Nf3}
\par\nobreak\medskip\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\medskip
\textbf{\ldots{}}\textbf{Nf6}
\par\nobreak\medskip\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 3, vmargin=false]\par\medskip
\textbf{4.} \textbf{g3} \textbf{g6} \textbf{5.} \textbf{Bg2} \textbf{Bg7}
\par\nobreak\medskip\chessboard[setfen=r1bqkbnr/pp1ppppp/2n5/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 3 5, vmargin=false]\par\medskip
\textbf{6.} \textbf{O-O} \textbf{O-O} \textbf{7.} \textbf{Nc3} \textbf{Qe8} \textbf{8.} \textbf{b3}
\par\nobreak\medskip\chessboard[setfen=r1bqkb1r/pp1ppppp/2n5/2p5/4P3/2N2N2/PPPP1PPP/R1BQKB1R b KQkq - 5 7, vmargin=false]\par\medskip
\textbf{\ldots{}}\textbf{Na6} \textbf{9.} \textbf{Ba3} \textbf{c6} \textbf{10.} \textbf{Qd3} \textbf{Rb8} \textbf{11.} \textbf{e4} \textbf{fxe4} \textbf{12.} \textbf{Nxe4} \textbf{Bf5} \textbf{13.} \textbf{Nxf6+} \textbf{Bxf6} \textbf{14.} \textbf{Qd2} \textbf{Nc7} \textbf{15.} \textbf{Rae1} \textbf{Qd7} \textbf{16.} \textbf{h4} \textbf{b5} \textbf{17.} \textbf{Re3} \textbf{bxc4} \textbf{18.} \textbf{bxc4} \textbf{Bh3} \textbf{19.} \textbf{Rfe1} \textbf{Bxg2} \newline last book move\par \textbf{20.} \textbf{Kxg2} \textbf{Qf5} \textbf{21.} \textbf{Re4} \textbf{Rbe8} \textbf{22.} \textbf{Rf4} \textbf{Qc8} \textbf{23.} \textbf{Qa5} \textbf{d5} \textbf{24.} \textbf{cxd5} \textbf{Nxd5} \textbf{25.} \textbf{Rfe4} \newline e7 seems the pivot of the position\par \textbf{\ldots{}Qf5} \textbf{26.} \textbf{Qd2} ( 26. Qxa7 ??{} taking the pawn is naive Qxf3+ Annihilates a defender: f3 27. Kxf3 Bxd4+ 28. Kg4 Bxa7 29. Bxe7 Rxf2 -+{} ( 29... Rxe7 30. Rxe7 Bxf2 31. a4 -+{} ) ( 29... Nxe7 ?!{} 30. Rxe7 Rxe7 31. Rxe7 Bxf2 32. a4 -+{} ) ) \textbf{Bg7} \textbf{27.} \textbf{Nh2} \textbf{Rf7} \textbf{28.} \textbf{Bc5} \newline The white bishop on an outpost\par \textbf{\ldots{}Qd7} \textbf{29.} \textbf{a4} \textbf{Nf6} \textbf{30.} \textbf{Re5} \textbf{Nd5} \textbf{31.} \textbf{R5e4} ( 31. R5e2 Rb8 \wbetter{} ) \textbf{Nf6} {} \textbf{32.} \textbf{Re5} \textbf{Kh8} ( 32... Nd5 33. R5e2 \wbetter{} ) \textbf{33.} \textbf{Kg1} \textbf{Nd5} \newline A valuable piece\par \textbf{34.} \textbf{R5e2} \textbf{a6} \textbf{35.} \textbf{Qd3} \textbf{Qh3} ( 35... Ra8 \wbetter{} ) \textbf{36.} \textbf{Nf3} ( 36. Qxa6 Ref8 37. Qa5 \wbetter{} ( 37. Qxc6 Rxf2 38. Rxf2 Qxg3+ 39. Kh1 Rxf2 ( 39... Qxf2 ?!{} 40. Rf1 Qa2 41. Rxf8+ Bxf8 42. Kg1 {} ) 40. Qa8+ Bf8 41. Qxf8+ Rxf8 42. Re2 Rf2 43. Rxf2 Qxf2 44. Ng4 Qf1+ 45. Kh2 Nf4 46. Ne3 Qf2+ 47. Kh1 Qxh4+ 48. Kg1 Qg3+ 49. Kf1 Qxe3 50. Bb6 Qe2+ 51. Kg1 Qg2\# ) ( 37. Bxe7 ??{} White will choke on that pawn Rxf2 ( 37... Nxe7 ?!{} 38. Rd1 -+{} ) 38. Rxf2 Qxg3+ 39. Kh1 Rxf2 ( 39... Qxf2 40. Re2 Qxd4 41. Bxf8 Qd1+ 42. Kg2 Nf4+ 43. Kf3 {} ) 40. Qc8+ Bf8 41. Bf6+ Nxf6 42. Qxf8+ Ng8 43. Qxf2 Qxf2 -+{} ) ) \textbf{Nf4} \bbetter{} \newline Do you see the mate threat?\par \textbf{37.} \textbf{gxf4} \textbf{Rxf4} \newline The mate threat is Rg4\par \textbf{38.} \textbf{Ne5} ( 38. Re4 !?{} is worthy of consideration Qg4+ 39. Kf1 Rxf3 40. Rxg4 Rxd3 41. Re6 \bbetter{} ) \textbf{Rg4+} !{} \ensuremath{\mp}{} \newline keeping the advantage\par \textbf{39.} \textbf{Nxg4} \newline Theme: Deflection from d3\par \textbf{\ldots{}Qxd3} \textbf{40.} \textbf{Re4} \textbf{Qf3} \textbf{41.} \textbf{Nh2} \textbf{Qf5} \textbf{42.} \textbf{Bxe7} ( 42. Kg2 Rf8 43. f3 Bf6 -+{} ) \textbf{Kg8} \textbf{43.} \textbf{f3} ( 43. R1e3 Qd5 -+{} ) \textbf{Qh3} -+{} \textbf{44.} \textbf{R1e2} ( 44. Kh1 -+{} ) \textbf{Qg3+} \textbf{45.} \textbf{Rg2} \textbf{Bxd4+} !{} \newline a devastating blow\par \textbf{46.} \textbf{Kh1} ( 46. Rxd4 A deflection Qe1+ Theme: Double Attack ) \textbf{Qh3} \textbf{47.} \textbf{Rgg4} ( 47. a5 Bc3 -+{} ) \textbf{c5} \textbf{48.} \textbf{h5} ( 48. Re1 does not improve anything Bf6 49. Rge4 Bxe7 50. Rxe7 Rxe7 51. Rxe7 Qxh4 -+{} ) \textbf{Rb8} \textbf{49.} \textbf{Re1} \textbf{Be5} \textbf{50.} \textbf{Rh4} \textbf{Qf5} \textbf{51.} \textbf{hxg6} \textbf{hxg6} \textbf{52.} \textbf{Re2} ( 52. Bxc5 doesn't change the outcome of the game Bg3 53. Rb4 Rc8 -+{} ( 53... Bxe1 ?!{} is clearly worse 54. Rxb8+ Kh7 55. Rb7+ Kh8 56. Rb8+ Kg7 57. Rb7+ Kf6 58. Ng4+ Kg5 59. Be3+ Kh4 60. Kg2 Qc2+ 61. Nf2 \bbetter{} ) ( 53... Qxc5 ?!{} is much worse 54. Rxb8+ Bxb8 55. Re8+ Kf7 56. Rxb8 -+{} ) ) \textbf{Rb1+} ( 52... Qb1+ !?{} keeps an even firmer grip 53. Kg2 Rb2 54. Rxb2 Qxb2+ 55. Kg1 -+{} ) \textbf{53.} \textbf{Kg2} \textbf{Bd4} \textbf{54.} \textbf{Ng4} ( 54. Rxd4 no good, but what else? cxd4 55. Ng4 -+{} ) \textbf{Rg1+} \textbf{55.} \textbf{Kh2} ( 55. Kh3 doesn't do any good Qxf3+ 56. Kh2 Rh1\# ) \textbf{Qf4+} ( 55... Qf4+ 56. Kh3 Qg3\# ) \textbf{0-1}|}
in
ae expected actual;
close_in ic
let test_pgn_with_clock_to_tex _ctxt =
let filename = "pgn_examples/2.pgn" in
let ic = In_channel.open_text filename in
let input_string = In_channel.input_all ic in
let actual = Pgn_to_tex.to_tex ~diagram_data ~clock:true input_string in
let expected =
{|\title{Casual Rapid game}
\author{montillo2015 (1919)\\reassessyourchess (1869)}
\date{2023.12.03, https://lichess.org/9Z0taHcG}\maketitle
\newchessgame
\textbf{1.} \textbf{d4} \textbf{1...} \textbf{Nf6} \textbf{2.} \textbf{Nf3} \textbf{2...} \textbf{d5} \textbf{3.} \textbf{e3}\par\medskip\noindent\begin{minipage}{\linewidth}0:10:01\par\nopagebreak\smallskip
\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3, vmargin=false]\par\nopagebreak\vspace{1em}
0:10:00\end{minipage}\par\medskip \textbf{3...} \textbf{e6}\par\medskip\noindent\begin{minipage}{\linewidth}0:10:01\par\nopagebreak\smallskip
\chessboard[setfen=rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 3, vmargin=false]\par\nopagebreak\vspace{1em}
0:10:01\end{minipage}\par\medskip \newline D05 Queen's Pawn Game: Colle System\par \textbf{4.} \textbf{a3} \textbf{4...} \textbf{c5} \textbf{5.} \textbf{c4} \textbf{5...} \textbf{Nc6}\par\medskip\noindent\begin{minipage}{\linewidth}0:10:02\par\nopagebreak\smallskip
\chessboard[setfen=r1bqkbnr/pp1ppppp/2n5/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 3 5, vmargin=false]\par\nopagebreak\vspace{1em}
0:10:02\end{minipage}\par\medskip \textbf{6.} \textbf{dxc5} \textbf{6...} \textbf{Bxc5} \textbf{7.} \textbf{b4} \textbf{7...} \textbf{Be7} \textbf{8.} \textbf{Bb2}\par\medskip\noindent\begin{minipage}{\linewidth}0:10:00\par\nopagebreak\smallskip
\chessboard[setfen=r1bqkb1r/pp1ppppp/2n5/2p5/4P3/2N2N2/PPPP1PPP/R1BQKB1R b KQkq - 5 7, vmargin=false]\par\nopagebreak\vspace{1em}
0:10:03\end{minipage}\par\medskip \textbf{8...} \textbf{dxc4} \textbf{9.} \textbf{Qxd8+} \textbf{9...} \textbf{Nxd8} \textbf{10.} \textbf{Bxc4} \textbf{10...} \textbf{O-O} \textbf{11.} \textbf{O-O} \textbf{11...} \textbf{a6} \textbf{12.} \textbf{Nc3} \textbf{12...} \textbf{b5} \textbf{13.} \textbf{Be2} \textbf{13...} \textbf{Bb7} \textbf{14.} \textbf{Nd4} \textbf{14...} \textbf{Ne4} \textbf{15.} \textbf{Nxe4} \textbf{15...} \textbf{Bxe4} \textbf{16.} \textbf{Bf3} \textbf{16...} \textbf{Bxf3} \textbf{17.} \textbf{Nxf3} \textbf{17...} \textbf{Nc6} \textbf{18.} \textbf{Rac1} \textbf{18...} \textbf{Rac8} \textbf{19.} \textbf{Rfd1} \textbf{19...} \textbf{Rfd8} \textbf{20.} \textbf{Rxd8+} \textbf{20...} \textbf{Rxd8} \textbf{21.} \textbf{g3} \textbf{21...} \textbf{Nb8} \textbf{22.} \textbf{Rc7} \textbf{22...} \textbf{Bd6} \textbf{23.} \textbf{Rc2} \textbf{23...} \textbf{f6} \textbf{24.} \textbf{Kg2} \textbf{24...} \textbf{e5} \textbf{25.} \textbf{h4} \textbf{25...} \textbf{e4} \textbf{26.} \textbf{Nd4} \textbf{26...} \textbf{Kf7} \textbf{27.} \textbf{Nf5} \textbf{27...} \textbf{Nd7} \textbf{28.} \textbf{Nxd6+} \newline Black resigns.\par \textbf{1-0}|}
in
ae expected actual;
close_in ic
let test_default_arguments _ctxt =
let actual = Pgn_to_tex.to_tex "1. e4 e5" in
let expected =
"\\title{}\n\
\\author{}\n\
\\date{}\\maketitle\n\
\\newchessgame\n\
\\textbf{1.} \\textbf{e4} \\textbf{e5}"
in
ae expected actual
let test_invalid_pgn_raises _ctxt =
assert_raises (Failure "Parse error") (fun () ->
ignore (Pgn_to_tex.to_tex "1. e4 e5 (2. Nf3"))
let suite =
"pgn_to_tex tests"
>::: [
"variation_diagram_to_tex" >:: test_variation_diagram_to_tex;
"basic_pgn_to_tex" >:: test_basic_pgn_to_tex;
"pgn_with_clock_to_tex" >:: test_pgn_with_clock_to_tex;
"default_arguments" >:: test_default_arguments;
"invalid_pgn_raises" >:: test_invalid_pgn_raises;
]
+119
View File
@@ -0,0 +1,119 @@
open OUnit2
let contains = Helpers.Text_helper.contains
let read_file path =
let ic = In_channel.open_text path in
Fun.protect
~finally:(fun () -> In_channel.close ic)
(fun () -> In_channel.input_all ic)
let render_fixture filename =
Pgn_to_tex.to_tex (read_file @@ "pgn_examples/" ^ filename)
let all_percents_escaped text =
let rec loop i =
if i >= String.length text then true
else if text.[i] = '%' then i > 0 && text.[i - 1] = '\\' && loop (i + 1)
else loop (i + 1)
in
loop 0
let test_author_with_players _ctxt =
let pgn =
{|[Event "Club Night"]
[White "Alice"]
[Black "Bob"]
[Author "Carol"]
[Date "2024.01.02"]
1. e4 e5 1-0
|}
in
let tex = Pgn_to_tex.to_tex pgn in
assert_bool "author header should be present"
(contains ~needle:"\\author{Carol}" tex);
assert_bool "author header should not be empty"
(not (contains ~needle:"\\author{}" tex));
assert_bool "player names should not replace the author"
(not (contains ~needle:"Alice" tex))
let test_title_without_subtitle _ctxt =
let pgn =
{|[Event "Club Night"]
[Title "Club Championship"]
[Date "2024.01.02"]
1. e4 e5 1-0
|}
in
let tex = Pgn_to_tex.to_tex pgn in
assert_bool "title should be rendered"
(contains ~needle:"\\title{Club Championship}" tex);
assert_bool "empty subtitle block should be omitted"
(not (contains ~needle:"\\large{}" tex));
assert_bool "subtitle separator should be omitted"
(not (contains ~needle:"\\\\[2ex]" tex))
let test_title_subtitle_author_fixture _ctxt =
let expected =
{|\title{Some Title\\[2ex]\large{Some Subtitle}}
\author{Some Author}
\date{2025.12.23}\maketitle
\newchessgame
\textbf{1.} \textbf{d4} \textbf{d5} \textbf{2.} \textbf{c4} \textbf{dxc4} \textbf{3.} \textbf{e4} \textbf{Nf6} \textbf{4.} \textbf{e5} \textbf{Nd5} \textbf{5.} \textbf{Bxc4} \textbf{Nb6} \textbf{6.} \textbf{Bb3} \textbf{e6} ( 6... Nc6 ) \textbf{7.} \textbf{Nf3} \textbf{c5} \textbf{8.} \textbf{Be3} \textbf{N8d7} \textbf{9.} \textbf{O-O} \textbf{c4} \textbf{10.} \textbf{Bc2} \textbf{Nd5} \textbf{11.} \textbf{Nc3} \textbf{N7b6} \textbf{12.} \textbf{Be4} \textbf{Bb4} \textbf{13.} \textbf{Bxd5} \textbf{Nxd5} ( 13... Bxc3 ) \textbf{14.} \textbf{Nxd5} \textbf{exd5} \textbf{15.} \textbf{Qa4+} \textbf{Bd7} \textbf{16.} \textbf{Qxb4} \textbf{Qb6} \textbf{17.} \textbf{Qxb6} \textbf{axb6} \textbf{18.} \textbf{Rfc1} \textbf{O-O} \textbf{19.} \textbf{Nd2} \textbf{Bf5} \textbf{20.} \textbf{b3} \textbf{b5} \textbf{21.} \textbf{a4} \textbf{cxb3} \textbf{22.} \textbf{Nxb3} \textbf{bxa4} \textbf{23.} \textbf{Nc5} \textbf{b5} \textbf{24.} \textbf{Bd2} \textbf{Rfe8} \textbf{25.} \textbf{Bb4} \textbf{Rac8} \textbf{26.} \textbf{Nb7} \textbf{Rxc1+} \textbf{27.} \textbf{Rxc1} \textbf{Rc8} \textbf{28.} \textbf{Rxc8+} \textbf{Bxc8} \textbf{29.} \textbf{Nd6} \textbf{Bd7} \textbf{30.} \textbf{f4} \textbf{g6} \textbf{31.} \textbf{Kf2} \textbf{Kg7} \textbf{32.} \textbf{h3} \textbf{*}|}
in
assert_equal ~printer:(fun x -> x) expected (render_fixture "12.pgn")
let test_empty_date_and_site_fixture_5 _ctxt =
let tex = render_fixture "5.pgn" in
assert_bool "event title should be rendered"
(contains ~needle:"\\title{Obert Barad 2024}" tex);
assert_bool "players should be rendered when no author is present"
(contains ~needle:"\\author{Alejandro Fernandez Cardenete (1873)" tex);
assert_bool "empty date and site should render an empty date"
(contains ~needle:"\\date{}\\maketitle" tex)
let test_empty_date_fixture_6 _ctxt =
let tex = render_fixture "6.pgn" in
assert_bool "empty date should fall back to the site"
(contains ~needle:"\\date{Terrassa}\\maketitle" tex);
assert_bool "a missing date must not render a comma-only date"
(not (contains ~needle:"\\date{,}" tex))
let test_percent_escaping_fixture_8 _ctxt =
let tex = render_fixture "8.pgn" in
assert_bool "percent in a comment should be LaTeX-escaped"
(contains ~needle:"\\%" tex);
assert_bool "every percent in the output should be escaped"
(all_percents_escaped tex)
let fixture_names () =
Sys.readdir "pgn_examples" |> Array.to_list
|> List.filter (fun name -> Filename.check_suffix name ".pgn")
|> List.sort String.compare
let test_fixture_smoke =
List.map
(fun filename ->
filename >:: fun _ctxt ->
let tex = render_fixture filename in
assert_bool
(filename ^ " should render non-empty LaTeX")
(String.length tex > 0);
assert_bool
(filename ^ " should contain document setup")
(contains ~needle:"\\newchessgame" tex))
(fixture_names ())
let suite =
"real-world header and fixture tests"
>::: [
"author with players" >:: test_author_with_players;
"title without subtitle" >:: test_title_without_subtitle;
"title/subtitle/author fixture" >:: test_title_subtitle_author_fixture;
"empty date/site fixture 5" >:: test_empty_date_and_site_fixture_5;
"empty date fixture 6" >:: test_empty_date_fixture_6;
"percent escaping fixture 8" >:: test_percent_escaping_fixture_8;
"all fixtures" >::: test_fixture_smoke;
]
+4 -1
View File
@@ -6,8 +6,11 @@ let () =
>::: [
Test_lexer.suite;
Test_latex.suite;
Test_pgn2tex.suite;
Test_pgn_to_tex.suite;
Test_parser.suite;
Test_diagram_resolution.suite;
Test_nag.suite;
Test_real_world.suite;
]
in
run_test_tt_main all