Improve resource-files code.

Makes it easy to add more files without more bindings.
This commit is contained in:
Eli Barzilay 2012-06-27 06:10:31 -04:00
parent 9d8b0b3051
commit 06f67b30f3
2 changed files with 32 additions and 34 deletions

View File

@ -178,30 +178,33 @@
(define (html-head-maker style favicon) (define (html-head-maker style favicon)
(define headers (define headers
@list{@meta[name: "generator" content: "Racket"] @list{
@meta[http-equiv: "Content-Type" content: "text/html; charset=utf-8"] @meta[name: "generator" content: "Racket"]
@favicon @meta[http-equiv: "Content-Type" content: "text/html; charset=utf-8"]
@style}) @favicon
@link[rel: "stylesheet" type: "text/css" href: style title: "default"]})
(λ (title* more-headers) (λ (title* more-headers)
(head "\n" (title title*) (head "\n" (title title*)
"\n" headers "\n" headers
(and more-headers (list "\n" more-headers)) (and more-headers (list "\n" more-headers))
"\n"))) "\n")))
(define (make-resources icon logo style) (define (make-resources files)
(let* ([favicon (html-favicon-maker icon)] (define (getfile what) (cadr (assq what files)))
[make-head (html-head-maker style favicon)] (define favicon (html-favicon-maker (getfile 'icon)))
[make-navbar (navbar-maker logo)]) (define make-head (html-head-maker (getfile 'style) favicon))
(λ (what . more) (define make-navbar (navbar-maker (getfile 'logo)))
(apply (case what (λ (what . more)
[(head) make-head] (apply (case what
[(navbar) make-navbar] [(head) make-head]
[(favicon-headers) favicon] [(navbar) make-navbar]
[(icon-path) (λ () (url-of icon))] [(favicon-headers) favicon]
[(logo-path) (λ () (url-of logo))] [(icon-path logo-path style-path)
[(style-path) (λ () (url-of style))] (λ () (let* ([x (symbol->string what)]
[else (error 'resources "internal error")]) [x (regexp-replace #rx"-path$" x "")])
more)))) (url-of (getfile (string->symbol x)))))]
[else (error 'resources "internal error")])
more)))
;; `define+provide-context' should be used in each toplevel directory (= each ;; `define+provide-context' should be used in each toplevel directory (= each
;; site) to have its own resources (and possibly other customizations). ;; site) to have its own resources (and possibly other customizations).
@ -214,10 +217,8 @@
[copyfile-id (datum->syntax stx 'copyfile)] [copyfile-id (datum->syntax stx 'copyfile)]
[symlink-id (datum->syntax stx 'symlink)] [symlink-id (datum->syntax stx 'symlink)]
[resources-id (datum->syntax stx 'the-resources)]) [resources-id (datum->syntax stx 'the-resources)])
(with-syntax ([resources (or resources (with-syntax ([resources (or resources #'(make-resources
#'(make-resources (make-icon dir) (make-resource-files dir)))]
(make-logo dir)
(make-style dir)))]
[provides (if provide? [provides (if provide?
#'(provide page-id plain-id copyfile-id #'(provide page-id plain-id copyfile-id
symlink-id resources-id) symlink-id resources-id)

View File

@ -10,20 +10,17 @@
(require "utils.rkt") (require "utils.rkt")
(provide make-logo make-icon make-style (provide make-resource-files
navbar-style page-sizes font-family) ; needed for the blog template navbar-style page-sizes font-family) ; needed for the blog template
(define ((make-file-copier file) dir) (define (make-resource-files dir)
(copyfile-resource (in-here file) (web-path dir file))) (define (copyfile file)
(copyfile-resource (in-here file) (web-path dir file)))
(define make-logo (make-file-copier "logo.png")) (define (writefile file contents)
(define make-icon (make-file-copier "plticon.ico")) (resource (web-path dir file) (file-writer output (list contents "\n"))))
`([logo ,(copyfile "logo.png")]
(define (make-style dir) [icon ,(copyfile "plticon.ico")]
(resource/referrer (web-path dir "plt.css") [style ,(writefile "plt.css" racket-style)]))
(file-writer output (list racket-style "\n"))
(λ (url) (link rel: "stylesheet" type: "text/css"
href: url title: "default"))))
(define page-sizes (define page-sizes
@list{ @list{