www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

asymptote.rkt (2293B)


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