Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 55 lines (45 sloc) 1.663 kb
f8a6c87 Rob Hunter importing the current leftparen
authored
1 #lang scheme/base
2
3 (require "util.scm")
4
5 (provide js-script-invoke
6 js-array
a45b130 Rob Hunter added js-hash fn
authored
7 js-hash
f8a6c87 Rob Hunter importing the current leftparen
authored
8 js-quote
9 js-call
10 js-call-on-load
11 )
12
13 (define (js-script-invoke . js-strs)
14 `(script ((language "javascript"))
15 ,(string-join js-strs "\n")))
16
17 (define (js-array scheme-lst)
18 (string-append "[" (string-join scheme-lst ", ") "]"))
19
a45b130 Rob Hunter added js-hash fn
authored
20 (define (js-hash scheme-hash)
21 (string-append
22 "{ "
23 (string-join (hash-map scheme-hash (lambda (k v) (format "~A:~A" (js-quote k) v)))
24 ", ")
25 "}"))
8ab1648 Rob Hunter safer quoting
authored
26
f8a6c87 Rob Hunter importing the current leftparen
authored
27 (define (js-quote thing)
28 (cond ((number? thing) (number->string thing))
8ab1648 Rob Hunter safer quoting
authored
29 ((or (string? thing) (symbol? thing))
30 (format "\"~A\"" (careful-string-quote (if (string? thing)
31 thing
32 (symbol->string thing)))))
f8a6c87 Rob Hunter importing the current leftparen
authored
33 ((eq? thing #t) "true")
34 ((eq? thing #f) "false")
35 (else (e "Don't know how to js-quote ~A." thing))))
36
8ab1648 Rob Hunter safer quoting
authored
37 (define (careful-string-quote scm-str)
38 (pregexp-replace-many scm-str
39 ("\n" => " ")
40 ("\r" => " ")
41 ("\"" => "'")))
42
f8a6c87 Rob Hunter importing the current leftparen
authored
43 (define (js-call-on-load fn-name #:quote-all (quote-all #f) . args)
44 (format "$(document).ready(function() { ~A })"
45 (apply js-call fn-name #:quote-all quote-all args)))
46
47 ;;
48 ;; js-call
49 ;;
50 ;; #:quote-all if #t, then assume all arguments are primitive JS values and js-quote them
51 ;;
52 (define (js-call fn-name #:quote-all (quote-all #f) . args)
53 (let ((args (if quote-all (map js-quote args) args)))
54 (string-append fn-name "(" (string-join args ", ") ")")))
Something went wrong with that request. Please try again.