本文共 7546 字,大约阅读时间需要 25 分钟。
emacs 替换模式
我在博客的头几年使用WordPress,但我确实想完全使用来发布它。 我尝试了 ,但仍然缺少某些东西,感觉不满意。 我尝试创建一个网站来发布Emacs配置,我首先使用 ,然后使用来命名为 (我知道这是一个不寻常的名称)。 Jekyll很酷,可以提供对内容和发布的更多控制,但是我仍然不能直接从Emacs博客,并且仍然缺少 。 尽管我尝试使用向Jekyll添加Org模式支持,但该框架似乎陌生。
。 我在搜索中偶然发现了组织发布,但起初,我认为对于博客来说太复杂了。 但是我尝试了一下,从那时起一直很高兴。许多网站,包括此的网站,都使用org-publish。 例如,伯恩特·汉森(Bernt Hansen)的不仅使用组织发布来发布内容,而且还提供了许多信息以使您对组织模式有更深入的了解。
组织发布包括以下功能:
提供了入门的基本模板。 我鼓励您阅读本教程,因为本教程中的基本设置足以使您对org-publish有一个简短的了解。 首先在myblog /项目目录内的publish.el文件中配置一个名为org-publish-project-alist的变量。 将以下内容放在publish.el中 :
(require 'ox-publish) (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"))))
第一行是导入语句。 变量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 / post_one.org的文件,并包含一些带有一些内容的基本标题。 使用Cc Ce#default和Cc Ce#html分别包含默认模板和HTML模板。
应如下所示:
#+title: Post One #+date: <2020-02-12 Wed> #+author: John Doe #+email: john.doe@example.com Lorem Ipsum is simply dummy text of the printing and typesetting industry.
设置几乎完成。 您可以使用Mx org-publish-all生成HTML并使用make处理发布。 以下是Makefile的内容:
# Makefile for myblog .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/*
这是项目的当前布局:
myblog ├── Makefile ├── posts │ └── post_one.org └── publish.el
执行make将在public /目录中生成sitemap.html和post_one.html :
myblog ├── Makefile ├── posts │ ├── post_one.org │ └── sitemap.org ├── public │ ├── post_one.html │ └── sitemap.html └── publish.el
您可以增强publish.el文件,使其包含CSS或图像之类的元素。 要尝试此操作,请为CSS添加一个部分或项目。 修改后的publish.el应该如下所示:
(require 'ox-publish) (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"))))
创建一个名为css /的新目录,并将代码从复制到其中。 现在,创建来测试CSS。
#+title: Post Two #+date: <2020-02-12 Wed> #+author: John Doe #+email: john.doe@example.com #+HTML_HEAD: Lorem Ipsum is simply dummy text of the printing and typesetting industry.
在此示例中,使用#+ HTML_HEAD:选项包含CSS。 建议使用#+ STYLE:选项包括样式表,但这对我不起作用。 相反,我使用了#+ HTML_HEAD:,这是Org模式手册中建议的 。
这是显示css /目录的布局:
myblog ├── css │ └── site.css ├── Makefile ├── posts │ ├── post_one.org │ └── post_two.org └── publish.el
必须在每个帖子中包含#+ HTML_HEAD:很快就会变得乏味。 网站中也有多个样式表。 要解决此问题,请使用#+ SETUPFILE:选项:
#+title: Post Two #+date: <2020-02-12 Wed> #+author: John Doe #+email: john.doe@example.com #+SETUPFILE: ../org-template/style.org Lorem Ipsum is simply dummy text of the printing and typesetting industry.
org-template / style.org文件包含样式表的路径:
#+HTML_HEAD:
以下是最终布局:
myblog ├── css │ └── site.css ├── Makefile ├── org-template │ └── style.org ├── posts │ ├── post_one.org │ └── post_two.org └── publish.el
最终配置将生成index.html文件而不是sitemap.html文件。 重命名标题并在网站上配置作者和电子邮件。 下面是完成的publish.el文件:
(require 'ox-publish) (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 "index.org" :sitemap-style list :author "John Doe" :email "john.doe@example.com" :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"))))
如果您在设置项目时遇到困难,可以在我的上查看整个项目。
从头开始创建具有org-publish的博客可能变得很乏味。 为了使它更容易,您可以使用我的存储库作为基本模板来使用org-publish发布自己的博客。
要使用它,请克隆blog_template分支:
git clone https: // gitlab.com / psachin / psachin.gitlab.io -b blog_template --single-branch myblog
使用make将组织页面导出为HTML。 public /目录将包含托管所需的所有文件:
cd myblog make
在posts / template.org中有一个示例博客文章供参考。 您可以使用.gitlab-ci.yaml文件将public /的内容发布为GitLab页面。
执行make命令后, public /目录将具有托管静态站点所需的所有文件。 您所要做的就是将网络服务器配置为提供此目录,或者您可以使用Python的内置http.server模块在本地呈现博客。
在Python 3.6中,使用:
cd myblog / public python -m http.server
如果您拥有Python 3.7,则可以使用以下命令提供public /服务:
cd myblog python -m http.server --directory =public
在网络浏览器中打开以查看您的网站。
这是我最喜欢的提示。 如果没有时间处理新博客文章的想法时,我会使用快速创建草稿。 我使用下面的模板定义通过键入Cc cp打开缓冲区窗口。 完成后,我输入Cc Cc保存草稿。
将此Elisp片段复制到现有的Emacs配置文件中(但要确保更改了文件路径):
(defun create-blog-post () "Create an org file in ~/source/myblog/posts." (interactive) (let ((name (read-string "Filename: "))) (expand-file-name (format "%s.org" name) "~/source/myblog/posts/"))) (setq org-capture-templates '(("p" "Post" plain (file create-blog-post) (file "~/.emacs.d/org-templates/post.orgcaptmpl"))))
这是〜/ .emacs.d / org-templates / post.orgcaptmpl的内容 :
#+title: %^{Name} #+date: <%<%Y-%m-%d>> #+keywords: draft #+setupfile: ../org-templates/post.org %? #+INCLUDE: "../disquss.inc"
有关Org捕获模板的更详尽说明,您可以观看我的 。
您是否曾经使用过组织模式来发布网站或博客,或者打算这样做? 让我们知道您在评论中的经验。
翻译自:
emacs 替换模式
转载地址:http://yoczd.baihongyu.com/