2 (load "~/.emacs.d/lisp/mustache-html.el")
4 (setq org-html-doctype "html5")
5 (setq org-html-head-include-default-style nil)
6 (setq org-html-htmlize-output-type 'css) ; default: 'inline-css
7 (setq org-time-stamp-custom-formats '("%A %e %B %Y" . "%A %e %B %Y at %H:%M"))
8 (setq org-display-custom-times t)
9 (setq my-blog-base-folder "~/websites/stage.vanrenterghem.biz")
10 (setq my-blog-source-folder "~/websites/stage.vanrenterghem.biz/source")
11 (setq my-blog-target-folder "~/websites/stage.vanrenterghem.biz/target")
12 (setq my-blog-mustache-folder (file-name-concat my-blog-base-folder "html"))
13 (setq my-blog-tags-folder (file-name-concat my-blog-source-folder "tags"))
14 (setq my-blog-posts-folder (file-name-concat my-blog-source-folder "posts"))
17 (defun my-org-get-all-filetags ()
18 "Get list of filetags from all org-files in my-blog-posts-folder."
19 (let ((files (directory-files my-blog-posts-folder t nil nil nil))
21 (save-window-excursion
22 (while (setq x (pop files))
23 (set-buffer (find-file-noselect x))
26 (let ((tagfiles (assoc y tagslist)))
28 (setcdr tagfiles (cons x (cdr tagfiles)))
29 (add-to-list 'tagslist (list y x)))))
30 (my-org-get-filetags)))
33 (defun my-org-get-filetags ()
34 "Get list of filetags for current buffer"
35 (let ((ftags org-file-tags)
39 (org-no-properties x))
42 (defun my-blog-create-tags-files (plist)
43 "Create org files for each tag defined in FILETAGS in posts, storing them in my-blog-tags-folder."
44 (unless (file-directory-p my-blog-tags-folder) (make-directory my-blog-tags-folder))
45 (with-temp-file (file-name-concat my-blog-source-folder "tag-index.org")
46 (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "tags-index.mustache") "\n"))
47 (insert (concat "#+TITLE: Blog - All tags\n"))
48 (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
50 (dolist (tag (sort (my-org-get-all-filetags) #'<))
51 (insert (concat "- [[file:" (file-name-concat my-blog-posts-folder (concat "tag-" (car tag) ".org")) "][" (car tag) "]]\n"))))
52 (dolist (tag (my-org-get-all-filetags))
53 (with-temp-file (file-name-concat my-blog-tags-folder (concat "tag-" (car tag) ".org"))
54 (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "tags.mustache") "\n"))
55 (insert (concat "#+TITLE: " (car tag) "\n"))
56 (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
58 (insert (concat "# " (car tag) "\n\n"))
59 (dolist (tagfile (cdr tag))
60 (let ((relpath (file-relative-name tagfile my-blog-posts-folder)));;not used
61 (message (concat "Processing " tagfile))
62 (insert (concat "- " (format "%s - [[file:%s][%s]]" ;;the date and filename are added after the entry
63 (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date tagfile plist))
65 (org-publish-find-title tagfile plist))
67 ;(my-org-publish-sitemap-default-entry tagfile nil plist) "\n")))))))
69 (defun my-org-publish-sitemap-default-entry (entry style project)
70 "My format for site map ENTRY, as a string.
71 ENTRY is a file name. STYLE is the style of the sitemap.
72 PROJECT is the current project."
73 (cond ((not (directory-name-p entry))
74 (format "%s - [[file:%s][%s]]" ;;the date and filename are added after the entry
75 (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date entry project))
77 (org-publish-find-title entry project)))
79 ;; Return only last subdir.
80 (file-name-nondirectory (directory-file-name entry)))
83 (defun my-blog-parse-sitemap-list (l)
84 "Convert the sitemap list in to a list of filenames."
86 (let ((link (with-temp-buffer
87 (let ((org-inhibit-startup nil))
90 (goto-char (point-min))
91 (org-element-link-parser)))))
93 (plist-get (cadr link) :path))))
96 (defun my-blog-sort-article-list (l p)
97 "sort the article list anti-chronologically."
98 (sort l #'(lambda (a b)
99 (let ((d-a (org-publish-find-date a p))
100 (d-b (org-publish-find-date b p)))
101 (not (time-less-p d-a d-b))))))
103 (defun my-blog-get-preview (file)
104 "Clips a section of a post in FILE to be used as preview in the sitemap.
105 Either the section between #+BEGIN_PREVIEW and +#END_PREVIEW is used, or the first section between 2 blank lines."
107 (insert-file-contents file)
108 (goto-char (point-min))
109 (let* ((beg (or (re-search-forward "^#\\+BEGIN_PREVIEW$" nil t 1)
110 (re-search-forward "^$")))
111 (end (or (if (re-search-forward "^#\\+END_PREVIEW$" nil t 1)
113 (progn (goto-char (+ 1 beg))
114 (re-search-forward "^$" nil t 2)))))
115 (buffer-substring beg end))))
117 (defun my-blog-sitemap (title list)
118 "Generate the sitemap landing page for my blog."
119 (my-plain-publish-sitemap-default title list) ; Create additional sitemap
121 ;; mangle the parsed list given to us into a plain lisp list of files
122 (let* ((filenames (my-blog-parse-sitemap-list list))
123 (project-plist (assoc "posts" org-publish-project-alist))
124 (articles (my-blog-sort-article-list filenames project-plist)))
125 (dolist (file filenames)
126 (let* ((abspath (file-name-concat "/home/frederik/websites/stage.vanrenterghem.biz/source/posts" file))
127 (relpath (file-relative-name abspath "/home/frederik/websites/stage.vanrenterghem.biz/source/posts/"))
128 (title (org-publish-find-title file project-plist))
129 (date (format-time-string (cdr org-time-stamp-custom-formats) (org-publish-find-date file project-plist)))
130 (preview (my-blog-get-preview abspath))
132 ;; insert a horizontal line before every post, kill the first one
135 (insert (concat "* [[file:" relpath "][" title "]]\n"))
136 ;; add properties for `ox-rss.el' here
137 ;(let ((rss-permalink (concat (file-name-sans-extension relpath) ".html"))
138 ; (rss-pubdate date))
139 ; (org-set-property "RSS_PERMALINK" rss-permalink)
140 ; (org-set-property "PUBDATE" rss-pubdate))
141 ;; insert the date, preview, & read more link
142 (insert (concat "Published: " date "\n\n"))
144 ;(insert (concat "#+INCLUDE: \"" relpath "\" :only-contents t :lines \"1-10\"\n"))
146 (insert (concat "[[file:" relpath "][Read More...]]\n"))))
147 ;; kill the first hrule to make this look OK
148 (goto-char (point-min))
149 (let ((kill-whole-line t)) (kill-line))
150 ;; insert a title and save
151 (insert "#+OPTIONS: title:nil\n")
152 (insert "#+TITLE: Blog\n")
153 (insert "#+AUTHOR: Frederik Vanrenterghem\n")
154 (insert "#+EMAIL: frederik@vanrenterghem.biz\n")
155 (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
156 (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "post-index.mustache") "\n"))
160 (defun my-plain-publish-sitemap-default (title list)
161 "Create a simple site map, as a string.
162 TITLE is the title of the site map. LIST is an internal
163 representation for the files to include, as returned by
165 (with-temp-file "~/websites/stage.vanrenterghem.biz/source/sitemap.org"
166 (let* ((filenames (my-blog-parse-sitemap-list list))
167 (project-plist (assoc "posts" org-publish-project-alist))
168 (articles (my-blog-sort-article-list filenames project-plist)))
169 (dolist (file filenames)
170 (let* ((abspath (file-name-concat "/home/frederik/websites/stage.vanrenterghem.biz/source/posts" file))
171 (relpath (file-relative-name abspath "/home/frederik/websites/stage.vanrenterghem.biz/source/"))
172 (title (org-publish-find-title file project-plist))
173 (date (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date file project-plist))))
174 (insert (concat "* " date " - [[file:" relpath "][" title "]]\n"))))
175 (goto-char (point-min))
176 (insert "#+OPTIONS: ^:nil\n")))) ; do not use underscores as subscript
178 (defun my-blog-cleanup-sitemaps (plist)
179 "Clean up temporary files created in the process of publishing"
180 (delete-file (file-name-concat my-blog-source-folder "sitemap.org"))
181 (delete-file (file-name-concat my-blog-posts-folder "sitemap.org"))
184 (setq org-publish-project-alist
186 :base-directory ,my-blog-source-folder
187 :base-extension "org"
188 :publishing-directory ,my-blog-target-folder
189 :publishing-function org-mustache-html-publish-to-html
190 :mustache-template ,(file-name-concat my-blog-mustache-folder "landing.mustache")
195 :html-content-class nil
196 :html-head-include-default-style nil
204 :base-directory ,my-blog-posts-folder
205 :base-extension "org"
206 :publishing-directory ,(file-name-concat my-blog-target-folder "posts")
207 :publishing-function org-mustache-html-publish-to-html
208 :mustache-template ,(file-name-concat my-blog-mustache-folder "post.mustache")
209 :preparation-function my-blog-create-tags-files
210 :exclude "html*\\|assets*\\|index.org" ;"assets*\\|sitemap.org\\|index.org" ;; regexp
211 :html-content-class nil
215 :sitemap-title "All posts"
216 :html-head-include-default-style nil
223 :sitemap-sort-folders ignore-errors
224 :sitemap-function my-blog-sitemap
225 ;:sitemap-function org-publish-sitemap-default
226 ;:sitemap-format-entry my-org-publish-sitemap-default-entry
227 :sitemap-sort-files anti-chronologically
228 :sitemap-filename "sitemap.org"
232 :base-directory ,(file-name-concat my-blog-source-folder "tags")
233 :base-extension "org"
234 :publishing-directory ,(file-name-concat my-blog-target-folder "posts")
235 :publishing-function org-mustache-html-publish-to-html
236 :mustache-template ,(file-name-concat my-blog-mustache-folder "tags.mustache")
237 :html-content-class nil
241 :html-head-include-default-style nil
251 :base-directory ,(file-name-concat my-blog-source-folder "assets")
254 :publishing-directory ,(file-name-concat my-blog-target-folder "assets")
255 :publishing-function org-publish-attachment
256 :completion-function my-blog-cleanup-sitemaps
259 ("website" :components ("posts" "tags" "landing" "assets"))))