Browse Source

org-xhtml.el: Support for captioning and labelling of equation images

* contrib/lisp/org-xhtml.el
(org-export-xhtml-do-preprocess-latex-fragments): Factored out
from the earlier org-export-xhtml-preprocess.
(org-export-xhtml-preprocess-latex-fragments): Check if
backend is xhtml. If yes, delegate to
org-export-xhtml-do-preprocess-latex-fragments. This is hooked
to org-export-preprocess-after-blockquote-hook.
(org-export-xhtml-preprocess-label-references): Factored out
from the earlier org-export-xhtml-preprocess.
(org-export-xhtml-preprocess): Call
org-export-xhtml-preprocess-label-references. Note that
processing of latex fragments is no longer done here.
(org-xhtml-format-image): Honor captions, label and attributes
even for images that are created by latex fragment processing.
(org-xhtml-unload-function): Unhook from
org-export-preprocess-after-blockquote-hook.

Thanks to Myles English for registering this requirement.
http://lists.gnu.org/archive/html/emacs-orgmode/2011-08/msg01116.html
Jambunathan K 13 years ago
parent
commit
590ad72f59
1 changed files with 65 additions and 45 deletions
  1. 65 45
      contrib/lisp/org-xhtml.el

+ 65 - 45
contrib/lisp/org-xhtml.el

@@ -633,24 +633,30 @@ with a link to this URL."
 (defvar org-export-xhtml-final-hook nil
   "Hook run at the end of HTML export, in the new buffer.")
 
-;;; HTML export
+(defun org-export-xhtml-preprocess-latex-fragments ()
+  (when (equal org-lparse-backend 'xhtml)
+    (org-export-xhtml-do-preprocess-latex-fragments)))
 
-(defun org-export-xhtml-preprocess (parameters)
+(defvar org-lparse-opt-plist)		    ; bound during org-do-lparse
+(defun org-export-xhtml-do-preprocess-latex-fragments ()
   "Convert LaTeX fragments to images."
-  (when (and org-current-export-file
-	     (plist-get parameters :LaTeX-fragments))
-    (org-format-latex
-     (concat "ltxpng/" (file-name-sans-extension
-			(file-name-nondirectory
-			 org-current-export-file)))
-     org-current-export-dir nil "Creating LaTeX image %s"
-     nil nil
-     (cond
-      ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim)
-      ((eq (plist-get parameters :LaTeX-fragments) 'mathjax ) 'mathjax)
-      ((eq (plist-get parameters :LaTeX-fragments) t        ) 'mathjax)
-      ((eq (plist-get parameters :LaTeX-fragments) 'dvipng  ) 'dvipng)
-      (t nil))))
+  (let* ((latex-frag-opt (plist-get org-lparse-opt-plist :LaTeX-fragments))
+	 (latex-frag-opt-1		; massage the options
+	  (cond
+	   ((eq latex-frag-opt 'verbatim) 'verbatim)
+	   ((eq latex-frag-opt 'mathjax ) 'mathjax)
+	   ((eq latex-frag-opt t        ) 'mathjax)
+	   ((eq latex-frag-opt 'dvipng  ) 'dvipng)
+	   (t nil))))
+    (when (and org-current-export-file latex-frag-opt)
+      (org-format-latex
+       (concat "ltxpng/" (file-name-sans-extension
+			  (file-name-nondirectory
+			   org-current-export-file)))
+       org-current-export-dir nil "Creating LaTeX image %s"
+       nil nil latex-frag-opt-1))))
+
+(defun org-export-xhtml-preprocess-label-references ()
   (goto-char (point-min))
   (let (label l1)
     (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
@@ -662,6 +668,19 @@ with a link to this URL."
 	    (setq l1 label)))
 	(replace-match (format "[[#%s][%s]]" label l1) t t)))))
 
+(defun org-export-xhtml-preprocess (parameters)
+  (org-export-xhtml-preprocess-label-references))
+
+;; Process latex fragments as part of
+;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
+;; is the one that is closest and well before the call to
+;; `org-export-attach-captions-and-attributes' in
+;; `org-export-preprocess-stirng'.  The above arrangement permits
+;; captions, labels and attributes to be attached to png images
+;; generated out of latex equations.
+(add-hook 'org-export-preprocess-after-blockquote-hook
+	  'org-export-xhtml-preprocess-latex-fragments)
+
 (defvar html-table-tag nil) ; dynamically scoped into this.
 
 
@@ -785,34 +804,34 @@ MAY-INLINE-P allows inlining it as an image."
 (defun org-xhtml-format-image (src)
   "Create image tag with source and attributes."
   (save-match-data
-    (if (string-match "^ltxpng/" src)
-	(format "<img src=\"%s\" alt=\"%s\"/>"
-                src (org-find-text-property-in-string 'org-latex-src src))
-      (let* ((caption (org-find-text-property-in-string 'org-caption src))
-	     (attr (org-find-text-property-in-string 'org-attributes src))
-	     (label (org-find-text-property-in-string 'org-label src))
-	     (caption (and caption (org-xml-encode-org-text caption)))
-	     (img (format "<img src=\"%s\"%s />"
-			  src
-			  (if (string-match "\\<alt=" (or attr ""))
-			      (concat " " attr )
-			    (concat " " attr " alt=\"" src "\""))))
-	     (extra (concat
-		     (and label
-			  (format "id=\"%s\" " (org-solidify-link-text label)))
-		     "class=\"figure\"")))
-	(if caption
-	    (with-temp-buffer
-	      (with-org-lparse-preserve-paragraph-state
-	       (insert
-		(org-lparse-format
-		 '("<div %s>" . "\n</div>")
-		 (concat
-		  (org-lparse-format '("\n<p>" . "</p>") img)
-		  (org-lparse-format '("\n<p>" . "</p>") caption))
-		 extra)))
-	      (buffer-string))
-	  img)))))
+    (let* ((caption (org-find-text-property-in-string 'org-caption src))
+	   (attr (org-find-text-property-in-string 'org-attributes src))
+	   (label (org-find-text-property-in-string 'org-label src))
+	   (caption (and caption (org-xml-encode-org-text caption)))
+	   (img-extras (if (string-match "^ltxpng/" src)
+			   (format " alt=\"%s\""
+				   (org-find-text-property-in-string
+				    'org-latex-src src))
+			 (if (string-match "\\<alt=" (or attr ""))
+			     (concat " " attr )
+			   (concat " " attr " alt=\"" src "\""))))
+	   (img (format "<img src=\"%s\"%s />" src img-extras))
+	   (extra (concat
+		   (and label
+			(format "id=\"%s\" " (org-solidify-link-text label)))
+		   "class=\"figure\"")))
+      (if caption
+	  (with-temp-buffer
+	    (with-org-lparse-preserve-paragraph-state
+	     (insert
+	      (org-lparse-format
+	       '("<div %s>" . "\n</div>")
+	       (concat
+		(org-lparse-format '("\n<p>" . "</p>") img)
+		(org-lparse-format '("\n<p>" . "</p>") caption))
+	       extra)))
+	    (buffer-string))
+	img))))
 
 (defun org-export-xhtml-get-bibliography ()
   "Find bibliography, cut it out and return it."
@@ -1202,8 +1221,9 @@ make any modifications to the exporter file.  For example,
 (org-lparse-register-backend 'xhtml)
 
 (defun org-xhtml-unload-function ()
-  ;; notify org-lparse library on unload
   (org-lparse-unregister-backend 'xhtml)
+  (remove-hook 'org-export-preprocess-after-blockquote-hook
+	       'org-export-xhtml-preprocess-latex-fragments)
   nil)
 
 (defun org-xhtml-begin-document-body (opt-plist)