aboutsummaryrefslogtreecommitdiffstats
path: root/publish.el
blob: 24e2bf36f70e8b6ba29d22dea8d0027df0fc5383 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
;;; publish.el --- Publishing configuration for ryuslash.org  -*- lexical-binding: t; -*-

;; Copyright (C) 2020  Tom Willemse

;; Author: Tom Willemse <tom@ryuslash.org>
;; Keywords:

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;

;;; Code:

(require 'ox-publish)
(require 'ox-rss)
(require 'subr-x)
(require 'dockerfile-mode)
(require 'rainbow-delimiters)

(add-hook 'prog-mode-hook 'rainbow-delimiters-mode)

(defun publish-calculate-reading-time (buffer)
  "Calculate the amount of minutes it would take to read the contents of BUFFER."
  (with-current-buffer buffer
    (max 1 (/ (count-words (point-min) (point-max)) 228))))

(defun publish-format-reading-time (time)
  "Return a string describing TIME."
  (format "%d minute%s"
          time
          (if (= time 1) "" "s")))

(defun publish-org-sitemap (title list)
  "Create a site map file. TITLE is used as the title, LIST is turned into a subtree."
  (concat "#+TITLE: " title "\n"
          "#+OPTIONS: toc:nil num:nil\n\n"
          ;; (message "Lets see here: %S" list)
          (org-list-to-subtree list)))

(defun publish-org-sitemap-format-entry (entry style project)
  "Format ENTRY as a healine.
STYLE is ignored and PROJECT is passed on directly to related functions."
  (unless (directory-name-p entry)
    (format "%s\n  :PROPERTIES:\n  :ID: %s\n  :RSS_PERMALINK: %s\n  :PUBDATE: %s\n  :END:\n  %s\n  Published on: %s\n\n  [[file:%s][Read More]] (%s)"
            (org-publish-find-title entry project)
            entry
            (concat (string-remove-suffix ".org" entry) ".html")
            (format-time-string
             (cdr org-time-stamp-formats)
             (org-publish-find-date entry project))

            (with-temp-buffer
              (insert-file-contents (concat (plist-get (cdr project) :base-directory) "/" entry))
              (goto-char (point-min))
              (let ((current-element (org-element-at-point)))
                (while (not (equal (org-element-type current-element) 'paragraph))
                  (org-forward-element)
                  (setq current-element (org-element-at-point)))
                (buffer-substring-no-properties
                 (plist-get (cadr current-element) :contents-begin)
                 (plist-get (cadr current-element) :contents-end))))
            (format-time-string
             (cdr org-time-stamp-formats)
             (org-publish-find-date entry project))
            entry
            (with-temp-buffer
              (insert-file-contents (concat (plist-get (cdr project) :base-directory) "/" entry))
              (publish-format-reading-time
               (publish-calculate-reading-time (current-buffer)))))))

(setq org-rss-use-entry-url-as-guid t)

(setq org-export-exclude-tags '("noexport" "draft"))

(setq org-html-htmlize-output-type 'css)

(setq org-publish-timestamp-directory
      (concat default-directory "/.org-timestamps/"))

(setq org-publish-project-alist
      '(("posts"
         :base-directory "posts/"
         :base-extension "org"
         :publishing-directory "public/"
         :recursive t
         :publishing-function org-html-publish-to-html
         :html-head "<link rel=\"stylesheet\" href=\"assets/css/main.css\" type=\"text/css\"/>"
         :html-preamble (lambda (project)
                          (let ((buffer (find-file-noselect (buffer-file-name))))
                            (unless (string= "ryuslash.org"
                                             (car (org-publish-find-property (buffer-file-name) :title project)))
                              (publish-format-reading-time
                               (publish-calculate-reading-time buffer)))))
         :auto-sitemap t
         :sitemap-filename "index.org"
         :sitemap-title "ryuslash.org"
         :sitemap-format-entry publish-org-sitemap-format-entry
         :sitemap-style list
         :sitemap-function publish-org-sitemap
         :sitemap-sort-files anti-chronologically)
        ("rss"
         :base-directory "posts/"
         :base-extension "org"
         :html-link-home "https://ryuslash.org/"
         :rss-link-home "https://ryuslash.org/"
         :html-link-use-abs-url t
         :rss-extension "xml"
         :publishing-directory "public"
         :publishing-function (org-rss-publish-to-rss)
         :section-number nil
         :exclude ".*"
         :include ("index.org")
         :table-of-contents nil)
        ("all" :components ("posts" "rss"))))

(provide 'publish)
;;; publish.el ends here