Thanks George

Of interest to me is that you eschew the use of syntax-parse / -case /
-rules in favour of a straight syntax->datum -> straight racket ->
datum->syntax solution. I suppose this approach trades away hygiene and
error-checking for simplicity.

Also, nice trick appropriating byte-strings:

* * *
I have a similar in spirit (but less sophisticated) macro that embeds
mustache-style templating in sxml in conjunction with Jens's Urlang JS
replacement in conjunction with Neil's html-writing library.

It currently does two things:

   1. Creates template variables: ($ foo) -> "{{foo}}"
   2. Maps embedded Urlang function calls to JS syntax: ($ (foo bar baz))
   -> "foo(bar, baz)"

E.g. this snippet of sxml

  `((h1 "SVG test")
    (div
     (input (@ (type "range") (value ,($ radius))
               (min "10") (max "95") (step "5")))
     (br) (br)
     (button (@ (on-click "swap")) "Change color"))

    (svg (@ (width "200") (height "200"))
         (circle (@ (cx "100") (cy "100") (r ,($ radius))
                    (stroke "green") (stroke-width "4")
                    (fill ,($ fill)))))))

maps to

<h1>SVG test</h1>
<div>
  <input type="range" value="{{radius}}" min="10" max="95" step="5">
  <br> <br>
  <button on-click="swap">Change color</button>
</div>
<svg width="200" height="200">
  <circle cx="100" cy="100" r="{{radius}}"
          stroke="green" stroke-width="4" fill="{{fill}}">
  </circle>
</svg>


when passed through (xexp->html ...) and formatted nicely. Here's my little
macro ...

(define-syntax ($ stx)
  (syntax-parse stx
    [(_ var:id)
     (with-syntax ([v (datum->syntax stx
                                     (format "{{~a}}" (syntax->datum
#'var)))])
       #'v)]
    [(_ (fn:id args:id ...))
     (with-syntax ([js (datum->syntax stx
                                      (format "{{~a(~a)}}"
                                                  (syntax->datum #'fn)
                                                  (string-join
                                                   (map (compose
                                                         (λ (x) (format
"~a" x))
                                                         syntax->datum)
                                                        (syntax->list
#'(args ...)))
                                                   ", ")))])
       #'js)]))


Compared to George's macro, it uses syntax-parse for pattern-matching
rather than match, and still uses datum->syntax / syntax->datum, but more
locally within with-syntax.

Dan

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to