org 文件使用 emacs --script 执行 org-publish-all 的时候代码块没有高亮?

一个有点奇怪的问题。我在emacs里用 M-x lsz/org-export-publish Ret 执行导出的时候,代码块是可以正常高亮的。但我一旦使用 emacs --script ~/.emacs.d/lisp/script-org-export-publish.el 来导出,代码块的高亮就没了

(use-package htmlize)

(use-package org
  :hook
  ('before-save . 'lsz/org-mode-hook-func-before-save)
  :config
  (setq org-startup-truncated nil)
  (setq org-support-shift-select t)

  (setq org-use-sub-superscripts '{})

  (setq org-use-fast-todo-selection t)
  (setq org-todo-keywords '((sequence "TODO(t)" "DOING(i)"
                                      "|"
                                      "DONE(d)" "ABORT(a)")))
  )

(use-package ox-html
  :ensure org
  :config
  (setq org-html-htmlize-output-type 'inline-css)
  (setq org-src-fontify-natively t)
  )

(defun lsz/org-export-publish()
  "Publish org site by publish script."
  (interactive)
  (load "~/.emacs.d/lisp/script-org-export-publish.el"))

(use-package ox-publish
  :ensure org
  :config

  ;; (setq org-html-postamble t)
  ;; (setq org-html-postamble-format '(("en" "")))

  (setq org-export-with-sub-superscripts '{})

  (setq
   org-publish-project-alist
   (let* ((lsz/site-path "~/Documents/LSZOrgArticle/")
          (lsz/site-pub-path "~/Documents/LSZOrgArticlePublish/")
          (get-content (lambda (x)
                         (with-temp-buffer
                           (insert-file-contents (concat lsz/site-path x))
                           (buffer-string))))
          (lsz/site-postamble (funcall get-content "template/postamble.html"))
          (lsz/site-preamble (funcall get-content "template/preamble.html"))
          (lsz/site-head (funcall get-content "template/head.html")))
     `(
       ("blog"
        :base-directory ,(concat lsz/site-path "article/blog/")
        :base-extension "org"
        :publishing-directory ,(concat lsz/site-pub-path "article/blog/")
        :publishing-function org-html-publish-to-html
        :recursive t
        :headline-levels 4

        :auto-sitemap t
        :sitemap-filename "sitemap-index.org"
        :sitemap-title "blog"

        :html-doctype "html5"
        :html-head ,lsz/site-head
        :html-preamble ,lsz/site-preamble
        :html-postamble ,lsz/site-postamble
        :htmlized-source t

        :with-toc t
        )
       ("reading"
        :base-directory ,(concat lsz/site-path "article/reading/")
        :base-extension "org"
        :publishing-directory ,(concat lsz/site-pub-path "article/reading/")
        :publishing-function org-html-publish-to-html
        :recursive t
        :headline-levels 4

        :auto-sitemap t
        :sitemap-filename "sitemap-index.org"
        :sitemap-title "reading"

        :html-doctype "html5"
        :html-head ,lsz/site-head
        :html-preamble ,lsz/site-preamble
        :html-postamble ,lsz/site-postamble
        :htmlized-source t

        :with-toc t
        )
       ("wiki"
        :base-directory ,(concat lsz/site-path "article/wiki/")
        :base-extension "org"
        :publishing-directory ,(concat lsz/site-pub-path "article/wiki/")
        ;; :publishing-function org-html-publish-to-html
        :recursive t
        :headline-levels 4

        :auto-sitemap t
        :sitemap-filename "sitemap-index.org"
        :sitemap-title "wiki"

        ;; :html-doctype "html5"
        :html-head ,lsz/site-head
        :html-preamble ,lsz/site-preamble
        :html-postamble ,lsz/site-postamble
        ;; :htmlized-source t

        :with-toc t
        )
       ("site"
        :base-directory ,(concat lsz/site-path "article/site/")
        :base-extension "org"
        :publishing-directory ,(concat lsz/site-pub-path "article/site/")
        :publishing-function org-html-publish-to-html
        :recursive t
        :headline-levels 4

        :html-doctype "html5"
        :html-head ,lsz/site-head
        :html-preamble ,lsz/site-preamble
        :html-postamble ,lsz/site-postamble
        :htmlized-source t

        :with-toc nil
        )
       ("static"
        :base-directory ,(concat lsz/site-path "article_static/")
        :base-extension "css\\|js\\|png\\|jpg\\|gif\\|zip\\|7z\\|rar\\|pdf"
        :publishing-directory ,(concat lsz/site-pub-path "/article_static")
        :publishing-function org-publish-attachment
        :recursive t
        )
       ("all" :components ("blog" "reading" "wiki" "site" "static"))
       ))))

这是 ~/.emacs.d/lisp/script-org-export-publish.el

;;; need environment
(package-initialize)
(load "~/.emacs.d/init.el")

;;; publish file
(org-publish-all)

这是正常渲染出来的,有代码高亮的 html:

<pre class="src src-python"><span style="color: #a020f0;">import</span> csv

<span style="color: #a020f0;">with</span> <span style="color: #483d8b;">open</span>(<span style="color: #8b2252;">'stock.csv'</span>,<span style="color: #8b2252;">'r'</span>) <span style="color: #a020f0;">as</span> fp:
    <span style="color: #a0522d;">reader</span> = csv.reader(fp)
    <span style="color: #a0522d;">titles</span> = <span style="color: #483d8b;">next</span>(reader)
    <span style="color: #a020f0;">for</span> x <span style="color: #a020f0;">in</span> reader:
        <span style="color: #a020f0;">print</span>(x)
</pre>

浏览器里长这样

Snipaste_2022-04-29_17-21-35

这是 emacs --script 渲染出来的 html:

<pre class="src src-python"><span style="font-weight: bold;">import</span> csv

<span style="font-weight: bold;">with</span> <span style="font-weight: bold;">open</span>(<span style="font-style: italic;">'stock.csv'</span>,<span style="font-style: italic;">'r'</span>) <span style="font-weight: bold;">as</span> fp:
    <span style="font-weight: bold; font-style: italic;">reader</span> = csv.reader(fp)
    <span style="font-weight: bold; font-style: italic;">titles</span> = <span style="font-weight: bold;">next</span>(reader)
    <span style="font-weight: bold;">for</span> x <span style="font-weight: bold;">in</span> reader:
        <span style="font-weight: bold;">print</span>(x)
</pre>

浏览器里长这样

Snipaste_2022-04-29_17-21-48

老哥们啊,这是咋回事,该从哪下手排查问题,我google了几个小时完全没有头绪。

可能有某些包没 require ? 手动require一下。可能有些包是你在emacs里的某些操作触发hook才 require的

我手动require了一下,但是和之前没有任何不同。是漏了什么,还是?

~/.emacs.d/lisp/script-org-export-publish.el

;;; need environment
(package-initialize)
(load "~/.emacs.d/init.el")

;;; publish file
(require 'org)
(require 'ox-publish)
(require 'ox-html)
(require 'htmlize)
(org-publish-all)

就从失败的渲染结果来说,看起来样式是有的,但是不知道为什么是错的。

我在 GitHub - hniksic/emacs-htmlize: Convert buffer text and decorations to HTML. 找到这么一句:

htmlize will do its best to work on non-windowing Emacs sessions but the result will be limited to colors supported by the terminal.

就是说,htmlize 在非窗口环境下,也就是终端环境下的处理结果中,代码块关键字颜色部分将只显示被终端支持的颜色。

或许我得去查查终端支持什么颜色。

找到原因了,与终端颜色无关。这里有详细的说明和解决方案。

https://orgmode.org/worg/org-faq.html#preserving-faces-during-batch-export

The reason is that when Emacs is run in batch mode, it does not have a display and therefore only rudimentary face definitions. If you’d like to use more complex styles, you’ll have to make sure that the export process only assigns classes instead of direct color values. This can be done by binding the variable org-export-htmlize-output-style to css for the duration of the export

1 个赞

文档有点老, 我正在改, 如果有好主意可以一起来: https://list.orgmode.org/875y77wm6u.fsf@localhost/T/#t

1 个赞

感谢修正。我一直没能搞懂这个 org mail list 要咋用 hhhh