Created
February 16, 2013 20:11
-
-
Save johnmastro/4968519 to your computer and use it in GitHub Desktop.
Some code I've been playing around with to automate the process of of creating a new Octopress page or post, with appropriate metadata, from an Emacs buffer's content.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; -*- lexical-binding: t -*- | |
;; Some code I've been playing around with to automate the process of of | |
;; creating a new Octopress page or post, with appropriate metadata, from a | |
;; buffer's content. | |
;; The `octopress-rake`, `octopress-extract-filename`, and `octopress-new` | |
;; functions are derived from the octopress-emacs code here: | |
;; github.com/gfreezy/octopress-emacs | |
;; John Mastro, http://jbm.io | |
(defvar octopress-workdir (expand-file-name "~/code/jbm.io/") | |
"The root directory of the blog.") | |
(defun octopress-rake (command) | |
"Run Rake COMMAND and return its result output." | |
(let ((command-str | |
(format "bash -l -c 'eval \"$(rbenv init -)\" && cd %s && rake %s'" | |
octopress-workdir | |
command))) | |
(shell-command-to-string command-str))) | |
(defun octopress-extract-filename (command-result class) | |
"Extract the filename from Rake's COMMAND-RESULT." | |
(let ((regexp-str (format "Creating new %s: " class))) | |
(replace-regexp-in-string | |
regexp-str | |
"" | |
(cadr (reverse (split-string command-result "\n")))))) | |
(defun octopress-new (class title) | |
"Create a new document of CLASS with TITLE." | |
(let* ((command-str (format "new_%s[\"%s\"]" class title)) | |
(command-result (octopress-rake command-str)) | |
(filename (concat octopress-workdir | |
(octopress-extract-filename command-result class)))) | |
filename)) | |
(defun octopress-append-and-visit (filename) | |
"Append the current buffer's contents to FILENAME and visit it. | |
The title is not appended since it's a part of the metadata added | |
by Rake." | |
(save-excursion | |
(goto-char (point-min)) | |
(let ((text-beginning (search-forward-regexp "^[^#]"))) | |
(append-to-file "\n" nil filename) | |
(append-to-file text-beginning (point-max) filename))) | |
(find-file filename)) | |
(defun octopress-extract-title-from-buffer () | |
"Extract the post title from the current buffer and return it. | |
Assumes the title is on the first line and looks like a | |
Markdown/atx-style heading." | |
(save-excursion | |
(goto-char (point-min)) | |
(let* ((title-beginning (search-forward-regexp "^[# ]+")) | |
(title-end (search-forward-regexp "$")) | |
(title (buffer-substring-no-properties title-beginning title-end))) | |
title))) | |
(defun octopress-new-from-buffer (class) | |
"Create a new Octopress CLASS from the current buffer's contents." | |
(let* ((title (funcall octopress-extract-title-from-buffer)) | |
(filename (octopress-new class title))) | |
(octopress-append-and-visit filename))) | |
(defun octopress-new-post-from-buffer () | |
"Create a new Octopress post from the current buffer's contents." | |
(interactive) | |
(octopress-new-from-buffer "post")) | |
(defun octopress-new-page-from-buffer () | |
"Create a new Octopress page from the current buffer's contents." | |
(interactive) | |
(octopress-new-from-buffer "page")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment