AtlatestRepositorysigil-doc

sigil-doc / tree / testtest-print-customization.sgl

1(import (sigil test) (sigil string) (sigil doc) (sigil doc publication)
2 (sigil doc print) (sigil doc render typst))
3
4(define loc (make-srcloc "customization.sgd" 1 1 0 1))
5(define (chapter title authors id section-title)
6 (make-document 'chapter title authors
7 (list (make-section id (list (make-text section-title loc))
8 (list (make-paragraph (list (make-text "καλημέρα from a customizable book." loc)) loc)
9 (make-code-block "sigil" "; theme witness\n(define answer \"forty-two\")\n(if #t 42 0)" loc))
10 loc))
11 loc))
12(define publication
13 (make-print-publication "Customization Pressure" "A second visual edition"
14 '("David Wilson")
15 (list
16 (make-publication-document "same-author"
17 (chapter "Same Author" '("David Wilson") "same-section" "Same Section"))
18 (make-publication-document "guest-author"
19 (chapter "Guest Author" '("Grace Hopper") "guest-section" "Guest Section")))
20 '()))
22(define alternate-options
23 (make-print-options
24 chapter-numbering: 'roman-upper
25 section-numbering?: #t
26 section-numbering-depth: 1
27 chapter-author-display: 'when-different
28 contents-depth: 2
29 include-unnumbered-in-contents?: #f
30 chapter-start: 'odd
31 chapter-label-style: 'chapter-word
32 running-header: 'publication-title))
34(define alternate-theme
35 (make-typst-theme
36 paper: 'a5
37 margin-x: (make-typst-length 16 'mm)
38 margin-y: (make-typst-length 18 'mm)
39 body-font: "Libertinus Serif"
40 body-size: (make-typst-length 10 'pt)
41 code-font: "DejaVu Sans Mono"
42 code-size: (make-typst-length 7.5 'pt)
43 title-size: (make-typst-length 23 'pt)
44 title-weight: 'semibold
45 subtitle-size: (make-typst-length 12 'pt)
46 chapter-size: (make-typst-length 18 'pt)
47 chapter-weight: 'bold
48 section-size: (make-typst-length 12.5 'pt)
49 section-weight: 'semibold
50 heading-color: "2f496e"
51 heading-spacing: (make-typst-length 12 'pt)
52 accent-color: "b44b34"
53 syntax-comment-color: "58715f"
54 syntax-string-color: "a03c62"
55 syntax-keyword-color: "4e55a6"
56 syntax-number-color: "b05f18"
57 syntax-plain-color: "20242b"
58 code-background: "edf1f5"
59 code-padding: (make-typst-length 7 'pt)
60 code-wrap-column: 64
61 admonition-background: "f2eee3"
62 admonition-accent: "8b6f35"
63 chapter-author-size: (make-typst-length 9 'pt)
64 chapter-author-color: "546171"
65 title-author-size: (make-typst-length 10 'pt)
66 page-number-position: 'right
67 running-header-font: "Libertinus Serif"
68 running-header-size: (make-typst-length 7.5 'pt)
69 running-header-color: "546171"
70 running-header-align: 'right))
72(define default-source (print-publication->typst publication))
73(define explicit-default-source
74 (print-publication->typst publication options: default-print-options theme: default-typst-theme))
75(define alternate-source
76 (print-publication->typst publication options: alternate-options theme: alternate-theme))
77(define chapter-header-source
78 (print-publication->typst publication
79 options: (make-print-options running-header: 'chapter-title)
80 theme: alternate-theme))
81(define unnumbered-source
82 (print-publication->typst publication
83 options: (make-print-options chapter-numbering: 'none
84 section-numbering?: #f chapter-author-display: 'never
85 include-unnumbered-in-contents?: #t)))
86(define always-authors-source
87 (print-publication->typst publication
88 options: (make-print-options chapter-author-display: 'always)))
90(test "explicit defaults preserve the one-argument compatibility contract"
91 (assert-equal default-source explicit-default-source)
92 (assert-true (string-contains? default-source
93 "#set page(paper: \"us-letter\", margin: (x: 0.9in, y: 0.8in)")))
95(test "publication policy changes numbering, contents, starts, authors, and running header"
96 (assert-true (string-contains? alternate-source "#set heading(numbering: none)"))
97 (assert-true (string-contains? alternate-source "Chapter #numbering(\"I\""))
98 (assert-true (string-contains? alternate-source "numbering: \"I.1.\""))
99 (assert-true (string-contains? alternate-source "#outline(title: [Contents], depth: 2"))
100 (assert-true (string-contains? alternate-source "#pagebreak(to: \"odd\")"))
101 (assert-false (string-contains? alternate-source "#text(\"David Wilson\")]\n\n#heading"))
102 (assert-true (string-contains? alternate-source "#text(\"Grace Hopper\")"))
103 (assert-true (string-contains? alternate-source "header: align(right)"))
104 (assert-true (string-contains? alternate-source "Customization Pressure"))
105 (assert-true (string-contains? chapter-header-source
106 "state(\"sigil-doc-running-chapter\""))
107 (assert-true (string-contains? chapter-header-source
108 "#sigil-doc-running-chapter.update(\"Guest Author\")"))
109 (assert-true (string-contains? unnumbered-source
110 "#heading(level: 1, outlined: true, numbering: none)"))
111 (assert-true (string-contains? unnumbered-source
112 "#heading(level: 2, outlined: true, numbering: none)"))
113 (assert-false (string-contains? unnumbered-source "#text(\"Grace Hopper\")"))
114 (assert-true (string-contains? always-authors-source "#text(\"David Wilson\")]\n\n"))
115 (assert-true (string-contains? always-authors-source "#text(\"Grace Hopper\")]\n\n")))
117(test "Typst theme changes paper, fonts, typography, colors, padding, wrap, and page placement"
118 (assert-true (string-contains? alternate-source "paper: \"a5\""))
119 (assert-true (string-contains? alternate-source "margin: (x: 16mm, y: 18mm)"))
120 (assert-true (string-contains? alternate-source "size: 18pt"))
121 (assert-true (string-contains? alternate-source "fill: rgb(\"2f496e\")"))
122 (assert-true (string-contains? alternate-source "fill: rgb(\"edf1f5\")"))
123 (assert-true (string-contains? alternate-source "rgb(\"4e55a6\")"))
124 (assert-true (string-contains? alternate-source "number-align: right")))
126(test "theme strings are owned and accessor copies cannot mutate the theme"
127 (let* ((font (string-copy "Libertinus Serif"))
128 (theme (make-typst-theme body-font: font))
129 (returned (typst-theme-body-font theme)))
130 (string-set! font 0 #\X)
131 (string-set! returned 0 #\X)
132 (assert-equal "Libertinus Serif" (typst-theme-body-font theme))))
134(test "invalid policy and unsafe theme value matrix fails closed"
135 (assert-error (make-print-options chapter-numbering: 'binary))
136 (assert-error (make-print-options section-numbering-depth: 7))
137 (assert-error (make-print-options chapter-author-display: 'sometimes))
138 (assert-error (make-print-options chapter-author-placement: 'margin))
139 (assert-error (make-print-options contents-depth: -1))
140 (assert-error (make-print-options chapter-start: 'same-page))
141 (assert-error (make-print-options running-header: 'raw-typst))
142 (assert-error (make-typst-length -1 'pt))
143 (assert-error (make-typst-length 1 'raw))
144 (assert-error (make-typst-theme paper: 'evil-paper))
145 (assert-error (make-typst-theme body-font: "Font\")]; #import \"evil.typ\"; ["))
146 (assert-error (make-typst-theme heading-color: "fff];#"))
147 (assert-error (make-typst-theme code-wrap-column: 8))
148 (assert-error (make-typst-theme page-number-position: 'top-right))
149 (assert-error (print-publication->typst publication options: alternate-theme))
150 (assert-error (print-publication->typst publication theme: alternate-options)))
152(run-tests)