changing the number format for section headings in org-mode html export

1. background

Whenever I export to html from an org mode buffer, section headings had numberings like “1 First section title”, “1.1 first subsection title”. I wanted to change that to “1. First section title”, “1.1. first subsection title”. The user option org-export-section-number-format used to be there for this kind of customization, but is no longer supported. Someone suggested org-html-format-headline-function which I have not figured out how to use. Nevertheless, I managed to do what I want with this code:

(defun my-html-filter-headline-yesdot (text backend info)
  "Ensure dots in headlines."
  (when (org-export-derived-backend-p backend 'html)
    (save-match-data
      (when (let ((case-fold-search t))
              (string-match (rx (group "<span class=\"section-number-" (+ (char digit)) "\">"
                                       (+ (char digit ".")))
                                (group "</span>"))
                            text))
        (replace-match "\\1.\\2"
                       t nil text)))))
(eval-after-load 'ox
  '(progn
     (add-to-list 'org-export-filter-headline-functions
                  'my-html-filter-headline-yesdot)))

2. how does it work

It seems org-export-filter-headline-functions is where you can add your own function that post-process html segments. When your function returns nil, it just means “I have no opinion on what to do in this case”, and when it returns a string, it should be the result of applying your own processing.

3. problems?

The code relies on the resulting html structure.

This entry was posted in Emacs and tagged , , . Bookmark the permalink.

One Response to changing the number format for section headings in org-mode html export

  1. Dan says:

    nice post, have been looking for this workaround for a while

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s