diff --git a/README.md b/README.md index db61c33..0ba2c69 100644 --- a/README.md +++ b/README.md @@ -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. +## 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: ``` diff --git a/lib/internal/latex_helpers.ml b/lib/internal/latex_helpers.ml index dcf6d2d..81a1e92 100644 --- a/lib/internal/latex_helpers.ml +++ b/lib/internal/latex_helpers.ml @@ -22,20 +22,22 @@ 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 ^ "}" - | _ -> "" + 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{" ^ escape_tex event ^ "}" + | _event, title, "" -> "\\title{" ^ escape_tex title ^ "}" | _event, title, subtitle -> - "\\title{" ^ title ^ "\\\\[2ex]\\large{" ^ subtitle ^ "}}" + "\\title{" ^ escape_tex title ^ "\\\\[2ex]\\large{" + ^ escape_tex subtitle ^ "}}" (* Date and Site string builder *) let build_date_site_string date site = diff --git a/test/test_nag.ml b/test/test_nag.ml index 777ed6a..78f09d2 100644 --- a/test/test_nag.ml +++ b/test/test_nag.ml @@ -19,12 +19,15 @@ let test_direct_nags = ("$15" >:: fun _ -> ae "\\bbetter" (nag_to_tex "$15")); ("$16" >:: fun _ -> ae "\\ensuremath{\\pm}" (nag_to_tex "$16")); ("$17" >:: fun _ -> ae "\\ensuremath{\\mp}" (nag_to_tex "$17")); + ("$18" >:: fun _ -> ae "+-" (nag_to_tex "$18")); + ("$19" >:: fun _ -> ae "-+" (nag_to_tex "$19")); ("$22" >:: fun _ -> ae "\\zugzwang" (nag_to_tex "$22")); ("$32" >:: fun _ -> ae "\\development" (nag_to_tex "$32")); ("$36" >:: fun _ -> ae "\\initiative" (nag_to_tex "$36")); ("$40" >:: fun _ -> ae "\\attack" (nag_to_tex "$40")); ("$44" >:: fun _ -> ae "\\compensation" (nag_to_tex "$44")); ("$132" >:: fun _ -> ae "\\counterplay" (nag_to_tex "$132")); + ("unknown fallback" >:: fun _ -> ae "" (nag_to_tex "$999")); ] let suite = "nag test" >::: [ "direct nags" >::: test_direct_nags ] diff --git a/test/test_real_world.ml b/test/test_real_world.ml new file mode 100644 index 0000000..38b8ce1 --- /dev/null +++ b/test/test_real_world.ml @@ -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; + ] diff --git a/test/test_suite.ml b/test/test_suite.ml index e9a50bb..1ff0d37 100644 --- a/test/test_suite.ml +++ b/test/test_suite.ml @@ -9,6 +9,7 @@ let () = Test_pgn_to_tex.suite; Test_diagram_resolution.suite; Test_nag.suite; + Test_real_world.suite; ] in run_test_tt_main all