Skip to content

Commit c5ef698

Browse files
committed
tweak(io): code refactoring
1 parent f949b46 commit c5ef698

File tree

1 file changed

+49
-50
lines changed

1 file changed

+49
-50
lines changed

elisp/+io.el

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
"Get MIME type for FILE based on magic codes provided by the 'file' command.
1414
Return a symbol of the MIME type, ex: `text/x-lisp', `text/plain',
1515
`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))
1919
(error "The \"file\" command isn't installed.")))
2020

2121
;;;###autoload
@@ -27,7 +27,7 @@ If \"file.ext\" exists, returns \"file-0.ext\"."
2727
(file (file-name-base filename))
2828
(filename-regex (concat "^" file "\\(?:-\\(?1:[[:digit:]]+\\)\\)?" (if ext (concat "\\." ext) "")))
2929
(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)))
3131
(num (1+ (string-to-number (or last-file-num "-1")))))
3232
(file-name-concat dir (format "%s%s%s" file (if last-file (format "-%d" num) "") (if ext (concat "." ext) "")))))
3333

@@ -190,48 +190,45 @@ If FORCE-P, overwrite the destination file if it exists, without confirmation."
190190
"Convert HTML file INFILE to PDF and save it to OUTFILE.
191191
When BACKEND is provided, the corresponding program is used, otherwise, the
192192
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)))
225224

226225
;;;###autoload
227226
(defun +txt2html (infile outfile &optional mail-mode-p)
228227
"Convert plain-text file INFILE to HTML and save it to OUTFILE.
229228
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))))
235232

236233
(defvar +save-as-pdf-filename nil
237234
"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."
263260
(user-error
264261
(if (file-exists-p outfile)
265262
"PDF created but with some errors!"
266-
"An error occured, cannot create the PDF!")))))
263+
"An error occurred, cannot create the PDF!")))))
267264

268265
;;;###autoload
269266
(defcustom +single-file-executable (executable-find "single-file")
@@ -272,13 +269,15 @@ When MAIL-MODE-P is non-nil, treat INFILE as a mail."
272269
;;;###autoload
273270
(defun +single-file (url out-file)
274271
"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+
283282

284283
;;; +io.el ends here

0 commit comments

Comments
 (0)