adding required require to the expanded form, because the expanded module may not have an original dependency on the image library

This commit is contained in:
Danny Yoo 2011-08-09 15:27:00 -04:00
parent 3dfaeded1b
commit 6c2b05e8cc
4 changed files with 62 additions and 58 deletions

View File

@ -42,7 +42,7 @@
1 0 32 #"(lib \"text-snipclass.ss\" \"xml\")\0"
1 0 15 #"test-case-box%\0"
2 0 1 6 #"wxloc\0"
0 0 55 0 1 #"\0"
0 0 57 0 1 #"\0"
0 75 1 #"\0"
0 12 90 -1 90 -1 3 -1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 255 255 255 1 -1 0 9
#"Standard\0"
@ -203,17 +203,22 @@
1.0 0 92 -1 -1 -1 -1 -1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 255 255 0 -1 -1 0
1 #"\0"
0 -1 1 #"\0"
1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 200 0 0 0 0 0 -1 -1
0 35 0 24 3 12 #"#lang planet"
0 0 4 3 1 #" "
0 0 14 3 14 #"dyoo/whalesong"
1 0 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 200 0 0 0 0 0 -1 -1 0 1
#"\0"
0 -1 1 #"\0"
0 10 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 1.0 1.0 1.0 1.0 1.0 1.0 0 0 0 0 0 0
-1 -1 2 1 #"\0"
0 -1 1 #"\0"
0 10 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 1.0 1.0 1.0 1.0 1.0 1.0 0 0 0 0 0 0
-1 -1 0 34 0 26 3 12 #"#lang planet"
0 0 26 3 15 #" dyoo/whalesong"
0 0 4 29 1 #"\n"
0 0 22 3 1 #"("
0 0 14 3 7 #"require"
0 0 4 3 1 #" "
0 0 17 3 1 #" "
0 0 22 3 1 #"("
0 0 14 3 6 #"planet"
0 0 4 3 1 #" "
0 0 17 3 1 #" "
0 0 14 3 20 #"dyoo/whalesong/image"
0 0 22 3 2 #"))"
0 0 4 29 1 #"\n"
@ -579,13 +584,13 @@
0 0 4 29 1 #"\n"
0 0 22 3 1 #"("
0 0 14 3 11 #"image-width"
0 0 4 3 1 #" "
0 0 17 3 1 #" "
0 0 14 3 6 #"a-snip"
0 0 22 3 1 #")"
0 0 4 29 1 #"\n"
0 0 22 3 1 #"("
0 0 14 3 12 #"image-height"
0 0 4 3 1 #" "
0 0 17 3 1 #" "
0 0 14 3 6 #"a-snip"
0 0 22 3 1 #")"
0 0 4 29 1 #"\n"

View File

@ -3,16 +3,26 @@
(require planet/version
syntax/kerncase
net/base64
(for-template (this-package-in lang/kernel))
(for-template (this-package-in lang/kernel)
#;(this-package-in image/main))
;; FIXME: I don't quite understand why I should be doing a require
;; of the image library at compile time, and not at template time.
(this-package-in image/main))
(provide expand-out-images)
;; expand-out-images: syntax -> compiled-code
;; expand-out-images: syntax -> syntax
;; Takes programs and rips out their image snips in favor of calls to
;; image-url.
(define (expand-out-images stx)
(define expanded (expand stx))
;; We need to translate image snips in the expanded form so we can
;; fruitfully use compiler/zo-parse.
(define rewritten
(kernel-syntax-case (syntax-disarm expanded code-insp) #f
[(#%expression expr)
@ -20,10 +30,11 @@
(#%expression #,(on-expr #'expr)))]
[(module id name-id (#%plain-module-begin module-level-form ...))
#`(module id name-id (#%plain-module-begin
(require (planet dyoo/whalesong/resource))
(quasisyntax/loc stx
(module id name-id (#%plain-module-begin
(require (planet dyoo/whalesong/image))
#,@(map convert-images-to-resources
(syntax->list #'(module-level-form ...)))))]
(syntax->list #'(module-level-form ...))))))]
[(begin top-level-form ...)
(quasisyntax/loc stx
(begin #,@(map convert-images-to-resources
@ -31,9 +42,9 @@
[else
(convert-images-to-resources expanded)]))
;; We need to translate image snips in the expanded form so we can
;; fruitfully use compiler/zo-parse.
(compile rewritten))
rewritten)
(define code-insp (current-code-inspector))
@ -135,13 +146,21 @@
(values
(dynamic-require '2htdp/image 'image?)
(dynamic-require 'file/convertible 'convert)))
;; Translates image values to embeddable uris. See:
;; http://en.wikipedia.org/wiki/Data_URI_scheme
;; This code is ripped out of the tracer library written by
;; Will Zimrin and Jeanette Miranda.
;; returns the data-uri encoding of an image.
(define (image->uri img)
(define base64-bytes (base64-encode (convert img 'png-bytes)))
(string-append "data:image/png;charset=utf-8;base64,"
(bytes->string/utf-8 base64-bytes)))
(cond
[(image? (syntax-e datum-stx))
(with-syntax ([image-uri
(string-append "data:image/png;charset=utf-8;base64,"
(bytes->string/utf-8
(base64-encode
(convert (syntax-e datum-stx) 'png-bytes))))])
(image->uri (syntax-e datum-stx))])
(quasisyntax/loc datum-stx
(image-url image-uri)))]

View File

@ -78,8 +78,11 @@
(get-module-code p)))
;; get-compiled-code-from-port: input-port -> compiled-code
;; Compiles the source from scratch.
(define (get-compiled-code-from-port ip)
(parameterize ([read-accept-reader #t]
[current-namespace base-namespace])
(define stx (read-syntax (object-name ip) ip))
(expand-out-images stx)))
(define expanded-stx (expand-out-images stx))
(compile expanded-stx)))

View File

@ -1,23 +0,0 @@
#lang racket/base
(require file/convertible
net/base64
racket/contract
2htdp/image)
(provide/contract [image->uri
(image? . -> . string?)])
;; This code is ripped out of the tracer library written by
;; Will Zimrin and Jeanette Miranda.
;; Translates image values to embeddable uris. See:
;; http://en.wikipedia.org/wiki/Data_URI_scheme
;; returns the data-uri encoding of an image.
(define (image->uri img)
(define base64-bytes (base64-encode (convert img 'png-bytes)))
(string-append "data:image/png;charset=utf-8;base64,"
(bytes->string/utf-8 base64-bytes)))