Skip to content

Instantly share code, notes, and snippets.

Created August 25, 2011 19:30
Show Gist options
  • Save scharan/1171592 to your computer and use it in GitHub Desktop.
Save scharan/1171592 to your computer and use it in GitHub Desktop.
Now adds a date: field when publishing (Original:
;; 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)
(while (re-search-forward "\n---\n" nil t) ; Find the end of YAML header
(replace-match (concat "\ndate: " (format-time-string "%Y-%m-%d %H:%M:%S") "\n---\n")) )
(set-window-point (selected-window) old-point)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment