| 1 | Upstream-status: https://github.com/dalanicolai/doc-tools-toc/pull/4
|
| 2 |
|
| 3 | diff --git a/doc-toc.el b/doc-toc.el
|
| 4 | index 8b7057c..64eca03 100644
|
| 5 | |
| 6 | |
| 7 | @@ -216,6 +216,7 @@
|
| 8 | (defvar pdf-filename)
|
| 9 |
|
| 10 | (declare-function pdf-cache-get-image "pdf-cache")
|
| 11 | +(declare-function pdf-view-active-region-text "pdf-view")
|
| 12 | (declare-function pdf-view-goto-page "pdf-view")
|
| 13 | (declare-function pdf-view-next-page "pdf-view")
|
| 14 | (declare-function pdf-view-previous-page "pdf-view")
|
| 15 | @@ -262,39 +263,60 @@ URL`http://handyoutlinerfo.sourceforge.net/'."
|
| 16 | String (i.e. surround with double quotes)."
|
| 17 | :type 'file)
|
| 18 |
|
| 19 | +(defvar doc-toc--ddjvu-program (executable-find "ddjvu"))
|
| 20 | +(defvar doc-toc--djvused-program (executable-find "djvused"))
|
| 21 | +(defvar doc-toc--djvutxt-program (executable-find "djvutxt"))
|
| 22 | +(defvar doc-toc--mutool-program (executable-find "mutool"))
|
| 23 | +(defvar doc-toc--pdfoutline-program (executable-find "pdfoutline"))
|
| 24 | +(defvar doc-toc--pdftocgen-program (executable-find "pdftocgen"))
|
| 25 | +(defvar doc-toc--pdftocio-program (executable-find "pdftocio"))
|
| 26 | +(defvar doc-toc--pdftotext-program (executable-find "pdftotext"))
|
| 27 | +(defvar doc-toc--pdfxmeta-program (executable-find "pdfxmeta"))
|
| 28 | +(defvar doc-toc--tesseract-program (executable-find "tesseract"))
|
| 29 | +
|
| 30 | +(defun doc-toc--process-to-string (program &rest args)
|
| 31 | + "Return the output of running PROGRAM with ARGS.
|
| 32 | +
|
| 33 | +Like `shell-command-to-string', but PROGRAM and ARGS are provided as
|
| 34 | +separate strings rather than a single space-separated, escaped string."
|
| 35 | + (with-temp-buffer
|
| 36 | + (apply #'call-process
|
| 37 | + program
|
| 38 | + nil
|
| 39 | + (list (current-buffer) nil)
|
| 40 | + nil
|
| 41 | + args)
|
| 42 | + (buffer-string)))
|
| 43 | +
|
| 44 | ;;;; pdf.tocgen
|
| 45 | ;;;###autoload
|
| 46 | (defun doc-toc-gen-set-level (level)
|
| 47 | - "Define the text properties of the heading level.
|
| 48 | -In a pdf-view buffer select a single word in the headline of a
|
| 49 | -certain level. Then run `doc-toc-gen-set-level' to write the text
|
| 50 | + "define the text properties of the heading level.
|
| 51 | +in a pdf-view buffer select a single word in the headline of a
|
| 52 | +certain level. then run `doc-toc-gen-set-level' to write the text
|
| 53 | properties to the recipe.toml file that is created in the
|
| 54 | -document's directory. You will be prompted to enter the LEVEL
|
| 55 | -number. The highest level should have number 1, the next level
|
| 56 | +document's directory. you will be prompted to enter the level
|
| 57 | +number. the highest level should have number 1, the next level
|
| 58 | number 2 etc."
|
| 59 | - (interactive "nWhich level you are setting (number): ")
|
| 60 | + (interactive "nwhich level you are setting (number): ")
|
| 61 | (let* ((page (pdf-view-current-page))
|
| 62 | (filename (url-filename (url-generic-parse-url buffer-file-name)))
|
| 63 | - (pdfxmeta-result (shell-command
|
| 64 | - (format "pdfxmeta --auto %s --page %s %s \"%s\" >> recipe.toml"
|
| 65 | - level
|
| 66 | - page
|
| 67 | - (shell-quote-argument filename)
|
| 68 | - (car (pdf-view-active-region-text))))))
|
| 69 | - ;; (pdfxmeta-result (call-process "pdfxmeta" nil "recipe.toml" nil
|
| 70 | - ;; "--auto" (number-to-string level)
|
| 71 | - ;; "--page" (number-to-string page)
|
| 72 | - ;; (shell-quote-argument filename)
|
| 73 | - ;; (concat "\"" (car (pdf-view-active-region-text)) "\""))))
|
| 74 | + (pdfxmeta-result (call-process doc-toc--pdfxmeta-program nil "recipe.toml" nil
|
| 75 | + "--auto" (number-to-string level)
|
| 76 | + "--page" (number-to-string page)
|
| 77 | + (shell-quote-argument filename)
|
| 78 | + (concat "\"" (car (pdf-view-active-region-text)) "\""))))
|
| 79 | (when (eq pdfxmeta-result 1)
|
| 80 | - (let ((page-text (shell-command-to-string
|
| 81 | - (format "mutool draw -F text %s %s"
|
| 82 | - (shell-quote-argument filename)
|
| 83 | - page
|
| 84 | - ))))
|
| 85 | + (let ((page-text (doc-toc--process-to-string
|
| 86 | + doc-toc--mutool-program
|
| 87 | + "draw"
|
| 88 | + "-f"
|
| 89 | + "text"
|
| 90 | + filename
|
| 91 | + page)))
|
| 92 | (pop-to-buffer "page-text")
|
| 93 | (insert
|
| 94 | - "COULD NOT SET HEADING LEVEL. MUPDF EXTRACTED FOLLOWING PAGE TEXT FROM PAGE:\n")
|
| 95 | + "could not set heading level. mupdf extracted following page text from page:\n")
|
| 96 | (add-text-properties 1 (point) '(face font-lock-warning-face))
|
| 97 | (let ((beg (point)))
|
| 98 | (insert "(try to select partial word)\n\n")
|
| 99 | @@ -312,8 +334,11 @@ be used after the headline text properties have been defined with
|
| 100 | the function `doc-toc-gen-set-level'"
|
| 101 | (interactive)
|
| 102 | (let ((filename buffer-file-name)
|
| 103 | - (toc (shell-command-to-string
|
| 104 | - (format "pdftocgen %s < recipe.toml" (shell-quote-argument buffer-file-name)))))
|
| 105 | + (toc (doc-toc--process-to-string
|
| 106 | + doc-toc--pdftocgen-program
|
| 107 | + buffer-file-name
|
| 108 | + "-r"
|
| 109 | + "recipe.toml")))
|
| 110 | (switch-to-buffer "toc")
|
| 111 | (doc-toc-pdftocgen-mode) ;; required before setting local variable
|
| 112 | (when (fboundp 'flyspell-mode)
|
| 113 | @@ -328,7 +353,8 @@ named output.pdf and opened in a new buffer. Don't forget to
|
| 114 | rename this new file."
|
| 115 | (interactive)
|
| 116 | (let* ((output-buf (get-buffer-create "*pdftocio-output*")))
|
| 117 | - (call-process-region (point-min) (point-max) "pdftocio"
|
| 118 | + (call-process-region (point-min) (point-max)
|
| 119 | + doc-toc--pdftocio-program
|
| 120 | nil output-buf nil pdf-filename)
|
| 121 | (kill-buffer-if-not-modified (find-file pdf-filename))
|
| 122 | (when (file-exists-p (concat (file-name-base pdf-filename) "_out.pdf"))
|
| 123 | @@ -483,14 +509,19 @@ ARG (\\[universal-argument]) to enter different separators."
|
| 124 | (default-process-coding-system
|
| 125 | (cond ((string= ".pdf" ext)'(windows-1252-unix . utf-8-unix))
|
| 126 | ((string= ".djvu" ext) '(utf-8-unix . utf-8-unix))))
|
| 127 | - (shell-command (cond ((string= ".pdf" ext) "pdftotext -f %s -l %s -layout %s -")
|
| 128 | - ((string= ".djvu" ext) "djvutxt --page=%s-%s %s")
|
| 129 | - (t (error "Buffer-filename does not have pdf or djvu extension"))))
|
| 130 | - (text (shell-command-to-string
|
| 131 | - (format shell-command
|
| 132 | - startpage
|
| 133 | - endpage
|
| 134 | - (shell-quote-argument buffer-file-name))))
|
| 135 | + (text (cond ((string= ".pdf" ext)
|
| 136 | + (doc-toc--process-to-string
|
| 137 | + doc-toc--pdftotext-program
|
| 138 | + "-f" startpage
|
| 139 | + "-l" endpage
|
| 140 | + "-layout" buffer-file-name
|
| 141 | + "-"))
|
| 142 | + ((string= ".djvu" ext)
|
| 143 | + (doc-toc--process-to-string
|
| 144 | + doc-toc--djvutxt-program
|
| 145 | + (format "--page=%s-%s" startpage endpage)
|
| 146 | + buffer-file-name))
|
| 147 | + (t (error "Buffer-filename does not have pdf or djvu extension"))))
|
| 148 | (buffer (get-buffer-create (file-name-sans-extension (buffer-name)))))
|
| 149 | (switch-to-buffer buffer)
|
| 150 | (doc-toc-cleanup-mode) ;; required before setting local variable
|
| 151 | @@ -522,7 +553,8 @@ For use in `doc-toc-ocr-languages'."
|
| 152 | (let ((print-length nil))
|
| 153 | (message (format "%s" (seq-subseq
|
| 154 | (split-string
|
| 155 | - (shell-command-to-string "tesseract --list-langs"))
|
| 156 | + (doc-toc--process-to-string doc-toc--tesseract-program
|
| 157 | + "--list-langs"))
|
| 158 | 5)))))
|
| 159 |
|
| 160 | ;;;###autoload
|
| 161 | @@ -557,7 +589,8 @@ unprocessed text."
|
| 162 | ((string= ".djvu" ext)
|
| 163 | ;; new code for djvu3
|
| 164 | (let ((outfile (format "/tmp/pageimagep%s" page)))
|
| 165 | - (shell-command (format "ddjvu -page=%s '%s' %s"
|
| 166 | + (shell-command (format "%s -page=%s '%s' %s"
|
| 167 | + doc-toc--ddjvu-program
|
| 168 | page
|
| 169 | buffer-file-name
|
| 170 | outfile))
|
| 171 | @@ -569,8 +602,12 @@ unprocessed text."
|
| 172 | ;; (number-to-string page)
|
| 173 | ;; (image-property djvu-doc-image :data))))))
|
| 174 | (apply #'call-process
|
| 175 | - (append (list "tesseract" nil (list buffer nil) nil file)
|
| 176 | - args))
|
| 177 | + doc-toc--tesseract-program
|
| 178 | + nil
|
| 179 | + (list buffer nil)
|
| 180 | + nil
|
| 181 | + file
|
| 182 | + args)
|
| 183 | (setq page (1+ page))))
|
| 184 | (switch-to-buffer buffer)
|
| 185 | (doc-toc-cleanup-mode) ;; required before setting local variable
|
| 186 | @@ -587,14 +624,20 @@ unprocessed text."
|
| 187 | (interactive)
|
| 188 | (let* ((source-buffer (current-buffer))
|
| 189 | (ext (url-file-extension (buffer-file-name (current-buffer))))
|
| 190 | - (shell-command (cond ((string= ".pdf" ext) (if (executable-find "mutool")
|
| 191 | - "mutool show %s outline"
|
| 192 | - "mutool command is not found"))
|
| 193 | - ((string= ".djvu" ext) "djvused -e 'print-outline' %s")
|
| 194 | - (t (error "Buffer-filename does not have pdf or djvu extension"))))
|
| 195 | - (text (shell-command-to-string
|
| 196 | - (format shell-command
|
| 197 | - (shell-quote-argument buffer-file-name))))
|
| 198 | +
|
| 199 | + (text (cond ((string= ".pdf" ext)
|
| 200 | + (unless doc-toc--mutool-program
|
| 201 | + (error "Command mutool is not found"))
|
| 202 | + (doc-toc--process-to-string doc-toc--mutool-program
|
| 203 | + "show"
|
| 204 | + buffer-file-name
|
| 205 | + "outline"))
|
| 206 | + ((string= ".djvu" ext)
|
| 207 | + (doc-toc--process-to-string doc-toc--djvused-program
|
| 208 | + "-e"
|
| 209 | + "print-outline"
|
| 210 | + buffer-file-name))
|
| 211 | + (t (error "Buffer-filename does not have pdf or djvu extension"))))
|
| 212 | (buffer (get-buffer-create (concat (file-name-sans-extension (buffer-name)) ".txt"))))
|
| 213 | (switch-to-buffer buffer)
|
| 214 | (setq-local doc-buffer source-buffer)
|
| 215 | @@ -628,7 +671,7 @@ Prompt for startpage and endpage and print OCR output to new buffer."
|
| 216 | nil
|
| 217 | (number-to-string page)
|
| 218 | (image-property djvu-doc-image :data))))))
|
| 219 | - (apply #'call-process "tesseract" nil (list buffer nil) nil
|
| 220 | + (apply #'call-process doc-toc--tesseract-program nil (list buffer nil) nil
|
| 221 | file args)
|
| 222 | (setq page (1+ page))))
|
| 223 | (switch-to-buffer buffer)))))
|
| 224 | @@ -992,7 +1035,7 @@ to `pdfoutline' shell command."
|
| 225 | This command uses the shell program `pdfoutline'."
|
| 226 | (interactive)
|
| 227 | (save-buffer)
|
| 228 | - (call-process "pdfoutline" nil "*pdfoutline*" nil
|
| 229 | + (call-process doc-toc--pdfoutline-program nil "*pdfoutline*" nil
|
| 230 | (concat (file-name-sans-extension (buffer-name)) ".pdf")
|
| 231 | (buffer-name)
|
| 232 | (if doc-toc-replace-original-file
|
| 233 | @@ -1009,12 +1052,13 @@ This command uses the shell program `djvused'."
|
| 234 | (buffer-name)
|
| 235 | (shell-quote-argument
|
| 236 | (concat (file-name-sans-extension (buffer-name)) ".djvu"))))
|
| 237 | - (shell-command-to-string
|
| 238 | - (format
|
| 239 | - "djvused -s -e \"set-outline '%s'\" %s"
|
| 240 | - (buffer-name)
|
| 241 | - (shell-quote-argument
|
| 242 | - (concat (file-name-sans-extension (buffer-name)) ".djvu")))))
|
| 243 | + (doc-toc--process-to-string
|
| 244 | + doc-toc--djvused-program
|
| 245 | + "-s"
|
| 246 | + "-e"
|
| 247 | + (format "set-outline '%s'"
|
| 248 | + (buffer-name)) ;; TODO: maybe escape?
|
| 249 | + (concat (file-name-sans-extension (buffer-name)) ".djvu")))
|
| 250 |
|
| 251 |
|
| 252 | (defun doc-toc--add-to-doc ()
|