emacs-org模式和html发布:如何更改生成的HTML的结构

时间:2013-01-14 17:53:11

标签: html emacs export org-mode

我是初学者,如果我忽略了一些简单的话,那就很抱歉......

我想在我的HTML页面中使用emacs org-mode。 “默认”设置很好并且有效,但是我想使用其中一个免费的网页模板,例如http://www.freecsstemplates.org/preview/goodlife/

这些模板提供了CSS文件,但是在org-mode的HTML导出中使用CSS似乎还不够。似乎正确使用这些模板我也需要维护HTML结构,如此模板所示。

如何强制组织模式生成我喜欢的HTML结构(即帧分割)?

似乎'org-export-generic.el'提供了一些选项。即使我说服通用导出为我提供单个HTML页面,它仍然无法完全解析HTML导出....

2 个答案:

答案 0 :(得分:1)

组织模式手册的这一部分提供了有关导出到html和使用css http://orgmode.org/manual/CSS-support.html#CSS-support的一些指导。这包括默认类org-mode用法的描述,因此您可以修改CSS。

如果要修改组织模式导出以匹配CSS类和ID,请使用组织标题中的:HTML_CONTAINER_CLASS:属性和用于创建ID的:CUSTOM_ID:属性。

我没有为每个文件设置内容,而是使用org模式的发布功能将许多组织文件输出到单个网站中。您可以在http://orgmode.org/worg/org-tutorials/org-publish-html-tutorial.html

找到相关教程

我的org-publish-project-alist看起来像:

'(org-publish-project-alist (quote (("requirements" :components ("req-static" "req-org"))
   ("req-static" :base-directory "~/org/requirements" :publishing-directory "~/public_html/requirements/" :base-extension "gif\\|css" :publishing-function org-publish-attachment) 
   ("req-org" :base-directory "~/org/requirements/" :publishing-directory "~/public_html/requirements/" :style "<link rel=\"stylesheet\" type=\"text/css\" href=\"./style.css\" />" :section-numbers nil :headline-levels 3 :table-of-contents 2 :auto-sitemap t :sitemap-filename "index.org" :sitemap-title "Requirements for My Software" :link-home "./index.html"))

答案 1 :(得分:0)

我同意。 org的内置导出生成的HTML很好,但不是我想要的。似乎通用导出基于elisp,而我更喜欢XSLT。

我编写了以下用于将组织文件转换为XML的代码,但我还没有编写发布转换。无论如何,这可能对您的参考有所帮助,特别是因为它显示了组织文档内部表示的结构。

(require 'org-element)

(defvar xml-content-encode-map
  '((?& . "&amp;")
    (?< . "&lt;")
    (?> . "&gt;")))

(defvar xml-attribute-encode-map
  (cons '(?\" . "&quot;") xml-content-encode-map))

(defun write-xml (o out parents depth)
  "Writes O as XML to OUT, assuming that lists have a plist as
their second element (for representing attributes).  Skips basic
cycles (elements pointing to ancestor), and compound values for
attributes."
  (if (not (listp o))
      ;; TODO: this expression is repeated below
      (princ o (lambda (charcode)
                 (princ 
                  (or (aget xml-content-encode-map charcode)
                      (char-to-string charcode))
                  out)))

    (unless (member o parents)
      (let ((parents-and-self (cons o parents))
            (attributes (second o)))

        (dotimes (x depth) (princ "\t" out))
        (princ "<" out)
        (princ (car o) out)

        (loop for x on attributes by 'cddr do
              (let ((key (first x))
                    (value (second x)))

                (when (and value (not (listp value)))
                  (princ " " out)
                  (princ (substring (symbol-name key) 1) out)
                  (princ "=\"" out)
                  (princ value  (lambda (charcode)
                                  (princ 
                                   (or (aget xml-attribute-encode-map charcode)
                                       (char-to-string charcode))
                                   out)))
                  (princ "\"" out))))

        (princ ">\n" out)

        (loop for e in (cddr o)  do
              (write-xml e out parents-and-self (+ 1 depth)))

        (dotimes (x depth) (princ "\t" out))
        (princ "</" out)
        (princ (car o) out)
        (princ ">\n" out)))))

(defun org-file-to-xml (orgfile xmlfile)
  "Serialize ORGFILE file as XML to XMLFILE."
  (save-excursion
    (find-file orgfile)
    (let ((org-doc (org-element-parse-buffer)))
      (with-temp-file xmlfile
        (let ((buffer (current-buffer)))
          (princ "<?xml version='1.0'?>\n" buffer)
          (write-xml org-doc buffer () 0)
          (nxml-mode)))))
  (find-file xmlfile)
  (nxml-mode))

(defun org-to-xml ()
  "Export the current org file to XML and open in new buffer.
Does nothing if the current buffer is not in org-mode."
  (interactive)
  (when (eq major-mode 'org-mode)
    (org-file-to-xml
     (buffer-file-name)
     (concat (buffer-file-name) ".xml"))))