add publish.el
[blog.git] / publish.el
1 ;;; publish.el --- Publish blog with org-mode        -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2017  Peng Li
4
5 ;; Author: Peng Li <seudut@gmail.com>
6 ;; Keywords: lisp, abbrev
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;; Using this emacs script to publish the blog org file 
24
25 ;;; Code:
26
27 ;; (defconst root-dir "~/Private/blog/")
28
29 (require 'org)
30 (require 'ox-publish)
31
32 (defconst root-dir (file-name-directory (or load-file-name buffer-file-name)))
33 (defvar publish-dir (concat root-dir "_site/"))
34 (defconst css-file "../css/worg.css")
35 (defvar force-publish nil)
36
37 ;; To prevent inline-css when exporting html. will use external css
38 (setq org-html-htmlize-output-type 'css)
39
40
41 (setq blog-extra-head
42       (concat
43        ;; "<link rel='stylesheet' href='" css-file "' />\n"
44        "<link rel='stylesheet' href='../css/main.css' />\n"
45        "<link rel='stylesheet' href='../css/code.css' />"
46        ))
47
48 (setq blog-header
49       (concat
50        " <header id= 'banner' > "
51        "<h1><a href= '/' >Peng Li</a></h1>"
52        "<hr>"
53        "<nav><ul>"
54        "<li><a href= '/' >About</a></li>"
55        "<li><a href= '/blog.html' >Blog</a></li>"
56        "<li><a href= '/home.html' >Home</a></li>"
57        "</ul></nav>"
58        "</header>"))
59
60 (setq blog-footer
61       "<hr />\n
62 <p><span style=\"float: left;\"><a href= \"/blog.xml\">RSS</a></span>
63 License: <a href= \"https://creativecommons.org/licenses/by-sa/4.0/\">CC BY-SA 4.0</a></p\n")
64
65 (defun blog-setup-project (output-dir)
66   "Set project alist."
67   (setq publish-dir output-dir)
68   (setq org-publish-project-alist
69         `(
70           ("blog-pages"
71            ;; publishing
72            :base-directory ,root-dir
73            :base-extension "org"
74            :publishing-directory ,publish-dir
75            :recursive nil
76            :publishing-function org-html-publish-to-html
77            
78            ;; html style
79            :htlm-link-home "/"
80            ;; disable home/up div
81            :html-home/up-format ""
82            :html-link-home ""
83            :html-link-up ""
84            ;;
85            :html-head nil
86            :html-head-include-default-style nil
87            :html-head-include-scripts nil
88            :html-head-extra  ,blog-extra-head
89            :html-preamble ,blog-header
90            :html-postamble ,blog-footer)
91
92           ("blog-posts"
93            ;; publishing
94            :base-directory ,(concat root-dir "/posts")
95            :base-extension "org"
96            :publishing-directory ,(concat publish-dir "/posts")
97            :recursive t
98            :publishing-function org-html-publish-to-html
99
100            ;; html style
101            :html-link-home "/"
102            ;; disable Home/Up
103            :html-home/up-format ""
104            :html-link-up ""
105            :html-link-home ""
106            ;; Add css file, preamble and posamble
107            :html-head nil
108            :html-head-include-default-style nil
109            :html-head-include-scripts nil
110            :html-head-extra ,blog-extra-head
111            :html-preamble ,blog-header
112            :html-postamble ,blog-footer
113
114            ;; sitemap
115            :auto-sitemap t
116            :sitemap-filename "sitemap.org"
117            :sitemap-title "Sitemap")
118           
119           ("blog-css"
120            :base-directory ,(concat root-dir "/css")
121            :base-extension ".*"
122            :publishing-directory ,(concat publish-dir "/css")
123            :publishing-function org-publish-attachment
124            :recursive t)
125           ("blog-cgi"
126            :base-directory ,(concat root-dir "/cgi-bin")
127            :base-extension ".*"
128            :publishing-directory ,(concat publish-dir "/cgi-bin")
129            :publishing-function org-publish-attachment
130            :recursive t)
131           ("blog" :components ("blog-pages" "blog-posts" "blog-css" "blog-cgi")))))
132
133 (blog-setup-project publish-dir)
134
135 (defun blog-publish (out-dir force)
136   "publish the project"
137   (interactive)
138   (blog-setup-project out-dir)
139   (org-publish-project "blog" force))
140
141
142 (provide 'publish)
143 ;;; publish.el ends here