(require 'ox-publish)
(setq org-html-doctype "html5")
(setq org-html-head-include-default-style nil)
;(setq org-html-htmlize-output-type 'css) ; default: 'inline-css
(defun my-org-publish-sitemap-default-entry (entry style project)
"My format for site map ENTRY, as a string.
ENTRY is a file name. STYLE is the style of the sitemap.
PROJECT is the current project."
(cond ((not (directory-name-p entry))
(format "%s - [[file:%s][%s]]" ;;the date and filename are added after the entry
(format-time-string "%e %b %Y" (org-publish-find-date entry project))
entry
(org-publish-find-title entry project)))
((eq style 'tree)
;; Return only last subdir.
(file-name-nondirectory (directory-file-name entry)))
(t entry)))
(defun my-blog-parse-sitemap-list (l)
"Convert the sitemap list in to a list of filenames."
(mapcar #'(lambda (i)
(let ((link (with-temp-buffer
(let ((org-inhibit-startup nil))
(insert (car i))
(org-mode)
(goto-char (point-min))
(org-element-link-parser)))))
(when link
(plist-get (cadr link) :path))))
(cdr l)))
(defun my-blog-sort-article-list (l p)
"sort the article list anti-chronologically."
(sort l #'(lambda (a b)
(let ((d-a (org-publish-find-date a p))
(d-b (org-publish-find-date b p)))
(not (time-less-p d-a d-b))))))
(defun my-blog-get-preview (file)
"Clips a section of a post in FILE to be used as preview in the sitemap.
Either the section between #+BEGIN_PREVIEW and +#END_PREVIEW is used, or the first section between 2 blank lines."
(with-temp-buffer
(insert-file-contents file)
(goto-char (point-min))
(let* ((beg (or (re-search-forward "^#\\+BEGIN_PREVIEW$" nil t 1)
(re-search-forward "^$")))
(end (or (if (re-search-forward "^#\\+END_PREVIEW$" nil t 1)
(match-beginning 0))
(progn (goto-char (+ 1 beg))
(re-search-forward "^$" nil t 2)))))
(buffer-substring beg end))))
(defun my-blog-sitemap (title list)
"Generate the sitemap landing page for my blog."
(my-plain-publish-sitemap-default title list)
(with-temp-buffer
;; mangle the parsed list given to us into a plain lisp list of files
(let* ((filenames (my-blog-parse-sitemap-list list))
(project-plist (assoc "posts" org-publish-project-alist))
(articles (my-blog-sort-article-list filenames project-plist)))
(dolist (file filenames)
(let* ((abspath (file-name-concat "/home/frederik/websites/stage.vanrenterghem.biz/source/posts" file))
(relpath (file-relative-name abspath "/home/frederik/websites/stage.vanrenterghem.biz/"))
(title (org-publish-find-title file project-plist))
(date (format-time-string (car org-time-stamp-formats) (org-publish-find-date file project-plist)))
(preview (my-blog-get-preview abspath))
)
;; insert a horizontal line before every post, kill the first one
;; before saving
(insert "-----\n")
(insert (concat "* [[file:" relpath "][" title "]]\n"))
;; add properties for `ox-rss.el' here
;(let ((rss-permalink (concat (file-name-sans-extension relpath) ".html"))
; (rss-pubdate date))
; (org-set-property "RSS_PERMALINK" rss-permalink)
; (org-set-property "PUBDATE" rss-pubdate))
;; insert the date, preview, & read more link
(insert (concat "Published: " date "\n\n"))
(insert preview)
;(insert (concat "#+INCLUDE: \"" relpath "\" :only-contents t :lines \"1-10\"\n"))
(insert "\n")
(insert (concat "[[file:" relpath "][Read More...]]\n"))))
;; kill the first hrule to make this look OK
(goto-char (point-min))
(let ((kill-whole-line t)) (kill-line))
;; insert a title and save
(insert "#+OPTIONS: title:nil\n")
(insert "#+TITLE: Blog\n")
(insert "#+AUTHOR: Frederik Vanrenterghem\n")
(insert "#+EMAIL: frederik@vanrenterghem.biz\n")
(insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
(insert "#+mustache-template: ~/websites/stage.vanrenterghem.biz/html/post-index.mustache\n")
(insert "\n")
(buffer-string))))
(defun my-plain-publish-sitemap-default (title list)
"Default site map, as a string.
TITLE is the title of the site map. LIST is an internal
representation for the files to include, as returned by
`org-list-to-lisp'. PROJECT is the current project."
(with-temp-file "~/websites/stage.vanrenterghem.biz/source/sitemap.org"
(insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
(insert (concat "#+TITLE: " title "\n\n"))
(insert (org-list-to-org list))))
(setq org-publish-project-alist
'(("landing"
:base-directory "~/websites/stage.vanrenterghem.biz/source/"
:base-extension "org"
:include ("posts/sitemap.org")
:publishing-directory "~/websites/stage.vanrenterghem.biz/target"
:publishing-function org-mustache-html-publish-to-html
:mustache-template "~/websites/stage.vanrenterghem.biz/html/landing.mustache"
:headline-levels 3
:section-numbers nil
:with-toc nil
:html-head-include-default-style nil
:html-head nil
:html-divs nil
:html-preamble nil
:html-postamble nil
)
("posts"
:base-directory "~/websites/stage.vanrenterghem.biz/source/posts/"
:base-extension "org"
:publishing-directory "~/websites/stage.vanrenterghem.biz/target/posts"
:publishing-function org-mustache-html-publish-to-html
:mustache-template "~/websites/stage.vanrenterghem.biz/html/post.mustache"
:exclude "assets*\\|index.org" ;"assets*\\|sitemap.org\\|index.org" ;; regexp
:html-content-class nil
:section-numbers nil
:with-toc nil
:with-title nil
:sitemap-title "All posts"
:html-head-include-default-style nil
:html-head nil
:html-divs nil
:recursive t
:auto-sitemap t
:html-preamble nil
:html-postamble nil
:sitemap-sort-folders ignore-errors
:sitemap-function my-blog-sitemap
;:sitemap-function org-publish-sitemap-default
;:sitemap-format-entry my-org-publish-sitemap-default-entry
:sitemap-sort-files anti-chronologically
:sitemap-filename "sitemap.org"
)
("assets"
:base-directory "~/websites/stage.vanrenterghem.biz/source/assets/"
:base-extension any
:recursive t
:publishing-directory "~/websites/stage.vanrenterghem.biz/target/assets/"
:publishing-function org-publish-attachment)
("website" :components ("posts" "landing" "assets"))))