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.