我正在寻找一个解决方案,让我编写本地Emacs Lisp代码,并在编译时把它变成HTML,如
Franz’s htmlgen:
(html ((:div class "post") (:h1 "Title") (:p "Hello,World!")))
当然我可以write my own macros,但如果有任何项目围绕这个问题,我有兴趣。
解决方法
found out,
xmlgen
从列表结构生成XML。我用“xmlgen”软件包发现令人失望的是,它支持的格式与Emacs的
xml parser相反。
我没有添加到我的xmlgen的副本:
;; this creates a routine to be the inverse of what xml-parse does ;;;###autoload (defun xml-gen (form &optional in-elm level) "Convert a sexp to xml: '(p :class \"big\")) => \"<p class=\\\"big\\\" />\"" (let ((level (or level 0))) (cond ((numberp form) (number-to-string form)) ((stringp form) form) ((listp form) (destructuring-bind (xml attrs) (xml-gen-extract-plist form) (let ((el (car xml))) (unless (symbolp el) (error "Element must be a symbol (got '%S')." el)) (setq el (symbol-name el)) (concat "<" el (xml-gen-attr-to-string attrs) (if (> (length xml) 1) (concat ">" (mapconcat (lambda (s) (xml-gen s el (1+ level))) (cdr xml) "") "</" el ">") "/>")))))))) (defun xml-gen-attr-to-string (plist) (reduce 'concat (mapcar (lambda (p) (concat " " (symbol-name (car p)) "=\"" (cdr p) "\"")) plist))) (defun xml-gen-extract-plist (list) (list (cons (car list) (let ((kids (xml-node-children list))) (if (= 1 (length kids)) kids (remove-if-not 'listp kids)))) (xml-node-attributes list)))
注意:这个接口是xml-gen(不是原始解析的xmlgen)。
使用此界面,以下内容如下:
(string-equal (xml-gen (car (xml-parse-region <some-region-of-xml>))) <some-region-of-xml>)
和
(equal (car (xml-parse-region (insert (xml-gen <some-xml-form>)))) <some-xml-form>)
新的xml-gen不会努力保留xml-parse-region例程生成的空格。