[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[O] Assets Packing
From: |
Sander Boer |
Subject: |
[O] Assets Packing |
Date: |
Fri, 27 May 2011 15:53:27 +0200 |
User-agent: |
Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.9) Gecko/20100915 Thunderbird/3.1.4 |
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.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [O] Assets Packing,
Sander Boer <=