13
13
" Get MIME type for FILE based on magic codes provided by the 'file' command.
14
14
Return a symbol of the MIME type, ex: `text/x-lisp' , `text/plain' ,
15
15
`application/x-object' , `application/octet-stream' , etc."
16
- (if ( executable-find " file" )
17
- ( let (( mime-type (shell-command-to-string (format " file --brief --mime-type %s " file))))
18
- (intern (string-trim-right mime-type) ))
16
+ (if-let ((file-cmd ( executable-find " file" ) )
17
+ ( mime-type (shell-command-to-string (format " %s --brief --mime-type %s " file-cmd file))))
18
+ (intern (string-trim-right mime-type))
19
19
(error " The \" file\" command isn't installed. " )))
20
20
21
21
;;;### autoload
@@ -27,7 +27,7 @@ If \"file.ext\" exists, returns \"file-0.ext\"."
27
27
(file (file-name-base filename))
28
28
(filename-regex (concat " ^" file " \\ (?:-\\ (?1:[[:digit:]]+\\ )\\ )?" (if ext (concat " \\ ." ext) " " )))
29
29
(last-file (car (last (directory-files dir nil filename-regex))))
30
- (last-file-num (when ( and last-file (string-match filename-regex last-file) (match-string 1 last-file) )))
30
+ (last-file-num (and last-file (string-match filename-regex last-file) (match-string 1 last-file)))
31
31
(num (1+ (string-to-number (or last-file-num " -1" )))))
32
32
(file-name-concat dir (format " %s%s%s " file (if last-file (format " -%d " num) " " ) (if ext (concat " ." ext) " " )))))
33
33
@@ -190,48 +190,45 @@ If FORCE-P, overwrite the destination file if it exists, without confirmation."
190
190
" Convert HTML file INFILE to PDF and save it to OUTFILE.
191
191
When BACKEND is provided, the corresponding program is used, otherwise, the
192
192
value of `+html2pdf-default-backend' is used."
193
- (let ((default-directory (file-name-directory infile))
194
- (backend (or backend +html2pdf-default-backend)))
195
- (pcase backend
196
- ('weasyprint
197
- (call-process
198
- " weasyprint" nil nil nil
199
- " --encoding" " utf-8"
200
- " --stylesheet" (expand-file-name " templates/weasyprint-pdf.css" minemacs-assets-dir)
201
- infile outfile))
202
- ('htmldoc
203
- (call-process
204
- " htmldoc" nil nil nil
205
- " --charset" " utf-8"
206
- " --bodyfont" " sans" " --textfont" " sans" " --headfootfont" " sans"
207
- " --top" " 10#mm" " --bottom" " 10#mm" " --right" " 10#mm" " --left" " 10#mm"
208
- " --fontsize" " 11"
209
- " --size" " a4"
210
- " --continuous"
211
- " --outfile" outfile infile))
212
- ('wkhtmltopdf
213
- (call-process
214
- " wkhtmltopdf" nil nil nil
215
- " --images" " --disable-javascript" " --enable-local-file-access"
216
- " --encoding" " utf-8"
217
- infile outfile))
218
- ('pandoc+context
219
- (call-process
220
- " pandoc" nil nil nil
221
- " --pdf-engine=context"
222
- " --variable" " fontsize=10pt"
223
- " --variable" " linkstyle=slanted"
224
- " -o" outfile infile)))))
193
+ (if-let ((default-directory (file-name-directory infile))
194
+ (backend (or backend +html2pdf-default-backend))
195
+ (backend-command
196
+ (pcase backend
197
+ ('weasyprint
198
+ (list " weasyprint"
199
+ " --encoding" " utf-8"
200
+ " --stylesheet" (expand-file-name " templates/weasyprint-pdf.css" minemacs-assets-dir)
201
+ infile outfile))
202
+ ('htmldoc
203
+ (list " htmldoc"
204
+ " --charset" " utf-8"
205
+ " --bodyfont" " sans" " --textfont" " sans" " --headfootfont" " sans"
206
+ " --top" " 10#mm" " --bottom" " 10#mm" " --right" " 10#mm" " --left" " 10#mm"
207
+ " --fontsize" " 11"
208
+ " --size" " a4"
209
+ " --continuous"
210
+ " --outfile" outfile infile))
211
+ ('wkhtmltopdf
212
+ (list " wkhtmltopdf"
213
+ " --images" " --disable-javascript" " --enable-local-file-access"
214
+ " --encoding" " utf-8"
215
+ infile outfile))
216
+ ('pandoc+context
217
+ (list " pandoc"
218
+ " --pdf-engine=context"
219
+ " --variable" " fontsize=10pt"
220
+ " --variable" " linkstyle=slanted"
221
+ " -o" outfile infile)))))
222
+ (apply #'call-process (append (list (car backend-command) nil nil nil ) (cdr backend-command)))
223
+ (user-error " Backend \" %s\" not available." backend)))
225
224
226
225
;;;### autoload
227
226
(defun +txt2html (infile outfile &optional mail-mode-p)
228
227
" Convert plain-text file INFILE to HTML and save it to OUTFILE.
229
228
When MAIL-MODE-P is non-nil, --mailmode is passed to \" txt2html\" ."
230
- (apply
231
- #'call-process
232
- (append '(" txt2html" nil nil nil " -8" )
233
- (when mail-mode-p '(" --mailmode" ))
234
- (list " --outfile" outfile infile))))
229
+ (apply #'call-process (append '(" txt2html" nil nil nil " -8" )
230
+ (when mail-mode-p '(" --mailmode" ))
231
+ (list " --outfile" outfile infile))))
235
232
236
233
(defvar +save-as-pdf-filename nil
237
234
" File name to use, if non-nil, for the output file." )
@@ -263,7 +260,7 @@ When MAIL-MODE-P is non-nil, treat INFILE as a mail."
263
260
(user-error
264
261
(if (file-exists-p outfile)
265
262
" PDF created but with some errors!"
266
- " An error occured , cannot create the PDF!" )))))
263
+ " An error occurred , cannot create the PDF!" )))))
267
264
268
265
;;;### autoload
269
266
(defcustom +single-file-executable (executable-find " single-file" )
@@ -272,13 +269,15 @@ When MAIL-MODE-P is non-nil, treat INFILE as a mail."
272
269
;;;### autoload
273
270
(defun +single-file (url out-file)
274
271
" Save URL into OUT-FILE as a standalone HTML file."
275
- (when +single-file-executable
276
- (make-process
277
- :name " single-file-cli"
278
- :buffer " *single-file*"
279
- :command (list
280
- +single-file-executable
281
- " --browser-executable-path" browse-url-chromium-program
282
- url out-file))))
272
+ (if (and +single-file-executable (executable-find +single-file-executable))
273
+ (make-process
274
+ :name " single-file-cli"
275
+ :buffer " *single-file*"
276
+ :command (list
277
+ +single-file-executable
278
+ " --browser-executable-path" browse-url-chromium-program
279
+ url out-file))
280
+ (user-error " Please set `+single-file-executable' accordingly." )))
281
+
283
282
284
283
; ;; +io.el ends here
0 commit comments