Skip to content

Commit

Permalink
Re-write org-include-img-from-pdf
Browse files Browse the repository at this point in the history
- 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
kaushalmodi committed Apr 5, 2017
1 parent efd165c commit db69815
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 64 deletions.
25 changes: 12 additions & 13 deletions 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.

20 changes: 9 additions & 11 deletions elisp/org-include-img-from-pdf/org-include-img-from-pdf-mwe.org
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
#+TITLE: PDF Image
#+NAME: fig:unicorn
# Below HEADER is required where the pdf version of the referenced png file exists.
#+HEADER: :convertfrompdf t
# The below caption line is optional
#+CAPTION: Org-mode Unicorn Logo
# ()convertfrompdf:t # This is a special comment; tells that the upcoming
# link points to a to-be-converted-to file.
# If you have a foo.pdf that you need to convert to foo.png, mention the
# to-be-converted-to name in the file link.
[[./org-mode-unicorn-logo.png]]

1. Save =org-include-img-from-pdf.el= to your =~/.emacs.d/elisp/org-include-img-from-pdf=.
2. Add below to your emacs setup and evaluate it.
#+BEGIN_SRC emacs-lisp
(use-package org-include-img-from-pdf
:load-path "elisp/org-include-img-from-pdf"
:config
(progn
(with-eval-after-load 'ox
(add-hook 'org-export-before-processing-hook #'modi/org-include-img-from-pdf))))
:load-path "elisp/org-include-img-from-pdf"
:config
(progn
(with-eval-after-load 'ox
(add-hook 'org-export-before-processing-hook #'modi/org-include-img-from-pdf))))
#+END_SRC
3. Have =org-mode-unicorn-logo.pdf= file in the same directory as this one.
4. Do =C-c C-e h o=.
Expand Down
74 changes: 40 additions & 34 deletions elisp/org-include-img-from-pdf/org-include-img-from-pdf.el
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)
12 changes: 6 additions & 6 deletions setup-files/setup-org.el
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;; Time-stamp: <2017-04-04 17:15:39 kmodi>
;; Time-stamp: <2017-04-04 23:03:22 kmodi>
;; Hi-lock: (("\\(^;\\{3,\\}\\)\\( *.*\\)" (1 'org-hide prepend) (2 '(:inherit org-level-1 :height 1.3 :weight bold :overline t :underline t) prepend)))
;; Hi-Lock: end

Expand Down Expand Up @@ -611,14 +611,14 @@ the languages in `modi/ob-enabled-languages'."
:load-path "elisp/org-include-img-from-pdf"
:config
(progn
;; ;; Execute `modi/org-include-img-from-pdf' before saving the file.
;; ;; Execute `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))
;; "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 #'modi/org-include-img-from-pdf-before-save)
;; Execute `modi/org-include-img-from-pdf' before exporting.
;; 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))))

;;; Extract image from included .zip
;; Auto extract images from zip files when saving org files.
Expand Down

0 comments on commit db69815

Please sign in to comment.