emacs 替换模式
我在博客的头几年使用WordPress,但我确实想完全使用GNU Emacs来发布它。 我尝试了Org2Blog ,但仍然缺少某些东西,感觉不满意。 我尝试创建一个网站来发布Emacs配置,我首先使用Django ,然后使用Jekyll来命名为Haqiba (我知道这是一个不寻常的名称)。 Jekyll很酷,可以提供对内容和发布的更多控制,但是我仍然不能直接从Emacs博客,并且仍然缺少Org模式 。 尽管我尝试使用jekyll-org向Jekyll添加Org模式支持,但该框架似乎有些陌生。
组织发布 。 我在搜索中偶然发现了组织发布,但起初,我认为对于博客来说太复杂了。 但是我尝试了一下,从那时起一直很高兴。
许多网站,包括此列表中的网站,都使用org-publish。 例如,伯恩特·汉森(Bernt Hansen)的组织模式-用纯文本组织您的生活,不仅使用组织发布来发布内容,而且还提供了许多信息以使您对组织模式有更深入的了解。
组织发布的优势
组织发布包括以下功能:
良好地控制配置,CSS,媒体和发布 组织模式格式化支持 静态文件生成 使用GitLab和GitHub CI / CD轻松部署 如果您希望将文件复制到远程服务器而不是使用GitLab页面或GitHub页面,则可以通过Apache / Nginx /文件服务器轻松托管 版本控制 GNU Emacs中的所有内容。 好极了!
基本设定
组织发布指南提供了入门的基本模板。 我鼓励您阅读本教程,因为本教程中的基本设置足以使您对org-publish有一个简短的了解。 首先在myblog /项目目录内的publish.el文件中配置一个名为org-publish-project-alist的变量。 将以下内容放在publish.el中:
(setq org-publish-project-alist '(("posts" :base-directory "posts/" :base-extension "org" :publishing-directory "public/" :recursive t :publishing-function org-html-publish-to-html :auto-sitemap t) ("all" :components ("posts"))))(require 'ox-publish)
第一行是导入语句。 变量org-publish-project-alist具有发布项目列表,以控制发布行为。 第一个元素posts,是完成博客文章特定的所有配置的地方。 例如,属性:base-directory配置用于保存所有帖子(组织格式)的目录。 同样,:publishing-directory将目录配置为保存由Org文件生成HTML文件。 将:recursive属性设置为t将从posts /及其子目录内的所有Org文件递归生成HTML。:auto-sitemap属性使用您的帖子列表生成sitemap.html(您将在下面进行调整)。 最后,:publishing-function org-html-publish-to-html将所有的org文件转换为HTML。 虽然您也可以定义自己的函数,但出于本演示的目的,请使用ox-publish提供的内置函数。
您需要一些测试用的帖子,因此创建一个名为posts /的文件,并包含一些带有一些内容的基本标题。 使用Cc Ce#default和Cc Ce#html分别包含默认模板和HTML模板。
您的文件应如下所示:
#+date: <-02-12 Wed> #+author: John Doe #+email: john.doe@ Lorem Ipsum is simply dummy text of the printing and typesetting industry.#+title: Post One
设置几乎完成。 您可以使用Mx org-publish-all生成HTML并使用make处理发布。 以下是Makefile的内容:
.PHONY: all publish publish_no_init all: publish publish: publish.el @echo "Publishing... with current Emacs configurations." emacs --batch --load publish.el --funcall org-publish-all publish_no_init: publish.el @echo "Publishing... with --no-init." emacs --batch --no-init --load publish.el --funcall org-publish-all clean: @echo "Cleaning up.." @rm -rvf *.elc @rm -rvf public @rm -rvf ~/.org-timestamps/*# Makefile for myblog
这是项目的当前布局:
├── Makefile ├── posts │ └── └── publish.elmyblog
执行make将在public /目录中生成sitemap.html和post_one.html:
├── Makefile ├── posts │ ├── │ └── ├── public │ ├── post_one.html │ └── sitemap.html └── publish.elmyblog
将CSS添加到您的帖子中
您可以增强publish.el文件,使其包含CSS或图像之类的元素。 要尝试此操作,请为CSS添加一个部分或项目。 修改后的publish.el应该如下所示:
(setq org-publish-project-alist '(("posts" :base-directory "posts/" :base-extension "org" :publishing-directory "public/" :recursive t :publishing-function org-html-publish-to-html :auto-sitemap t) ("css" :base-directory "css/" :base-extension "css" :publishing-directory "public/css" :publishing-function org-publish-attachment :recursive t) ("all" :components ("posts" "css"))))(require 'ox-publish)
创建一个名为css /的新目录,并将代码从site.css复制到其中。 现在,创建第二篇文章来测试CSS。
#+date: <-02-12 Wed> #+author: John Doe #+email: john.doe@ #+HTML_HEAD: <link rel="stylesheet" type="text/css" href="../css/site.css" /> Lorem Ipsum is simply dummy text of the printing and typesetting industry.#+title: Post Two
在此示例中,使用#+ HTML_HEAD:选项包含CSS。 组织发布教程建议使用#+ STYLE:选项包括样式表,但这对我不起作用。 相反,我使用了#+ HTML_HEAD:,这是Org模式手册中建议的CSS支持 。
这是显示css /目录的布局:
├── css │ └── site.css ├── Makefile ├── posts │ ├── │ └── └── publish.elmyblog
必须在每个帖子中包含#+ HTML_HEAD:很快就会变得乏味。 网站中也有多个样式表。 要解决此问题,请使用#+ SETUPFILE:选项:
#+date: <-02-12 Wed> #+author: John Doe #+email: john.doe@ #+SETUPFILE: ../org-template/ Lorem Ipsum is simply dummy text of the printing and typesetting industry.#+title: Post Two
org-template /文件包含样式表的路径:
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="../css/site.css" />
以下是最终布局:
├── css │ └── site.css ├── Makefile ├── org-template │ └── ├── posts │ ├── │ └── └── publish.elmyblog
调整站点地图
最终配置将生成index.html文件而不是sitemap.html文件。 重命名标题并在网站上配置作者和电子邮件。 下面是完成的publish.el文件:
(setq org-publish-project-alist '(("posts" :base-directory "posts/" :base-extension "org" :publishing-directory "public/" :recursive t :publishing-function org-html-publish-to-html :auto-sitemap t :sitemap-title "Blog Index" :sitemap-filename "" :sitemap-style list :author "John Doe" :email "john.doe@" :with-creator t) ("css" :base-directory "css/" :base-extension "css" :publishing-directory "public/css" :publishing-function org-publish-attachment :recursive t) ("all" :components ("posts" "css"))))(require 'ox-publish)
如果您在设置项目时遇到困难,可以在我的GitLab页面上查看整个项目。
使用现有的组织发布设置
从头开始创建具有org-publish的博客可能变得很乏味。 为了使它更容易,您可以使用我的存储库作为基本模板来使用org-publish发布自己的博客。
要使用它,请克隆blog_template分支:
git clone https: // / psachin / psachin.gitlab.io -b blog_template --single-branch myblog
使用make将组织页面导出为HTML。public /目录将包含托管所需的所有文件:
makecd myblog
在posts / 中有一个示例博客文章供参考。 您可以使用.gitlab-ci.yaml文件将public /的内容发布为GitLab页面。
奖金小费1
执行make命令后,public /目录将具有托管静态站点所需的所有文件。 您所要做的就是将网络服务器配置为提供此目录,或者您可以使用Python的内置http.server模块在本地呈现博客。
在Python 3.6中,使用:
python -m http.servercd myblog / public
如果您拥有Python 3.7,则可以使用以下命令提供public /服务:
python -m http.server --directory =publiccd myblog
在网络浏览器中打开http:// localhost:8000 /以查看您的网站。
奖金小费2
这是我最喜欢的提示。 如果没有时间处理新博客文章的想法时,我会使用Org捕获模板快速创建草稿。 我使用下面的模板定义通过键入Cc cp打开缓冲区窗口。 完成后,我输入Cc Cc保存草稿。
将此Elisp片段复制到现有的Emacs配置文件中(但要确保更改了文件路径):
"Create an org file in ~/source/myblog/posts." (interactive) (let ((name (read-string "Filename: "))) (expand-file-name (format "%" name) "~/source/myblog/posts/"))) (setq org-capture-templates '(("p" "Post" plain (file create-blog-post) (file "~/.emacs.d/org-templates/captmpl"))))(defun create-blog-post ()
这是〜/ .emacs.d / org-templates / captmpl的内容:
#+date: <%<%Y-%m-%d>> #+keywords: draft #+setupfile: ../org-templates/ %? #+INCLUDE: "../disquss.inc"#+title: %^{Name}
有关Org捕获模板的更详尽说明,您可以观看我的视频演示 。
您是否曾经使用过组织模式来发布网站或博客,或者打算这样做? 让我们知道您在评论中的经验。
翻译自: /article/20/3/blog-emacs
emacs 替换模式