|
|
|
#lang racket/base
|
|
|
|
(require racket/contract racket/match xml/path racket/bool)
|
|
|
|
(require "tools.rkt" "world.rkt" "debug.rkt" "decode.rkt")
|
|
|
|
|
|
|
|
(module+ test (require rackunit))
|
|
|
|
|
|
|
|
(provide pnode? ptree? parent children previous next)
|
|
|
|
|
|
|
|
(define/contract (pnode? x)
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
(and (stringish? x) (not (whitespace? (->string x)))))
|
|
|
|
|
|
|
|
(define/contract (pnode?/error x)
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
(or (pnode? x) (error "Not a valid pnode:" x)))
|
|
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(check-true (pnode? "foo-bar"))
|
|
|
|
(check-true (pnode? "Foo_Bar_0123"))
|
|
|
|
(check-true (pnode? 'foo-bar))
|
|
|
|
(check-true (pnode? "foo-bar.p"))
|
|
|
|
(check-true (pnode? "/Users/MB/foo-bar"))
|
|
|
|
(check-false (pnode? #f))
|
|
|
|
(check-false (pnode? ""))
|
|
|
|
(check-false (pnode? " ")))
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (ptree? x)
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
(and (tagged-xexpr? x) (andmap (λ(i) (or (pnode? i) (ptree? i))) x)))
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(check-true (ptree? '(foo)))
|
|
|
|
(check-true (ptree? '(foo (hee))))
|
|
|
|
(check-true (ptree? '(foo (hee (uncle "foo"))))))
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (file->ptree path)
|
|
|
|
(pathish? . -> . ptree?)
|
|
|
|
(message "Loading ptree file" (file-name-from-path path))
|
|
|
|
(dynamic-require path POLLEN_ROOT))
|
|
|
|
|
|
|
|
(define/contract (directory->ptree dir)
|
|
|
|
(directory-pathish? . -> . ptree?)
|
|
|
|
(let ([files (map remove-ext (filter (λ(x) (has-ext? x POLLEN_DECODER_EXT)) (directory-list dir)))])
|
|
|
|
(message "Generating ptree from file listing of" dir)
|
|
|
|
(ptree-root->ptree (cons POLLEN_TREE_ROOT_NAME files))))
|
|
|
|
|
|
|
|
|
|
|
|
;; Try loading from ptree file, or failing that, synthesize ptree.
|
|
|
|
(define/contract (make-project-ptree [project-dir PROJECT_ROOT])
|
|
|
|
(() (directory-pathish?) . ->* . ptree?)
|
|
|
|
(define ptree-source (build-path project-dir DEFAULT_POLLEN_TREE))
|
|
|
|
(if (file-exists? ptree-source)
|
|
|
|
(file->ptree ptree-source)
|
|
|
|
(directory->ptree project-dir)))
|
|
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(let ([sample-main `(POLLEN_TREE_ROOT_NAME "foo" "bar" (one (two "three")))])
|
|
|
|
(check-equal? (ptree-root->ptree sample-main)
|
|
|
|
`(POLLEN_TREE_ROOT_NAME "foo" "bar" (one (two "three"))))))
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (parent pnode [ptree (current-ptree)])
|
|
|
|
(((or/c pnode? false?)) (ptree?) . ->* . (or/c pnode? false?))
|
|
|
|
(and pnode
|
|
|
|
(if (member (->string pnode) (map (λ(x) (->string (if (list? x) (car x) x))) (cdr ptree)))
|
|
|
|
(->string (car ptree))
|
|
|
|
(ormap (λ(x) (parent pnode x)) (filter list? ptree)))))
|
|
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(define test-ptree-main `(ptree-main "foo" "bar" (one (two "three"))))
|
|
|
|
(define test-ptree (ptree-root->ptree test-ptree-main))
|
|
|
|
(check-equal? (parent 'three test-ptree) "two")
|
|
|
|
(check-equal? (parent "three" test-ptree) "two")
|
|
|
|
(check-false (parent #f test-ptree))
|
|
|
|
(check-false (parent 'nonexistent-name test-ptree)))
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (children pnode [ptree (current-ptree)])
|
|
|
|
(((or/c pnode? false?)) (ptree?) . ->* . (or/c (listof pnode?) false?))
|
|
|
|
(and pnode
|
|
|
|
(if (equal? (->string pnode) (->string (car ptree)))
|
|
|
|
(map (λ(x) (->string (if (list? x) (car x) x))) (cdr ptree))
|
|
|
|
(ormap (λ(x) (children pnode x)) (filter list? ptree)))))
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(check-equal? (children 'one test-ptree) (list "two"))
|
|
|
|
(check-equal? (children 'two test-ptree) (list "three"))
|
|
|
|
(check-false (children 'three test-ptree))
|
|
|
|
(check-false (children #f test-ptree))
|
|
|
|
(check-false (children 'fooburger test-ptree)))
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (siblings pnode [ptree (current-ptree)])
|
|
|
|
(((or/c pnode? false?)) (ptree?) . ->* . (or/c (listof string?) false?))
|
|
|
|
(children (parent pnode ptree) ptree))
|
|
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(check-equal? (siblings 'one test-ptree) '("foo" "bar" "one"))
|
|
|
|
(check-equal? (siblings 'foo test-ptree) '("foo" "bar" "one"))
|
|
|
|
(check-equal? (siblings 'two test-ptree) '("two"))
|
|
|
|
(check-false (siblings #f test-ptree))
|
|
|
|
(check-false (siblings 'invalid-key test-ptree)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; flatten tree to sequence
|
|
|
|
(define/contract (ptree->list [ptree (current-ptree)])
|
|
|
|
(ptree? . -> . (listof string?))
|
|
|
|
; use cdr to get rid of root tag at front
|
|
|
|
(map ->string (cdr (flatten ptree))))
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(check-equal? (ptree->list test-ptree) '("foo" "bar" "one" "two" "three")))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (adjacents side pnode [ptree (current-ptree)])
|
|
|
|
(and pnode
|
|
|
|
(let* ([proc (if (equal? side 'left) takef takef-right)]
|
|
|
|
[result (proc (ptree->list ptree) (λ(x) (not (equal? (->string pnode) (->string x)))))])
|
|
|
|
(and (not (empty? result)) result))))
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (left-adjacents pnode [ptree (current-ptree)])
|
|
|
|
(((or/c pnode? false?)) (ptree?) . ->* . (or/c (listof pnode?) false?))
|
|
|
|
(adjacents 'left pnode ptree))
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(check-equal? (left-adjacents 'one test-ptree) '("foo" "bar"))
|
|
|
|
(check-equal? (left-adjacents 'three test-ptree) '("foo" "bar" "one" "two"))
|
|
|
|
(check-false (left-adjacents 'foo test-ptree)))
|
|
|
|
|
|
|
|
(define/contract (right-adjacents pnode [ptree (current-ptree)])
|
|
|
|
(((or/c pnode? false?)) (ptree?) . ->* . (or/c (listof pnode?) false?))
|
|
|
|
(adjacents 'right pnode ptree))
|
|
|
|
|
|
|
|
(define/contract (previous pnode [ptree (current-ptree)])
|
|
|
|
(((or/c pnode? false?)) (ptree?) . ->* . (or/c pnode? false?))
|
|
|
|
(let ([result (left-adjacents pnode ptree)])
|
|
|
|
(and result (last result))))
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(check-equal? (previous 'one test-ptree) "bar")
|
|
|
|
(check-equal? (previous 'three test-ptree) "two")
|
|
|
|
(check-false (previous 'foo test-ptree)))
|
|
|
|
|
|
|
|
|
|
|
|
(define (next pnode [ptree (current-ptree)])
|
|
|
|
(((or/c pnode? false?)) (ptree?) . ->* . (or/c pnode? false?))
|
|
|
|
(let ([result (right-adjacents pnode ptree)])
|
|
|
|
(and result (first result))))
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(check-equal? (next 'foo test-ptree) "bar")
|
|
|
|
(check-equal? (next 'one test-ptree) "two")
|
|
|
|
(check-false (next 'three test-ptree)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (pnode->url pnode [files (current-url-context)])
|
|
|
|
((pnode?) ((listof pathish?)) . ->* . (or/c pnode? false?))
|
|
|
|
;; upconvert all files to their output path
|
|
|
|
;; then remove duplicates because some sources might have already been rendered
|
|
|
|
(define output-paths (remove-duplicates (map ->output-path files) equal?))
|
|
|
|
;; find ones that match name
|
|
|
|
(define matching-paths (filter (λ(x) (equal? (->string x) (->string pnode))) output-paths))
|
|
|
|
|
|
|
|
(cond
|
|
|
|
[((len matching-paths) . = . 1) (->string (car matching-paths))]
|
|
|
|
[((len matching-paths) . > . 1) (error "More than one matching URL for" pnode)]
|
|
|
|
[else #f] ))
|
|
|
|
|
|
|
|
(define ptree-name->url pnode->url)
|
|
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(define files '("foo.html" "bar.html" "bar.html.p" "zap.html" "zap.xml"))
|
|
|
|
(check-equal? (pnode->url 'foo.html files) "foo.html")
|
|
|
|
(check-equal? (pnode->url 'bar.html files) "bar.html")
|
|
|
|
;; (check-equal? (name->url 'zap files) 'error) ;; todo: how to test error?
|
|
|
|
(check-false (pnode->url 'hee files)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; this sets default input for following functions
|
|
|
|
(define/contract (ptree-root->ptree tx)
|
|
|
|
;; (not/c ptree) prevents ptrees from being accepted as input
|
|
|
|
((and/c tagged-xexpr?) . -> . ptree?)
|
|
|
|
tx)
|
|
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(set! test-ptree-main `(ptree-main "foo" "bar" (one (two "three"))))
|
|
|
|
(check-equal? (ptree-root->ptree test-ptree-main)
|
|
|
|
`(ptree-main "foo" "bar" (one (two "three")))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; contract for ptree-source-decode
|
|
|
|
(define/contract (valid-names? x)
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
(andmap pnode?/error (filter-not whitespace? (flatten x))))
|
|
|
|
|
|
|
|
;; contract for ptree-source-decode
|
|
|
|
(define/contract (unique-names? x)
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
;; use map ->string to make keys comparable
|
|
|
|
(elements-unique? #:loud #t (map ->string (filter-not whitespace? (flatten x)))))
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (ptree-source-decode . elements)
|
|
|
|
(() #:rest (and/c valid-names? unique-names?) . ->* . ptree?)
|
|
|
|
(ptree-root->ptree (decode (cons POLLEN_TREE_ROOT_NAME elements)
|
|
|
|
#:xexpr-elements-proc (λ(xs) (filter-not whitespace? xs)))))
|
|
|
|
|
|
|
|
|
|
|
|
(define current-ptree (make-parameter `(,POLLEN_TREE_ROOT_NAME)))
|
|
|
|
(define current-url-context (make-parameter PROJECT_ROOT))
|
|
|
|
|
|
|
|
|
|
|
|
;; used to convert here-path into here
|
|
|
|
(define/contract (path->pnode path)
|
|
|
|
(pathish? . -> . pnode?)
|
|
|
|
(->string (->output-path (find-relative-path PROJECT_ROOT (->path path)))))
|
|
|
|
|
|
|
|
|
|
|
|
#|
|
|
|
|
(module+ main
|
|
|
|
(displayln "Running module main")
|
|
|
|
(set-current-ptree (make-project-ptree (->path "/Users/MB/git/bpt/")))
|
|
|
|
(set-current-url-context "/Users/MB/git/bpt/")
|
|
|
|
(ptree-previous (ptree-previous 'what-is-typography.html))
|
|
|
|
(name->url "how-to-pay-for-this-book.html"))
|
|
|
|
|#
|