Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions scribble-doc/scribblings/scribble/running.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,14 @@ and rendered, which could affect the content that
@item{@DFlag{errortrace} --- enable @racketmodname[errortrace #:indirect]}
]

@;{TODO: document the --watch flag here, as an item in the list above:
- it takes a single file only;
- it re-renders whenever the file or one of its local dependencies
(files in the document's folder) changes;
- it combines with any output format, e.g.
scribble --typst-pdf --watch doc.scrbl
Then add to the history below:
#:changed "1.70" @elem{Added @DFlag{watch}.}}

@history[#:changed "1.38" @elem{Added the @DFlag{errortrace} flag.}
#:changed "1.44" @elem{Added the @DFlag{make}/@Flag{y} flag.}]
104 changes: 104 additions & 0 deletions scribble-lib/scribble/private/build.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#lang racket/base
;; Loading and rendering documents with the settings that `scribble`'s command
;; line sets.
(require "../xref.rkt"
"../render.rkt"
(prefix-in html: "../html-render.rkt"))
(provide mk-doc
build-docs
current-lib-mode
current-doc-binding
current-doc-load-handler
current-render-mixin
current-html
current-dest-directory
current-dest-name
current-info-output-file
current-info-input-files
current-xref-input-modules
current-prefix-file
current-style-file
current-style-extra-files
current-extra-files
current-redirect
current-redirect-main
current-directory-depth
current-quiet
helper-file-prefix
keep-existing-helper-files?
current-image-prefs)

;; Loading
(define current-lib-mode (make-parameter #f))
(define current-doc-binding (make-parameter 'doc))
;; #f, or a `current-load/use-compiled` handler to load documents with (--make)
(define current-doc-load-handler (make-parameter #f))

;; mk-doc : string -> any
;; Loads `file` (a `lib` path in lib mode) and returns its document, taken from
;; the `doc` submodule if there is one.
(define (mk-doc file)
(define (go)
(define mp (if (current-lib-mode)
`(lib ,file)
`(file ,file)))
(define sub `(submod ,mp ,(current-doc-binding)))
(dynamic-require (if (module-declared? sub #t) sub mp) (current-doc-binding)))
(define handler (current-doc-load-handler))
(if handler
(parameterize ([current-load/use-compiled handler])
(go))
(go)))

;; Rendering
(define current-render-mixin (make-parameter html:render-mixin))
(define current-html (make-parameter #t))
(define current-dest-directory (make-parameter #f))
(define current-dest-name (make-parameter #f))
(define current-info-output-file (make-parameter #f))
(define current-info-input-files (make-parameter null))
(define current-xref-input-modules (make-parameter null))
(define current-prefix-file (make-parameter #f))
(define current-style-file (make-parameter #f))
(define current-style-extra-files (make-parameter null))
(define current-extra-files (make-parameter null))
(define current-redirect (make-parameter #f))
(define current-redirect-main (make-parameter #f))
(define current-directory-depth (make-parameter 0))
(define current-quiet (make-parameter #f))
(define helper-file-prefix (make-parameter #f))
(define keep-existing-helper-files? (make-parameter #f))
(define current-image-prefs (make-parameter null)) ; reverse order

(define (build-docs docs files)
(when (and (current-dest-name)
((length files) . > . 1))
(raise-user-error 'scribble "cannot supply a destination name with multiple inputs"))
(render docs
(map (lambda (fn)
(let-values ([(base name dir?) (split-path fn)])
(or (current-dest-name) name)))
files)
Comment on lines +78 to +81

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map-to-for: This map operation can be replaced with a for/list loop.

Suggested change
(map (lambda (fn)
(let-values ([(base name dir?) (split-path fn)])
(or (current-dest-name) name)))
files)
(for/list ([fn (in-list files)])
(define-values (base name dir?) (split-path fn))
(or (current-dest-name) name))
Debugging details
Textual replacement
(line-replacement
  #:new-lines
    '#("          (for/list ([fn (in-list files)])"
       "            (define-values (base name dir?) (split-path fn))"
       "            (or (current-dest-name) name))")
  #:original-lines
    '#("          (map (lambda (fn)"
       "                 (let-values ([(base name dir?) (split-path fn)])"
       "                   (or (current-dest-name) name)))"
       "               files)")
  #:start-line 78)
Syntactic replacement
(syntax-replacement
  #:introduction-scope #<procedure:...and/syntax-local.rkt:149:2>
  #:new-syntax
    #<syntax:/home/runner/.local/share/racket/9.4.0.6/pkgs/resyntax/default-recommendations/loops/list-loopification.rkt:125:2 (for/list ((fn (in-list files))) (define-values (base name dir?) (split-path fn)) (or (current-dest-name) name))>
  #:original-syntax
    #<syntax:scribble-lib/scribble/private/build.rkt:78:10 (map (lambda (fn) (let-values (((base name dir?) (split-path fn))) (or (current-dest-name) name))) files)>
  #:source
    (file-source
     #<path:/home/runner/work/scribble/scribble/scribble-lib/scribble/private/build.rkt>)
  #:uses-universal-tagged-syntax? #f)

Comment on lines +78 to +81

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map-to-for: This map operation can be replaced with a for/list loop.

Suggested change
(map (lambda (fn)
(let-values ([(base name dir?) (split-path fn)])
(or (current-dest-name) name)))
files)
(for/list ([fn (in-list files)])
(define-values (base name dir?) (split-path fn))
(or (current-dest-name) name))
Debugging details
Textual replacement
(line-replacement
  #:new-lines
    '#("          (for/list ([fn (in-list files)])"
       "            (define-values (base name dir?) (split-path fn))"
       "            (or (current-dest-name) name))")
  #:original-lines
    '#("          (map (lambda (fn)"
       "                 (let-values ([(base name dir?) (split-path fn)])"
       "                   (or (current-dest-name) name)))"
       "               files)")
  #:start-line 78)
Syntactic replacement
(syntax-replacement
  #:introduction-scope #<procedure:...and/syntax-local.rkt:149:2>
  #:new-syntax
    #<syntax:/home/runner/.local/share/racket/9.4.0.6/pkgs/resyntax/default-recommendations/loops/list-loopification.rkt:125:2 (for/list ((fn (in-list files))) (define-values (base name dir?) (split-path fn)) (or (current-dest-name) name))>
  #:original-syntax
    #<syntax:scribble-lib/scribble/private/build.rkt:78:10 (map (lambda (fn) (let-values (((base name dir?) (split-path fn))) (or (current-dest-name) name))) files)>
  #:source
    (file-source
     #<path:/home/runner/work/scribble/scribble/scribble-lib/scribble/private/build.rkt>)
  #:uses-universal-tagged-syntax? #f)

Comment on lines +78 to +81

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map-to-for: This map operation can be replaced with a for/list loop.

Suggested change
(map (lambda (fn)
(let-values ([(base name dir?) (split-path fn)])
(or (current-dest-name) name)))
files)
(for/list ([fn (in-list files)])
(define-values (base name dir?) (split-path fn))
(or (current-dest-name) name))
Debugging details
Textual replacement
(line-replacement
  #:new-lines
    '#("          (for/list ([fn (in-list files)])"
       "            (define-values (base name dir?) (split-path fn))"
       "            (or (current-dest-name) name))")
  #:original-lines
    '#("          (map (lambda (fn)"
       "                 (let-values ([(base name dir?) (split-path fn)])"
       "                   (or (current-dest-name) name)))"
       "               files)")
  #:start-line 78)
Syntactic replacement
(syntax-replacement
  #:introduction-scope #<procedure:...and/syntax-local.rkt:149:2>
  #:new-syntax
    #<syntax:/home/runner/.local/share/racket/9.4.0.6/pkgs/resyntax/default-recommendations/loops/list-loopification.rkt:125:2 (for/list ((fn (in-list files))) (define-values (base name dir?) (split-path fn)) (or (current-dest-name) name))>
  #:original-syntax
    #<syntax:scribble-lib/scribble/private/build.rkt:78:10 (map (lambda (fn) (let-values (((base name dir?) (split-path fn))) (or (current-dest-name) name))) files)>
  #:source
    (file-source
     #<path:/home/runner/work/scribble/scribble/scribble-lib/scribble/private/build.rkt>)
  #:uses-universal-tagged-syntax? #f)

#:dest-dir (current-dest-directory)
#:render-mixin (current-render-mixin)
#:image-preferences (reverse (current-image-prefs))
#:prefix-file (current-prefix-file)
#:style-file (current-style-file)
#:style-extra-files (reverse (current-style-extra-files))
#:extra-files (reverse (current-extra-files))
#:helper-file-prefix (helper-file-prefix)
#:keep-existing-helper-files? (keep-existing-helper-files?)
#:redirect (and (current-html) (current-redirect))
#:redirect-main (and (current-html) (current-redirect-main))
#:directory-depth (current-directory-depth)
#:quiet? (current-quiet)
#:info-in-files (reverse (current-info-input-files))
#:xrefs (for/list ([mod+id (in-list (reverse (current-xref-input-modules)))])
(let* ([get-xref (dynamic-require (car mod+id) (cdr mod+id))]
[xr (get-xref)])
(unless (xref? xr)
(raise-user-error
'scribble "result from `~s' of `~s' is not an xref: ~e"
(cdr mod+id) (car mod+id) xr))
xr))
Comment on lines +96 to +103

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let-to-define: Internal definitions are recommended instead of let expressions, to reduce nesting.

Suggested change
#:xrefs (for/list ([mod+id (in-list (reverse (current-xref-input-modules)))])
(let* ([get-xref (dynamic-require (car mod+id) (cdr mod+id))]
[xr (get-xref)])
(unless (xref? xr)
(raise-user-error
'scribble "result from `~s' of `~s' is not an xref: ~e"
(cdr mod+id) (car mod+id) xr))
xr))
#:xrefs (for/list ([mod+id (in-list (reverse (current-xref-input-modules)))])
(define get-xref (dynamic-require (car mod+id) (cdr mod+id)))
(define xr (get-xref))
(unless (xref? xr)
(raise-user-error 'scribble
"result from `~s' of `~s' is not an xref: ~e"
(cdr mod+id)
(car mod+id)
xr))
xr)
Debugging details
Textual replacement
(line-replacement
  #:new-lines
    '#("          #:xrefs (for/list ([mod+id (in-list (reverse (current-xref-input-modules)))])"
       "                    (define get-xref (dynamic-require (car mod+id) (cdr mod+id)))"
       "                    (define xr (get-xref))"
       "                    (unless (xref? xr)"
       "                      (raise-user-error 'scribble"
       "                                        \"result from `~s' of `~s' is not an xref: ~e\""
       "                                        (cdr mod+id)"
       "                                        (car mod+id)"
       "                                        xr))"
       "                    xr)")
  #:original-lines
    '#("          #:xrefs (for/list ([mod+id (in-list (reverse (current-xref-input-modules)))])"
       "                    (let* ([get-xref (dynamic-require (car mod+id) (cdr mod+id))]"
       "                           [xr (get-xref)])"
       "                      (unless (xref? xr)"
       "                        (raise-user-error"
       "                         'scribble \"result from `~s' of `~s' is not an xref: ~e\""
       "                         (cdr mod+id) (car mod+id) xr))"
       "                      xr))")
  #:start-line 96)
Syntactic replacement
(syntax-replacement
  #:introduction-scope #<procedure:...and/syntax-local.rkt:149:2>
  #:new-syntax
    #<syntax:.../resyntax/base.rkt:215:28 (for/list ((mod+id (in-list (reverse (current-xref-input-modules))))) (define get-xref (dynamic-require (car mod+id) (cdr mod+id))) (define xr (get-xref)) (unless (xref? xr) (raise-user-error (quote scribble) "result from `~s' of `~s' is not an xref: ~e...>
  #:original-syntax
    #<syntax:scribble-lib/scribble/private/build.rkt:96:18 (for/list ((mod+id (in-list (reverse (current-xref-input-modules))))) (let* ((get-xref (dynamic-require (car mod+id) (cdr mod+id))) (xr (get-xref))) (unless (xref? xr) (raise-user-error (quote scribble) "result from `~s' of `~s' is not an xref: ~e" (cdr...>
  #:source
    (file-source
     #<path:/home/runner/work/scribble/scribble/scribble-lib/scribble/private/build.rkt>)
  #:uses-universal-tagged-syntax? #f)

Comment on lines +96 to +103

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let-to-define: Internal definitions are recommended instead of let expressions, to reduce nesting.

Suggested change
#:xrefs (for/list ([mod+id (in-list (reverse (current-xref-input-modules)))])
(let* ([get-xref (dynamic-require (car mod+id) (cdr mod+id))]
[xr (get-xref)])
(unless (xref? xr)
(raise-user-error
'scribble "result from `~s' of `~s' is not an xref: ~e"
(cdr mod+id) (car mod+id) xr))
xr))
#:xrefs (for/list ([mod+id (in-list (reverse (current-xref-input-modules)))])
(define get-xref (dynamic-require (car mod+id) (cdr mod+id)))
(define xr (get-xref))
(unless (xref? xr)
(raise-user-error 'scribble
"result from `~s' of `~s' is not an xref: ~e"
(cdr mod+id)
(car mod+id)
xr))
xr)
Debugging details
Textual replacement
(line-replacement
  #:new-lines
    '#("          #:xrefs (for/list ([mod+id (in-list (reverse (current-xref-input-modules)))])"
       "                    (define get-xref (dynamic-require (car mod+id) (cdr mod+id)))"
       "                    (define xr (get-xref))"
       "                    (unless (xref? xr)"
       "                      (raise-user-error 'scribble"
       "                                        \"result from `~s' of `~s' is not an xref: ~e\""
       "                                        (cdr mod+id)"
       "                                        (car mod+id)"
       "                                        xr))"
       "                    xr)")
  #:original-lines
    '#("          #:xrefs (for/list ([mod+id (in-list (reverse (current-xref-input-modules)))])"
       "                    (let* ([get-xref (dynamic-require (car mod+id) (cdr mod+id))]"
       "                           [xr (get-xref)])"
       "                      (unless (xref? xr)"
       "                        (raise-user-error"
       "                         'scribble \"result from `~s' of `~s' is not an xref: ~e\""
       "                         (cdr mod+id) (car mod+id) xr))"
       "                      xr))")
  #:start-line 96)
Syntactic replacement
(syntax-replacement
  #:introduction-scope #<procedure:...and/syntax-local.rkt:149:2>
  #:new-syntax
    #<syntax:.../resyntax/base.rkt:215:28 (for/list ((mod+id (in-list (reverse (current-xref-input-modules))))) (define get-xref (dynamic-require (car mod+id) (cdr mod+id))) (define xr (get-xref)) (unless (xref? xr) (raise-user-error (quote scribble) "result from `~s' of `~s' is not an xref: ~e...>
  #:original-syntax
    #<syntax:scribble-lib/scribble/private/build.rkt:96:18 (for/list ((mod+id (in-list (reverse (current-xref-input-modules))))) (let* ((get-xref (dynamic-require (car mod+id) (cdr mod+id))) (xr (get-xref))) (unless (xref? xr) (raise-user-error (quote scribble) "result from `~s' of `~s' is not an xref: ~e" (cdr...>
  #:source
    (file-source
     #<path:/home/runner/work/scribble/scribble/scribble-lib/scribble/private/build.rkt>)
  #:uses-universal-tagged-syntax? #f)

Comment on lines +96 to +103

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let-to-define: Internal definitions are recommended instead of let expressions, to reduce nesting.

Suggested change
#:xrefs (for/list ([mod+id (in-list (reverse (current-xref-input-modules)))])
(let* ([get-xref (dynamic-require (car mod+id) (cdr mod+id))]
[xr (get-xref)])
(unless (xref? xr)
(raise-user-error
'scribble "result from `~s' of `~s' is not an xref: ~e"
(cdr mod+id) (car mod+id) xr))
xr))
#:xrefs (for/list ([mod+id (in-list (reverse (current-xref-input-modules)))])
(define get-xref (dynamic-require (car mod+id) (cdr mod+id)))
(define xr (get-xref))
(unless (xref? xr)
(raise-user-error 'scribble
"result from `~s' of `~s' is not an xref: ~e"
(cdr mod+id)
(car mod+id)
xr))
xr)
Debugging details
Textual replacement
(line-replacement
  #:new-lines
    '#("          #:xrefs (for/list ([mod+id (in-list (reverse (current-xref-input-modules)))])"
       "                    (define get-xref (dynamic-require (car mod+id) (cdr mod+id)))"
       "                    (define xr (get-xref))"
       "                    (unless (xref? xr)"
       "                      (raise-user-error 'scribble"
       "                                        \"result from `~s' of `~s' is not an xref: ~e\""
       "                                        (cdr mod+id)"
       "                                        (car mod+id)"
       "                                        xr))"
       "                    xr)")
  #:original-lines
    '#("          #:xrefs (for/list ([mod+id (in-list (reverse (current-xref-input-modules)))])"
       "                    (let* ([get-xref (dynamic-require (car mod+id) (cdr mod+id))]"
       "                           [xr (get-xref)])"
       "                      (unless (xref? xr)"
       "                        (raise-user-error"
       "                         'scribble \"result from `~s' of `~s' is not an xref: ~e\""
       "                         (cdr mod+id) (car mod+id) xr))"
       "                      xr))")
  #:start-line 96)
Syntactic replacement
(syntax-replacement
  #:introduction-scope #<procedure:...and/syntax-local.rkt:149:2>
  #:new-syntax
    #<syntax:.../resyntax/base.rkt:215:28 (for/list ((mod+id (in-list (reverse (current-xref-input-modules))))) (define get-xref (dynamic-require (car mod+id) (cdr mod+id))) (define xr (get-xref)) (unless (xref? xr) (raise-user-error (quote scribble) "result from `~s' of `~s' is not an xref: ~e...>
  #:original-syntax
    #<syntax:scribble-lib/scribble/private/build.rkt:96:18 (for/list ((mod+id (in-list (reverse (current-xref-input-modules))))) (let* ((get-xref (dynamic-require (car mod+id) (cdr mod+id))) (xr (get-xref))) (unless (xref? xr) (raise-user-error (quote scribble) "result from `~s' of `~s' is not an xref: ~e" (cdr...>
  #:source
    (file-source
     #<path:/home/runner/work/scribble/scribble/scribble-lib/scribble/private/build.rkt>)
  #:uses-universal-tagged-syntax? #f)

#:info-out-file (current-info-output-file)))
65 changes: 65 additions & 0 deletions scribble-lib/scribble/private/walk.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#lang racket/base
;; Pruned walker that tracks local dependencies in every phase, using the
;; declarations already in the current namespace.
(require syntax/modresolve racket/path racket/list)
(provide local-graph local-deps module-file)

;; A module name is a complete path or (list* 'submod path (listof symbol)).
(define (module-file m) (if (pair? m) (cadr m) m))

;; A graph is (hash module-name (listof module-name)): its keys are the local
;; modules reached, each mapped to everything it imports at every phase
;; (including for-label). Non-local imports appear only as leaves, never as
;; keys. A local import that isn't declared is a key with no imports.

;; local-graph : (listof module-name) path-string -> graph
;; Precondition: every module in `mods` is declared in `current-namespace`
;; (e.g., the document has been loaded). Follows `module->imports` from `mods`
;; into the modules under `root`. Only the submodules that are actually
;; imported are followed. Nothing is loaded or compiled.
(define (local-graph mods root)
(unless (and (path-string? root) (directory-exists? root))
(raise-argument-error 'local-graph "existing directory path" 1 mods root))
(define starts
(for/list ([m (in-list mods)])
(define m* (cond [(path-string? m) (simple-form-path m)]
[(and (pair? m) (eq? (car m) 'submod) (pair? (cdr m))
(path-string? (cadr m)))
(list* 'submod (simple-form-path (cadr m)) (cddr m))]
[else #f]))
(unless (and m* (module-declared? m* #f))
(raise-argument-error 'local-graph "(listof declared module name)" 0 mods root))
m*))

(define root-elems (explode-path (simple-form-path root)))
(define (local? m) (list-prefix? root-elems (explode-path (module-file m))))

;; imports-of : module-name -> (listof module-name)
(define (imports-of m)
(if (module-declared? m #f)
(remove-duplicates
(for*/list ([phase+mpis (in-list (module->imports m))]
[mpi (in-list (cdr phase+mpis))]
;; Resolve relative to the file; `mpi` itself carries the
;; submodule context needed for (submod ".." ...) imports.
[p (in-value (resolve-module-path-index mpi (module-file m)))]
;; a symbol is a primitive module
#:unless (symbol? p))
(if (path? p)
(simple-form-path p)
(list* 'submod (simple-form-path (cadr p)) (cddr p)))))
'()))

(for/fold ([graph (hash)]) ([start (in-list starts)])
(let loop ([graph graph] [m start])
(if (hash-has-key? graph m)
graph
(let ([imports (imports-of m)])
(for/fold ([graph (hash-set graph m imports)])
([i (in-list imports)] #:when (local? i))
(loop graph i)))))))
Comment on lines +55 to +60

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if-let-to-cond: cond with internal definitions is preferred over if with let, to reduce nesting

Suggested change
(if (hash-has-key? graph m)
graph
(let ([imports (imports-of m)])
(for/fold ([graph (hash-set graph m imports)])
([i (in-list imports)] #:when (local? i))
(loop graph i)))))))
(cond
[(hash-has-key? graph m) graph]
[else
(define imports (imports-of m))
(for/fold ([graph (hash-set graph m imports)])
([i (in-list imports)]
#:when (local? i))
(loop graph i))]))))
Debugging details
Textual replacement
(line-replacement
  #:new-lines
    '#("      (cond"
       "        [(hash-has-key? graph m) graph]"
       "        [else"
       "         (define imports (imports-of m))"
       "         (for/fold ([graph (hash-set graph m imports)])"
       "                   ([i (in-list imports)]"
       "                    #:when (local? i))"
       "           (loop graph i))]))))")
  #:original-lines
    '#("      (if (hash-has-key? graph m)"
       "          graph"
       "          (let ([imports (imports-of m)])"
       "            (for/fold ([graph (hash-set graph m imports)])"
       "                      ([i (in-list imports)] #:when (local? i))"
       "              (loop graph i)))))))")
  #:start-line 55)
Syntactic replacement
(syntax-replacement
  #:introduction-scope #<procedure:...and/syntax-local.rkt:149:2>
  #:new-syntax
    #<syntax:/home/runner/.local/share/racket/9.4.0.6/pkgs/resyntax/default-recommendations/let-replacement/cond-let-replacement.rkt:47:2 (cond ((hash-has-key? graph m) graph) (else (define imports (imports-of m)) (for/fold ((graph (hash-set graph m imports))) ((i (in-list imports)) #:when (local? i)) (loop graph i))))>
  #:original-syntax
    #<syntax:scribble-lib/scribble/private/walk.rkt:55:6 (if (hash-has-key? graph m) graph (let ((imports (imports-of m))) (for/fold ((graph (hash-set graph m imports))) ((i (in-list imports)) #:when (local? i)) (loop graph i))))>
  #:source
    (file-source
     #<path:/home/runner/work/scribble/scribble/scribble-lib/scribble/private/walk.rkt>)
  #:uses-universal-tagged-syntax? #f)

Comment on lines +55 to +60

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if-let-to-cond: cond with internal definitions is preferred over if with let, to reduce nesting

Suggested change
(if (hash-has-key? graph m)
graph
(let ([imports (imports-of m)])
(for/fold ([graph (hash-set graph m imports)])
([i (in-list imports)] #:when (local? i))
(loop graph i)))))))
(cond
[(hash-has-key? graph m) graph]
[else
(define imports (imports-of m))
(for/fold ([graph (hash-set graph m imports)])
([i (in-list imports)]
#:when (local? i))
(loop graph i))]))))
Debugging details
Textual replacement
(line-replacement
  #:new-lines
    '#("      (cond"
       "        [(hash-has-key? graph m) graph]"
       "        [else"
       "         (define imports (imports-of m))"
       "         (for/fold ([graph (hash-set graph m imports)])"
       "                   ([i (in-list imports)]"
       "                    #:when (local? i))"
       "           (loop graph i))]))))")
  #:original-lines
    '#("      (if (hash-has-key? graph m)"
       "          graph"
       "          (let ([imports (imports-of m)])"
       "            (for/fold ([graph (hash-set graph m imports)])"
       "                      ([i (in-list imports)] #:when (local? i))"
       "              (loop graph i)))))))")
  #:start-line 55)
Syntactic replacement
(syntax-replacement
  #:introduction-scope #<procedure:...and/syntax-local.rkt:149:2>
  #:new-syntax
    #<syntax:/home/runner/.local/share/racket/9.4.0.6/pkgs/resyntax/default-recommendations/let-replacement/cond-let-replacement.rkt:47:2 (cond ((hash-has-key? graph m) graph) (else (define imports (imports-of m)) (for/fold ((graph (hash-set graph m imports))) ((i (in-list imports)) #:when (local? i)) (loop graph i))))>
  #:original-syntax
    #<syntax:scribble-lib/scribble/private/walk.rkt:55:6 (if (hash-has-key? graph m) graph (let ((imports (imports-of m))) (for/fold ((graph (hash-set graph m imports))) ((i (in-list imports)) #:when (local? i)) (loop graph i))))>
  #:source
    (file-source
     #<path:/home/runner/work/scribble/scribble/scribble-lib/scribble/private/walk.rkt>)
  #:uses-universal-tagged-syntax? #f)

Comment on lines +55 to +60

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if-let-to-cond: cond with internal definitions is preferred over if with let, to reduce nesting

Suggested change
(if (hash-has-key? graph m)
graph
(let ([imports (imports-of m)])
(for/fold ([graph (hash-set graph m imports)])
([i (in-list imports)] #:when (local? i))
(loop graph i)))))))
(cond
[(hash-has-key? graph m) graph]
[else
(define imports (imports-of m))
(for/fold ([graph (hash-set graph m imports)])
([i (in-list imports)]
#:when (local? i))
(loop graph i))]))))
Debugging details
Textual replacement
(line-replacement
  #:new-lines
    '#("      (cond"
       "        [(hash-has-key? graph m) graph]"
       "        [else"
       "         (define imports (imports-of m))"
       "         (for/fold ([graph (hash-set graph m imports)])"
       "                   ([i (in-list imports)]"
       "                    #:when (local? i))"
       "           (loop graph i))]))))")
  #:original-lines
    '#("      (if (hash-has-key? graph m)"
       "          graph"
       "          (let ([imports (imports-of m)])"
       "            (for/fold ([graph (hash-set graph m imports)])"
       "                      ([i (in-list imports)] #:when (local? i))"
       "              (loop graph i)))))))")
  #:start-line 55)
Syntactic replacement
(syntax-replacement
  #:introduction-scope #<procedure:...and/syntax-local.rkt:149:2>
  #:new-syntax
    #<syntax:/home/runner/.local/share/racket/9.4.0.6/pkgs/resyntax/default-recommendations/let-replacement/cond-let-replacement.rkt:47:2 (cond ((hash-has-key? graph m) graph) (else (define imports (imports-of m)) (for/fold ((graph (hash-set graph m imports))) ((i (in-list imports)) #:when (local? i)) (loop graph i))))>
  #:original-syntax
    #<syntax:scribble-lib/scribble/private/walk.rkt:55:6 (if (hash-has-key? graph m) graph (let ((imports (imports-of m))) (for/fold ((graph (hash-set graph m imports))) ((i (in-list imports)) #:when (local? i)) (loop graph i))))>
  #:source
    (file-source
     #<path:/home/runner/work/scribble/scribble/scribble-lib/scribble/private/walk.rkt>)
  #:uses-universal-tagged-syntax? #f)


;; local-deps : (listof module-name) path-string -> (listof path)
;; The files of the local modules that `mods` reach; see `local-graph`.
(define (local-deps mods root)
(remove-duplicates (map module-file (hash-keys (local-graph mods root)))))
209 changes: 209 additions & 0 deletions scribble-lib/scribble/private/watch.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
#lang racket/base
;; Re-render a document whenever it or one of its local dependencies changes.
(require compiler/cm
racket/exn
racket/list
racket/path
racket/set
syntax/modresolve
"build.rkt"
"walk.rkt")
(provide watch-doc
cm-rebuild)

;; The modules whose structs the renderers dispatch on. Every build's namespace
;; shares them with the namespace `watch-doc` starts in, so that a document's
;; parts and style properties are the ones the renderer knows.
(define shared-modules
'(scribble/core
scribble/html-properties
scribble/latex-properties
scribble/typst-properties
scribble/render-struct
scribble/decode-struct
scribble/manual-struct
scribble/struct))

;; doc-path-of : string -> path
;; The complete path of the document `file` names (a `lib` path in lib mode).
(define (doc-path-of file)
(simple-form-path (if (current-lib-mode)
(resolve-module-path `(lib ,file) #f)
file)))


;; watch-doc : string [#:on-watch ((listof path) -> any)] -> bottom

;; Start a watcher to watch changes to `file` and all its dependency. The
;; watcher uses `ccm-rebuild` to compile and load their modules, and it then
;; renders the outputs with `build-docs`

;; An `on-watch` callback gets the watched files whenever the watcher starts waiting.
(define (watch-doc file
#:on-watch [on-watch void])
(define doc-path (doc-path-of file))
(define root (path-only doc-path))
(define root-elems (explode-path root))
(define base-ns (current-namespace))

(define (fresh-namespace)
(define ns (make-base-empty-namespace))
(parameterize ([current-namespace base-ns])
(for ([m (in-list shared-modules)]
#:when (module-declared? m #f))
(namespace-attach-module base-ns m ns)))
ns)

(define (render doc)
(with-handlers ([exn:fail? (lambda (e) (eprintf "~a" (exn->string e)))])
(build-docs (list doc) (list file))))

(define (error-files e)
(if (exn:srclocs? e)
(for*/list ([loc (in-list ((exn:srclocs-accessor e) e))]
[src (in-value (and (srcloc? loc) (srcloc-source loc)))]
#:when (path? src)
[p (in-value (simple-form-path src))]
#:when (list-prefix? root-elems (explode-path p)))
p)
'()))

;; build :: (maybe namespace) -> (maybe namespace) -> Boolean -> (Values namespace graph)
(define (build prev-ns prev-graph changed)
(define ns (fresh-namespace))
(with-handlers ([exn:fail? (lambda (e)
(eprintf "~a" (exn->string e))
(values #f prev-graph (error-files e)))])
(define-values (doc graph) (cm-rebuild file root ns prev-ns prev-graph changed))
(render doc)
(values ns graph '())))

;; watch-files : graph (listof path) -> (listof path)
;; The files to watch: those of the graph's modules (the document's file if
;; it has never loaded) and the files a load error points to, so fixing them
;; triggers a rebuild.
(define (watch-files graph err-files)
(remove-duplicates
(append (if (hash-empty? graph)
(list doc-path)
(map module-file (hash-keys graph)))
err-files)))

;; The first build also declares the dependencies, so they can be watched
;; from the start.
(define-values (ns0 graph0 err-files0) (build #f (hash) '()))
;; `watches` maps each watched file to its change evt (#f while the file is
;; missing). `ns` and `graph` are those of the last successful build (#f and
;; empty before one), and `pending` holds the files changed since then.
(let loop ([watches (arm-evts (watch-files graph0 err-files0))]
[ns ns0]
[graph graph0]
[pending '()])
(define evts (filter values (hash-values watches)))
(on-watch (hash-keys watches))
(cond
[(null? evts)
;; Every file is momentarily missing: wait and retry instead of blocking.
(sleep 0.1)
(loop (arm-evts (hash-keys watches) watches) ns graph pending)]
[else
(sync (apply choice-evt evts))
(define changed (remove-duplicates (append pending (changed-files watches))))
(cancel-evts watches)
;; Watch again before building: an evt only sees changes made after it
;; exists, so a save during the build makes the next wait return at once
;; and triggers another build.
(define armed (arm-evts (hash-keys watches)))
(define-values (new-ns new-graph err-files) (build ns graph changed))
;; Reusing `armed` keeps the evts that fired during the build.
(define next (arm-evts (watch-files new-graph err-files) armed))
(cancel-evts armed next)
(if new-ns
(loop next new-ns new-graph '())
;; Keep the changed files pending, so a module that broke this build
;; is not reused from `ns` by the next one.
(loop next ns graph changed))])))

;; cm-rebuild : string path namespace (or/c #f namespace) graph (listof path)
;; -> (values any graph)
;; Loads the document `file`, whose local dependencies are under `root`, into
;; `new-ns` and returns it together with its local dependency graph in
;; `new-ns`. `prev-ns` (with its graph `prev-graph`) is the namespace of the
;; previous build, or #f; `changed` are the files changed since then.
;;
;; The modules of `prev-ns` that don't depend on a changed file, and the
;; non-local modules, are attached to `new-ns` and so reused as is. The rest
;; are loaded with a compilation manager, which recompiles every changed file
;; and every dependent whose dependencies changed (e.g. a module using a
;; changed macro), and writes their compiled files. Its checks skip files
;; outside `root`. Raises an exception if the document fails to load.
(define (cm-rebuild file root new-ns prev-ns prev-graph changed)
(define root-elems (explode-path (simple-form-path root)))
(define (local? p) (list-prefix? root-elems (explode-path (simple-form-path p))))
(when prev-ns
(define dirty (dirty-files prev-graph (map simple-form-path changed)))
(define clean
(for/list ([m (in-hash-keys prev-graph)]
#:unless (set-member? dirty (module-file m)))
m))
(define libraries
(for*/list ([imports (in-hash-values prev-graph)]
[i (in-list imports)]
#:unless (hash-has-key? prev-graph i))
i))
(for ([m (in-list (remove-duplicates (append clean libraries)))])
(attach-module prev-ns m new-ns)))
(define doc-path (doc-path-of file))
(parameterize ([current-namespace new-ns])
(parameterize* ([manager-skip-file-handler
(lambda (p)
(and (not (local? p))
(file-stamp-in-paths p (list (path-only p)))))]
[current-doc-load-handler
;; created in `new-ns`: a handler only compiles in the
;; namespace it was created in
(make-compilation-manager-load/use-compiled-handler)])
(define doc (mk-doc file))
(define doc-mods
(filter (lambda (m) (module-declared? m #f))
(list doc-path `(submod ,doc-path ,(current-doc-binding)))))
(values doc (local-graph doc-mods root)))))

;; dirty-files : graph (listof path) -> (set path)
(define (dirty-files graph changed)
;; importers : (hash path (listof path)), from a file to the files that
;; import one of its modules
(define importers
(for*/fold ([importers (hash)])
([(m imports) (in-hash graph)]
[i (in-list imports)]
#:when (hash-has-key? graph i))
(hash-update importers (module-file i)
(lambda (files) (cons (module-file m) files))
'())))
(let loop ([todo changed] [dirty (set)])
(cond
[(null? todo) dirty]
[(set-member? dirty (car todo)) (loop (cdr todo) dirty)]
[else (loop (append (hash-ref importers (car todo) '()) (cdr todo))
(set-add dirty (car todo)))])))

(define (attach-module from m to)
(with-handlers ([exn:fail:contract?
(lambda (e) (namespace-attach-module-declaration from m to))])
(namespace-attach-module from m to)))

(define (changed-files watches)
(for/list ([(p e) (in-hash watches)]
#:when (or (not e) (sync/timeout 0 e)))
p))

(define (arm-evts paths [have (hash)])
(for/hash ([p (in-list paths)])
(values p (or (hash-ref have p #f)
(filesystem-change-evt p (lambda () #f))))))

(define (cancel-evts watches [keep (hash)])
(for ([(p e) (in-hash watches)]
#:when (and e (not (hash-has-key? keep p))))
(filesystem-change-evt-cancel e)))
Loading