AtlatestRepositorysigil-doc
1
(import (sigil test) (sigil string) (sigil doc) (sigil doc publication)2
(sigil doc print) (sigil doc render typst))4
(define loc (make-srcloc "customization.sgd" 1 1 0 1))5
(define (chapter title authors id section-title)6
(make-document 'chapter title authors7
(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 publication13
(make-print-publication "Customization Pressure" "A second visual edition"14
'("David Wilson")15
(list16
(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-options23
(make-print-options24
chapter-numbering: 'roman-upper25
section-numbering?: #t26
section-numbering-depth: 127
chapter-author-display: 'when-different28
contents-depth: 229
include-unnumbered-in-contents?: #f30
chapter-start: 'odd31
chapter-label-style: 'chapter-word32
running-header: 'publication-title))34
(define alternate-theme35
(make-typst-theme36
paper: 'a537
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: 'semibold45
subtitle-size: (make-typst-length 12 'pt)46
chapter-size: (make-typst-length 18 'pt)47
chapter-weight: 'bold48
section-size: (make-typst-length 12.5 'pt)49
section-weight: 'semibold50
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: 6461
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: 'right67
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-source74
(print-publication->typst publication options: default-print-options theme: default-typst-theme))75
(define alternate-source76
(print-publication->typst publication options: alternate-options theme: alternate-theme))77
(define chapter-header-source78
(print-publication->typst publication79
options: (make-print-options running-header: 'chapter-title)80
theme: alternate-theme))81
(define unnumbered-source82
(print-publication->typst publication83
options: (make-print-options chapter-numbering: 'none84
section-numbering?: #f chapter-author-display: 'never85
include-unnumbered-in-contents?: #t)))86
(define always-authors-source87
(print-publication->typst publication88
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-source93
"#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-source106
"state(\"sigil-doc-running-chapter\""))107
(assert-true (string-contains? chapter-header-source108
"#sigil-doc-running-chapter.update(\"Guest Author\")"))109
(assert-true (string-contains? unnumbered-source110
"#heading(level: 1, outlined: true, numbering: none)"))111
(assert-true (string-contains? unnumbered-source112
"#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)