使用advice自动更新ox-publish的待发布org-roam文件目录
最近在研究用Emacs写博客。由于并不在意博客样式,因此选择用ox-publish直接将指定的org-roam文件导出为html再上传GitHub通过GitHub Pages托管。
参考文献:
Emacs: 使用 org publish 生成个人博客
26.ox-html 浅析与 ox-publish 使用介绍
由于org-roam的所有文件均在一个文件夹内,又不需要将所有的文件作为博客内容发表,因此接下来会利用org-roam的功能,动态发布打上了 blog 标签的文件。
1. 获取带blog标签的文件
参考Vulpea的org-roam动态agenda文件,创建函数如下:
(defun my/ox-files ()
"Return a list of note files containing 'blog' tag." ;
(seq-uniq
(seq-map
#'car
(org-roam-db-query
[:select [nodes:file]
:from tags
:left-join nodes
:on (= tags:node-id nodes:id)
:where (like tag (quote "%\"blog\"%"))]))))
上述函数可以返回org-roam-db中标有 blog 的文件路径列表。
2. 配置ox-publish
根据参考文献,需要指定 org-publish-project-alist 如下:
(setq org-publish-project-alist
`(("posts"
:base-directory ,website-directory
:base-extension "org"
:publishing-directory ,my/publish-directory
:publishing-function org-html-publish-to-html
:with-author t
:auto-sitemap t
:recursive nil
:exclude ".*"
:include ,(my/ox-files)
:html-link-home "index.html"
:sitemap-filename "index.org"
:sitemap-title "目录"
:sitemap-sort-files anti-chronologically
:html-head:css "<link rel='stylesheet' type='text/css' href='/org.css'/>")
("personal-website" :components ("posts"))))
上图代码通过 :exclude ".*" 排除所有文件,然后通过 :include ,(my/ox-files) 将带有 blog 标签的org-roam文件路径纳入 ox-publish 的处理范畴中
3. 动态更新待发布文件列表
由于上述 org-publish-project-alist 仅会执行单次,因此需要通过 advice-add 的方式让其在每次发布时均执行一次,达成动态更新的效果。
(defun my/org-project-update (&optional ARG PRED) ;;使用before会接收到原函数参数,因此需要添加optional
(setq org-publish-project-alist
`(("posts"
:base-directory ,website-directory
:base-extension "org"
:publishing-directory ,my/publish-directory
:publishing-function org-html-publish-to-html
:with-author t
:auto-sitemap t
:recursive nil
:exclude ".*"
:include ,(my/ox-files)
:html-link-home "index.html"
:sitemap-filename "index.org"
:sitemap-title "目录"
:sitemap-sort-files anti-chronologically
:html-head:css "<link rel='stylesheet' type='text/css' href='/org.css'/>")
("personal-website" :components ("posts")))))
(advice-add 'org-export-dispatch :before 'my/org-project-update)
这样,每次 org-export-dispatch 函数(按键为 C-c C-e )运行前,均会更新待发布文件路径。
4. 为每个html文件插入CSS
利用 org-export 的hook自动设置,读取 ~/.emacs.d/org.css 文件内容并插入发布的html文件中
(defun my/org-inline-css-hook (exporter)
"Insert custom inline css"
(when (eq exporter 'html)
(setq org-html-head-include-default-style nil)
(setq org-html-head (concat "<style type=\"text/css\">\n <!--/*--><![CDATA[/*><!--*/\n" (with-temp-buffer (insert-file-contents "~/.emacs.d/org.css") (buffer-string)) "/*] ]>*/-->\n </style>\n"))))
(add-hook 'org-export-before-processing-hook 'my/org-inline-css-hook)
:现在改为引用独立的 =org.css=,缩减HTML文件大小。
(defun my/org-inline-css-hook (exporter)
"在导出的'html'文件首页插入自定义的CSS文件"
(when (eq exporter 'html)
;; 取消默认CSS
(setq org-html-head-include-default-style nil)
;; 关闭html validation链接
(setq org-html-validation-link nil)
;; 设置CSS
(setq org-html-head "<link rel='stylesheet' type='text/css' href='/org.css'/>")))
(defun adv/org-publish-project-done-message (&optional one two three )
"更新'org.css'"
(with-current-buffer (find-file-noselect (concat my/desktop-path "org.css"))
(erase-buffer)
(insert-file-contents "~/.emacs.d/org.css")
(save-buffer)))
(advice-add #'org-publish-current-project :after #'adv/org-publish-project-done-message)