-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Use regular comment "# ()convertfrompdf:t" instead of using "#+HEADER" to mark file references that need to be converted. Ref: http://lists.gnu.org/archive/html/emacs-orgmode/2017-01/msg00260.html - Update MWE and example output HTML - Improve code - Use org-bracket-link-regexp - Add user-error if convert binary is not found - Optimize the let form - Improve org-include-img-from-pdf docstring
- Loading branch information
1 parent
efd165c
commit db69815
Showing
4 changed files
with
67 additions
and
64 deletions.
There are no files selected for viewing
25 changes: 12 additions & 13 deletions
25
elisp/org-include-img-from-pdf/org-include-img-from-pdf-mwe.html
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
20 changes: 9 additions & 11 deletions
20
elisp/org-include-img-from-pdf/org-include-img-from-pdf-mwe.org
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 40 additions & 34 deletions
74
elisp/org-include-img-from-pdf/org-include-img-from-pdf.el
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,55 @@ | ||
;; Time-stamp: <2016-09-09 11:10:20 kmodi> | ||
;; Time-stamp: <2017-04-04 23:03:47 kmodi> | ||
|
||
;; http://emacs.stackexchange.com/a/401/115 | ||
|
||
;; How to use this package: | ||
;; | ||
;; ;; Execute `modi/org-include-img-from-pdf' before saving the file. | ||
;; (defun modi/org-include-img-from-pdf-before-save () | ||
;; "Execute `modi/org-include-img-from-pdf' just before saving the file." | ||
;; (add-hook 'before-save-hook #'modi/org-include-img-from-pdf nil :local)) | ||
;; (add-hook 'org-mode-hook #'modi/org-include-img-from-pdf-before-save) | ||
;; ;; Execute `modi/org-include-img-from-pdf' before exporting. | ||
;; ;; Execute `org-include-img-from-pdf' before saving the file. | ||
;; (defun my/org-include-img-from-pdf-before-save () | ||
;; "Execute `org-include-img-from-pdf' just before saving the file." | ||
;; (add-hook 'before-save-hook #'org-include-img-from-pdf nil :local)) | ||
;; (add-hook 'org-mode-hook #'my/org-include-img-from-pdf-before-save) | ||
;; ;; Execute `org-include-img-from-pdf' before exporting. | ||
;; (with-eval-after-load 'ox | ||
;; (add-hook 'org-export-before-processing-hook #'modi/org-include-img-from-pdf)) | ||
;; (add-hook 'org-export-before-processing-hook #'org-include-img-from-pdf)) | ||
|
||
;;;###autoload | ||
(defun modi/org-include-img-from-pdf (&rest _) | ||
"Convert the pdf files to image files. | ||
(defun org-include-img-from-pdf (&rest _) | ||
"Convert pdf files to image files in org-mode bracket links. | ||
Only looks at #+HEADER: lines that have \":convertfrompdf t\"." | ||
# ()convertfrompdf:t # This is a special comment; tells that the upcoming | ||
# link points to the to-be-converted-to file. | ||
# If you have a foo.pdf that you need to convert to foo.png, use the | ||
# foo.png file name in the link. | ||
[[./foo.png]] | ||
" | ||
(interactive) | ||
(when (derived-mode-p 'org-mode) | ||
(save-excursion | ||
(goto-char (point-min)) | ||
(while (search-forward-regexp | ||
"^\\s-*#\\+HEADER:.*\\s-:convertfrompdf\\s-+t" | ||
nil 'noerror) | ||
(let* (filenoext imgext imgfile pdffile cmd) | ||
;; Keep on going on to the next line till it finds a line with | ||
;; `[[FILE]]' | ||
(if (executable-find "convert") | ||
(save-excursion | ||
(goto-char (point-min)) | ||
(while (re-search-forward "^[ \t]*#\\s-+()convertfrompdf\\s-*:\\s-*t" | ||
nil :noerror) | ||
;; Keep on going to the next line till it finds a line with bracketed | ||
;; file link. | ||
(while (progn | ||
(forward-line 1) | ||
(not (looking-at "\\[\\[\\(.*\\)\\.\\(.*\\)\\]\\]")))) | ||
(when (looking-at "\\[\\[\\(.*\\)\\.\\(.*\\)\\]\\]") | ||
(setq filenoext (match-string-no-properties 1)) | ||
(setq imgext (match-string-no-properties 2)) | ||
(setq imgfile (expand-file-name (concat filenoext "." imgext))) | ||
(setq pdffile (expand-file-name (concat filenoext "." "pdf"))) | ||
(setq cmd (concat "convert -density 96 -quality 85 " | ||
pdffile " " imgfile)) | ||
(when (file-newer-than-file-p pdffile imgfile) | ||
;; This block is executed only if pdffile is newer than imgfile | ||
;; or if imgfile does not exist | ||
;; https://www.gnu.org/software/emacs/manual/html_node/elisp/Testing-Accessibility.html | ||
(message "%s" cmd) | ||
(shell-command cmd)))))))) | ||
(not (looking-at org-bracket-link-regexp)))) | ||
;; Get the sub-group 1 match, the link, from `org-bracket-link-regexp' | ||
(let ((link (match-string-no-properties 1))) | ||
(when (stringp link) | ||
(let* ((imgfile (expand-file-name link)) | ||
(pdffile (expand-file-name | ||
(concat (file-name-sans-extension imgfile) | ||
"." "pdf"))) | ||
(cmd (concat "convert -density 96 -quality 85 " | ||
pdffile " " imgfile))) | ||
(when (and (file-readable-p pdffile) | ||
(file-newer-than-file-p pdffile imgfile)) | ||
;; This block is executed only if pdffile is newer than | ||
;; imgfile or if imgfile does not exist. | ||
(shell-command cmd) | ||
(message "%s" cmd))))))) | ||
(user-error "`convert' executable (part of Imagemagick) is not found"))) | ||
|
||
|
||
(provide 'org-include-img-from-pdf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters