新千葉 ガーベージ・コレクション

FPGA マガジンやインターフェースで書けなかったこと等をちょぼちょぼ書いてます。@ryos36

defmacro もっかい

やりたいことは cl-who で mark-up を HTML に変換すること。cl-who のサンプルではこんな感じになっている。

(defmacro with-html (&body body)
  `(with-html-output-to-string (*standard-output* nil :prologue t)
     ,@body))

でもって、このように書く。

  (with-html
    (:html (:head (:title "Ryos cookie test"))
     (:body
         (:div "hello"))))

しかし、mark-up を返す関数とは親和性が低い。

(defun make-a-href (href str)
    `(:a :href ,href ,str))

これをつかって

(with-html 
   (make-a-href "abc" "def"))

みたいなことはうまくいかない。

(format t "~s~%"
          `(with-html (:xml ,(wiki-to-who "hello"))))

おしい。eval をつければ動く。

(format t "~s~%"
          (eval `(with-html (:xml ,(wiki-to-who "hello")))))

う〜ん。eval を削るにはmacro を使う。

(defmacro eval-with-html (&body body)
          `(eval `(with-html ,,@body)))

(format t "~s~%"
          (eval-with-html `(:xml ,(make-a-href "hello" "again"))))

できたけどいみがわからん、、、

hunchentoot のサンプルは次のようにシームレスに書かれている、、、なにかまちがってるな> 自分

(defun cookie-test ()
  (set-cookie "pumpkin" :value "barking")
  (no-cache)
  (with-html
    (:html
     (:head (:title "Hunchentoot cookie test"))
     (:body
      (:h2 (hunchentoot-link)
       " cookie test")
      (:p "You might have to reload this page to see the cookie value.")
      (info-table (cookie-in "pumpkin")
                  (mapcar 'car (cookies-in*)))))))