add `load-pagetree` and improve `path->pagenode`

pull/102/head
Matthew Butterick 9 years ago
parent 8c8844bc16
commit a8a94d0af9

@ -97,12 +97,19 @@
(decode-pagetree (map ->symbol (unique-sorted-output-paths (filter not-pollen-cache? (directory-list dir))))) (decode-pagetree (map ->symbol (unique-sorted-output-paths (filter not-pollen-cache? (directory-list dir)))))
(error (format "directory->pagetree: directory ~a doesn't exist" dir)))) (error (format "directory->pagetree: directory ~a doesn't exist" dir))))
(define+provide/contract (load-pagetree source-path)
(pathish? . -> . pagetree?)
(cached-require source-path (world:current-main-export)))
;; Try loading from pagetree file, or failing that, synthesize pagetree. ;; Try loading from pagetree file, or failing that, synthesize pagetree.
(define+provide/contract (make-project-pagetree project-dir) (define+provide/contract (make-project-pagetree project-dir)
(pathish? . -> . pagetree?) (pathish? . -> . pagetree?)
(with-handlers ([exn:fail? (λ(exn) (directory->pagetree project-dir))]) (with-handlers ([exn:fail? (λ(exn) (directory->pagetree project-dir))])
(define pagetree-source (build-path project-dir (world:current-default-pagetree))) (define pagetree-source (build-path project-dir (world:current-default-pagetree)))
(cached-require pagetree-source (world:current-main-export)))) (load-pagetree pagetree-source)))
(define+provide/contract (parent pnish [pt (current-pagetree)]) (define+provide/contract (parent pnish [pt (current-pagetree)])
(((or/c #f pagenodeish?)) (pagetree?) . ->* . (or/c #f pagenode?)) (((or/c #f pagenodeish?)) (pagetree?) . ->* . (or/c #f pagenode?))
@ -220,10 +227,13 @@
(check-false (next 'three test-pagetree))) (check-false (next 'three test-pagetree)))
(define/contract+provide (path->pagenode path [starting-path (world:current-project-root)])
(define/contract+provide (path->pagenode path) ((coerce/path?) (coerce/path?) . ->* . coerce/symbol?)
(coerce/path? . -> . coerce/symbol?) (define starting-dir
(->output-path (find-relative-path (world:current-project-root) (->complete-path path)))) (if (directory-exists? starting-path)
starting-path
(get-enclosing-dir starting-path)))
(->output-path (find-relative-path (->complete-path starting-dir) (->complete-path path))))
(define+provide/contract (in-pagetree? pnish [pt (current-pagetree)]) (define+provide/contract (in-pagetree? pnish [pt (current-pagetree)])

@ -416,6 +416,15 @@ Return the pagenode immediately after @racket[_p]. For @racket[next*], return al
@subsection{Utilities} @subsection{Utilities}
@defproc[
(load-pagetree
[pagetree-source pathish?])
pagetree?
]
Load a pagetree from a @filepath{ptree} source file, namely @racket[_pagetree-source].
@defproc[ @defproc[
(pagetree->list (pagetree->list
[pagetree pagetree?]) [pagetree pagetree?])
@ -433,7 +442,8 @@ Report whether @racket[_pagenode] is in @racket[_pagetree].
@defproc[ @defproc[
(path->pagenode (path->pagenode
[p pathish?]) [p pathish?]
[starting-path pathish? (world:current-project-root)])
pagenode? pagenode?
] ]
Convert path @racket[_p] to a pagenode — meaning, make it relative to @racket[current-project-root], run it through @racket[->output-path], and convert it to a symbol. Does not tell you whether the resultant pagenode actually exists in the current pagetree (for that, use @racket[in-pagetree?]). Convert path @racket[_p] to a pagenode — meaning, make it relative to @racket[_starting-path], run it through @racket[->output-path], and convert it to a symbol. Does not tell you whether the resultant pagenode actually exists in the current pagetree (for that, use @racket[in-pagetree?]).
Loading…
Cancel
Save