您的位置:首页 > 其它

用 Emacs Lisp 开发 CGI 程序

2012-09-02 10:14 393 查看
Emacs Lisp 作为编程语言也是非常强大的。尤其 Emacs 作为一款编辑器,自带了很多处理文本的函数,用起来很方便。
我一直希望用 Emacs Lisp 作为服务端脚本语言来开发 Web 程序。在网上搜索了很久,还真有人做过类似的事情:http://www.emacswiki.org/emacs/cgi.el。但他封装的还不够彻底,用起来还是挺麻烦,于是自己动手写了一个。目前支持:
script-let,即可在 <% %> 或 <%= %> 之中插入 lisp 代码;
原生 s-exp 生成 html 文档;
GET/POST 请求支持;
存取 Cookie。
程序作为 CGI 运行于 Apache 下,这里提供一个简单的求斐波那契数的例程作为演示。
BTW,这也是我第一次使用 Github 托管代码:https://github.com/redraiment/emacs-cgi
#!/usr/local/bin/emacs --script

(require 'cgi)

(cgi/cookie "n"
 (or (cgi/param "n")
     (cgi/cookie "n")
     "15"))

(html
 (head
  (title "Fabonacci 1 -> n")
  (meta (http-equiv . "Content-Type")
        (content . "text/html; charset=UTF-8")))
 (body
  (form (method . post)
   (span "n = ")
   (input (type . text) (name . n))
   (button (type . submit) "Submit"))
  (table (border . 1) (width . "100%")
   (thead
    (caption "Fabonacci")
    (tr
     (th "#")
     (th "Value")))
   (tbody
    <%
    (let ((a 0) (b 1) x)
      (dotimes (i (string-to-number
                   (cgi/cookie "n")))
        (tr
         (td <%= (1+ i) %>)
         (td <%= (setq x a a b b (+ x b)) %>))))
    %>))))
运行效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: