emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] Add figure/table numbers to HTML captions


From: Yoshinari Nomura
Subject: [O] Add figure/table numbers to HTML captions
Date: Thu, 27 Jun 2013 19:52:26 +0900 (JST)

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 |
-----------------------------------------
>From b406e083af930644017d84144554b992cf8a788f Mon Sep 17 00:00:00 2001
From: Yoshinari Nomura <address@hidden>
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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]