Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Emacs lisp commands for generating and inserting partials.
;;; Support for creation and insertion of partials - v 0.1
;;; (c) 2013 Adolfo Villafiorita - Distributed under the MIT license
;;; Use it at your own risk.
(defun rails-region-to-partial (filename)
"Get current region and make it into a partial."
(interactive "FPartial file name: ")
(if (file-exists-p filename)
(message "File exists. It is a bad idea to overwrite it. Aborted.")
(write-region (region-beginning) (region-end) filename)
(delete-region (region-beginning) (region-end))
(insert (rails-partial-string filename) "\n\n" ))))
(defun rails-insert-partial (filename)
"Ask for a filename (of a partial) and insert a partial directive at point."
(interactive "fPartial filename: ")
(insert (rails-partial-string filename)))
(defun rails-partial-string (filename)
"Return a partial command string."
(concat "<%= render :partial => \""
(rails-filename-extract-partial filename)
"\", :locals => {} %>"))
(defun rails-filename-extract-partial (filename)
"Get a full filename and make it into a partial's name.
For instance:
- input: /a/b/c/_e.html.erb
- output: c/e"
(file-relative-name filename
(concat (file-name-as-directory filename) "../..")))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment