Browse Source

ox-html: Number source blocks with listings

* lisp/ox-html.el (org-html-src-block): Number source block if it has
  a caption.  Numbering is done among source blocks with a caption,
  only.

Suggested-by: Clément Pit--Claudel <clement.pit@gmail.com>
<http://permalink.gmane.org/gmane.emacs.orgmode/108808>
Nicolas Goaziou 8 years ago
parent
commit
579569cc84
2 changed files with 22 additions and 7 deletions
  1. 5 0
      etc/ORG-NEWS
  2. 17 7
      lisp/ox-html.el

+ 5 - 0
etc/ORG-NEWS

@@ -189,6 +189,11 @@ If the block has a =#+NAME:= attribute assigned, then the HTML element
 will have an ~id~ attribute with that name in the HTML export. This
 will have an ~id~ attribute with that name in the HTML export. This
 enables one to create links to these elements in other places, e.g.,
 enables one to create links to these elements in other places, e.g.,
 ~<a href="#name">text</a>~.
 ~<a href="#name">text</a>~.
+**** Listings with captions are now numbered in HTML export
+The class associated to the numbering is "listing-number".  If you
+don't want these blocks to be numbered, as it was the case until now,
+You may want to add ~.listing-number { display: none; }~ to the CSS
+used.
 **** Line Numbering in SRC/EXAMPLE blocks support arbitrary start number
 **** Line Numbering in SRC/EXAMPLE blocks support arbitrary start number
 The ~-n~ option to ~SRC~ and ~EXAMPLE~ blocks can now take a numeric
 The ~-n~ option to ~SRC~ and ~EXAMPLE~ blocks can now take a numeric
 argument to specify the staring line number for the source or example
 argument to specify the staring line number for the source or example

+ 17 - 7
lisp/ox-html.el

@@ -3319,18 +3319,28 @@ contextual information."
   (if (org-export-read-attribute :attr_html src-block :textarea)
   (if (org-export-read-attribute :attr_html src-block :textarea)
       (org-html--textarea-block src-block)
       (org-html--textarea-block src-block)
     (let ((lang (org-element-property :language src-block))
     (let ((lang (org-element-property :language src-block))
-	  (caption (org-export-get-caption src-block))
 	  (code (org-html-format-code src-block info))
 	  (code (org-html-format-code src-block info))
 	  (label (let ((lbl (and (org-element-property :name src-block)
 	  (label (let ((lbl (and (org-element-property :name src-block)
 				 (org-export-get-reference src-block info))))
 				 (org-export-get-reference src-block info))))
 		   (if lbl (format " id=\"%s\"" lbl) ""))))
 		   (if lbl (format " id=\"%s\"" lbl) ""))))
       (if (not lang) (format "<pre class=\"example\"%s>\n%s</pre>" label code)
       (if (not lang) (format "<pre class=\"example\"%s>\n%s</pre>" label code)
-	(format
-	 "<div class=\"org-src-container\">\n%s%s\n</div>"
-	 (if (not caption) ""
-	   (format "<label class=\"org-src-name\">%s</label>"
-		   (org-export-data caption info)))
-	 (format "\n<pre class=\"src src-%s\"%s>%s</pre>" lang label code))))))
+	(format "<div class=\"org-src-container\">\n%s%s\n</div>"
+		;; Build caption.
+		(let ((caption (org-export-get-caption src-block)))
+		  (if (not caption) ""
+		    (let ((listing-number
+			   (format
+			    "<span class=\"listing-number\">%s </span>"
+			    (format
+			     (org-html--translate "Listing %d:" info)
+			     (org-export-get-ordinal
+			      src-block info nil #'org-html--has-caption-p)))))
+		      (format "<label class=\"org-src-name\">%s%s</label>"
+			      listing-number
+			      (org-trim (org-export-data caption info))))))
+		;; Contents.
+		(format "<pre class=\"src src-%s\"%s>%s</pre>"
+			lang label code))))))
 
 
 ;;;; Statistics Cookie
 ;;;; Statistics Cookie