ox: Tiny refactoring

* lisp/ox.el (org-export-expand-include-keyword):
(org-export--prepare-file-contents):
(org-export-with-backend): Wrap line of code within 80 columns.
(org-export--inclusion-absolute-lines): Remove unecessary `format'
call for error message.
This commit is contained in:
Nicolas Goaziou 2015-05-11 01:30:45 +02:00
parent c0dec9a8bc
commit f653495419
1 changed files with 54 additions and 38 deletions

View File

@ -3104,22 +3104,25 @@ storing and resolving footnotes. It is created automatically."
(let* ((value (org-element-property :value element)) (let* ((value (org-element-property :value element))
(ind (org-get-indentation)) (ind (org-get-indentation))
location location
(file (and (string-match (file
"^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value) (and (string-match
(prog1 "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value)
(save-match-data (prog1
(let ((matched (match-string 1 value))) (save-match-data
(when (string-match "\\(::\\(.*?\\)\\)\"?\\'" matched) (let ((matched (match-string 1 value)))
(setq location (match-string 2 matched)) (when (string-match "\\(::\\(.*?\\)\\)\"?\\'"
(setq matched matched)
(replace-match "" nil nil matched 1))) (setq location (match-string 2 matched))
(expand-file-name (setq matched
(org-remove-double-quotes (replace-match "" nil nil matched 1)))
matched) (expand-file-name
dir))) (org-remove-double-quotes
(setq value (replace-match "" nil nil value))))) matched)
dir)))
(setq value (replace-match "" nil nil value)))))
(only-contents (only-contents
(and (string-match ":only-contents *\\([^: \r\t\n]\\S-*\\)?" value) (and (string-match ":only-contents *\\([^: \r\t\n]\\S-*\\)?"
value)
(prog1 (org-not-nil (match-string 1 value)) (prog1 (org-not-nil (match-string 1 value))
(setq value (replace-match "" nil nil value))))) (setq value (replace-match "" nil nil value)))))
(lines (lines
@ -3141,7 +3144,8 @@ storing and resolving footnotes. It is created automatically."
(if (string-match ":minlevel +\\([0-9]+\\)" value) (if (string-match ":minlevel +\\([0-9]+\\)" value)
(prog1 (string-to-number (match-string 1 value)) (prog1 (string-to-number (match-string 1 value))
(setq value (replace-match "" nil nil value))) (setq value (replace-match "" nil nil value)))
(get-text-property (point) :org-include-induced-level)))) (get-text-property (point)
:org-include-induced-level))))
(src-args (and (eq env 'literal) (src-args (and (eq env 'literal)
(match-string 1 value))) (match-string 1 value)))
(block (and (string-match "\\<\\(\\S-+\\)\\>" value) (block (and (string-match "\\<\\(\\S-+\\)\\>" value)
@ -3187,22 +3191,23 @@ storing and resolving footnotes. It is created automatically."
file location only-contents lines) file location only-contents lines)
lines))) lines)))
(org-mode) (org-mode)
(insert (org-export--prepare-file-contents (insert
file lines ind minlevel (org-export--prepare-file-contents
(or (gethash file file-prefix) file lines ind minlevel
(puthash file (incf current-prefix) file-prefix)) (or (gethash file file-prefix)
footnotes))) (puthash file (incf current-prefix) file-prefix))
footnotes)))
(org-export-expand-include-keyword (org-export-expand-include-keyword
(cons (list file lines) included) (cons (list file lines) included)
(file-name-directory file) (file-name-directory file)
footnotes) footnotes)
(buffer-string))))) (buffer-string)))))
;; Expand footnotes after all files have been ;; Expand footnotes after all files have been included.
;; included. Footnotes are stored at end of buffer. ;; Footnotes are stored at end of buffer.
(unless included (unless included
(org-with-wide-buffer (org-with-wide-buffer
(goto-char (point-max)) (goto-char (point-max))
(maphash (lambda (ref def) (insert (format "\n[%s] %s\n" ref def))) (maphash (lambda (k v) (insert (format "\n[%s] %s\n" k v)))
footnotes))))))))))) footnotes)))))))))))
(defun org-export--inclusion-absolute-lines (file location only-contents lines) (defun org-export--inclusion-absolute-lines (file location only-contents lines)
@ -3225,7 +3230,7 @@ Return a string of lines to be included in the format expected by
(let ((org-link-search-must-match-exact-headline t)) (let ((org-link-search-must-match-exact-headline t))
(org-link-search location)) (org-link-search location))
(error (error
(error (format "%s for %s::%s" (error-message-string err) file location)))) (error "%s for %s::%s" (error-message-string err) file location)))
(let* ((element (org-element-at-point)) (let* ((element (org-element-at-point))
(contents-begin (contents-begin
(and only-contents (org-element-property :contents-begin element)))) (and only-contents (org-element-property :contents-begin element))))
@ -3285,7 +3290,8 @@ Return the new label."
(insert (format "%d-" id))) (insert (format "%d-" id)))
(1- (search-forward "]"))))) (1- (search-forward "]")))))
(defun org-export--prepare-file-contents (file &optional lines ind minlevel id footnotes) (defun org-export--prepare-file-contents
(file &optional lines ind minlevel id footnotes)
"Prepare contents of FILE for inclusion and return it as a string. "Prepare contents of FILE for inclusion and return it as a string.
When optional argument LINES is a string specifying a range of When optional argument LINES is a string specifying a range of
@ -3365,7 +3371,7 @@ the included document.
;; sections. ;; sections.
(org-map-entries (org-map-entries
(lambda () (if (< offset 0) (delete-char (abs offset)) (lambda () (if (< offset 0) (delete-char (abs offset))
(insert (make-string offset ?*))))))))))) (insert (make-string offset ?*)))))))))))
;; Append ID to all footnote references and definitions, so they ;; Append ID to all footnote references and definitions, so they
;; become file specific and cannot collide with footnotes in other ;; become file specific and cannot collide with footnotes in other
;; included files. Further, collect relevant footnotes outside of ;; included files. Further, collect relevant footnotes outside of
@ -3378,7 +3384,8 @@ the included document.
(let ((reference (org-element-context))) (let ((reference (org-element-context)))
(when (eq (org-element-type reference) 'footnote-reference) (when (eq (org-element-type reference) 'footnote-reference)
(let* ((label (org-element-property :label reference)) (let* ((label (org-element-property :label reference))
(digit-label (and label (org-string-match-p "\\`[0-9]+\\'" label)))) (digit-label
(and label (org-string-match-p "\\`[0-9]+\\'" label))))
;; Update the footnote-reference at point and collect ;; Update the footnote-reference at point and collect
;; the new label, which is only used for footnotes ;; the new label, which is only used for footnotes
;; outsides LINES. ;; outsides LINES.
@ -3386,20 +3393,28 @@ the included document.
;; If label is akin to [1] convert it to [fn:ID-1]. ;; If label is akin to [1] convert it to [fn:ID-1].
;; Otherwise add "ID-" after "fn:". ;; Otherwise add "ID-" after "fn:".
(let ((new-label (org-export--update-footnote-label (let ((new-label (org-export--update-footnote-label
(org-element-property :begin reference) digit-label id))) (org-element-property :begin reference)
digit-label id)))
(unless (eq (org-element-property :type reference) 'inline) (unless (eq (org-element-property :type reference) 'inline)
(org-with-wide-buffer (org-with-wide-buffer
(let* ((definition (org-footnote-get-definition label)) (let* ((definition (org-footnote-get-definition label))
(beginning (nth 1 definition))) (beginning (nth 1 definition)))
(unless definition (unless definition
(error "Definition not found for footnote %s in file %s" label file)) (error
(if (or (< beginning marker-min) (> beginning marker-max)) "Definition not found for footnote %s in file %s"
;; Store since footnote-definition is outside of LINES. label file))
(if (or (< beginning marker-min)
(> beginning marker-max))
;; Store since footnote-definition is
;; outside of LINES.
(puthash new-label (puthash new-label
(org-element-normalize-string (nth 3 definition)) (org-element-normalize-string
(nth 3 definition))
footnotes) footnotes)
;; Update label of definition since it is included directly. ;; Update label of definition since it is
(org-export--update-footnote-label beginning digit-label id))))))))))) ;; included directly.
(org-export--update-footnote-label
beginning digit-label id)))))))))))
(set-marker marker-min nil) (set-marker marker-min nil)
(set-marker marker-max nil))) (set-marker marker-max nil)))
(org-element-normalize-string (buffer-string)))) (org-element-normalize-string (buffer-string))))
@ -3514,9 +3529,10 @@ the communication channel used for export, as a plist."
(funcall (funcall
transcoder data contents transcoder data contents
(org-combine-plists (org-combine-plists
info (list :back-end backend info (list
:translate-alist all-transcoders :back-end backend
:exported-data (make-hash-table :test 'eq :size 401))))))))) :translate-alist all-transcoders
:exported-data (make-hash-table :test #'eq :size 401)))))))))
;;;; For Export Snippets ;;;; For Export Snippets