Sfoglia il codice sorgente

org-export: New `org-export-get-parent-paragraph' function

* contrib/lisp/org-export.el (org-export-get-genealogy,
org-export-get-parent-paragraph): New functions.
(org-export-get-parent-headline, org-export-get-previous-element,
org-export-get-next-element): Internally use new function.
* EXPERIMENTAL/org-e-latex.el (org-e-latex-link--inline-image,
org-e-latex-link): Apply changes to topology functions.

This new function is especially useful since every object derives its
properties from those the containing paragraph.
Nicolas Goaziou 13 anni fa
parent
commit
664271cde7
2 ha cambiato i file con 65 aggiunte e 18 eliminazioni
  1. 16 13
      EXPERIMENTAL/org-e-latex.el
  2. 49 5
      contrib/lisp/org-export.el

+ 16 - 13
EXPERIMENTAL/org-e-latex.el

@@ -1311,18 +1311,23 @@ CONTENTS is nil.  INFO is a plist holding contextual information."
 
 ;;;; Link
 
-(defun org-e-latex-link--inline-image (path info)
-  "Return LaTeX code for an image at PATH.
-INFO is a plist containing export options."
-  (let* ((parent-props (nth 1 (car (plist-get info :genealogy))))
+(defun org-e-latex-link--inline-image (link info)
+  "Return LaTeX code for an inline image.
+LINK is the link pointing to the inline image.  INFO is a plist
+used as a communication channel."
+  (let* ((parent (org-export-get-parent-paragraph link info))
+	 (path (let ((raw-path (org-element-get-property :path link)))
+		 (if (not (file-name-absolute-p raw-path)) raw-path
+		   (expand-file-name raw-path))))
 	 (caption (org-e-latex--caption/label-string
-		   (plist-get parent-props :caption)
-		   (plist-get parent-props :name)
+		   (org-element-get-property :caption parent)
+		   (org-element-get-property :name parent)
 		   info))
 	 ;; Retrieve latex attributes from the element around.
 	 (attr (let ((raw-attr
 		      (mapconcat #'identity
-				 (plist-get parent-props :attr_latex) " ")))
+				 (org-element-get-property :attr_latex parent)
+				 " ")))
 		 (unless (string= raw-attr "") raw-attr)))
 	 (disposition
 	  (cond
@@ -1352,15 +1357,15 @@ INFO is a plist containing export options."
 		     (t (or org-e-latex-image-default-option ""))))
     ;; Return proper string, depending on DISPOSITION.
     (case disposition
-      ('wrap (format "\\begin{wrapfigure}%s
+      (wrap (format "\\begin{wrapfigure}%s
 \\centering
 \\includegraphics[%s]{%s}
 %s\\end{wrapfigure}" placement attr path caption))
-      ('mulicolumn (format "\\begin{figure*}%s
+      (mulicolumn (format "\\begin{figure*}%s
 \\centering
 \\includegraphics[%s]{%s}
 %s\\end{figure*}" placement attr path caption))
-      ('float (format "\\begin{figure}%s
+      (float (format "\\begin{figure}%s
 \\centering
 \\includegraphics[%s]{%s}
 %s\\end{figure}" placement attr path caption))
@@ -1381,8 +1386,6 @@ INFO is a plist holding contextual information.  See
 	 (path (cond
 		((member type '("http" "https" "ftp" "mailto"))
 		 (concat type ":" raw-path))
-		(imagep (if (not (file-name-absolute-p raw-path)) raw-path
-			  (expand-file-name raw-path)))
 		((string= type "file")
 		 (when (string-match "\\(.+\\)::.+" raw-path)
 		   (setq raw-path (match-string 1 raw-path)))
@@ -1395,7 +1398,7 @@ INFO is a plist holding contextual information.  See
 	 protocol)
     (cond
      ;; Image file.
-     (imagep (org-e-latex-link--inline-image path info))
+     (imagep (org-e-latex-link--inline-image link info))
      ;; Target or radioed target: replace link with the normalized
      ;; custom-id/target name.
      ((member type '("target" "radio"))

+ 49 - 5
contrib/lisp/org-export.el

@@ -3048,13 +3048,57 @@ affiliated keyword."
 
 ;;;; Topology
 
+;; Here are various functions to retrieve information about the
+;; neighbourhood of a given element or object.  Neighbours of interest
+;; are parent headline (`org-export-get-parent-headline'), parent
+;; paragraph (`org-export-get-parent-paragraph'), previous element or
+;; object (`org-export-get-previous-element') and next element or
+;; object (`org-export-get-next-element').
+
+;; All of these functions are just a specific use of the more generic
+;; `org-export-get-genealogy', which returns the genealogy relative to
+;; the element or object.
+
+(defun org-export-get-genealogy (blob info)
+  "Return genealogy relative to a given element or object.
+BLOB is the element or object being considered.  INFO is a plist
+used as a communication channel."
+  ;; LOCALP tells if current `:genealogy' is sufficient to find parent
+  ;; headline, or if it should be computed.
+  (let ((localp (member blob (org-element-get-contents
+			      (car (plist-get info :genealogy))))))
+    (if localp (plist-get info :genealogy)
+      (catch 'exit
+	(org-element-map
+	 (plist-get info :parse-tree) (car blob)
+	 (lambda (el local) (when (equal el blob) (throw 'exit local)))
+	 info)))))
+
 (defun org-export-get-parent-headline (blob info)
-  "Return BLOB's closest parent headline or nil.
-INFO is a plist used as a communication channel."
+  "Return closest parent headline or nil.
+
+BLOB is the element or object being considered.  INFO is a plist
+used as a communication channel."
   (catch 'exit
     (mapc
      (lambda (el) (when (eq (car el) 'headline) (throw 'exit el)))
-     (plist-get info :genealogy))
+     (org-export-get-genealogy blob info))
+    nil))
+
+(defun org-export-get-parent-paragraph (object info)
+  "Return parent paragraph or nil.
+
+INFO is a plist used as a communication channel.
+
+Optional argument OBJECT, when provided, is the object to consider.
+Otherwise, return the paragraph containing current object.
+
+This is useful for objects, which share attributes with the
+paragraph containing them."
+  (catch 'exit
+    (mapc
+     (lambda (el) (when (eq (car el) 'paragraph) (throw 'exit el)))
+     (org-export-get-genealogy object info))
     nil))
 
 (defun org-export-get-previous-element (blob info)
@@ -3064,7 +3108,7 @@ BLOB is an element or object.  INFO is a plist used as
 a communication channel.
 
 Return previous element or object, a string, or nil."
-  (let ((parent (car (plist-get info :genealogy))))
+  (let ((parent (car (org-export-get-genealogy blob info))))
     (cadr (member blob (reverse (org-element-get-contents parent))))))
 
 (defun org-export-get-next-element (blob info)
@@ -3074,7 +3118,7 @@ BLOB is an element or object.  INFO is a plist used as
 a communication channel.
 
 Return next element or object, a string, or nil."
-  (let ((parent (car (plist-get info :genealogy))))
+  (let ((parent (car (org-export-get-genealogy blob info))))
     (cadr (member blob (org-element-get-contents parent)))))