使用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)
5. 一些其他的小设置
5.1. 导出时单换行也视为换行
(setq org-export-preserve-breaks t)
5.3. org.css文件内容
/*@import url("https://fonts.googleapis.com/css2?family=Lora:wght@500&display=fallback");
@import url("https://fonts.googleapis.com/css2?family=Inconsolata&display=fallback");
*/
body {
margin: 2% auto;
width: 100%;
max-width: 100%;
background-color: rgba(242,229,188,0.5);
/* background-color: rgba(255,255,255,0.5); */
font-size: 18px;
line-height: 1.4em;
}
@media screen and (min-width: 600px) {
body {
font-size: 18px;
}
}
@media screen and (min-width: 910px) {
body {
width: 900px;
}
}
#preamble {
width: 80%;
max-width: 980px;
text-align: left;
margin: auto;
font-size: 80%;
}
hr {
border: none;
border-top: 5px double #333;
margin: 0px 0;
}
.button {
border: none;
color: black;
padding: 6px 14px;
text-align: center;
text-decoration: none;
display: inline-block;
margin: 4px 2px;
transition-duration: 0.4s;
cursor: pointer;
}
.button:hover {
background-color: #3a1616;
color: white;
}
.todo {
background-color: #9d0006;
color: #fbf1c7;
padding: .1em 0.3em;
border-radius: 3px;
background-clip: padding-box;
font-size: $code-size;
font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas, Liberation Mono, monospace;
line-height: 1;
}
.done {
background-color: #427b58;
color: #fbf1c7;
padding: .1em 0.3em;
border-radius: 3px;
background-clip: padding-box;
font-size: $code-size;
font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas, Liberation Mono, monospace;
line-height: 1;
}
#postamble p {
font-size: 80%;
margin: .2em;
text-align: center;
}
#content, .content {
width: 80%;
max-width: 980px;
margin: auto;
}
#org-div-home-and-up{
position: fixed;
right: 0.5em;
margin-top: 70px;
font-family:sans-serif;
}
#table-of-contents {
margin-top: 105px;
font-size: 12pt;
position: fixed;
left: 0em;
top: 0em;
background: white;
max-height: 80%;
overflow: auto;
}
#table-of-contents h2 {
display: none;
}
#table-of-contents li {
clear: both;
}
.timestamp {
color: #928374;
font-size: $smaller;
}
.timestamp-kwd {
color: #5f9ea0;
}
@media screen and (max-width: 1200px) {
#table-of-contents {
position: relative;
margin-top: 0px;
}
}
/* p:not(blockquote > p) :not(ol > p) { */
/* text-indent: 2em; */
/* } */
p {
margin: 1em auto;
}
.title {
font-size: 30px;
line-height: 1.3;
font-weight: bold;
color: #0c2340;
}
h2:not(#table-of-contents > h2) {
font-size: 30px;
line-height: 1.2;
position: relative;
color: #204060;
}
h2::after {
content: "";
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 2px;
background-color: black;
}
h3:not(#table-of-contents > h3) {
font-size: 20px;
line-height: 1.1;
position: relative;
color: #4b748d;
}
h3::after {
content: "";
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 2px;
/* border-bottom: 1px dashed black; */
}
h4 {
font-size: 14px;
line-height: 1.1;
font-weight: bold;
}
a {
cursor: pointer;
text-decoration: none;
}
a:link {
color: #458588;
/* text-decoration: underline; */
}
a:hover {
/* font-weight: bold; */
text-decoration: underline;
/* color: #076678; */
}
.figure {
text-align: center;
}
a:visited{
color: #8f3f71;
/* text-decoration: underline; */
}
img {
max-width: 100%;
vertical-align: middle;
}
.MathJax_Display {
margin: 0!important;
width: $smaller!important;
}
@media screen and (max-width: 1200px) {
img {
width: 400px;
}
.MathJax_Display {
margin: 0!important;
width: $smaller!important;
}
}
.org-src-container {
font-size: 14px;
outline: 2px dashed #ccc;
outline-offset: 10px;
position: relative;
}
.org-src-container>pre {
overflow: auto;
font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Cascadia Code,Consolas, Liberation Mono, monospace;
}
.org-src-container>pre:before {
display: block;
position: absolute;
background-color: #b3b3b3;
top: 0;
right: 0;
padding: 0 0.5em;
border-bottom-left-radius: 8px;
border: 0;
color: white;
font-size: $code-size;
}
pre {
overflow: auto;
}
.example{
font-size: 14px;
outline: 2px dashed #ccc;
outline-offset: 10px;
position: relative;
}
.example::before {
content: "Results";
}
pre:before {
display: block;
position: absolute;
background-color: #b3b3b3;
top: 0;
right: 0;
padding: 0 0.5em;
border-bottom-left-radius: 8px;
border: 0;
color: white;
font-size: $code-size;
}
/* .org-src-container::before { */
/* content: "Copy"; */
/* position: absolute; */
/* top: 5px; */
/* right: 5px; */
/* padding: 5px; */
/* background-color: #ccc; */
/* color: #fff; */
/* border-radius: 3px; */
/* cursor: pointer; */
/* } */
code {
font-size: 16px;
color: #427b58;
font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Cascadia Code,Consolas, Liberation Mono, monospace;
}
.subtitle {
text-align: right;
}
blockquote {
border-left: 3px solid #ccc;
padding-left: 10px; /* Optional: add some left padding for the content */
margin-inline-start: 0px;
margin-inline-end: 0px;
}
.info {
overflow: hidden;
}
.created {
float: right;
}
.updated {
float: left;
}
.org-ol {
list-style-type: decimal; /* Set the list style type to decimal numbers */
/* margin-left: 0.5em; /\* Add left margin to the list *\/ */
}
b {
color: #9d0006;
}
#footnotes {
position: fixed;
left: auto;
right: 0;
top: 105px;
bottom: 0;
width: 20%;
padding: 10px;
overflow: auto;
font-size: 14px;
z-index: 9999;
}
#footnotes h2 {
display: none;
}
@media screen and (max-width: 1200px) {
#footnotes {
position: static;
left: 0;
right: 0;
bottom: 0;
width: 100%;
max-height: 30%;
overflow: auto;
padding: 10px;
font-size: 14px;
z-index: 9999;
}
}