Common Lisp:如何使用宏为CL-WHO生成S表达式?

时间:2012-04-12 13:08:29

标签: html common-lisp

说我为CL-WHO定义了一个宏:

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

然后:

(test-html (:h1 "hallo"))

给予(第一行删除):

"<html>
  <body>
    <h1>
      hallo
    </h1>
  </body>
</html>"

正如所料。现在我已经定义了一个函数来生成CL-WHO使用的s表达式:

(defun test-header (txt)
  `(:h1 ,txt))

使用“hallo”返回时调用

(:h1 "hallo")

但现在我打电话

(test-html (test-header "hallo"))

它返回:

"<html>
  <body>

  </body>
</html>"

出了什么问题,为什么?

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。至于我可以谷歌出来的是,这在cl的正式版本中是不可能的:http://lisp-univ-etc.blogspot.com/2009/03/cl-who-macros.html

我使用的是此版本,它支持宏:https://github.com/vseloved/cl-who

答案 1 :(得分:1)

我倾向于解决这个问题的方法是定义一个像

这样的快捷方式宏
(defmacro html-to-stout (&body body)
  "Outputs HTML to standard out."
  `(with-html-output (*standard-output* nil :indent t) ,@body))

或字符串等效。这里的关键是它不输出:prologue,因此它可以输出HTML存储块而不是整页。一旦你有了这个,你可以做像

这样的事情
(defun test-header (text)
  (html-to-stout 
    (:h1 (str text))))

(test-html (test-header "Hello Hello"))
相关问题