* Assets Packing
@ 2011-05-27 13:53 Sander Boer
0 siblings, 0 replies; only message in thread
From: Sander Boer @ 2011-05-27 13:53 UTC (permalink / raw)
To: emacs-orgmode
Hi all,
Maybe this already exists in org, but I needed a function that looks at
all the image links, signals if the link is broken or not, copies the
files into a folder and relinks it.
Comments and crits are greatly appreciated, I started coding elisp 2
weeks ago, so any and all pointers and tips are welcome.
-------8<------------------------------------- cut here
(defcustom sndr-assets-dir nil
"the directory org linked assets are moved to, do not forget trailing
slash..."
:type '(string)
:group'sndr
)
(defun sndr-assets-package()
"finds file links and moves them to a dir specified, or ./assets ,
relative to the org file"
(interactive)
(let (p0 p1)
(save-excursion
(goto-char (point-min))
(while (search-forward-regexp
"\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]" nil t)
(match-beginning 1)
(setq p0 (match-beginning 1))
(setq p1 (match-end 1))
(sndr-link-check-and-move p0 p1)
)
)))
(defun sndr-link-check-and-move (p0 p1)
"gets a buffer section, checks if it is a file, moves the file to a
dir specified (or ./assets) and updates the buffer accordingly"
(let (file newdir newfile)
(setq file (buffer-substring-no-properties p0 p1 ))
(file-exists-p file)
(setq newdir (or sndr-assets-dir (concat (file-name-directory
buffer-file-name) "assets/")))
(setq newfile (concat newdir(file-name-nondirectory file)))
(if (not (file-exists-p newdir))
(make-directory newdir t))
(if (file-exists-p file)
(progn
(if (not(file-exists-p newfile)) (copy-file file newfile))
(goto-char p0)
(delete-region p0 p1)
(insert newfile)
)
(progn
(goto-char p0)
(delete-region p0 p1)
(insert (concat "BROKEN#" file "#BROKEN"))
))))
-------8<------------------------------------- cut here
sander.
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2011-05-27 13:53 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-27 13:53 Assets Packing Sander Boer
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).