cover real-world headers, NAGs, and fixtures

This commit is contained in:
2026-10-23 16:38:24 +02:00
committed by owenrees
parent 9cd7878ad8
commit 135491e915
5 changed files with 186 additions and 8 deletions
+46
View File
@@ -53,6 +53,52 @@ Resolution rules:
Diagrams inside a variation are rendered inline within that variation and at a slightly smaller size (`smallboard`) so they don't dominate the subline. Diagrams inside a variation are rendered inline within that variation and at a slightly smaller size (`smallboard`) so they don't dominate the subline.
## Supported headers
The following PGN headers are rendered:
- `Event` is used as the title when `Title` is absent.
- `Title` is rendered as the document title. When `Subtitle` is also present it
is rendered below the title; a title without a subtitle renders no subtitle
block.
- `Author` is rendered as 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 renders 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` | `\pm` |
| `$17` | `\mp` |
| `$18` | `+-` |
| `$19` | `-+` |
| `$22` | `\zugzwang` |
| `$32` | `\development` |
| `$36` | `\initiative` |
| `$40` | `\attack` |
| `$44` | `\compensation` |
| `$132` | `\counterplay` |
Unmapped NAGs are ignored.
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: 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:
``` ```
+10 -8
View File
@@ -22,20 +22,22 @@ let build_elo_string elo = if String.length elo > 0 then "(" ^ elo ^ ")" else ""
(* Author string builder *) (* Author string builder *)
let build_author_string author white black white_elo black_elo = let build_author_string author white black white_elo black_elo =
match (author, white, black) with if author <> "" then "\\author{" ^ escape_tex author ^ "}"
| "", "", "" -> "\\author{}" else
| "", white, black -> match (white, black) with
"\\author{" ^ escape_tex white ^ " " ^ build_elo_string white_elo ^ "\\\\" | "", "" -> "\\author{}"
^ escape_tex black ^ " " ^ build_elo_string black_elo ^ "}" | white, black ->
| author, "", "" -> "\\author{" ^ escape_tex author ^ "}" "\\author{" ^ escape_tex white ^ " " ^ build_elo_string white_elo
| _ -> "" ^ "\\\\" ^ escape_tex black ^ " " ^ build_elo_string black_elo ^ "}"
(* Title string builder *) (* Title string builder *)
let build_title_string event title subtitle = let build_title_string event title subtitle =
match (event, title, subtitle) with match (event, title, subtitle) with
| event, "", "" -> "\\title{" ^ escape_tex event ^ "}" | event, "", "" -> "\\title{" ^ escape_tex event ^ "}"
| _event, title, "" -> "\\title{" ^ escape_tex title ^ "}"
| _event, title, subtitle -> | _event, title, subtitle ->
"\\title{" ^ title ^ "\\\\[2ex]\\large{" ^ subtitle ^ "}}" "\\title{" ^ escape_tex title ^ "\\\\[2ex]\\large{"
^ escape_tex subtitle ^ "}}"
(* Date and Site string builder *) (* Date and Site string builder *)
let build_date_site_string date site = let build_date_site_string date site =
+3
View File
@@ -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 ]
+126
View File
@@ -0,0 +1,126 @@
open OUnit2
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
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;
]
+1
View File
@@ -9,6 +9,7 @@ let () =
Test_pgn_to_tex.suite; Test_pgn_to_tex.suite;
Test_diagram_resolution.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