My first elisp functions: image processing and blog deployment

I wrote some elisp functions to speed up my blog posting! I realized that emacs plays well with shell scripts, and that every time I add images and deploy a page to the blog, I have to go through a long dance of resizing images, changing folders, pushing to github, etc. So I thought, why not define a function I can call right from my editor to handle all of it? Without further ado, here are three functions to add photos and screenshots, and deploy the blog.

Image insertion

(defun blog-resize-insert-image ()
  "Quick piece of code to insert images into blog with appropriate resizing"
  (interactive)
  (let (fname commandName))
  (setq fname (ido-read-file-name "Enter file name:"))
  (setq commandName (concat "sips -Z 1500 " fname))
  (eshell-command commandName)
  (insert "#+attr_html: :width 600")
  (newline)
  (insert "[[file:" (file-relative-name fname) "]]")
  )

Screenshot

(defun blog-screenshot (&optional arg)
  "Take a screenshot and insert org link.
with prefix arg, minimize emacs first.
Resizes image and adds link to blog.
Only works on Mac OSX."
  (interactive "P")
  (when arg
    (suspend-frame))

  (unless (f-directory? "screenshots")
    (make-directory "screenshots"))
  (sit-for 0.2)

  (let ((fname (concat (format-time-string "date-%d-%m-%Y-time-%H-%M-%S" (current-time)) ".png")))
    (do-applescript
     (mapconcat
      'identity
      (list (format "set screenshotFilePath to \"%s\"" (expand-file-name fname "screenshots"))
	"do shell script \"screencapture \" & \"-s\" & \" \" & quoted form of screenshotFilePath"
	(concat "set result to \"[[./" fname "]]\"")
	"set the clipboard to result")
      "\n"))
    (insert (format "#+ATTR_HTML: :width 800px\n#+ATTR_ORG: :width 500\n[[file:./%s]]" (concat "screenshots/" fname)))
    (raise-frame)))

Example:

date-02-09-2018-time-09-20-57.png

Blog deployment

(defun blog-deploy ()
  (interactive)
  (eshell-command "cd /Users/kubo/Dropbox/org/Hugo/blog && ./deploy.sh"))

Here’s what ./deploy.sh looks like:

#!/bin/bash

echo -e "\033[0;32mDeploying updates to GitHub...\033[0m"

cp ./public/.git ./gitbackup
# Build the project.
hugo --cleanDestinationDir # if using a theme, replace with `hugo -t <YOURTHEME>`
cp ./gitbackup ./public/.git
rm ./gitbackup
echo "git restored"
# Go To Public folder
cd public
# Add changes to git.
git add .

# Commit changes.
msg="rebuilding site `date`"
if [ $# -eq 1 ]
  then msg="$1"
fi
git commit -m "$msg"

# Push source and build repos.
git push origin master

# Come Back up to the Project Root
cd ..
 
comments powered by Disqus