導入環境

という仕組みです。

Emacsの設定

全てをEmacsで処理したかったので下記サイトを参考にしました。感謝!

hugo-base-dirを切り替えて複数対応させます。avy-men.elでメニューを作りました。

init.elにコピペするか、init-loderで読み込みます。

;; Default base-dir 
(setq hugo-base-dir "~/Dropbox/blog1/"
  hugo-buffer "*hugo*")

;; Switching base-dir
;; avy-menu.el -> package install
(defun avy-menu-2 ()
  (interactive)
  (eval (avy-menu
         "*avy-menu-2*"
         '("Hugo Menu"
           ("ブログを切り替える"
            ("blog1" . (setq hugo-base-dir "~/Dropbox/blog1/"
                             hugo-buffer "*hugo*"))
            ("blog2" . (setq hugo-base-dir "~/Dropbox/blog2/"
                             hugo-buffer "*hugo*"))
            ("blog3" . (setq hugo-base-dir "~/Dropbox/blog3/"
                             hugo-buffer "*hugo*")))
           ("切り替えをやめる"
            ("quit" . (message "quit ok"))))
          t)))

(defun hugo-edit ()
(interactive)
(find-file (concat hugo-base-dir "content/post/")))

(defun hugo-new-post ()
(interactive)
(let* ((title (read-from-minibuffer "Title: "))
       (filename (concat "post/"
                       (read-from-minibuffer "Filename: "
                                             (replace-regexp-in-string "-\\.md" ".md"
                                               (concat (format-time-string "%Y-%m%d-%H%M") (downcase
                                                       (replace-regexp-in-string "[^a-z0-9]+" "-"
                                                                                 title))
                                                       ".md")))))
     (path (concat hugo-base-dir "content/" filename)))
(if (file-exists-p path)
    (message "File already exists!")
  (hugo-command "new" filename)
  (find-file path)
  (hugo-replace-key "title" title)
  (goto-char (point-max))
  (save-buffer))))


;; helper functions
(defun hugo-command (&rest args)
(let ((default-directory (expand-file-name hugo-base-dir)))
(apply 'call-process "hugo" nil hugo-buffer t args)))

(defun hugo-replace-key (key val)
(save-excursion
(goto-char (point-min))
;; quoted value
(if (and (re-search-forward (concat key " = \"") nil t)
           (re-search-forward "[^\"]+" (line-end-position) t))
    (or (replace-match val) t) ;; ensure we return t
  ;; unquoted value
  (when (and (re-search-forward (concat key " = ") nil t)
             (re-search-forward ".+" (line-end-position) t))
    (or (replace-match val) t)))))


;; publishing the blog
(defun hugo-publish ()
(interactive)
(let* ((default-directory (concat (expand-file-name hugo-base-dir) "/")))
(when (call-process "bash" nil hugo-buffer t  "./upload.sh")
  (message "Blog published"))))

;; undrafting post
(defun hugo-undraft ()
(interactive)
(when (and (hugo-replace-key "date" (iso-timestamp))
         (hugo-replace-key "draft" "false"))
(save-buffer)
(message "Removed draft status and updated timestamp")))

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

;; Running the Hugo dev server
(defun hugo-server (&optional arg)
(interactive "P")
(let* ((default-directory (concat (expand-file-name hugo-base-dir) "/"))
     (proc (get-buffer-process hugo-buffer)))
(if (and proc (process-live-p proc))
    (progn (interrupt-process proc)
           (message "Stopped Hugo server"))
  (start-process "hugo" hugo-buffer "hugo" "server" "--baseUrl=http://localhost/" "--buildDrafts" "--watch" "-d" "dev")
  (message "Started Hugo server")
  (unless arg
    (browse-url "http://localhost:1313/")))))

(define-key global-map "\C-ce" 'hugo-edit)
(define-key global-map "\C-cn" 'hugo-new-post)
(define-key global-map "\C-cs" 'hugo-server)
(define-key global-map "\C-cp" 'hugo-publish)
(define-key global-map "\C-cm" 'avy-menu-2)

自動化のためにupload.shを作る

各ブログフォルダのルート(config.tomlと同じ場所)にupload.shを作成しておく。

#!/bin/bash
cd {$PWD}

rm -rf public
hugo -d public

# gzip files so nginx can serve the compressed versions
find public \( -name '*.js' -or -name '*.css' -or -name '*.svg' -or -name '*.html' \) -exec gzip -k9 '{}' \;

rsync -rtpl --delete public/ -e 'ssh -p 2222' user@ip_addr:~/web/path/to/dir/
/

最下行rsyncコマンドのオプションは自分の環境に変えてくださいね。