Arc Forumnew | comments | leaders | submitlogin
What about letting tag take simple css selectors like (tag a.class) or (tag a#id)?
6 points by brett 5930 days ago | 1 comment
Since classes and ids are pretty common it could shorten a lot of calls to tag.

  arc> (tag (a class "foo"))
  <a class="foo"></a>
becomes

  arc> (tag a.foo)
  <a class="foo"></a>
likewise:

  arc> (tag (a id "foo" href "http://foo.com"))
  <a id=foo href="http://foo.com"></a>
  arc> (tag (a#foo href "http://foo.com"))
  <a id=foo href="http://foo.com"></a>
I gave it a shot

  (def decssize-spec (spec)
    (let tname (coerce (carif spec) 'string)
      (with (first# (findsubseq "#" tname) first. (findsubseq "." tname))
        (if (or first# first.)
            (with (new-tname (subseq tname 0 (or first# first.))
                   opt-pair (if first#
                                `(id ,(subseq tname (+ 1 first#)))
                                `(class ,(subseq tname (+ 1 first.)))))
               (if (atom spec)
                   (flat (list new-tname opt-pair))
                   (flat (list new-tname opt-pair (cdr spec)))))
            spec))))
         
  (mac tag (spec . body)
    (let spec (decssize-spec spec)
      `(do ,(start-tag spec)
           ,@body
           ,(end-tag spec))))
I'm pretty new to lisp, any tips to make it cleaner or more idiomatic would be nice. I guess next up would be stringing multiple classes and ids together like a#foo.bar.bat


2 points by oddbod 5929 days ago | link

http://haml.hamptoncatlin.com/

    arc> (tag (a #some-id .some-class))
    <a id="some-id" class="some-class"></a>

    arc> (tag (#box (tag #menu)))
    <div id="box"><div id="menu"></div></div>

    arc> (tag (#box .fancy .hmm))
    <div id="box" class="fancy hmm"></div>

-----