这几天在折腾 org-publish 生成到 html,在我设置好了 org-publish-project-alist
然后在位于某个项目的 buffer 中调用 org-publish-current-file
时出现了点问题,echo area 显示:
org-publish-file: File "(path-to-my-org-file)" is not part of any known project
通过查找定义我得知 org-publish-get-project-from-filename
负责根据当前 buffer 的文件路径确定其所属 org-publish 项目。部分代码如下:
(let* ((filename (expand-file-name filename))
(project
(cl-some
(lambda (p)
;; Ignore meta-projects.
(unless (org-publish-property :components p)
(let ((base (expand-file-name
(org-publish-property :base-directory p))))
(cond
;; Check if FILENAME is explicitly included in one
;; project.
((cl-some (lambda (f) (file-equal-p f filename))
(mapcar (lambda (f) (expand-file-name f base))
(org-publish-property :include p)))
p)
;; Exclude file names matching :exclude property.
((let ((exclude-re (org-publish-property :exclude p)))
(and exclude-re
(string-match-p exclude-re
(file-relative-name filename base))))
nil)
;; Check :extension. Handle special `any'
;; extension.
((let ((extension (org-publish-property :base-extension p)))
(not (or (eq extension 'any)
(string= (or extension "org")
(file-name-extension filename)))))
nil)
;; Check if FILENAME belong to project's base
;; directory, or some of its sub-directories
;; if :recursive in non-nil.
((member filename (org-publish-get-base-files p)) p)
(t nil)))))
org-publish-project-alist)))
经过 edebug 断点测试,我发现原因是我将 project 的 :extension
指定为了 "html\\|org
,而 org-publish-get-project-from-filename
在处理 :extension
时使用的是 string=
来判断该文件是否属于某个 project. 这也叫导致该函数直接跳过了下面的 ((member filename (org-publish-get-base-files p)) p)
分支,从而返回 nil。
修复方法很简单,将 string=
更换为 string-match-p
即可。
我去 https://git.savannah.gnu.org/cgit/emacs/org-mode.git/plain/lisp/ox-publish.el 看了一眼,还是 string=
,也许我可以学习一下怎么提 bug