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"
  (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")
  (insert "[[file:" (file-relative-name fname) "]]")


(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

  (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")))
      (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")
    (insert (format "#+ATTR_HTML: :width 800px\n#+ATTR_ORG: :width 500\n[[file:./%s]]" (concat "screenshots/" fname)))



Blog deployment

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

Here’s what ./ looks like:


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"
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