+(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))))
+