Expand Cut Tags

No cut tags
cbrachyrhynchos: (Default)
[personal profile] cbrachyrhynchos

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 locate-dominating-file.

(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)))
From:
Anonymous( )Anonymous This account has disabled anonymous posting.
OpenID( )OpenID You can comment on this post while signed in with an account from many other sites, once you have confirmed your email address. Sign in using OpenID.
User
Account name:
Password:
If you don't have an account you can create one now.
Subject:
HTML doesn't work in the subject.

Message:

 
Notice: This account is set to log the IP addresses of everyone who comments.
Links will be displayed as unclickable URLs to help prevent spam.

Profile

cbrachyrhynchos: (Default)
cbrachyrhynchos

July 2017

S M T W T F S
       1
234 5678
91011 1213 1415
16 17 1819202122
23 2425 26272829
3031     

Most Popular Tags

Style Credit

Page generated Jul. 28th, 2017 07:00 pm
Powered by Dreamwidth Studios