|
|
@ -1,17 +1,17 @@
|
|
|
|
#lang racket/base
|
|
|
|
#lang racket/base
|
|
|
|
(require racket/contract racket/match racket/path xml/path racket/bool racket/rerequire)
|
|
|
|
(require (for-syntax racket/base))
|
|
|
|
(require "tools.rkt" "world.rkt" "debug.rkt" "decode.rkt" sugar txexpr)
|
|
|
|
(require racket/path racket/bool racket/rerequire racket/contract)
|
|
|
|
|
|
|
|
(require "tools.rkt" "world.rkt" "decode.rkt" sugar txexpr)
|
|
|
|
|
|
|
|
|
|
|
|
(module+ test (require rackunit))
|
|
|
|
(module+ test (require rackunit))
|
|
|
|
|
|
|
|
|
|
|
|
(provide pnode? ptree? parent children previous next pnode->url ptree-source-decode path->pnode ptree->list file->ptree make-project-ptree current-ptree current-url-context)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (pnode? x)
|
|
|
|
(define+provide/contract (pnode? x)
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
(try (not (whitespace? (->string x)))
|
|
|
|
(try (not (whitespace? (->string x)))
|
|
|
|
(except [exn:fail? (λ(e) #f)])))
|
|
|
|
(except [exn:fail? (λ(e) #f)])))
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (pnode?/error x)
|
|
|
|
(define+provide/contract (pnode?/error x)
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
(or (pnode? x) (error "Not a valid pnode:" x)))
|
|
|
|
(or (pnode? x) (error "Not a valid pnode:" x)))
|
|
|
|
|
|
|
|
|
|
|
@ -26,7 +26,7 @@
|
|
|
|
(check-false (pnode? " ")))
|
|
|
|
(check-false (pnode? " ")))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (ptree? x)
|
|
|
|
(define+provide/contract (ptree? x)
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
(and (txexpr? x) (andmap (λ(i) (or (pnode? i) (ptree? i))) x)))
|
|
|
|
(and (txexpr? x) (andmap (λ(i) (or (pnode? i) (ptree? i))) x)))
|
|
|
|
|
|
|
|
|
|
|
@ -45,21 +45,20 @@
|
|
|
|
(and (hash-has-key? ptree-source-mod-dates ptree-source-path)
|
|
|
|
(and (hash-has-key? ptree-source-mod-dates ptree-source-path)
|
|
|
|
((file-or-directory-modify-seconds ptree-source-path) . = . (hash-ref ptree-source-mod-dates ptree-source-path))))
|
|
|
|
((file-or-directory-modify-seconds ptree-source-path) . = . (hash-ref ptree-source-mod-dates ptree-source-path))))
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (file->ptree p)
|
|
|
|
(define+provide/contract (file->ptree p)
|
|
|
|
(pathish? . -> . ptree?)
|
|
|
|
(pathish? . -> . ptree?)
|
|
|
|
(define path (->path p))
|
|
|
|
(define path (->path p))
|
|
|
|
;; (message "Loading ptree file" (->string (file-name-from-path path)))
|
|
|
|
;; (message "Loading ptree file" (->string (file-name-from-path path)))
|
|
|
|
(dynamic-rerequire path)
|
|
|
|
(dynamic-rerequire path)
|
|
|
|
(dynamic-require path MAIN_POLLEN_EXPORT))
|
|
|
|
(dynamic-require path MAIN_POLLEN_EXPORT))
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (directory->ptree dir)
|
|
|
|
(define+provide/contract (directory->ptree dir)
|
|
|
|
(directory-pathish? . -> . ptree?)
|
|
|
|
(directory-pathish? . -> . ptree?)
|
|
|
|
(let ([files (map remove-ext (filter (λ(x) (has-ext? x DECODER_SOURCE_EXT)) (directory-list dir)))])
|
|
|
|
(let ([files (map remove-ext (filter (λ(x) (has-ext? x DECODER_SOURCE_EXT)) (directory-list dir)))])
|
|
|
|
(message "Generating ptree from file listing of" dir)
|
|
|
|
|
|
|
|
(ptree-root->ptree (cons PTREE_ROOT_NODE files))))
|
|
|
|
(ptree-root->ptree (cons PTREE_ROOT_NODE files))))
|
|
|
|
|
|
|
|
|
|
|
|
;; Try loading from ptree file, or failing that, synthesize ptree.
|
|
|
|
;; Try loading from ptree file, or failing that, synthesize ptree.
|
|
|
|
(define/contract (make-project-ptree project-dir)
|
|
|
|
(define+provide/contract (make-project-ptree project-dir)
|
|
|
|
(directory-pathish? . -> . ptree?)
|
|
|
|
(directory-pathish? . -> . ptree?)
|
|
|
|
(define ptree-source (build-path project-dir DEFAULT_PTREE))
|
|
|
|
(define ptree-source (build-path project-dir DEFAULT_PTREE))
|
|
|
|
(if (file-exists? ptree-source)
|
|
|
|
(if (file-exists? ptree-source)
|
|
|
@ -77,7 +76,7 @@
|
|
|
|
`(POLLEN_TREE_ROOT_NAME "foo" "bar" (one (two "three"))))))
|
|
|
|
`(POLLEN_TREE_ROOT_NAME "foo" "bar" (one (two "three"))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (parent pnode [ptree (current-ptree)])
|
|
|
|
(define+provide/contract (parent pnode [ptree (current-ptree)])
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? pnode?))
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? pnode?))
|
|
|
|
(and pnode
|
|
|
|
(and pnode
|
|
|
|
(if (member (->string pnode) (map (λ(x) (->string (if (list? x) (car x) x))) (cdr ptree)))
|
|
|
|
(if (member (->string pnode) (map (λ(x) (->string (if (list? x) (car x) x))) (cdr ptree)))
|
|
|
@ -94,7 +93,7 @@
|
|
|
|
(check-false (parent 'nonexistent-name test-ptree)))
|
|
|
|
(check-false (parent 'nonexistent-name test-ptree)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (children pnode [ptree (current-ptree)])
|
|
|
|
(define+provide/contract (children pnode [ptree (current-ptree)])
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? (listof pnode?)))
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? (listof pnode?)))
|
|
|
|
(and pnode
|
|
|
|
(and pnode
|
|
|
|
(if (equal? (->string pnode) (->string (car ptree)))
|
|
|
|
(if (equal? (->string pnode) (->string (car ptree)))
|
|
|
@ -109,7 +108,7 @@
|
|
|
|
(check-false (children 'fooburger test-ptree)))
|
|
|
|
(check-false (children 'fooburger test-ptree)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (siblings pnode [ptree (current-ptree)])
|
|
|
|
(define+provide/contract (siblings pnode [ptree (current-ptree)])
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? (listof string?)))
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? (listof string?)))
|
|
|
|
(children (parent pnode ptree) ptree))
|
|
|
|
(children (parent pnode ptree) ptree))
|
|
|
|
|
|
|
|
|
|
|
@ -124,7 +123,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; flatten tree to sequence
|
|
|
|
;; flatten tree to sequence
|
|
|
|
(define/contract (ptree->list [ptree (current-ptree)])
|
|
|
|
(define+provide/contract (ptree->list [ptree (current-ptree)])
|
|
|
|
(ptree? . -> . (listof string?))
|
|
|
|
(ptree? . -> . (listof string?))
|
|
|
|
; use cdr to get rid of root tag at front
|
|
|
|
; use cdr to get rid of root tag at front
|
|
|
|
(map ->string (cdr (flatten ptree))))
|
|
|
|
(map ->string (cdr (flatten ptree))))
|
|
|
@ -134,14 +133,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (adjacents side pnode [ptree (current-ptree)])
|
|
|
|
(define+provide/contract (adjacents side pnode [ptree (current-ptree)])
|
|
|
|
|
|
|
|
((symbol? (or/c false? pnode?)) (ptree?) . ->* . (or/c false? (listof pnode?)))
|
|
|
|
(and pnode
|
|
|
|
(and pnode
|
|
|
|
(let* ([proc (if (equal? side 'left) takef takef-right)]
|
|
|
|
(let* ([proc (if (equal? side 'left) takef takef-right)]
|
|
|
|
[result (proc (ptree->list ptree) (λ(x) (not (equal? (->string pnode) (->string x)))))])
|
|
|
|
[result (proc (ptree->list ptree) (λ(x) (not (equal? (->string pnode) (->string x)))))])
|
|
|
|
(and (not (empty? result)) result))))
|
|
|
|
(and (not (empty? result)) result))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (left-adjacents pnode [ptree (current-ptree)])
|
|
|
|
(define+provide/contract (left-adjacents pnode [ptree (current-ptree)])
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? (listof pnode?)))
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? (listof pnode?)))
|
|
|
|
(adjacents 'left pnode ptree))
|
|
|
|
(adjacents 'left pnode ptree))
|
|
|
|
|
|
|
|
|
|
|
@ -150,11 +150,11 @@
|
|
|
|
(check-equal? (left-adjacents 'three test-ptree) '("foo" "bar" "one" "two"))
|
|
|
|
(check-equal? (left-adjacents 'three test-ptree) '("foo" "bar" "one" "two"))
|
|
|
|
(check-false (left-adjacents 'foo test-ptree)))
|
|
|
|
(check-false (left-adjacents 'foo test-ptree)))
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (right-adjacents pnode [ptree (current-ptree)])
|
|
|
|
(define+provide/contract (right-adjacents pnode [ptree (current-ptree)])
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? (listof pnode?)))
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? (listof pnode?)))
|
|
|
|
(adjacents 'right pnode ptree))
|
|
|
|
(adjacents 'right pnode ptree))
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (previous pnode [ptree (current-ptree)])
|
|
|
|
(define+provide/contract (previous pnode [ptree (current-ptree)])
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? pnode?))
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? pnode?))
|
|
|
|
(let ([result (left-adjacents pnode ptree)])
|
|
|
|
(let ([result (left-adjacents pnode ptree)])
|
|
|
|
(and result (last result))))
|
|
|
|
(and result (last result))))
|
|
|
@ -165,7 +165,7 @@
|
|
|
|
(check-false (previous 'foo test-ptree)))
|
|
|
|
(check-false (previous 'foo test-ptree)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (next pnode [ptree (current-ptree)])
|
|
|
|
(define+provide/contract (next pnode [ptree (current-ptree)])
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? pnode?))
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? pnode?))
|
|
|
|
(let ([result (right-adjacents pnode ptree)])
|
|
|
|
(let ([result (right-adjacents pnode ptree)])
|
|
|
|
(and result (first result))))
|
|
|
|
(and result (first result))))
|
|
|
@ -178,7 +178,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; this is a helper function to permit unit tests
|
|
|
|
;; this is a helper function to permit unit tests
|
|
|
|
(define (pnode->url/paths pnode url-list)
|
|
|
|
(define+provide (pnode->url/paths pnode url-list)
|
|
|
|
;; check for duplicates because some sources might have already been rendered
|
|
|
|
;; check for duplicates because some sources might have already been rendered
|
|
|
|
(define output-paths (remove-duplicates (map ->output-path url-list) equal?))
|
|
|
|
(define output-paths (remove-duplicates (map ->output-path url-list) equal?))
|
|
|
|
(define matching-paths (filter (λ(x) (equal? (->string x) (->string pnode))) output-paths))
|
|
|
|
(define matching-paths (filter (λ(x) (equal? (->string x) (->string pnode))) output-paths))
|
|
|
@ -196,7 +196,7 @@
|
|
|
|
(check-false (pnode->url/paths 'hee files)))
|
|
|
|
(check-false (pnode->url/paths 'hee files)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (pnode->url pnode [url-context (current-url-context)])
|
|
|
|
(define+provide/contract (pnode->url pnode [url-context (current-url-context)])
|
|
|
|
((pnode?) (pathish?) . ->* . (or/c false? pnode?))
|
|
|
|
((pnode?) (pathish?) . ->* . (or/c false? pnode?))
|
|
|
|
(parameterize ([current-url-context url-context])
|
|
|
|
(parameterize ([current-url-context url-context])
|
|
|
|
(pnode->url/paths pnode (directory-list (current-url-context)))))
|
|
|
|
(pnode->url/paths pnode (directory-list (current-url-context)))))
|
|
|
@ -205,7 +205,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; this sets default input for following functions
|
|
|
|
;; this sets default input for following functions
|
|
|
|
(define/contract (ptree-root->ptree tx)
|
|
|
|
(define+provide/contract (ptree-root->ptree tx)
|
|
|
|
;; (not/c ptree) prevents ptrees from being accepted as input
|
|
|
|
;; (not/c ptree) prevents ptrees from being accepted as input
|
|
|
|
((and/c txexpr?) . -> . ptree?)
|
|
|
|
((and/c txexpr?) . -> . ptree?)
|
|
|
|
tx)
|
|
|
|
tx)
|
|
|
@ -217,13 +217,13 @@
|
|
|
|
`(,PTREE_ROOT_NODE "foo" "bar" (one (two "three")))))
|
|
|
|
`(,PTREE_ROOT_NODE "foo" "bar" (one (two "three")))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (pnodes-unique?/error x)
|
|
|
|
(define+provide/contract (pnodes-unique?/error x)
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
(define members (filter-not whitespace? (flatten x)))
|
|
|
|
(define members (filter-not whitespace? (flatten x)))
|
|
|
|
(and (andmap pnode?/error members)
|
|
|
|
(and (andmap pnode?/error members)
|
|
|
|
(members-unique?/error (map ->string members))))
|
|
|
|
(members-unique?/error (map ->string members))))
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (ptree-source-decode . elements)
|
|
|
|
(define+provide/contract (ptree-source-decode . elements)
|
|
|
|
(() #:rest pnodes-unique?/error . ->* . ptree?)
|
|
|
|
(() #:rest pnodes-unique?/error . ->* . ptree?)
|
|
|
|
(ptree-root->ptree (decode (cons PTREE_ROOT_NODE elements)
|
|
|
|
(ptree-root->ptree (decode (cons PTREE_ROOT_NODE elements)
|
|
|
|
#:xexpr-elements-proc (λ(xs) (filter-not whitespace? xs)))))
|
|
|
|
#:xexpr-elements-proc (λ(xs) (filter-not whitespace? xs)))))
|
|
|
@ -231,10 +231,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
(define current-ptree (make-parameter `(,PTREE_ROOT_NODE)))
|
|
|
|
(define current-ptree (make-parameter `(,PTREE_ROOT_NODE)))
|
|
|
|
(define current-url-context (make-parameter PROJECT_ROOT))
|
|
|
|
(define current-url-context (make-parameter PROJECT_ROOT))
|
|
|
|
|
|
|
|
(provide current-ptree current-url-context)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; used to convert here-path into here
|
|
|
|
;; used to convert here-path into here
|
|
|
|
(define/contract (path->pnode path)
|
|
|
|
(define+provide/contract (path->pnode path)
|
|
|
|
(pathish? . -> . pnode?)
|
|
|
|
(pathish? . -> . pnode?)
|
|
|
|
(->string (->output-path (find-relative-path PROJECT_ROOT (->path path)))))
|
|
|
|
(->string (->output-path (find-relative-path PROJECT_ROOT (->path path)))))
|
|
|
|
|
|
|
|
|
|
|
|