2012-04-12 118 views
1

说我已经定义为CL-WHO宏:Common Lisp:如何使用宏为CL-WHO生成S表达式?

(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>" 

正如所预期的。现在我已经定义了一个函数生成S-表达由CL-WHO使用:

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

当所谓的“你好”返回

(:h1 "hallo") 

但现在当我打电话

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

它返回:

"<html> 
    <body> 

    </body> 
</html>" 

出了什么问题,为什么?

回答

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")) 
1

我有同样的问题。至于我可以谷歌退出了,这是不可能的正式版CL-谁:http://lisp-univ-etc.blogspot.com/2009/03/cl-who-macros.html

我用这个版本代替,它支持宏:https://github.com/vseloved/cl-who

+1

感谢您的链接!这很有趣......在阅读整篇文章并进入评论部分后,我意识到2个月前我阅读了这篇文章并留下了评论。我的记忆让我失望 - 我会尝试Vsevolod的叉子! – mck 2012-04-12 13:50:56