Find file
Fetching contributors…
Cannot retrieve contributors at this time
88 lines (80 sloc) 2.96 KB
;; jekyll.el
;; Emacs support for Jekyll blogs.
;; To use, just put this file somewhere in the load path and
;; (require 'jekyll)
;; Here are my key bindings:
;; C-c b d - Show all drafts
;; C-c b p - Show all posts
;; C-c b n - Create new draft
;; C-c b P - Publish current draft
;; (global-set-key (kbd "C-c b n") 'jekyll-draft-post)
;; (global-set-key (kbd "C-c b P") 'jekyll-publish-post)
;; (global-set-key (kbd "C-c b p") (lambda ()
;; (interactive)
;; (find-file "~/Sources/blog/_posts/")))
;; (global-set-key (kbd "C-c b d") (lambda ()
;; (interactive)
;; (find-file "~/Sources/blog/_drafts/")))
(provide 'jekyll)
(defvar jekyll-directory nil
"Path to Jekyll blog.")
(defvar jekyll-drafts-dir "_drafts/"
"Relative path to drafts directory.")
(defvar jekyll-posts-dir "_posts/"
"Relative path to posts directory.")
(defvar jekyll-post-ext ".markdown"
"File extension of Jekyll posts.")
(defvar jekyll-post-template
"---\ntitle: %s\n---\n\n"
"Default template for Jekyll posts. %s will be replace by the post title.")
(defun jekyll-make-slug (s)
"Turn a string into a slug."
" " "-" (downcase
"[^A-Za-z0-9 ]" "" s))))
(defun jekyll-yaml-escape (s)
"Escape a string for YAML."
(if (or (string-match ":" s)
(string-match "\"" s))
(concat "\"" (replace-regexp-in-string "\"" "\\\\\"" s) "\"")
(defun jekyll-draft-post (title)
"Create a new Jekyll blog post."
(interactive "sPost Title: ")
(let ((draft-file (concat jekyll-directory jekyll-drafts-dir
(jekyll-make-slug title)
(if (file-exists-p draft-file)
(find-file draft-file)
(find-file draft-file)
(insert (format jekyll-post-template (jekyll-yaml-escape title))))))
(defun jekyll-publish-post ()
"Move a draft post to the posts directory, and rename it so that it
contains the date."
((not (equal
(file-name-directory (buffer-file-name (current-buffer)))
(concat jekyll-directory jekyll-drafts-dir)))
(message "This is not a draft post.")
(insert (file-name-directory (buffer-file-name (current-buffer))) "\n"
(concat jekyll-directory jekyll-drafts-dir)))
(message "Can't publish post; buffer has modifications."))
(let ((filename
(concat jekyll-directory jekyll-posts-dir
(format-time-string "%Y-%m-%d-")
(buffer-file-name (current-buffer)))))
(old-point (point)))
(rename-file (buffer-file-name (current-buffer))
(kill-buffer nil)
(find-file filename)
(set-window-point (selected-window) old-point)))))