forked from soegaard/bracket
-
Notifications
You must be signed in to change notification settings - Fork 4
/
asymptote.rkt
57 lines (53 loc) · 2.24 KB
/
asymptote.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#lang racket
(require scribble/manual
file/md5
racket/system)
(provide asymptote)
(define (asymptote #:cache [cache? #t] s . strs)
(define single-str
(with-output-to-string
(lambda () (for ([str (in-list `(,s . ,strs))])
(displayln str)))))
(if cache?
;; cache:
(let* ([asymptote-dir "asymptote-images"]
[md (bytes->string/utf-8 (md5 single-str))]
[asy-name (string-append md ".asy")]
[asy-path (build-path asymptote-dir asy-name)]
[png-name (string-append md ".png")]
[png-path (build-path asymptote-dir png-name)]
[eps-name (string-append md ".eps")]
[eps-path (build-path asymptote-dir eps-name)]
[pdf-name (string-append md ".pdf")]
[pdf-path (build-path asymptote-dir pdf-name)]
[svg-name (string-append md ".svg")]
[svg-path (build-path asymptote-dir svg-name)])
(display (current-directory)) (display md) (newline)
;; create dir if neccessary
(unless (directory-exists? asymptote-dir)
(make-directory asymptote-dir))
;; save asymptote code to <md5-of-input>.asy
(with-output-to-file asy-path
(lambda () (display single-str))
#:exists 'replace)
(parameterize ([current-directory (build-path (current-directory)
asymptote-dir)])
;; run asymptote to generate eps
(unless (file-exists? svg-name)
(system (format "asy -v -f svg ~a" asy-name)))
;; run asymptote to generate pdf
(unless (file-exists? pdf-name)
(system (format "asy -v -f pdf ~a" asy-name)))
;; run asymptote to generate png
(unless (file-exists? png-name)
(system (format "asy -v -f png ~a" asy-name)))
(image (build-path asymptote-dir md)
#:suffixes (list ".pdf" ".svg" ".png"))))
;; no cache:
(let ([tmp-file (make-temporary-file "asy-~a.png")])
(with-input-from-string
single-str
(λ ()
;(with-output-to-string
(system (format "asy -v -f png -o ~a" tmp-file))))
tmp-file))) ; HTML png PDF pdf