Windows
注意:
- windows 自己有一个 convert.exe 命令
- 需要把自己安装的 convert 命令 在 PATH 中设置到前面
函数用途
org-download-image
- 把 url, file-path 存储到指定位置
- 插入 org image 链接
org-download–dir
- org-download–dir1 : org-download-image-dir
- org-download–dir2 : org-download-heading-lvl
org-download-annotate-function
- 注解文本制作,使用 url
- 记录下载时间
- eg: #+DOWNLOADED: …
org-download-insert-link (link, filename)
- 插入图片链接,和额外信息, eg: #+attr_html 等以 #+ 开头的内容
org-download-link-format-function (filename)
for markdown
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
| (defun my-markdown-download-clipboard (&optional basename)
"Capture the image from the clipboard and insert the resulting file."
(interactive)
(let ((org-download-screenshot-method
(cl-case system-type
(gnu/linux
(if (string= "wayland" (getenv "XDG_SESSION_TYPE"))
(if (executable-find "wl-paste")
"wl-paste -t image/png > %s"
(user-error
"Please install the \"wl-paste\" program included in wl-clipboard"))
(if (executable-find "xclip")
"xclip -selection clipboard -t image/png -o > %s"
(user-error
"Please install the \"xclip\" program"))))
((windows-nt cygwin)
(if (executable-find "convert")
"convert clipboard: %s"
(user-error
"Please install the \"convert\" program included in ImageMagick")))
((darwin berkeley-unix)
(if (executable-find "pngpaste")
"pngpaste %s"
(user-error
"Please install the \"pngpaste\" program from Homebrew."))))))
(message "%s" org-download-screenshot-method)
(my-markdown-download-screenshot basename)))
(defun my-markdown-download-screenshot (&optional basename)
"Capture screenshot and insert the resulting file.
The screenshot tool is determined by `org-download-screenshot-method'."
(interactive)
(let* ((screenshot-dir (file-name-directory org-download-screenshot-file))
(org-download-screenshot-file
(if basename
(concat screenshot-dir basename) org-download-screenshot-file)))
(make-directory screenshot-dir t)
(if (functionp org-download-screenshot-method)
(funcall org-download-screenshot-method
org-download-screenshot-file)
(shell-command-to-string
(format org-download-screenshot-method
org-download-screenshot-file)))
(when (file-exists-p org-download-screenshot-file)
(my-markdown-download-image org-download-screenshot-file)
(delete-file org-download-screenshot-file))))
(defun my-markdown-download-markdown-mode-p ()
"Return `t' if major-mode or derived-mode-p equals 'org-mode, otherwise `nil'."
(or (eq major-mode 'markdown-mode) (when (derived-mode-p 'markdown-mode) t)))
(defun my-markdown-download--dir ()
"Return the directory path for image storage.
The path is composed from `org-download--dir-1' and `org-download--dir-2'.
The directory is created if it didn't exist before."
(if (my-markdown-download-markdown-mode-p)
(let* ((dir my-markdown-download-image-dir)
)
(unless (file-exists-p dir)
(make-directory dir t))
dir)
default-directory))
(setq my-markdown-download-image-dir "./img/")
(defun my-markdown-download--fullname (link &optional ext)
"Return the file name where LINK will be saved to.
It's affected by `org-download--dir'.
EXT can hold the file extension, in case LINK doesn't provide it."
(let ((filename
(replace-regexp-in-string
"%20" " "
(file-name-nondirectory
(car (url-path-and-query
(url-generic-parse-url link))))))
(dir (my-markdown-download--dir)))
(when (string-match ".*?\\.\\(?:png\\|jpg\\)\\(.*\\)$" filename)
(setq filename (replace-match "" nil nil filename 1)))
(when ext
(setq filename (concat filename "." ext)))
(abbreviate-file-name
(expand-file-name
(funcall org-download-file-format-function filename)
dir))))
(defun my-markdown-download-image (link)
"Save image at address LINK to `org-download--dir'."
(interactive "sUrl: ")
(let* ((link-and-ext (org-download--parse-link link))
(org-download--dir #'my-markdown-download--dir)
(filename
(cond ((fboundp org-download-method)
(funcall org-download-method link))
(t
(apply #'my-markdown-download--fullname link-and-ext))))
)
(setq org-download-path-last-file filename)
(org-download--image link filename)
(when (my-markdown-download-markdown-mode-p)
(my-markdown-download-insert-link link filename))
(when (and (eq org-download-delete-image-after-download t)
(not (url-handler-file-remote-p (current-kill 0))))
(delete-file link delete-by-moving-to-trash))))
(defun my-markdown-download-insert-link (link filename)
(let* ((beg (point))
(line-beg (line-beginning-position))
(indent (- beg line-beg))
(in-item-p (org-in-item-p))
(escaped-filename (file-truename (org-link-escape filename)))
str)
;; (if (looking-back "^[ \t]+" line-beg)
;; (delete-region (match-beginning 0) (match-end 0))
;; (newline))
(insert (format "\n" (file-name-nondirectory filename)
(file-relative-name
escaped-filename
(file-truename (file-name-directory (buffer-file-name))))))
;; (insert (format ""
;; (org-link-escape
;; (funcall org-download-abbreviate-filename-function filename))))
(setq str (buffer-substring-no-properties line-beg (point)))
(when in-item-p
(indent-region line-beg (point) indent))
str))
(defun org-download--image (link filename)
"Save LINK to FILENAME asynchronously and show inline images in current buffer."
(when (string= "file" (url-type (url-generic-parse-url link)))
(setq link (url-unhex-string (url-filename (url-generic-parse-url link)))))
(cond ((and (not (file-remote-p link))
(file-exists-p link))
(copy-file link (expand-file-name filename)))
(org-download--file-content
(copy-file org-download--file-content (expand-file-name filename))
(setq org-download--file-content nil))
((eq org-download-backend t)
(org-download--image/url-retrieve link filename))
(t
(org-download--image/command org-download-backend link filename))))
|
文章作者
上次更新
2024-01-05
(5c92d1c)