An Emacs Blogging Workflow

Hugo is a static site generator. It natively supports org-mode, builds fast, and has live reloading.

I have two repos for blog content and settings and hugo output.

I have defined some emacs lisp utilities to make interacting with Hugo easier.

deploy-blog will goto your blog content, run hugo, cleanup old html, and push the updates.

start-blog-server will run hugo server if not already running, and open the webpage in your browser.

My blog workflow is then to startup the server (SPC a a), write some content in org-mode and see it reflected in the browser on each save, and deploy (SPC a b).

All the code:

(require 'cl)
(require 'dash)

(setq blog-dir "~/dev/blog"
      public-blog-dir "~/dev/public-blog"
      hugo-process "Hugo Server"
      hugo-server-site "http://localhost:1313/")

(defmacro with-dir (DIR &rest FORMS)
  "Execute FORMS in DIR."
  (let ((orig-dir (gensym)))
    `(progn (setq ,orig-dir default-directory)
            (cd ,DIR) ,@FORMS (cd ,orig-dir))))

(defun deploy-blog ()
  "Run hugo and push changes upstream."
  (with-dir public-blog-dir
            (shell-command "git rm -rf .")
            (shell-command "git clean -fxd")

            (with-dir blog-dir (->> public-blog-dir
                                    (concat "hugo -d ")

            (shell-command "git add .")
            (--> (current-time-string)
                 (concat "git commit -m \"" it "\"")
                 (shell-command it))
            (magit-push-current-to-upstream nil)))

(defun start-blog-server ()
  "Run hugo server if not already running and open its webpage."
  (with-dir blog-dir
            (unless (get-process hugo-process)
              (start-process hugo-process nil "hugo" "server"))
            (browse-url hugo-server-site)))

(defun end-blog-server ()
  "End hugo server process if running."
  (--when-let (get-process hugo-process)
    (delete-process it)))

(spacemacs/set-leader-keys (kbd "ab") 'deploy-blog)
(spacemacs/set-leader-keys (kbd "aa") 'start-blog-server)
(spacemacs/set-leader-keys (kbd "ae") 'end-blog-server)

For reference, this blog post is an ordinary org file with just the headers:

#+TITLE: An Emacs Blogging Workflow
#+SLUG: org-mode-blogging
#+DATE: 2017-06-20
#+CATEGORIES: emacs org-mode
#+SUMMARY: Easy org-mode blogging with Hugo
#+DRAFT: false

And hugo takes care of the rest.

comments powered by Disqus