Endless Parentheses

Concise ramblings on Emacs productivity.

Support on Gratipay
profile for Malabarba on Stack Exchange

Better time-stamps in org-export

org-mode has a very useful command, org-time-stamp, which helps you insert dates from a calendar. So you can quickly type C-c . RET to insert <2015-10-05 Mon>, for instance. These time-stamps are used by Org in a variety of ways, so they are wrapped in <> to make them easy to parse. The downside being that they look less than optimal when exported.

I was bit by this again today while updating the post on donations, and I finally decided to look for a way to fix it. Of course, org-mode is nothing if not configurable, so the answer wasn’t very far away.

(add-to-list 'org-export-filter-timestamp-functions
(defun endless/filter-timestamp (trans back _comm)
  "Remove <> around time-stamps."
  (pcase back
    ((or `jekyll `html)
     (replace-regexp-in-string "&[lg]t;" "" trans))
     (replace-regexp-in-string "[<>]" "" trans))))

The loyal readers might notice how similar this is to the second lambda we used for exporting Youtube links. Org is quite consistent in its use of export filters.

The above is enough to remove the surrounding <>, but we can still make it better. The YYYY-MM-DD weekday format isn’t commonly used in prose, so let’s switch that as well.

(setq-default org-display-custom-times t)
;;; Before you ask: No, removing the <> here doesn't work.
(setq org-time-stamp-custom-formats
      '("<%d %b %Y>" . "<%d/%m/%y %a %H:%M>"))

As a bonus, this format will also be used to display time-stamps in your org-mode buffers. If don’t want that, you can let-bind the org-display-custom-times variable when calling the export function, instead of setting it globally.

If any of this doesn’t work for you, you might need to update your Org package. Fortunately, Org is on GElpa, so anyone can do that with M-x list-packages.

Tags: org-mode, init.el, emacs

Support on Gratipay
comments powered by Disqus