From 4d2fe9af8d01434bf2e5c3e6d2e01671b072911a Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 15 Aug 2013 17:00:34 -0700 Subject: [PATCH] working on regenerate --- command.rkt | 8 +++--- readability.rkt | 12 +++++++-- regenerate.rkt | 65 +++++++++++++++++++++++++++++++++++++------------ server.rkt | 2 +- tools.rkt | 8 ++++++ 5 files changed, 73 insertions(+), 22 deletions(-) diff --git a/command.rkt b/command.rkt index 7784d0c..7df2dde 100644 --- a/command.rkt +++ b/command.rkt @@ -6,12 +6,12 @@ "" (vector-ref (current-command-line-arguments) 0))]) (case arg - [("start") - `(require (planet mb/pollen/server))] + [("serve") + `(require "server.rkt")] [("regenerate") `(begin (displayln "Regenerate all...") - (require (planet mb/pollen/regenerate)) + (require "regenerate.rkt") (regenerate-all-files))] [("clone") (let ([target-path (if (> (vector-length (current-command-line-arguments)) 1) @@ -21,7 +21,7 @@ `(begin (displayln "Clone & bone...") (require racket/file) - (require (planet mb/pollen/tools)) + (require "tools.rkt") (define (pollen-related-file? file) (any (list diff --git a/readability.rkt b/readability.rkt index 050a4d0..45ea1cd 100644 --- a/readability.rkt +++ b/readability.rkt @@ -37,6 +37,14 @@ ; todo: on bad input, it will pop a string error rather than symbol error (string->symbol (->string thing))) +;; general way of coercing to path +(define (->path thing) + ; todo: on bad input, it will pop a string error rather than symbol error + (string->path (->string thing))) + +(define (->complete-path thing) + (path->complete-path (->path thing))) + ;; general way of coercing to a list (define/contract (->list x) @@ -105,14 +113,14 @@ (any/c . -> . boolean?) (ormap (λ(proc) (proc x)) (list list? string? symbol? vector?))) -(define/contract (container? x) +(define/contract (gettable-container? x) (any/c . -> . boolean?) (ormap (λ(proc) (proc x)) (list sliceable-container? hash?))) ;; general way of fetching an item from a container (define/contract (get container start [end #f]) - ((container? any/c) ((λ(i)(or (integer? i) (and (symbol? i) (equal? i 'end))))) + ((gettable-container? any/c) ((λ(i)(or (integer? i) (and (symbol? i) (equal? i 'end))))) . ->* . any/c) (set! end diff --git a/regenerate.rkt b/regenerate.rkt index 3e008aa..271b851 100644 --- a/regenerate.rkt +++ b/regenerate.rkt @@ -16,12 +16,12 @@ (define (mod-date . paths) (set! paths (flatten paths)) - (when (all file-exists? paths) + (when (andmap file-exists? paths) (map file-or-directory-modify-seconds paths))) (define (log-refresh . paths) (set! paths (flatten paths)) - (change mod-dates paths (mod-date paths))) + (hash-set! mod-dates paths (mod-date paths))) (define (source-needs-refresh? . paths) (set! paths (flatten paths)) @@ -31,7 +31,7 @@ ; when you want to generate everything fresh, but not force everything (define (reset-mod-dates) (let [(keys (hash-keys mod-dates))] - (map (ƒ(k) (hash-remove mod-dates k)) keys))) + (map (λ(k) (hash-remove mod-dates k)) keys))) ; helper functions for regenerate functions (define pollen-file-root (current-directory)) @@ -41,6 +41,11 @@ (displayln (format "Regenerating: ~a" f)) (regenerate path))) +;; todo: maybe move this tools.rkt as a utility +(define (filename-of path) + (let-values ([(dir filename ignored) (split-path path)]) + filename)) + (define (regenerate-pmap-pages pmap) (define pmap-sequence (make-page-sequence (main->tree (dynamic-require pmap 'main)))) @@ -48,7 +53,7 @@ (for-each regenerate-file pmap-sequence)) (define (get-pollen-files-with-ext ext) - (filter (ƒ(f) (has-ext? f ext)) (directory-list pollen-file-root))) + (filter (λ(f) (has-ext? f ext)) (directory-list pollen-file-root))) ; burn all files (define (regenerate-all-files) @@ -64,19 +69,44 @@ +(define (preproc-source? path) + (has-ext? path POLLEN_PREPROC_EXT)) + +(define (make-preproc-in-path path) + (add-ext path POLLEN_PREPROC_EXT)) + +(define (make-preproc-out-path path) + (remove-ext path)) + +(define (has-preproc-source? path) + (file-exists? (make-preproc-in-path path))) + +(define (pollen-source? path) + (has-ext? path POLLEN_SOURCE_EXT)) + +(define (make-pollen-source-path thing) + (add-ext (remove-ext (->path thing)) POLLEN_SOURCE_EXT)) + +(define (has-pollen-source? path) + (file-exists? (make-pollen-source-path path))) + +(define (needs-preproc? path) + ; it's a preproc source file, or a file that's the result of a preproc source + (ormap (λ(proc) (proc path)) (list preproc-source? has-preproc-source?))) + +(define (needs-template? path) + ; it's a pollen source file + ; or a file (e.g., html) that has a pollen source file + (ormap (λ(proc) (proc path)) (list pollen-source? has-pollen-source?))) + +(define (pmap-source? path) + (has-ext? path POLLEN_MAP_EXT)) + (define (regenerate path #:force [force #f]) ; dispatches path-in to the right place - (define (needs-preproc? path) - ; it's a preproc source file, or a file that's the result of a preproc source - (any (list preproc-source? has-preproc-source?) path)) - (define (needs-template? path) - ; it's a pollen source file - ; or a file (e.g., html) that has a pollen source file - (any (list pollen-source? has-pollen-source?) path)) - - (let ([path (as-complete-path path)]) + (let ([path (->complete-path path)]) (cond [(needs-preproc? path) (do-preproc path #:force force)] [(needs-template? path) (do-template path #:force force)] @@ -85,7 +115,7 @@ (define (regenerate-message path) - (message "Regenerated:" (as-string (file-name-from-path path)))) + (message "Regenerated:" (->string (file-name-from-path path)))) (define (do-preproc path #:force [force #f]) ; set up preproc-in-path & preproc-out-path values @@ -113,7 +143,7 @@ ; take full path or filename ; return full path of templated file - (define source-path (as-complete-path + (define source-path (->complete-path (if (pollen-source? path) path (make-pollen-source-path path)))) @@ -141,6 +171,11 @@ ; todo: template file in body may not refer to a file that exists. ; todo: consider whether file-was-reloaded could change metas ; (because here, I'm retrieving them from existing source) + + ;;;;;;;;;;;;;; + ;; todo: next + ;;;;;;;;;;;;;; + (define meta-hash (make-meta-hash (put source-path))) (set! template-name (hash-ref-or meta-hash TEMPLATE_META_KEY DEFAULT_TEMPLATE))) (define template-path (build-path source-dir template-name)) diff --git a/server.rkt b/server.rkt index b6747b2..9e89e90 100755 --- a/server.rkt +++ b/server.rkt @@ -24,7 +24,7 @@ (define (get-query-value url key) ; query is parsed as list of pairs, key is symbol, value is string ; '((key . "value") ... ) - (let ([result (memf (ƒ(x) (=str (car x) key)) (url-query url))]) + (let ([result (memf (λ(x) (equal? (car x) key)) (url-query url))]) (if result (cdar result) ; second value of first result result))) diff --git a/tools.rkt b/tools.rkt index 8c0ca59..51983ee 100644 --- a/tools.rkt +++ b/tools.rkt @@ -46,6 +46,14 @@ (check-false (has-ext? foo.bar.txt-path 'doc))) ; wrong extension +;; put extension on path +(define/contract (add-ext path ext) + (path? (or/c symbol? string?) . -> . path?) + (string->path (string-append (->string path) "." (->string ext)))) + +(module+ test + (check-equal? (add-ext (string->path "foo") "txt") (string->path "foo.txt"))) + ;; take one extension off path (define/contract (remove-ext path) (path? . -> . path?)