Created
November 21, 2010 20:53
-
-
Save lovett/709138 to your computer and use it in GitHub Desktop.
Figure out the relative path from the current buffer to the specified file; Create an image tag from the specified file path
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
(defun relpath (file &optional skipinsert) | |
"Figure out the relative path from the current buffer to the specified file""" | |
(interactive "FPath: ") | |
(let* ( | |
(basePath | |
(file-name-directory | |
(expand-file-name | |
(buffer-file-name (current-buffer))))) | |
(targetPath | |
(expand-file-name file)) | |
(basePathSegments | |
(split-string basePath "/")) | |
(targetPathSegments | |
(split-string targetPath "/")) | |
segment1 | |
segment2 | |
) | |
(while basePathSegments | |
(setq segment1 (pop basePathSegments)) | |
(setq segment2 (pop targetPathSegments)) | |
(unless (equal segment1 segment2) | |
(progn | |
(push segment2 targetPathSegments) | |
(if (> (length basePathSegments) 0) | |
(push ".." targetPathSegments))) | |
)) | |
(setq targetPath (mapconcat 'identity targetPathSegments "/")) | |
(if (eq nil skipinsert) | |
(insert targetPath) | |
targetPath))) | |
(defun img-tag (file alt) | |
"Create an image tag from the specified file path" | |
(interactive "FPath: \nsAlt text: ") | |
(let ((image-size | |
(let ((ximg (create-image file))) | |
(image-size ximg t))) | |
(relative-path (relpath file t))) | |
(insert "<img src=\"" relative-path "\" " | |
"alt=\"" alt "\" " | |
"width=\"" (number-to-string (car image-size)) | |
"\" " | |
"height=\"" (number-to-string (cdr image-size)) | |
"\" />"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment