]> git.vanrenterghem.biz Git - www2.vanrenterghem.biz.git/blob - maak-website.el
Werkt enigszins.
[www2.vanrenterghem.biz.git] / maak-website.el
1 (require 'cl-lib)
2 (require 'org)
3 (require 'ox-publish)
4 (require 'ox-rss)
5 (load "~/.emacs.d/lisp/mustache-html.el")
7 (setq org-html-doctype "html5")
8 (setq org-html-head-include-default-style nil)
9 (setq org-html-htmlize-output-type 'css) ; default: 'inline-css
10 (setq org-time-stamp-custom-formats '("%A %e %B %Y" . "%A %e %B %Y at %H:%M"))
11 (setq org-display-custom-times t)
12 (setq my-blog-base-folder "~/websites/stage.vanrenterghem.biz")
13 (setq my-blog-source-folder "~/websites/stage.vanrenterghem.biz/source")
14 (setq my-blog-target-folder "~/websites/stage.vanrenterghem.biz/target")
15 (setq my-blog-target-url "https://www.vanrenterghem.biz/posts/")
16 (setq my-blog-mustache-folder (file-name-concat my-blog-base-folder "html"))
17 (setq my-blog-tags-folder (file-name-concat my-blog-source-folder "tags"))
18 (setq my-blog-posts-folder (file-name-concat my-blog-source-folder "posts"))
20 (setq org-export-time-stamp-file nil)
21 (setq org-rss-use-entry-url-as-guid nil)
23 (defun my-org-get-all-filetags ()
24   "Get list of filetags from all org-files in my-blog-posts-folder."
25   (let ((files (directory-files my-blog-posts-folder t nil nil nil))
26         tagslist x)
27     (save-window-excursion
28       (while (setq x (pop files))
29         (set-buffer (find-file-noselect x))
30         (mapc
31          (lambda (y)
32            (let ((tagfiles (assoc y tagslist)))
33              (if tagfiles
34                  (setcdr tagfiles (cons x (cdr tagfiles)))
35                (add-to-list 'tagslist (list y x)))))
36          (my-org-get-filetags)))
37       tagslist)))
39 (defun my-org-get-filetags ()
40   "Get list of filetags for current buffer"
41   (let ((ftags org-file-tags)
42         x)
43     (mapcar
44      (lambda (x)
45        (org-no-properties x))
46      ftags)))
48 (defun my-blog-create-tags-files (plist)
49   "Create org files for each tag defined in FILETAGS in posts, storing them in my-blog-tags-folder."
50   (unless (file-directory-p my-blog-tags-folder) (make-directory my-blog-tags-folder))
51   (with-temp-file (file-name-concat my-blog-source-folder "tag-index.org")
52       (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "tags-index.mustache") "\n"))
53       (insert (concat "#+TITLE: Blog - All tags\n"))
54       (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
55       (insert "\n")
56       (dolist (tag (sort (my-org-get-all-filetags) (lambda (x y) (string-lessp (car x) (car y))) ))
57         (insert (concat "- [[file:" (file-name-concat my-blog-posts-folder (concat "tag-" (car tag) ".org")) "][" (car tag) "]]\n")))) 
58   (dolist (tag (my-org-get-all-filetags))
59     (with-temp-file (file-name-concat my-blog-tags-folder (concat "tag-" (car tag) ".org"))
60       (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "tags.mustache") "\n"))
61       (insert (concat "#+TITLE: " (car tag) "\n"))
62       (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
63       (insert "\n")
64       (insert (concat "# " (car tag) "\n\n"))
65       (dolist (tagfile (my-blog-sort-article-list (cdr tag) plist))
66         (let ((relpath (file-relative-name tagfile my-blog-posts-folder)));;not used
67           (message (concat "Processing " tagfile))
68           (insert (concat "- " (format "%s - [[file:%s][%s]]" ;;the date and filename are added after the entry
69                  (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date tagfile plist))
70                  relpath
71                  (org-publish-find-title tagfile plist))
72                           "\n")))))))
73                                         ;(my-org-publish-sitemap-default-entry tagfile nil plist) "\n")))))))
75 (defun my-org-publish-sitemap-default-entry (entry style project)
76   "My format for site map ENTRY, as a string.
77 ENTRY is a file name.  STYLE is the style of the sitemap.
78 PROJECT is the current project."
79   (cond ((not (directory-name-p entry))
80          (format "%s - [[file:%s][%s]]" ;;the date and filename are added after the entry
81                  (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date entry project))
82                  entry
83                  (org-publish-find-title entry project)))
84         ((eq style 'tree)
85          ;; Return only last subdir.
86          (file-name-nondirectory (directory-file-name entry)))
87         (t entry)))
89 (defun my-blog-parse-sitemap-list (l)
90   "Convert the sitemap list in to a list of filenames."
91   (mapcar #'(lambda (i)
92               (let ((link (with-temp-buffer
93                             (let ((org-inhibit-startup nil))
94                               (insert (car i))
95                               (org-mode)
96                               (goto-char (point-min))
97                               (org-element-link-parser)))))
98                 (when link
99                   (plist-get (cadr link) :path))))
100           (cdr l)))
102 (defun my-blog-sort-article-list (l p)
103   "sort the article list anti-chronologically."
104   (sort l #'(lambda (a b)
105               (let ((d-a (org-publish-find-date a p))
106                     (d-b (org-publish-find-date b p)))
107                 (not (time-less-p d-a d-b))))))
109 (defun my-blog-get-preview (file)
110   "Clips a section of a post in FILE to be used as preview in the sitemap.
111 Either the section between #+BEGIN_PREVIEW and +#END_PREVIEW is used, or the first section between 2 blank lines."
112   (with-temp-buffer
113     (insert-file-contents file)
114     (goto-char (point-min))
115     (let* ((beg (or (re-search-forward "^#\\+BEGIN_PREVIEW$" nil t 1)
116                     (re-search-forward "^$")))
117            (end (or (if (re-search-forward "^#\\+END_PREVIEW$" nil t 1)
118                         (match-beginning 0))
119                     (progn (goto-char (+ 1 beg))
120                            (re-search-forward "^$" nil t 2)))))
121       (buffer-substring beg end))))
123 (defun my-blog-sitemap (title list)
124   "Generate the sitemap landing page for my blog."
125   (my-plain-publish-sitemap-default title list) ; Create additional sitemap
126   (with-temp-buffer
127     ;; mangle the parsed list given to us into a plain lisp list of files
128     (let* ((filenames (my-blog-parse-sitemap-list list))
129            (project-plist (assoc "posts" org-publish-project-alist))
130            (articles (my-blog-sort-article-list filenames project-plist)))
131       (dolist (file filenames)
132         (let* ((abspath (file-name-concat "/home/frederik/websites/stage.vanrenterghem.biz/source/posts" file))
133                (relpath (file-relative-name abspath "/home/frederik/websites/stage.vanrenterghem.biz/source/posts/"))
134                (title (org-publish-find-title file project-plist))
135                (date (format-time-string (cdr org-time-stamp-custom-formats) (org-publish-find-date file project-plist)))
136                (preview (my-blog-get-preview abspath))
137                )
138           ;; insert a horizontal line before every post, kill the first one
139           ;; before saving
140           (insert "-----\n")
141           (insert (concat "* [[file:" relpath "][" title "]]\n"))
142           ;; add properties for `ox-rss.el' here
143           ;(let ((rss-permalink (concat (file-name-sans-extension relpath) ".html"))
144         ;       (rss-pubdate date))
145          ;   (org-mode)
146           ;  (org-set-property "RSS_PERMALINK" rss-permalink)
147            ; (org-set-property "PUBDATE" rss-pubdate))
148           ;; insert the date, preview, & read more link
149           (insert (concat "Published: " date "\n\n"))
150           (insert preview)
151                                         ;(insert (concat "#+INCLUDE: \"" relpath "\" :only-contents t :lines \"1-10\"\n"))
152           (insert "\n")
153           (insert (concat "[[file:" relpath "][Read More...]]\n"))))
154       ;; kill the first hrule to make this look OK
155       (goto-char (point-min))
156       (let ((kill-whole-line t)) (kill-line))
157       ;; insert a title and save
158       (insert "#+OPTIONS: title:nil\n")
159       (insert "#+TITLE: Blog\n")
160       (insert "#+AUTHOR: Frederik Vanrenterghem\n")
161       (insert "#+EMAIL: frederik@vanrenterghem.biz\n")
162       (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
163       (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "post-index.mustache") "\n"))
164       (insert "\n")
165       (buffer-string))))
167 (defun my-plain-publish-sitemap-default (title list)
168   "Create a simple site map, as a string.
169 TITLE is the title of the site map.  LIST is an internal
170 representation for the files to include, as returned by
171 `org-list-to-lisp'."
172   (with-temp-file "~/websites/stage.vanrenterghem.biz/source/sitemap.org"
173     (let* ((filenames (my-blog-parse-sitemap-list list))
174            (project-plist (assoc "posts" org-publish-project-alist))
175            (articles (my-blog-sort-article-list filenames project-plist)))
176       (dolist (file filenames)
177         (let* ((abspath (file-name-concat "/home/frederik/websites/stage.vanrenterghem.biz/source/posts" file))
178                (relpath (file-relative-name abspath "/home/frederik/websites/stage.vanrenterghem.biz/source/"))
179                (title (org-publish-find-title file project-plist))
180                (date (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date file project-plist))))
181           (insert (concat "* " date " - [[file:" relpath "][" title "]]\n"))))
182       (goto-char (point-min))
183       (insert "#+OPTIONS: ^:nil\n")))) ; do not use underscores as subscript
185 (defun my-org-rss-publish-to-rss (plist filename pub-dir)
186   "Publish RSS with PLIST, only when FILENAME is 'rss.org'.
187 PUB-DIR is when the output will be placed."
188   (if (equal "rss.org" (file-name-nondirectory filename))
189       (org-rss-publish-to-rss plist filename pub-dir)))
191 (defun my-format-rss-feed (title list)
192   "Generate RSS feed, as a string.
193 TITLE is the title of the RSS feed.  LIST is an internal
194 representation for the files to include, as returned by
195 `org-list-to-lisp'.  PROJECT is the current project."
196   (concat "#+OPTIONS: ^:nil\n" ; do not use underscores as subscript
197           "#+TITLE: " title "\n\n"
198           (org-list-to-subtree list 0)))
200 (defun my-format-rss-feed-entry (entry style project)
201   "Format ENTRY for the RSS feed.
202 ENTRY is a file name.  STYLE is either 'list' or 'tree'.
203 PROJECT is the current project."
204   (cond ((not (directory-name-p entry))
205          (let* ((file (org-publish--expand-file-name entry project))
206                 (title (org-publish-find-title entry project))
207                 (date (format-time-string "%Y-%m-%d" (org-publish-find-date entry project)))
208                 (link (concat my-blog-target-url (file-name-sans-extension entry) ".html")))
209            (with-temp-buffer
210              (org-mode)
211              (insert (format "* [[file:%s][%s]]\n" file title))
212              (org-set-property "RSS_PERMALINK" link)
213              (org-set-property "PUBDATE" date)
214              (insert-file-contents file)
215              (buffer-string))))
216         ((eq style 'tree)
217          ;; Return only last subdir.
218          (file-name-nondirectory (directory-file-name entry)))
219         (t entry)))
222 (defun my-blog-publish-rss-sitemap (title list)
223   "Create a simple site map, as a string.
224 TITLE is the title of the site map.  LIST is an internal
225 representation for the files to include, as returned by
226 `org-list-to-lisp'."
227   (with-temp-buffer
228     (message "Creating RSS index.")
229     (org-mode)
230     (let* ((filenames (my-blog-parse-sitemap-list list))
231            (project-plist (assoc "posts" org-publish-project-alist))
232            ;;(articles (my-blog-sort-article-list filenames project-plist))
233            )
234       (insert "#+AUTHOR: Frederik Vanrenterghem\n")
235       (dolist (file filenames)
236         (unless (eq (file-name-base file) "rss.org")
237           (message "Processing %s for RSS index." file)
238           (let* ((abspath (file-name-concat "/home/frederik/websites/stage.vanrenterghem.biz/source/posts" file))
239                  (relpath (file-relative-name abspath "/home/frederik/websites/stage.vanrenterghem.biz/source/"))
240                  (title (org-publish-find-title file project-plist))
241                  (date (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date file project-plist)))
242                  (rss-permalink (file-name-sans-extension relpath))
243                  (preview (my-blog-get-preview abspath)))
244             (insert (concat "* [[file:" relpath "][" title "]]"))
245             (org-set-property "RSS_PERMALINK" rss-permalink)
246             (org-set-property "PUBDATE" date)
247             (org-set-property "RSS_TITLE" title)
248             (insert preview)
249             (insert "\n"))))
250       (goto-char (point-min))
251       (insert "#+OPTIONS: ^:nil\n"))
252     (write-file "~/websites/stage.vanrenterghem.biz/source/posts/rss.org")))
255 (defun my-blog-cleanup-sitemaps (plist)
256   "Clean up temporary files created in the process of publishing"
257   (delete-file (file-name-concat my-blog-source-folder "sitemap.org"))
258   (delete-file (file-name-concat my-blog-posts-folder "sitemap.org"))
259   )
260   
261 (setq org-publish-project-alist
262       `(("landing"
263          :base-directory ,my-blog-source-folder
264          :base-extension "org"
265          :publishing-directory ,my-blog-target-folder
266          :publishing-function org-mustache-html-publish-to-html
267          :mustache-template ,(file-name-concat my-blog-mustache-folder "landing.mustache")
268          :headline-levels 2
269          :section-numbers nil
270          :with-toc nil
271          :with-title nil
272          :html-content-class nil
273          :html-head-include-default-style nil
274          :html-head nil
275          :html-divs nil
276          :html-preamble nil
277          :html-postamble nil
278          )
280         ("posts"
281          :base-directory ,my-blog-posts-folder
282          :base-extension "org"
283          :publishing-directory ,(file-name-concat my-blog-target-folder "posts")
284          :publishing-function org-mustache-html-publish-to-html
285          :mustache-template ,(file-name-concat my-blog-mustache-folder "post.mustache")
286          :preparation-function my-blog-create-tags-files
287          :exclude "html*\\|assets*\\|index.org\\|rss.org" ;"assets*\\|sitemap.org\\|index.org" ;; regexp
288          :html-content-class nil
289          :section-numbers nil
290          :with-toc nil
291          :with-title nil
292          :sitemap-title "All posts"
293          :html-head-include-default-style nil
294          :html-head nil
295          :html-divs nil
296          :recursive t
297          :auto-sitemap t
298          :html-preamble nil
299          :html-postamble nil
300          :sitemap-sort-folders ignore-errors
301          :sitemap-function my-blog-sitemap
302          :sitemap-sort-files anti-chronologically
303          :sitemap-filename "sitemap.org"
304          )
306         ("tags"
307          :base-directory ,(file-name-concat my-blog-source-folder "tags")
308          :base-extension "org"
309          :publishing-directory ,(file-name-concat my-blog-target-folder "posts")
310          :publishing-function org-mustache-html-publish-to-html
311          :mustache-template ,(file-name-concat my-blog-mustache-folder "tags.mustache")
312          :html-content-class nil
313          :section-numbers nil
314          :with-toc nil
315          :with-title nil
316          :html-head-include-default-style nil
317          :html-head nil
318          :html-divs nil
319          :recursive nil
320          :auto-sitemap nil
321          :html-preamble nil
322          :html-postamble nil
323          )
325         ("assets"
326          :base-directory ,(file-name-concat my-blog-source-folder "assets")
327          :base-extension any
328          :recursive t
329          :publishing-directory ,(file-name-concat my-blog-target-folder "assets")
330          :publishing-function org-publish-attachment
331          :completion-function my-blog-cleanup-sitemaps
332          )
333         
334         ("rss"
335          :base-directory ,my-blog-posts-folder
336          :base-extension "org"
337          :exclude ,(regexp-opt '("rss.org" "index.org" "sitemap.org"))
338          :publishing-directory ,(file-name-concat my-blog-target-folder "posts")
339          :publishing-function my-org-rss-publish-to-rss
340          ;:with-author t
341          :html-link-home ,my-blog-target-url
342          :html-link-use-abs-url t
343          ;:html-link-org-files-as-html t
344          :auto-sitemap t
345          :sitemap-filename "rss.org"
346          :sitemap-title "Frederik Vanrenterghem blog"
347          :sitemap-style list
348          :sitemap-sort-files anti-chronologically
349          :sitemap-function my-format-rss-feed
350          :sitemap-format-entry my-format-rss-feed-entry
351          )
352         
353         ("website" :components ("posts" "rss" "tags" "landing" "assets"))))
355 (org-publish-initialize-cache "website")