emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Add figure/table numbers to HTML captions
@ 2013-06-27 10:52 Yoshinari Nomura
  2013-06-27 11:34 ` Rasmus
  2013-06-27 14:46 ` Nicolas Goaziou
  0 siblings, 2 replies; 14+ messages in thread
From: Yoshinari Nomura @ 2013-06-27 10:52 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: Text/Plain, Size: 1718 bytes --]

Hi all,

I sometimes need to export an org document into both HTML and LaTeX.
In such case, I want HTML exporter to create numbered captions for
figures and tables.

So, I've written a small patch to add figure/table numbers to HTML
captions. (see 0001-Add-figure-table-numbers-to-HTML-captions.patch)

I'll be very happy if this feature is merged into the master branch.

This is my first post to this list.
Please teach me if we have some better way to submit the patch.

Thanks.
--
Yoshinari Nomura
https://github.com/yoshinari-nomura


Example:
-----------------------------------------
#+TITLE: Add figure/table numbers to HTML captions
#+LATEX_CLASS: article
#+LANGUAGE: en
#+OPTIONS: toc:nil

* Figure Test
  See Figure[[fig:screenshot]] for screenshot.
  See Figure[[fig:manual]] for printed version of Org's manual.

  #+CAPTION: Org mode screen shot
  #+name: fig:screenshot
  [[http://orgmode.org/img/main.jpg]]

  This is org-mode logo: [[http://orgmode.org/img/org-mode-unicorn-logo.png]]
  (no caption, excluded from ordinal list).

  #+CAPTION: Org mode manual
  #+name: fig:manual
  [[http://orgmode.org/img/org-mode-7-network-theory.jpg]]

* Table Test
  Table[[tab:phone]] shows phone numbers.
  Table[[tab:age]] shows age list.

  #+caption: Phones
  #+name: tab:phone
  | Name  | Phone | Age |
  |-------+-------+-----|
  | Peter |  1234 |  17 |
  | Anna  |  4321 |  25 |

  This is a plain table (no caption, excluded from ordinal list):
  | No. | Letter |
  |-----+--------|
  |   1 | A      |
  |   2 | B      |

  #+caption: Ages
  #+name: tab:age
  | Name  | Age | Phone |
  |-------+-----+-------|
  | Peter |  17 |  1234 |
  | Anna  |  25 |  4321 |
-----------------------------------------

[-- Attachment #2: 0001-Add-figure-table-numbers-to-HTML-captions.patch --]
[-- Type: Text/X-Patch, Size: 4358 bytes --]

From b406e083af930644017d84144554b992cf8a788f Mon Sep 17 00:00:00 2001
From: Yoshinari Nomura <nom@quickhack.net>
Date: Thu, 27 Jun 2013 18:15:36 +0900
Subject: [PATCH] Add figure/table numbers to HTML captions

---
 lisp/ox-html.el | 29 ++++++++++++++++++++++++++---
 lisp/ox.el      | 10 ++++++++--
 2 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/lisp/ox-html.el b/lisp/ox-html.el
index a996b40..7217eab 100644
--- a/lisp/ox-html.el
+++ b/lisp/ox-html.el
@@ -2534,6 +2534,15 @@ Inline images can have these attributes:
 	 (parent (org-export-get-parent-element link))
 	 (caption (org-export-data (org-export-get-caption parent) info))
 	 (label (org-element-property :name parent)))
+    (unless (string= caption "")
+      (let* ((org-html-standalone-image-predicate
+	      (lambda (img) (org-element-property :caption img)))
+	     (number (org-export-get-ordinal
+		      link info nil 'org-html-standalone-image-p)))
+	(setq caption
+	      (concat
+	       (format (org-html--translate "Figure %d:" info) number)
+	       " " caption))))
     ;; Return proper string, depending on DISPOSITION.
     (org-html-format-inline-image
      path info caption label
@@ -2725,14 +2734,23 @@ INFO is a plist holding contextual information.  See
 		     (org-export-solidify-link-text href) attributes desc)))
 	  ;; Fuzzy link points to a target.  Do as above.
 	  (t
-	   (let ((path (org-export-solidify-link-text path)) number)
+	   (let ((path (org-export-solidify-link-text path)) number
+		 caption-predicate org-html-standalone-image-predicate)
 	     (unless desc
+	       (setq caption-predicate
+		     (if (org-element-property :caption destination)
+			 (lambda (elem &optional info)
+			   (org-element-property :caption elem))
+		       (lambda (elem &optional info)
+			 (not (org-element-property :caption elem)))))
+	       (setq org-html-standalone-image-predicate caption-predicate)
 	       (setq number (cond
 			     ((org-html-standalone-image-p destination info)
 			      (org-export-get-ordinal
 			       (assoc 'link (org-element-contents destination))
 			       info 'link 'org-html-standalone-image-p))
-			     (t (org-export-get-ordinal destination info))))
+			     (t (org-export-get-ordinal
+				 destination info nil caption-predicate))))
 	       (setq desc (when number
 			    (if (atom number) (number-to-string number)
 			      (mapconcat 'number-to-string number ".")))))
@@ -3145,6 +3163,9 @@ contextual information."
     (t
      (let* ((label (org-element-property :name table))
 	    (caption (org-export-get-caption table))
+	    (number (org-export-get-ordinal
+		     table info nil
+		     (lambda (tbl info) (org-element-property :caption tbl))))
 	    (attributes
 	     (if (org-html-html5-p info) ""
 	       (org-html--make-attribute-string
@@ -3183,7 +3204,9 @@ contextual information."
 		 (format (if org-html-table-caption-above
 			     "<caption align=\"above\">%s</caption>"
 			   "<caption align=\"bottom\">%s</caption>")
-			 (org-export-data caption info)))
+			 (concat
+			  (format (org-html--translate "Table %d:" info) number)
+			  " " (org-export-data caption info))))
 	       (funcall table-column-specs table info)
 	       contents)))))
 
diff --git a/lisp/ox.el b/lisp/ox.el
index fb56da4..1133f21 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -5265,7 +5265,12 @@ them."
      ("fr" :ascii "Equation" :default "Équation"))
     ("Figure"
      ("de" :default "Abbildung")
-     ("es" :default "Figura"))
+     ("es" :default "Figura")
+     ("ja" :html "&#22259;" :utf-8 "図"))
+    ("Figure %d:"
+     ("de" :default "Abbildung %d:")
+     ("es" :default "Figura %d:")
+     ("ja" :html "&#22259;%d:" :utf-8 "図%d:"))
     ("Footnotes"
      ("ca" :html "Peus de p&agrave;gina")
      ("cs" :default "Pozn\xe1mky pod carou")
@@ -5312,7 +5317,8 @@ them."
      ("de" :default "Tabelle %d")
      ("es" :default "Tabla %d")
      ("fr"
-      :ascii "Tableau %d :" :default "Tableau nº %d :" :latin1 "Tableau %d :"))
+      :ascii "Tableau %d :" :default "Tableau nº %d :" :latin1 "Tableau %d :")
+     ("ja" :html "&#34920;%d:" :utf-8 "表%d:"))
     ("Table of Contents"
      ("ca" :html "&Iacute;ndex")
      ("cs" :default "Obsah")
-- 
1.8.2.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2013-08-08 12:22 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-06-27 10:52 Add figure/table numbers to HTML captions Yoshinari Nomura
2013-06-27 11:34 ` Rasmus
2013-06-27 23:37   ` Yoshinari Nomura
2013-06-27 14:46 ` Nicolas Goaziou
2013-06-28  1:15   ` Yoshinari Nomura
2013-06-28  9:23     ` Nicolas Goaziou
2013-06-28 11:51       ` Yoshinari Nomura
2013-06-29  7:01       ` Yoshinari Nomura
2013-06-29  8:02         ` Nicolas Goaziou
2013-06-29 10:00           ` Yoshinari Nomura
2013-06-29 12:09             ` Nicolas Goaziou
2013-06-29 13:48               ` Yoshinari Nomura
     [not found]                 ` <rt-3.4.5-3864-1375881898-1936.839264-5-0@rt.gnu.org>
2013-08-08  0:43                   ` Yoshinari Nomura
2013-08-08 12:22                     ` Nicolas Goaziou

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).