|
|
|
@ -1,22 +1,45 @@
|
|
|
|
|
#lang racket/base
|
|
|
|
|
(require racket/path racket/bool xml)
|
|
|
|
|
(require "tools.rkt" "world.rkt" "decode.rkt" sugar txexpr "cache.rkt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define+provide current-ptree (make-parameter #f))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define+provide (pnode? x)
|
|
|
|
|
(->boolean (and (xexpr? x) (try (not (whitespace/nbsp? (->string x)))
|
|
|
|
|
(->boolean (and (symbol? x) (try (not (whitespace/nbsp? (->string x)))
|
|
|
|
|
(except [exn:fail? (λ(e) #f)])))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define+provide/contract (pnode?/error x)
|
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
|
(or (pnode? x) (error "Not a valid pnode:" x)))
|
|
|
|
|
(define+provide (pnodeish? x)
|
|
|
|
|
(try (pnode? (->symbol x))
|
|
|
|
|
(except [exn:fail? (λ(e) #f)])))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract+provide (->pnode x)
|
|
|
|
|
(pnodeish? . -> . pnode?)
|
|
|
|
|
(->symbol x))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define+provide/contract (decode-ptree xs)
|
|
|
|
|
(txexpr-elements? . -> . any/c) ; because ptree is being explicitly validated
|
|
|
|
|
(validate-ptree
|
|
|
|
|
(decode (cons world:ptree-root-node xs)
|
|
|
|
|
#:txexpr-elements-proc (λ(xs) (filter (compose1 not whitespace?) xs))
|
|
|
|
|
#:string-proc string->symbol))) ; because faster than ->pnode
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define+provide (validate-ptree x)
|
|
|
|
|
(let ([pnodes (ptree->list x)])
|
|
|
|
|
(and
|
|
|
|
|
(andmap (λ(p) (or (pnode? p) (error (format "validate-ptree: \"~a\" is not a valid pnode" p)))) pnodes)
|
|
|
|
|
(try (members-unique?/error pnodes)
|
|
|
|
|
(except [exn:fail? (λ(e) (error (format "validate-ptree: ~a" (exn-message e))))]))
|
|
|
|
|
x)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define+provide (ptree? x)
|
|
|
|
|
(->boolean (and (txexpr? x)
|
|
|
|
|
(andmap (λ(i) (or (pnode? i) (ptree? i))) x)
|
|
|
|
|
(members-unique? (ptree->list x)))))
|
|
|
|
|
(try (->boolean (validate-ptree x))
|
|
|
|
|
(except [exn:fail? (λ(e) #f)])))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Try loading from ptree file, or failing that, synthesize ptree.
|
|
|
|
@ -26,107 +49,64 @@
|
|
|
|
|
(cached-require ptree-source world:main-pollen-export))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define+provide/contract (parent pnode [ptree (current-ptree)])
|
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? pnode?))
|
|
|
|
|
(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)))))
|
|
|
|
|
(define+provide/contract (parent p [ptree (current-ptree)])
|
|
|
|
|
(((or/c #f pnodeish?)) (ptree?) . ->* . (or/c #f pnode?))
|
|
|
|
|
(and ptree p
|
|
|
|
|
(let ([pnode (->pnode p)])
|
|
|
|
|
(if (member pnode (map (λ(x) (if (list? x) (car x) x)) (cdr ptree)))
|
|
|
|
|
(car ptree)
|
|
|
|
|
(ormap (λ(x) (parent pnode x)) (filter list? ptree))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define+provide/contract (children pnode [ptree (current-ptree)])
|
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? (listof pnode?)))
|
|
|
|
|
(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)))))
|
|
|
|
|
(define+provide/contract (children p [ptree (current-ptree)])
|
|
|
|
|
(((or/c #f pnodeish?)) (ptree?) . ->* . (or/c #f (listof pnode?)))
|
|
|
|
|
(and ptree p
|
|
|
|
|
(let ([pnode (->pnode p)])
|
|
|
|
|
(if (equal? pnode (car ptree))
|
|
|
|
|
(map (λ(x) (if (list? x) (car x) x)) (cdr ptree))
|
|
|
|
|
(ormap (λ(x) (children pnode x)) (filter list? ptree))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define+provide/contract (siblings pnode [ptree (current-ptree)])
|
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? (listof string?)))
|
|
|
|
|
(children (parent pnode ptree) ptree))
|
|
|
|
|
(define+provide/contract (siblings p [ptree (current-ptree)])
|
|
|
|
|
(((or/c #f pnodeish?)) (ptree?) . ->* . (or/c #f (listof pnode?)))
|
|
|
|
|
(children (parent p ptree) ptree))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; flatten tree to sequence
|
|
|
|
|
(define+provide/contract (ptree->list [ptree (current-ptree)])
|
|
|
|
|
(ptree? . -> . (listof string?))
|
|
|
|
|
(define+provide/contract (ptree->list ptree)
|
|
|
|
|
(ptree? . -> . (listof pnode?))
|
|
|
|
|
; use cdr to get rid of root tag at front
|
|
|
|
|
(map ->string (cdr (flatten ptree))))
|
|
|
|
|
(cdr (flatten ptree)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define+provide/contract (adjacents side pnode [ptree (current-ptree)])
|
|
|
|
|
((symbol? (or/c false? pnode?)) (ptree?) . ->* . (or/c false? (listof pnode?)))
|
|
|
|
|
(and pnode
|
|
|
|
|
(let* ([proc (if (equal? side 'left) takef takef-right)]
|
|
|
|
|
[result (proc (ptree->list ptree) (λ(x) (not (equal? (->string pnode) (->string x)))))])
|
|
|
|
|
(define (adjacents side p [ptree (current-ptree)])
|
|
|
|
|
; ((symbol? (or/c #f pnodeish?)) (ptree?) . ->* . (or/c #f (listof pnode?)))
|
|
|
|
|
(and ptree p
|
|
|
|
|
(let* ([pnode (->pnode p)]
|
|
|
|
|
[proc (if (equal? side 'left) takef takef-right)]
|
|
|
|
|
[result (proc (ptree->list ptree) (λ(x) (not (equal? pnode x))))])
|
|
|
|
|
(and (not (empty? result)) result))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define+provide/contract (left-adjacents pnode [ptree (current-ptree)])
|
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? (listof pnode?)))
|
|
|
|
|
(define+provide/contract (previous* pnode [ptree (current-ptree)])
|
|
|
|
|
(((or/c #f pnodeish?)) (ptree?) . ->* . (or/c #f (listof pnode?)))
|
|
|
|
|
(adjacents 'left pnode ptree))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define+provide/contract (right-adjacents pnode [ptree (current-ptree)])
|
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? (listof pnode?)))
|
|
|
|
|
(define+provide/contract (next* pnode [ptree (current-ptree)])
|
|
|
|
|
(((or/c #f pnodeish?)) (ptree?) . ->* . (or/c #f (listof pnode?)))
|
|
|
|
|
(adjacents 'right pnode ptree))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define+provide/contract (previous pnode [ptree (current-ptree)])
|
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? pnode?))
|
|
|
|
|
(let ([result (left-adjacents pnode ptree)])
|
|
|
|
|
(((or/c #f pnodeish?)) (ptree?) . ->* . (or/c #f pnode?))
|
|
|
|
|
(let ([result (previous* pnode ptree)])
|
|
|
|
|
(and result (last result))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define+provide/contract (next pnode [ptree (current-ptree)])
|
|
|
|
|
(((or/c false? pnode?)) (ptree?) . ->* . (or/c false? pnode?))
|
|
|
|
|
(let ([result (right-adjacents pnode ptree)])
|
|
|
|
|
(((or/c #f pnodeish?)) (ptree?) . ->* . (or/c #f pnode?))
|
|
|
|
|
(let ([result (next* pnode ptree)])
|
|
|
|
|
(and result (first result))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; this is a helper function to permit unit tests
|
|
|
|
|
(define+provide (pnode->url/paths pnode url-list)
|
|
|
|
|
;; check for duplicates because some sources might have already been rendered
|
|
|
|
|
(define output-paths (remove-duplicates (map ->output-path url-list) equal?))
|
|
|
|
|
(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+provide/contract (pnode->url pnode [url-context (current-url-context)])
|
|
|
|
|
((pnode?) (pathish?) . ->* . (or/c false? pnode?))
|
|
|
|
|
(parameterize ([current-url-context url-context])
|
|
|
|
|
(pnode->url/paths pnode (directory-list (current-url-context)))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; this sets default input for following functions
|
|
|
|
|
(define+provide/contract (ptree-root->ptree tx)
|
|
|
|
|
;; (not/c ptree) prevents ptrees from being accepted as input
|
|
|
|
|
((and/c txexpr?) . -> . ptree?)
|
|
|
|
|
tx)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define+provide/contract (pnodes-unique?/error x)
|
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
|
(define members (filter-not whitespace? (flatten x)))
|
|
|
|
|
(and (andmap pnode?/error members)
|
|
|
|
|
(members-unique?/error (map ->string members))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define+provide/contract (ptree-source-decode . elements)
|
|
|
|
|
(() #:rest pnodes-unique?/error . ->* . ptree?)
|
|
|
|
|
(ptree-root->ptree (decode (cons world:ptree-root-node elements)
|
|
|
|
|
#:txexpr-elements-proc (λ(xs) (filter-not whitespace? xs)))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define current-ptree (make-parameter #f))
|
|
|
|
|
(define current-url-context (make-parameter (world:current-project-root)))
|
|
|
|
|
(provide current-ptree current-url-context)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; used to convert here-path into here
|
|
|
|
|
(define+provide/contract (path->pnode path)
|
|
|
|
|
(pathish? . -> . pnode?)
|
|
|
|
|
(->string (->output-path (find-relative-path (world:current-project-root) (->path path)))))
|