mirror of
https://github.com/TheRealOwenRees/pgn_to_tex.git
synced 2026-11-18 12:52:06 +00:00
Compare commits
14
Commits
5831ddfb5e
...
v0.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dfa7d5bc7e | ||
|
|
c654beb5a8 | ||
|
|
4d20084f7e | ||
|
|
16fcaa3b13 | ||
|
|
4f50ff020e | ||
|
|
2bf11e92a8 | ||
|
|
786bad8956 | ||
|
|
135491e915 | ||
|
|
9cd7878ad8 | ||
|
|
548f98f303 | ||
|
|
8df30f1746 | ||
|
|
79eda1beab
|
||
|
|
c500b1625d
|
||
|
|
fe9eb8ba45 |
@@ -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
|
||||||
@@ -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
@@ -1,2 +1,2 @@
|
|||||||
profile = default
|
profile = default
|
||||||
version = 0.28.1
|
version = 0.29.0
|
||||||
|
|||||||
@@ -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`.
|
||||||
@@ -1,3 +1,72 @@
|
|||||||
|
## 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
|
## v0.0.1
|
||||||
|
|
||||||
- fix bug whereby a `subtitle` custom PGN header was rendering on a separate page. It now appears under the title
|
- fix bug whereby a `subtitle` custom PGN header was rendering on a separate page. It now appears under the title
|
||||||
|
|||||||
+59
-4
@@ -1,13 +1,68 @@
|
|||||||
# Contributing
|
# 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
|
## 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`.
|
- 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.
|
- 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.
|
||||||
|
|||||||
@@ -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:
|
- [API documentation](https://therealowenrees.github.io/pgn_to_tex/)
|
||||||
`opam install pgn_to_tex`
|
- [Changelog](CHANGELOG.md)
|
||||||
- Reference `pgn_to_tex` in the libraries stanza in the relevant dune file
|
- [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
|
```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 tex = Pgn_to_tex.to_tex pgn
|
||||||
let diagram_data = Pgn2tex.parse_diagrams_json diagrams_json_stringified
|
|
||||||
|
|
||||||
let game_tex = Pgn2tex.to_tex pgn ~diagram_data ~clock:false
|
|
||||||
```
|
```
|
||||||
|
|
||||||
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{document}
|
||||||
\begin{multicols}{2}
|
\begin{multicols}{2}
|
||||||
game_tex goes here
|
% Paste the string returned by Pgn_to_tex.to_tex here.
|
||||||
\end{multicols}
|
\end{multicols}
|
||||||
\end{document}
|
\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.
|
||||||
|
|
||||||
```
|
## JavaScript build
|
||||||
make 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
|
## Dependencies
|
||||||
|
|
||||||
- dune
|
Runtime and build dependencies:
|
||||||
- sedlex
|
|
||||||
- menhir
|
- `ocaml` >= 5.4
|
||||||
- yojson
|
- `dune` >= 3.21
|
||||||
- js_of_ocaml-compiler
|
- `sedlex` >= 3.7
|
||||||
- ocamlformat
|
- `menhir` >= 20260209
|
||||||
- ocaml-lsp-server
|
- `yojson` >= 3.0
|
||||||
- ounit2
|
|
||||||
|
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).
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
(executable
|
(executable
|
||||||
(name main)
|
(name pgn_to_tex_js)
|
||||||
(modes js)
|
(modes js)
|
||||||
|
(optional)
|
||||||
(libraries pgn_to_tex js_of_ocaml yojson)
|
(libraries pgn_to_tex js_of_ocaml yojson)
|
||||||
(preprocess
|
(preprocess
|
||||||
(pps js_of_ocaml-ppx))
|
(pps js_of_ocaml-ppx)))
|
||||||
(enabled_if
|
|
||||||
(= %{env:CI=false} "false")))
|
|
||||||
|
|||||||
-35
@@ -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)
|
|
||||||
@@ -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)
|
||||||
@@ -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}
|
||||||
@@ -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…_
|
||||||
@@ -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.
|
||||||
@@ -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.
|
||||||
+11
-3
@@ -16,9 +16,9 @@
|
|||||||
(license AGPL-3.0-or-later)
|
(license AGPL-3.0-or-later)
|
||||||
|
|
||||||
(documentation
|
(documentation
|
||||||
https://github.com/therealowenrees/pgn_to_tex/blob/main/README.md)
|
https://therealowenrees.github.io/pgn_to_tex/)
|
||||||
|
|
||||||
(version 0.0.1)
|
(version 0.1.0)
|
||||||
|
|
||||||
(package
|
(package
|
||||||
(name pgn_to_tex)
|
(name pgn_to_tex)
|
||||||
@@ -38,8 +38,16 @@
|
|||||||
(and
|
(and
|
||||||
:with-test
|
:with-test
|
||||||
(>= 2.2.7)))
|
(>= 2.2.7)))
|
||||||
|
(js_of_ocaml
|
||||||
|
(and
|
||||||
|
:with-test))
|
||||||
|
(js_of_ocaml-ppx
|
||||||
|
(and
|
||||||
|
:with-test))
|
||||||
("ocamlformat"
|
("ocamlformat"
|
||||||
(and :with-dev-setup)))
|
(and
|
||||||
|
:with-dev-setup
|
||||||
|
(= 0.29.0))))
|
||||||
(tags
|
(tags
|
||||||
("pgn" "latex" "chess" "parser")))
|
("pgn" "latex" "chess" "parser")))
|
||||||
|
|
||||||
|
|||||||
-14
@@ -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 }
|
|
||||||
@@ -1,10 +1,5 @@
|
|||||||
(library
|
(library
|
||||||
(name pgn_logic)
|
(name pgn_to_tex)
|
||||||
(public_name pgn_to_tex)
|
(public_name pgn_to_tex)
|
||||||
(libraries sedlex menhirLib yojson)
|
(modules pgn_to_tex)
|
||||||
(modules ast parser lexer parsing latex latex_helpers pgn2tex nag)
|
(libraries pgn_to_tex_internal))
|
||||||
(preprocess
|
|
||||||
(pps sedlex.ppx)))
|
|
||||||
|
|
||||||
(menhir
|
|
||||||
(modules parser))
|
|
||||||
|
|||||||
@@ -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 }
|
||||||
@@ -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
|
||||||
@@ -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))
|
||||||
@@ -27,7 +27,10 @@ let tags_to_tex tags =
|
|||||||
^ build_date_site_string date site
|
^ build_date_site_string date site
|
||||||
^ "\\maketitle\n\\newchessgame"
|
^ "\\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
|
| Number n -> if is_mainline then "\\textbf{" ^ n ^ "}" else n
|
||||||
| Move m ->
|
| Move m ->
|
||||||
if is_mainline then "\\textbf{" ^ escape_tex m ^ "}" else escape_tex 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"
|
if is_mainline then "\\newline " ^ escape_tex c ^ "\\par"
|
||||||
else escape_tex c
|
else escape_tex c
|
||||||
| Result r -> "\\textbf{" ^ r ^ "}"
|
| 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 ^ "{}"
|
| Nag g -> " " ^ Nag.nag_to_tex g ^ "{}"
|
||||||
| Clock c -> c
|
| Clock c -> c
|
||||||
|
|
||||||
and render_game is_mainline ?(diagram_data = MoveMap.empty) ?(clock = false)
|
and render_game is_mainline ?(diagram_data = Ast.empty_diagrams)
|
||||||
items =
|
?(clock = false) ?(path = []) items =
|
||||||
let rec aux ply interrupted ?(white_time = "0:00") ?(black_time = "0:00") =
|
let rec aux ply interrupted ~white_time ~black_time ~path ~branch_path =
|
||||||
function
|
function
|
||||||
| [] -> ""
|
| [] -> ""
|
||||||
| Comment c :: Move m :: tail when is_mainline && ply mod 2 != 0 ->
|
| Comment c :: Move m :: tail when is_mainline && ply mod 2 != 0 ->
|
||||||
let next_ply = ply + 1 in
|
let next_ply = ply + 1 in
|
||||||
|
let next_path = path @ [ m ] in
|
||||||
let rendered_comment = "\\newline " ^ escape_tex c ^ "\\par" in
|
let rendered_comment = "\\newline " ^ escape_tex c ^ "\\par" in
|
||||||
let rendered_move = "\\textbf{\\ldots{}" ^ escape_tex m ^ "}" in
|
let rendered_move = "\\textbf{\\ldots{}" ^ escape_tex m ^ "}" in
|
||||||
let diag_str =
|
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
|
| Some s -> s
|
||||||
| None -> ""
|
| None -> ""
|
||||||
in
|
in
|
||||||
let has_diag = diag_str <> "" in
|
let has_diag = diag_str <> "" in
|
||||||
rendered_comment ^ " " ^ rendered_move ^ diag_str ^ " "
|
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 -> (
|
| head :: tail -> (
|
||||||
match head with
|
match head with
|
||||||
| Clock c ->
|
| Clock c ->
|
||||||
if ply mod 2 = 0 then
|
if ply mod 2 = 0 then
|
||||||
aux ply interrupted ~white_time ~black_time:c tail
|
aux ply interrupted ~white_time ~black_time:c ~path ~branch_path
|
||||||
else aux ply interrupted ~white_time:c ~black_time tail
|
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 =
|
let next_ply =
|
||||||
match head with Move _ when is_mainline -> ply + 1 | _ -> ply
|
match head with Move _ when is_mainline -> ply + 1 | _ -> ply
|
||||||
in
|
in
|
||||||
|
|
||||||
|
let next_path =
|
||||||
|
match move_san with Some m -> path @ [ m ] | None -> path
|
||||||
|
in
|
||||||
|
|
||||||
let prefix =
|
let prefix =
|
||||||
if is_move && is_mainline && next_ply mod 2 == 0 && interrupted
|
if is_move && is_mainline && next_ply mod 2 == 0 && interrupted
|
||||||
then "\\ldots{}"
|
then "\\ldots{}"
|
||||||
@@ -77,7 +93,7 @@ and render_game is_mainline ?(diagram_data = MoveMap.empty) ?(clock = false)
|
|||||||
|
|
||||||
let rendered_head =
|
let rendered_head =
|
||||||
let raw_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
|
in
|
||||||
if prefix <> "" && raw_head <> "" then
|
if prefix <> "" && raw_head <> "" then
|
||||||
"\\textbf{" ^ prefix ^ "}" ^ raw_head
|
"\\textbf{" ^ prefix ^ "}" ^ raw_head
|
||||||
@@ -85,16 +101,23 @@ and render_game is_mainline ?(diagram_data = MoveMap.empty) ?(clock = false)
|
|||||||
in
|
in
|
||||||
|
|
||||||
let diag_str =
|
let diag_str =
|
||||||
if is_move && is_mainline then
|
if is_move then
|
||||||
get_diagram next_ply diagram_data ~clock ~white_time ~black_time
|
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
|
else None
|
||||||
in
|
in
|
||||||
|
|
||||||
let diag_output = match diag_str with Some s -> s | 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 is_comment = match head with Comment _ -> true | _ -> false in
|
||||||
let next_interrupted = diag_output <> "" || is_comment in
|
let next_interrupted = diag_output <> "" || is_comment in
|
||||||
|
let next_branch_path = if is_move then path else branch_path in
|
||||||
let rest =
|
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
|
in
|
||||||
|
|
||||||
if rendered_head = "" then rest
|
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
|
rendered_head ^ diag_output ^ if rest = "" then "" else " " ^ rest
|
||||||
)
|
)
|
||||||
in
|
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 game_to_tex game ~diagram_data ~clock =
|
||||||
let header_tex = tags_to_tex game.tags in
|
let header_tex = tags_to_tex game.tags in
|
||||||
@@ -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
|
||||||
@@ -76,8 +76,8 @@ and read_comment_clock buf =
|
|||||||
| clock_val ->
|
| clock_val ->
|
||||||
let time = Utf8.lexeme buf in
|
let time = Utf8.lexeme buf in
|
||||||
begin match%sedlex buf with
|
begin match%sedlex buf with
|
||||||
| ']' -> begin
|
| ']' ->
|
||||||
match%sedlex buf with
|
begin match%sedlex buf with
|
||||||
| Star white_space, '}' -> CLOCK time
|
| Star white_space, '}' -> CLOCK time
|
||||||
| _ -> failwith "Expected closing '}' after clock comment"
|
| _ -> failwith "Expected closing '}' after clock comment"
|
||||||
end
|
end
|
||||||
@@ -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")
|
|
||||||
@@ -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
|
|
||||||
@@ -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
|
||||||
@@ -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
@@ -1,6 +1,6 @@
|
|||||||
# This file is generated by dune, edit dune-project instead
|
# This file is generated by dune, edit dune-project instead
|
||||||
opam-version: "2.0"
|
opam-version: "2.0"
|
||||||
version: "0.0.1"
|
version: "0.1.0"
|
||||||
synopsis: "A chess PGN to TeX conversion tool"
|
synopsis: "A chess PGN to TeX conversion tool"
|
||||||
description:
|
description:
|
||||||
"Convert PGN files to LaTeX markdown, including rendered images of selected positions"
|
"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"
|
license: "AGPL-3.0-or-later"
|
||||||
tags: ["pgn" "latex" "chess" "parser"]
|
tags: ["pgn" "latex" "chess" "parser"]
|
||||||
homepage: "https://github.com/therealowenrees/pgn_to_tex"
|
homepage: "https://github.com/therealowenrees/pgn_to_tex"
|
||||||
doc: "https://github.com/therealowenrees/pgn_to_tex/blob/main/README.md"
|
doc: "https://therealowenrees.github.io/pgn_to_tex/"
|
||||||
bug-reports: "https://github.com/therealowenrees/pgn_to_tex/issues"
|
bug-reports: "https://github.com/therealowenrees/pgn_to_tex/issues"
|
||||||
depends: [
|
depends: [
|
||||||
"dune" {>= "3.21"}
|
"dune" {>= "3.21"}
|
||||||
@@ -18,7 +18,9 @@ depends: [
|
|||||||
"menhir" {>= "20260209"}
|
"menhir" {>= "20260209"}
|
||||||
"yojson" {>= "3.0"}
|
"yojson" {>= "3.0"}
|
||||||
"ounit2" {with-test & >= "2.2.7"}
|
"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}
|
"odoc" {with-doc}
|
||||||
]
|
]
|
||||||
build: [
|
build: [
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
(test
|
(test
|
||||||
(name test_suite)
|
(name test_suite)
|
||||||
(modules (:standard))
|
(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
|
(deps
|
||||||
(source_tree pgn_examples)))
|
(source_tree pgn_examples)))
|
||||||
|
|||||||
+2
-1
@@ -1,3 +1,4 @@
|
|||||||
(library
|
(library
|
||||||
(name helpers)
|
(name helpers)
|
||||||
(modules latex_helper))
|
(modules latex_helper text_helper)
|
||||||
|
(libraries pgn_to_tex_internal))
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
module MoveMap = Map.Make (Int)
|
open Pgn_to_tex_internal
|
||||||
|
open Ast
|
||||||
|
|
||||||
let diagram_data =
|
let diagram_data =
|
||||||
|
{
|
||||||
|
by_ply =
|
||||||
MoveMap.empty
|
MoveMap.empty
|
||||||
|> MoveMap.add 5
|
|> MoveMap.add 5
|
||||||
"rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3"
|
"rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 3"
|
||||||
@@ -9,4 +12,6 @@ let diagram_data =
|
|||||||
|> MoveMap.add 10
|
|> MoveMap.add 10
|
||||||
"r1bqkbnr/pp1ppppp/2n5/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 3 5"
|
"r1bqkbnr/pp1ppppp/2n5/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 3 5"
|
||||||
|> MoveMap.add 15
|
|> 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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -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");
|
||||||
@@ -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;
|
||||||
|
]
|
||||||
+24
-7
@@ -1,5 +1,5 @@
|
|||||||
open OUnit2
|
open OUnit2
|
||||||
open Pgn_logic
|
open Pgn_to_tex_internal
|
||||||
open Ast
|
open Ast
|
||||||
open Helpers
|
open Helpers
|
||||||
|
|
||||||
@@ -105,6 +105,10 @@ let test_date_site =
|
|||||||
ae "\\date{}" (Latex_helpers.build_date_site_string "" "") );
|
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 =
|
let test_title =
|
||||||
[
|
[
|
||||||
( "Event only" >:: fun _ ->
|
( "Event only" >:: fun _ ->
|
||||||
@@ -114,6 +118,15 @@ let test_title =
|
|||||||
ae "\\title{Some title\\\\[2ex]\\large{Some subtitle}}"
|
ae "\\title{Some title\\\\[2ex]\\large{Some subtitle}}"
|
||||||
(Latex_helpers.build_title_string "irrelevant" "Some title"
|
(Latex_helpers.build_title_string "irrelevant" "Some title"
|
||||||
"Some subtitle") );
|
"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 =
|
let test_author =
|
||||||
@@ -121,12 +134,15 @@ let test_author =
|
|||||||
( "Author only" >:: fun _ ->
|
( "Author only" >:: fun _ ->
|
||||||
ae "\\author{Some author}"
|
ae "\\author{Some author}"
|
||||||
(Latex_helpers.build_author_string "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 _ ->
|
( "Nothing" >:: fun _ ->
|
||||||
ae "\\author{}" (Latex_helpers.build_author_string "" "" "" "" "") );
|
ae "\\author{}" (Latex_helpers.build_author_string "" "" "" "" "") );
|
||||||
]
|
]
|
||||||
|
|
||||||
let test_diagram _ctxt =
|
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 ->
|
| Some actual ->
|
||||||
let expected =
|
let expected =
|
||||||
"\n\\par\\nobreak\\medskip\\chessboard[setfen="
|
"\n\\par\\nobreak\\medskip\\chessboard[setfen="
|
||||||
@@ -138,8 +154,8 @@ let test_diagram _ctxt =
|
|||||||
|
|
||||||
let test_diagram_clock _ctxt =
|
let test_diagram_clock _ctxt =
|
||||||
match
|
match
|
||||||
Latex_helpers.get_diagram 5 Helpers.Latex_helper.diagram_data ~clock:true
|
Latex_helpers.get_diagram ~ply:5 Helpers.Latex_helper.diagram_data
|
||||||
~white_time:"1:00" ~black_time:"0:21"
|
~clock:true ~white_time:"1:00" ~black_time:"0:21"
|
||||||
with
|
with
|
||||||
| Some actual ->
|
| Some actual ->
|
||||||
let expected =
|
let expected =
|
||||||
@@ -151,7 +167,9 @@ let test_diagram_clock _ctxt =
|
|||||||
| None -> assert_failure "Diagram for ply 5 was not found"
|
| None -> assert_failure "Diagram for ply 5 was not found"
|
||||||
|
|
||||||
let test_no_diagram _ctxt =
|
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
|
match result with
|
||||||
| None -> ()
|
| None -> ()
|
||||||
@@ -172,11 +190,10 @@ let suite =
|
|||||||
"diagram_placement" >:: test_diagram_placement;
|
"diagram_placement" >:: test_diagram_placement;
|
||||||
"elo tests" >::: test_player_elo;
|
"elo tests" >::: test_player_elo;
|
||||||
"date & site tests" >::: test_date_site;
|
"date & site tests" >::: test_date_site;
|
||||||
|
"escape tex" >:: test_escape_tex;
|
||||||
"title tests" >::: test_title;
|
"title tests" >::: test_title;
|
||||||
"author tests" >::: test_author;
|
"author tests" >::: test_author;
|
||||||
"diagram_test" >:: test_diagram;
|
"diagram_test" >:: test_diagram;
|
||||||
"diagram_clock" >:: test_diagram_clock;
|
"diagram_clock" >:: test_diagram_clock;
|
||||||
"no_diagram" >:: test_no_diagram;
|
"no_diagram" >:: test_no_diagram;
|
||||||
]
|
]
|
||||||
|
|
||||||
let () = run_test_tt_main suite
|
|
||||||
|
|||||||
+25
-3
@@ -1,5 +1,5 @@
|
|||||||
open OUnit2
|
open OUnit2
|
||||||
open Pgn_logic
|
open Pgn_to_tex_internal
|
||||||
open Parser
|
open Parser
|
||||||
|
|
||||||
let lex_all ?(is_header = false) s =
|
let lex_all ?(is_header = false) s =
|
||||||
@@ -170,9 +170,33 @@ let test_clock_in_comment _ctxt =
|
|||||||
in
|
in
|
||||||
assert_tokens ~ctxt:_ctxt ~input ~is_header:false ~expected
|
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 =
|
let suite =
|
||||||
"lexer tests"
|
"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_simple" >:: test_header_simple;
|
||||||
"header_escaped_string" >:: test_header_escaped_string;
|
"header_escaped_string" >:: test_header_escaped_string;
|
||||||
"numbers_and_moves" >:: test_numbers_and_moves;
|
"numbers_and_moves" >:: test_numbers_and_moves;
|
||||||
@@ -187,5 +211,3 @@ let suite =
|
|||||||
"clock_inline" >:: test_clock_inline;
|
"clock_inline" >:: test_clock_inline;
|
||||||
"clock_in_comment" >:: test_clock_in_comment;
|
"clock_in_comment" >:: test_clock_in_comment;
|
||||||
]
|
]
|
||||||
|
|
||||||
let () = run_test_tt_main suite
|
|
||||||
|
|||||||
+4
-1
@@ -1,5 +1,5 @@
|
|||||||
open OUnit2
|
open OUnit2
|
||||||
open Pgn_logic
|
open Pgn_to_tex_internal
|
||||||
open Ast
|
open Ast
|
||||||
open Nag
|
open Nag
|
||||||
|
|
||||||
@@ -19,12 +19,15 @@ let test_direct_nags =
|
|||||||
("$15" >:: fun _ -> ae "\\bbetter" (nag_to_tex "$15"));
|
("$15" >:: fun _ -> ae "\\bbetter" (nag_to_tex "$15"));
|
||||||
("$16" >:: fun _ -> ae "\\ensuremath{\\pm}" (nag_to_tex "$16"));
|
("$16" >:: fun _ -> ae "\\ensuremath{\\pm}" (nag_to_tex "$16"));
|
||||||
("$17" >:: fun _ -> ae "\\ensuremath{\\mp}" (nag_to_tex "$17"));
|
("$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"));
|
("$22" >:: fun _ -> ae "\\zugzwang" (nag_to_tex "$22"));
|
||||||
("$32" >:: fun _ -> ae "\\development" (nag_to_tex "$32"));
|
("$32" >:: fun _ -> ae "\\development" (nag_to_tex "$32"));
|
||||||
("$36" >:: fun _ -> ae "\\initiative" (nag_to_tex "$36"));
|
("$36" >:: fun _ -> ae "\\initiative" (nag_to_tex "$36"));
|
||||||
("$40" >:: fun _ -> ae "\\attack" (nag_to_tex "$40"));
|
("$40" >:: fun _ -> ae "\\attack" (nag_to_tex "$40"));
|
||||||
("$44" >:: fun _ -> ae "\\compensation" (nag_to_tex "$44"));
|
("$44" >:: fun _ -> ae "\\compensation" (nag_to_tex "$44"));
|
||||||
("$132" >:: fun _ -> ae "\\counterplay" (nag_to_tex "$132"));
|
("$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 ]
|
let suite = "nag test" >::: [ "direct nags" >::: test_direct_nags ]
|
||||||
|
|||||||
@@ -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;
|
||||||
|
]
|
||||||
@@ -1,47 +1,39 @@
|
|||||||
open OUnit2
|
open OUnit2
|
||||||
open Pgn_logic
|
|
||||||
open Ast
|
|
||||||
module MoveMap = Pgn2tex.MoveMap
|
|
||||||
|
|
||||||
let ae expected actual = assert_equal ~printer:(fun x -> x) expected actual
|
let ae expected actual = assert_equal ~printer:(fun x -> x) expected actual
|
||||||
|
let contains = Helpers.Text_helper.contains
|
||||||
|
|
||||||
let diagram_json_str =
|
let game_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"}] |}
|
{| [
|
||||||
|
{"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 =
|
let diagram_data = Pgn_to_tex.parse_diagrams_json game_diagram_json_str
|
||||||
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 variation_diagram_json_str =
|
||||||
let result = Pgn2tex.parse_diagrams_json diagram_json_str in
|
{| [{"ply":4,"fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1","path":["e4","e5","Nf3","d6"]}] |}
|
||||||
let actual_bindings = MoveMap.bindings result in
|
|
||||||
|
|
||||||
let expected_bindings =
|
let test_variation_diagram_to_tex _ctxt =
|
||||||
[
|
let pgn = "1. e4 e5 2. Nf3 Nc6 (2... d6) 3. Bb5" in
|
||||||
(1, "rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b KQkq - 0 1");
|
let diagrams = Pgn_to_tex.parse_diagrams_json variation_diagram_json_str in
|
||||||
(2, "rnbqkbnr/ppp1pppp/8/3p4/3P4/8/PPP1PPPP/RNBQKBNR w KQkq - 0 2");
|
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
|
||||||
in
|
assert_bool
|
||||||
|
(Printf.sprintf "variation diagram misplaced in:\n%s" rendered)
|
||||||
let string_of_bindings pairs =
|
(contains
|
||||||
let string_of_pair (ply, fen) = Printf.sprintf "(%d, %S)" ply fen in
|
~needle:("2... d6\n\\par\\nobreak\\medskip\\chessboard[setfen=" ^ fen)
|
||||||
"[" ^ String.concat "; " (List.map string_of_pair pairs) ^ "]"
|
rendered);
|
||||||
in
|
assert_bool "variation diagram should use smallboard"
|
||||||
|
(contains ~needle:"smallboard" rendered)
|
||||||
assert_equal expected_bindings actual_bindings ~printer:string_of_bindings
|
|
||||||
|
|
||||||
let test_basic_pgn_to_tex _ctxt =
|
let test_basic_pgn_to_tex _ctxt =
|
||||||
let filename = "pgn_examples/1.pgn" in
|
let filename = "pgn_examples/1.pgn" in
|
||||||
let ic = In_channel.open_text filename in
|
let ic = In_channel.open_text filename in
|
||||||
let input_string = In_channel.input_all ic in
|
let input_string = In_channel.input_all ic in
|
||||||
let actual = Pgn2tex.to_tex input_string ~diagram_data ~clock:false in
|
let actual = Pgn_to_tex.to_tex ~diagram_data ~clock:false input_string in
|
||||||
let expected =
|
let expected =
|
||||||
{|\title{URS-chJ}
|
{|\title{URS-chJ}
|
||||||
\author{Ibragimov, Ildar (2455)\\Kramnik, Vladimir (2480)}
|
\author{Ibragimov, Ildar (2455)\\Kramnik, Vladimir (2480)}
|
||||||
@@ -64,7 +56,7 @@ let test_pgn_with_clock_to_tex _ctxt =
|
|||||||
let filename = "pgn_examples/2.pgn" in
|
let filename = "pgn_examples/2.pgn" in
|
||||||
let ic = In_channel.open_text filename in
|
let ic = In_channel.open_text filename in
|
||||||
let input_string = In_channel.input_all ic in
|
let input_string = In_channel.input_all ic in
|
||||||
let actual = Pgn2tex.to_tex input_string ~diagram_data ~clock:true in
|
let actual = Pgn_to_tex.to_tex ~diagram_data ~clock:true input_string in
|
||||||
let expected =
|
let expected =
|
||||||
{|\title{Casual Rapid game}
|
{|\title{Casual Rapid game}
|
||||||
\author{montillo2015 (1919)\\reassessyourchess (1869)}
|
\author{montillo2015 (1919)\\reassessyourchess (1869)}
|
||||||
@@ -83,12 +75,27 @@ let test_pgn_with_clock_to_tex _ctxt =
|
|||||||
ae expected actual;
|
ae expected actual;
|
||||||
close_in ic
|
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 =
|
let suite =
|
||||||
"pgn2tex tests"
|
"pgn_to_tex tests"
|
||||||
>::: [
|
>::: [
|
||||||
"json_str_to_map" >:: test_json_str_to_map;
|
"variation_diagram_to_tex" >:: test_variation_diagram_to_tex;
|
||||||
"basic_pgn_to_tex" >:: test_basic_pgn_to_tex;
|
"basic_pgn_to_tex" >:: test_basic_pgn_to_tex;
|
||||||
"pgn_with_clock_to_tex" >:: test_pgn_with_clock_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;
|
||||||
]
|
]
|
||||||
|
|
||||||
let () = run_test_tt_main suite
|
|
||||||
@@ -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
@@ -6,8 +6,11 @@ let () =
|
|||||||
>::: [
|
>::: [
|
||||||
Test_lexer.suite;
|
Test_lexer.suite;
|
||||||
Test_latex.suite;
|
Test_latex.suite;
|
||||||
Test_pgn2tex.suite;
|
Test_pgn_to_tex.suite;
|
||||||
|
Test_parser.suite;
|
||||||
|
Test_diagram_resolution.suite;
|
||||||
Test_nag.suite;
|
Test_nag.suite;
|
||||||
|
Test_real_world.suite;
|
||||||
]
|
]
|
||||||
in
|
in
|
||||||
run_test_tt_main all
|
run_test_tt_main all
|
||||||
|
|||||||
Reference in New Issue
Block a user