AtlatestRepositorysigil-doc

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

1#lang sigil/doc/book
2@(import (fixtures practical-author-support))
3@(define tip-calls 0)
4@(define (tip body)
5 (set! tip-calls (+ tip-calls 1))
6 body)
7@(define-syntax strong
8 (syntax-rules () ((_ child) (em child))))
9@chapter [
10 "A Useful First Script"
11 authors: '("David Wilson")
14@section ["A Useful First Script" id: "first-script"]
16@p {
17This is practical Scheme—small functions composed without ceremony.
20@p {
21The important idea is to shape the problem as a pipeline: obtain text, split it
22into lines, select what matters, and render the answer.
25@code ["sigil"] {
26(import (sigil core) (sigil fs))
27(define lines (string-split (read-file-string "tasks.txt") "\n"))
28(define todos (filter (lambda (line) (string-prefix? "TODO " line)) lines))
29(display (string-append "Open tasks: " (number->string (length todos))))
30(newline)
31(for-each (lambda (line) (display (string-append "- " line "\n"))) todos)
34@tip
35@section ["run the script on a deliberately messy file" id: "messy-input"]
36@p: Unicode such as καλημέρα, and a line that merely contains the word TODO.
37@end section
38@end tip
40@p: This @strong{macro-expanded phrase} is authored with an ordinary local macro.
42@(tip (p (text "computed inline expression")))
44@end section
46@section ["Change the Question" id: "change-the-question"]
48@p {
49Once a small program works, its value comes from how readily it can answer the
50next question.
53@exercise [
54 "change-the-question"
55 "add a command-line word that selects TODO or DONE"
56] {
57@p {
58Then add three small examples that cover an empty list, mixed tasks, and
59non-ASCII task text.
63@p {
64Return to @ref ["first-script"] {the first script} when the revised version
65starts to feel complicated.
68@end section
70@repeat-sections [
71 "Computed Section"
72 count: (+ 1 1)
73] {}
75@section["Verified Examples" id: "verified-examples"]{
76@example["stdout" expect-stdout: "hello\n" timeout: 30]{
77@code["sigil"]{(display "hello\n")}
79@example["value" expect-value: "42" timeout: 30]{
80@code["sigil"]{(+ 20 22)}
82@example["expected-error" expect-error: "error: expected boom" timeout: 30]{
83@code["sigil"]{(error "expected boom")}
85@example["session-one" session: "progress" expect-value: "#<undefined>" timeout: 30]{
86@code["sigil"]{(define answer 40)}
88@example["session-two" session: "progress" expect-value: "42" timeout: 30]{
89@code["sigil"]{(+ answer 2)}
91@example["file-source" source-file: "d3-project/file-example.sgl" expect-stdout: "file-ok\n" timeout: 30]{}
92@example["timeout" expect-timeout: #t timeout: 1 display: 'hidden]{
93@code["sigil"]{(let loop () (loop))}
95@project-check["d3-project" root: "d3-project"
96 files: '("package.sgl" "src/d3-project/main.sgl")
97 expect-stdout: "project-ok\n" timeout: 30]{}
100@end chapter