I've been fooling around with writing up some convenience functions for writing hugo posts. A lot of my earlier work from last year is described in this post.
Problem 1: Finding the root folder
There's an emacs function for that
(defun locate-hugo-root () "Search up from current file to find the root of the hugo directory." (let ((fPath (if (equal major-mode 'dired-mode) default-directory (buffer-file-name)))) (locate-dominating-file fPath "config.toml")))
Problem 2: Creating a new post
Prompt with ido find file. Check if the buffer is empty, if the buffer is empty run a header skeleton.
(defun hugo-new-post () "Prompt for a filename and then create a new post using hugo-yaml-skeleton." (interactive) (ido-find-file-in-dir default-directory) (when (= 0 (buffer-size)) (hugo-yaml-skeleton)))
Problem 3: Better searching
Built-in emacs searching doesn't really have good defaults for this to accommodate older versions of grep, and I can't be certain of having ag, the silver searcher everywhere. So I run grep with a custom command string, using the (almost) common lisp
format to build my command string. For grep, I include
--include='*.md' to ensure I'm only searching markdown files.
(require 'cl) (defun hugo-search-markdown (search-string directory) "Search markdown files for search-string in directory." (interactive "sSearch string: \nDDirectory: ") (grep (format "grep -r -nH --include='*.md' --include='*.markdown' '%s' %s" search-string directory)))
Problem 4: Starting a server
Start a hugo server to preview the blog before publication.
(defun hugo-start-server () "Start a server in the root of the current working directory." (interactive) (if (locate-hugo-root) (async-shell-command (concat "hugo server -s " (locate-hugo-root) "")) (message "Can't find hugo config file at %s" default-directory)))