April 25th 2026 - Things I Worked On

Set blog post title according to name of file

When creating a blog post I want the name of the file to be used as the title.

Example: 2026-04-19_how-to-write-tests.org

Title: How to Write Tests

This creates a post title from a file name with proper capitalization.

(defun my/blog--create-post-title (file-name)
  "return the title of the post properly capitalized extracted
 from the file name"
  (let* ((last-part (car (last (split-string file-name "_" t))))
         (title-part (car (split-string last-part "\\." t )))
         (title-words (split-string title-part "-" t)))
    (my/title-case (string-join title-words " "))))

It seems there's no built in way to do title casing that ignores words that shouldn't be capitalized. This part does the correct title casing. I'm sure there are some edge cases I've missed. As I use it I'll find them.

(defun my/title-case (string)
  "Capitalize STRING, keeping small words lowercase unless they
 are first."
  (let ((small-words '("a" "an" "the" "and" "but"
                       "or" "for" "nor" "on" "at"
                       "to" "from" "by" "is")))
    (let* ((words (split-string string " " t))
           (first-word (capitalize (car words)))
           (rest-words (mapcar (lambda (w) 
                                 (if (member (downcase w) small-words)
                                     (downcase w)
                                   (capitalize w)))
                               (cdr words))))
      (string-join (cons first-word rest-words) " "))))