AtlatestRepositorysigil-doc

sigil-doc / tree / test / fixturespractical-chapter.sgl

1(define-library (fixtures practical-chapter)
2 (import (sigil core) (sigil fs) (sigil syntax) (sigil doc))
3 (export practical-chapter fixture-source fixture-manuscript fixture-location-expectations locate-text source-slice-at)
4 (begin
5 (define fixture-source "test/fixtures/practical-sigil-chapter.txt")
6 (define fixture-manuscript (read-file-string fixture-source))
7 (define (starts-at? source needle at)
8 (let ((n (string-length needle)))
9 (and (<= (+ at n) (string-length source))
10 (string=? needle (substring source at (+ at n))))))
11 (define (find-nth needle nth)
12 (let loop ((at 0) (seen 0))
13 (cond ((> (+ at (string-length needle)) (string-length fixture-manuscript))
14 (error "fixture needle not found" needle nth))
15 ((starts-at? fixture-manuscript needle at)
16 (if (= seen nth) at (loop (+ at 1) (+ seen 1))))
17 (else (loop (+ at 1) seen)))))
18 (define (utf8-width c)
19 (let ((n (char->integer c))) (cond ((<= n #x7f) 1) ((<= n #x7ff) 2) ((<= n #xffff) 3) (else 4))))
20 (define (byte-count text end)
21 (let loop ((i 0) (n 0)) (if (= i end) n (loop (+ i 1) (+ n (utf8-width (string-ref text i)))))))
22 (define (byte->char-index byte-position)
23 (let loop ((i 0) (bytes 0))
24 (cond ((= bytes byte-position) i)
25 ((or (> bytes byte-position) (= i (string-length fixture-manuscript)))
26 (error "location is not on a UTF-8 scalar boundary" byte-position))
27 (else (loop (+ i 1) (+ bytes (utf8-width (string-ref fixture-manuscript i))))))))
28 (define (source-slice-at loc)
29 (let ((start (byte->char-index (srcloc-position loc)))
30 (end (byte->char-index (+ (srcloc-position loc) (srcloc-span loc)))))
31 (substring fixture-manuscript start end)))
32 (define (line-column at)
33 (let loop ((i 0) (line 1) (column 1))
34 (if (= i at) (cons line column)
35 (if (char=? (string-ref fixture-manuscript i) #\newline)
36 (loop (+ i 1) (+ line 1) 1)
37 (loop (+ i 1) line (+ column 1))))))
38 (define (locate-text needle nth)
39 (let* ((at (find-nth needle nth)) (lc (line-column at)))
40 (make-srcloc fixture-source (car lc) (cdr lc)
41 (byte-count fixture-manuscript at)
42 (byte-count needle (string-length needle)))))
43 (define fixture-location-expectations '())
44 (define (located constructor-text occurrence make)
45 (let ((node (make (locate-text constructor-text occurrence))))
46 (set! fixture-location-expectations
47 (cons (list node constructor-text occurrence) fixture-location-expectations))
48 node))
49 (define (txt s . occurrence)
50 (located s (if (null? occurrence) 0 (car occurrence)) (lambda (loc) (make-text s loc))))
51 (define code-source
52 "(import (sigil core) (sigil fs))\n(define lines (string-split (read-file-string \"tasks.txt\") \"\\n\"))\n(define todos (filter (lambda (line) (string-prefix? \"TODO \" line)) lines))\n(display (string-append \"Open tasks: \" (number->string (length todos))))\n(newline)\n(for-each (lambda (line) (display (string-append \"- \" line \"\\n\"))) todos)")
53 (define practical-chapter
54 (located "A Useful First Script" 0
55 (lambda (root-loc)
56 (make-document 'chapter "A Useful First Script" '("David Wilson")
57 (list
58 (located "[#first-script]" 0 (lambda (loc) (make-anchor "first-script" loc)))
59 (located "A Useful First Script" 1
60 (lambda (section-loc)
61 (make-section "first-script" (list (txt "A Useful First Script" 1))
62 (list
63 (located "This is practical Scheme—small\nfunctions composed without ceremony." 0
64 (lambda (loc)
65 (make-paragraph
66 (list (txt "This is practical Scheme—small\nfunctions composed without ceremony.")) loc)))
67 (located "The important\nidea is to shape the problem as a pipeline" 0
68 (lambda (loc)
69 (make-paragraph
70 (list (txt "The important\nidea is to shape the problem as a pipeline")
71 (txt ": obtain text, split it into lines,\nselect what matters, and render the answer.")) loc)))
72 (located code-source 0 (lambda (loc) (make-code-block "sigil" code-source loc)))
73 (located "Tip: run the script on a deliberately messy file." 0
74 (lambda (loc)
75 (make-admonition 'tip (list (txt "run the script on a deliberately messy file"))
76 (list (located "UTF-8 should feel normal" 0
77 (lambda (p-loc) (make-paragraph
78 (list (txt "Unicode such\nas καλημέρα") (txt ", and a line that merely contains the word TODO.")) p-loc)))) loc))))
79 section-loc)))
80 (located "[#change-the-question]" 0 (lambda (loc) (make-anchor "change-the-question" loc)))
81 (located "Change the Question" 0
82 (lambda (section-loc)
83 (make-section "change-the-question" (list (txt "Change the Question"))
84 (list
85 (located "Once a small program works, its value comes from how readily it can answer the\nnext question." 0
86 (lambda (loc) (make-paragraph
87 (list (txt "Once a small program works, its value comes from how readily it can answer the\nnext question.")) loc)))
88 (located "Exercise change-the-question" 0
89 (lambda (loc) (make-exercise "change-the-question" (list (txt "add a command-line word that selects TODO or DONE"))
90 (list (located "Then add three small\nexamples that cover an empty list, mixed tasks, and non-ASCII task text." 0
91 (lambda (p-loc) (make-paragraph
92 (list (txt "Then add three small\nexamples that cover an empty list, mixed tasks, and non-ASCII task text.")) p-loc)))) loc)))
93 (located "Return to the first script when the revised version starts to feel complicated." 0
94 (lambda (loc) (make-paragraph
95 (list (txt "Return to ")
96 (located "the first script" 0 (lambda (r-loc) (make-reference "first-script" (list (txt "the first script")) r-loc)))
97 (txt " when the revised version starts to feel complicated.")) loc))))
98 section-loc))))
99 root-loc))))
100 (set! fixture-location-expectations (reverse fixture-location-expectations))))