Creating new Org Files for Hugo

As the command `hugo new` does not work for org files I'm using emacs for that task.

The code is based on what I found at Peter Smith's which seem to be based on the code from Tohojo.

First we've to set the location where the blog source files are.

(setq hugo-base-dir "~/Documents/repo/TechBlog/")

Then we need the current time in two formats. The first for the name of the file we are creating and the second is used in the frontmatter.

(defun today-is ()
  (format-time-string "%Y-%m-%d-"))

(defun now-is ()	 
 (concat (format-time-string "%Y-%m-%dT%T")
   ((lambda (x) (concat (substring x 0 3) ":" (substring x 3 5)))
   (format-time-string "%z"))))

Then a new buffer is created and populated with the frontmatter. I found no way to set the mode other than hardcoding it to org-mode.

(defun org-hugo-new-post ()
  "Creates a new org file in Hugo's content/post directory"
  (let* ((title (read-from-minibuffer "Title: "))
         (filename (read-from-minibuffer "Filename: "
					 (replace-regexp-in-string "-\\.org" ".org"
								   (concat (downcase
									    (replace-regexp-in-string "[^a-z0-9]+" "-" title))
	 (blogfile (concat "post/" (today-is) filename))
	 (url (concat (downcase
		       (replace-regexp-in-string "[^a-z0-9]+" "-"
         (path (concat hugo-base-dir "content/" blogfile)))

    (if (file-exists-p path)
        (message "File already exists!")
      (message blogfile)
      (switch-to-buffer (generate-new-buffer blogfile))
      (insert "#+DATE: " (now-is))
      (insert "\n#+TITLE: " title)
      (insert "\n#+URL: /" url "/")
      (insert "\n#+BANNER: \n#+CATEGORIES: \n#+TAGS:\n\n")
      (write-file path)
      (goto-char (point-max)))))

Banner image courtesy of Lohrelei