diff --git a/sphinx/config.py b/sphinx/config.py
index 60626cc7d..f0ff1f09b 100644
--- a/sphinx/config.py
+++ b/sphinx/config.py
@@ -72,7 +72,10 @@ class Config(object):
nitpick_ignore = ([], 'html'),
numfig = (False, 'env'),
numfig_secnum_depth = (1, 'env'),
- numfig_prefix = ({'figure': 'Fig.'}, 'env'),
+ numfig_prefix = ({'figure': 'Fig.',
+ 'table': 'Table ',
+ 'code-block': 'List '},
+ 'env'),
# HTML options
html_theme = ('default', 'html'),
diff --git a/sphinx/environment.py b/sphinx/environment.py
index aa76ea411..214dd8223 100644
--- a/sphinx/environment.py
+++ b/sphinx/environment.py
@@ -234,7 +234,8 @@ class BuildEnvironment:
# used to determine when to show the TOC
# in a sidebar (don't show if it's only one item)
self.toc_secnumbers = {} # docname -> dict of sectionid -> number
- self.toc_fignumbers = {} # docname -> dict of figureid -> number
+ self.toc_fignumbers = {} # docname -> dict of figtype ->
+ # dict of figureid -> number
self.toctree_includes = {} # docname -> list of toctree includefiles
self.files_to_rebuild = {} # docname -> set of files
@@ -1704,6 +1705,13 @@ class BuildEnvironment:
self.toc_fignumbers = {}
fignum_counter = {}
+ def has_child(node, cls):
+ for child in node:
+ if isinstance(child, cls):
+ return True
+
+ return False
+
def get_section_number(docname, section):
anchorname = '#' + section['ids'][0]
secnumbers = self.toc_secnumbers.get(docname, {})
@@ -1714,13 +1722,19 @@ class BuildEnvironment:
return secnum or tuple()
- def get_next_figure_number(secnum):
+ def get_next_fignumber(figtype, secnum):
+ counter = fignum_counter.setdefault(figtype, {})
+
secnum = secnum[:self.config.numfig_secnum_depth]
- fignum_counter[secnum] = fignum_counter.get(secnum, 0) + 1
- return secnum + (fignum_counter[secnum],)
+ counter[secnum] = counter.get(secnum, 0) + 1
+ return secnum + (counter[secnum],)
+
+ def register_fignumber(docname, secnum, figtype, figure_id):
+ self.toc_fignumbers.setdefault(docname, {})
+ fignumbers = self.toc_fignumbers[docname].setdefault(figtype, {})
+ fignumbers[figure_id] = get_next_fignumber(figtype, secnum)
def _walk_doctree(docname, doctree, secnum):
- fignums = self.toc_fignumbers.setdefault(docname, {})
for subnode in doctree.children:
if isinstance(subnode, nodes.section):
next_secnum = get_section_number(docname, subnode)
@@ -1741,7 +1755,14 @@ class BuildEnvironment:
if isinstance(subnode, nodes.figure):
figure_id = subnode['ids'][0]
- fignums[figure_id] = get_next_figure_number(secnum)
+ register_fignumber(docname, secnum, 'figure', figure_id)
+ elif isinstance(subnode, nodes.table):
+ table_id = subnode['ids'][0]
+ register_fignumber(docname, secnum, 'table', table_id)
+ elif isinstance(subnode, nodes.container):
+ if has_child(subnode, nodes.literal_block):
+ code_block_id = subnode['ids'][0]
+ register_fignumber(docname, secnum, 'code-block', code_block_id)
_walk_doctree(docname, subnode, secnum)
diff --git a/sphinx/transforms.py b/sphinx/transforms.py
index e4806092f..7624ad170 100644
--- a/sphinx/transforms.py
+++ b/sphinx/transforms.py
@@ -122,6 +122,12 @@ class AutoNumbering(Transform):
elif isinstance(node, nodes.image):
if has_child(node.parent, nodes.caption):
self.document.note_implicit_target(node.parent)
+ elif isinstance(node, nodes.table):
+ if has_child(node, nodes.title):
+ self.document.note_implicit_target(node)
+ elif isinstance(node, nodes.literal_block):
+ if has_child(node.parent, nodes.caption):
+ self.document.note_implicit_target(node.parent)
class SortIds(Transform):
diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py
index de384ba1e..82c228bdc 100644
--- a/sphinx/writers/html.py
+++ b/sphinx/writers/html.py
@@ -251,13 +251,19 @@ class HTMLTranslator(BaseTranslator):
self.secnumber_suffix)
def add_fignumber(self, node):
- if isinstance(node.parent, nodes.figure):
- figure_id = node.parent['ids'][0]
- if self.builder.fignumbers.get(figure_id):
- prefix = self.builder.config.numfig_prefix.get('figure')
- numbers = self.builder.fignumbers[figure_id]
+ def append_fignumber(figtype, figure_id):
+ if figure_id in self.builder.fignumbers.get(figtype, {}):
+ prefix = self.builder.config.numfig_prefix.get(figtype, '')
+ numbers = self.builder.fignumbers[figtype][figure_id]
self.body.append(prefix + '.'.join(map(str, numbers)) + " ")
+ if isinstance(node.parent, nodes.figure):
+ append_fignumber('figure', node.parent['ids'][0])
+ elif isinstance(node.parent, nodes.table):
+ append_fignumber('table', node.parent['ids'][0])
+ elif isinstance(node.parent, nodes.container):
+ append_fignumber('code-block', node.parent['ids'][0])
+
# overwritten to avoid emitting empty
def visit_bullet_list(self, node):
if len(node) == 1 and node[0].tagname == 'toctree':
@@ -268,6 +274,7 @@ class HTMLTranslator(BaseTranslator):
def visit_title(self, node):
BaseTranslator.visit_title(self, node)
self.add_secnumber(node)
+ self.add_fignumber(node)
# overwritten
def visit_literal_block(self, node):
diff --git a/tests/roots/test-tocdepth/bar.rst b/tests/roots/test-tocdepth/bar.rst
index 96c692c56..f9d00ab28 100644
--- a/tests/roots/test-tocdepth/bar.rst
+++ b/tests/roots/test-tocdepth/bar.rst
@@ -15,6 +15,16 @@ should be 2.1
should be Fig.2.1
+.. csv-table:: should be Table 2.1
+ :header-rows: 0
+
+ hello,world
+
+.. code-block:: python
+ :caption: should be List 2.1
+
+ print('hello world')
+
.. toctree::
baz
@@ -23,6 +33,16 @@ should be 2.1
should be Fig.2.3
+.. csv-table:: should be Table 2.3
+ :header-rows: 0
+
+ hello,world
+
+.. code-block:: python
+ :caption: should be List 2.3
+
+ print('hello world')
+
Bar B
=====
@@ -36,3 +56,13 @@ should be 2.2.1
.. figure:: rimg.png
should be Fig.2.4
+
+.. csv-table:: should be Table 2.4
+ :header-rows: 0
+
+ hello,world
+
+.. code-block:: python
+ :caption: should be List 2.4
+
+ print('hello world')
diff --git a/tests/roots/test-tocdepth/baz.rst b/tests/roots/test-tocdepth/baz.rst
index a1cb05127..a7dcfad94 100644
--- a/tests/roots/test-tocdepth/baz.rst
+++ b/tests/roots/test-tocdepth/baz.rst
@@ -6,3 +6,13 @@ should be 2.1.1
.. figure:: rimg.png
should be Fig.2.2
+
+.. csv-table:: should be Table 2.2
+ :header-rows: 0
+
+ hello,world
+
+.. code-block:: python
+ :caption: should be List 2.2
+
+ print('hello world')
diff --git a/tests/roots/test-tocdepth/foo.rst b/tests/roots/test-tocdepth/foo.rst
index 7fad902d7..f4227234f 100644
--- a/tests/roots/test-tocdepth/foo.rst
+++ b/tests/roots/test-tocdepth/foo.rst
@@ -8,6 +8,16 @@ should be 1
should be Fig.1.1
+.. csv-table:: should be Table 1.1
+ :header-rows: 0
+
+ hello,world
+
+.. code-block:: python
+ :caption: should be List 1.1
+
+ print('hello world')
+
Foo A
=====
@@ -21,6 +31,26 @@ should be 1.1
should be Fig.1.3
+.. csv-table:: should be Table 1.2
+ :header-rows: 0
+
+ hello,world
+
+.. csv-table:: should be Table 1.3
+ :header-rows: 0
+
+ hello,world
+
+.. code-block:: python
+ :caption: should be List 1.2
+
+ print('hello world')
+
+.. code-block:: python
+ :caption: should be List 1.3
+
+ print('hello world')
+
Foo A1
------
@@ -39,3 +69,13 @@ should be 1.2.1
.. figure:: rimg.png
should be Fig.1.4
+
+.. csv-table:: should be Table 1.4
+ :header-rows: 0
+
+ hello,world
+
+.. code-block:: python
+ :caption: should be List 1.4
+
+ print('hello world')
diff --git a/tests/roots/test-tocdepth/index.rst b/tests/roots/test-tocdepth/index.rst
index ee32ef4c4..564018e36 100644
--- a/tests/roots/test-tocdepth/index.rst
+++ b/tests/roots/test-tocdepth/index.rst
@@ -14,3 +14,23 @@ test-tocdepth
.. figure:: rimg.png
should be Fig.2
+
+.. csv-table:: should be Table 1
+ :header-rows: 0
+
+ hello,world
+
+.. csv-table:: should be Table 2
+ :header-rows: 0
+
+ hello,world
+
+.. code-block:: python
+ :caption: should be List 1
+
+ print('hello world')
+
+.. code-block:: python
+ :caption: should be List 2
+
+ print('hello world')
diff --git a/tests/test_build_html.py b/tests/test_build_html.py
index 02a4f02dd..c4f6d4996 100644
--- a/tests/test_build_html.py
+++ b/tests/test_build_html.py
@@ -384,6 +384,12 @@ def test_tocdepth(app, status, warning):
'^should be Fig.1$', True),
(".//div[@class='figure']/p[@class='caption']",
'^should be Fig.2$', True),
+ (".//table/caption", '^should be Table 1$', True),
+ (".//table/caption", '^should be Table 2$', True),
+ (".//div[@class='code-block-caption']",
+ '^should be List 1$', True),
+ (".//div[@class='code-block-caption']",
+ '^should be List 2$', True),
],
'foo.html': [
(".//h1", '1. Foo', True),
@@ -399,6 +405,18 @@ def test_tocdepth(app, status, warning):
'^should be Fig.1.3$', True),
(".//div[@class='figure']/p[@class='caption']",
'^should be Fig.1.4$', True),
+ (".//table/caption", '^should be Table 1.1$', True),
+ (".//table/caption", '^should be Table 1.2$', True),
+ (".//table/caption", '^should be Table 1.3$', True),
+ (".//table/caption", '^should be Table 1.4$', True),
+ (".//div[@class='code-block-caption']",
+ '^should be List 1.1$', True),
+ (".//div[@class='code-block-caption']",
+ '^should be List 1.2$', True),
+ (".//div[@class='code-block-caption']",
+ '^should be List 1.3$', True),
+ (".//div[@class='code-block-caption']",
+ '^should be List 1.4$', True),
],
'bar.html': [
(".//h1", '2. Bar', True),
@@ -411,11 +429,23 @@ def test_tocdepth(app, status, warning):
'^should be Fig.2.3$', True),
(".//div[@class='figure']/p[@class='caption']",
'^should be Fig.2.4$', True),
+ (".//table/caption", '^should be Table 2.1$', True),
+ (".//table/caption", '^should be Table 2.3$', True),
+ (".//table/caption", '^should be Table 2.4$', True),
+ (".//div[@class='code-block-caption']",
+ '^should be List 2.1$', True),
+ (".//div[@class='code-block-caption']",
+ '^should be List 2.3$', True),
+ (".//div[@class='code-block-caption']",
+ '^should be List 2.4$', True),
],
'baz.html': [
(".//h1", '2.1.1. Baz A', True),
(".//div[@class='figure']/p[@class='caption']",
'^should be Fig.2.2$', True),
+ (".//table/caption", '^should be Table 2.2$', True),
+ (".//div[@class='code-block-caption']",
+ '^should be List 2.2$', True),
],
}
@@ -447,6 +477,12 @@ def test_unnumbered_toctree_with_numfig(app, status, warning):
'^Fig.9 should be Fig.1$', True),
(".//div[@class='figure']/p[@class='caption']",
'^Fig.10 should be Fig.2$', True),
+ (".//table/caption", '^Table 9 should be Table 1$', True),
+ (".//table/caption", '^Table 10 should be Table 2$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 9 should be List 1$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 10 should be List 2$', True),
],
'foo.html': [
(".//div[@class='figure']/p[@class='caption']",
@@ -457,6 +493,18 @@ def test_unnumbered_toctree_with_numfig(app, status, warning):
'^Fig.3 should be Fig.1.3$', True),
(".//div[@class='figure']/p[@class='caption']",
'^Fig.4 should be Fig.1.4$', True),
+ (".//table/caption", '^Table 1 should be Table 1.1$', True),
+ (".//table/caption", '^Table 2 should be Table 1.2$', True),
+ (".//table/caption", '^Table 3 should be Table 1.3$', True),
+ (".//table/caption", '^Table 4 should be Table 1.4$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 1 should be List 1.1$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 2 should be List 1.2$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 3 should be List 1.3$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 4 should be List 1.4$', True),
],
'bar.html': [
(".//div[@class='figure']/p[@class='caption']",
@@ -465,10 +513,22 @@ def test_unnumbered_toctree_with_numfig(app, status, warning):
'^Fig.7 should be Fig.2.3$', True),
(".//div[@class='figure']/p[@class='caption']",
'^Fig.8 should be Fig.2.4$', True),
+ (".//table/caption", '^Table 5 should be Table 2.1$', True),
+ (".//table/caption", '^Table 7 should be Table 2.3$', True),
+ (".//table/caption", '^Table 8 should be Table 2.4$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 5 should be List 2.1$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 7 should be List 2.3$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 8 should be List 2.4$', True),
],
'baz.html': [
(".//div[@class='figure']/p[@class='caption']",
'^Fig.6 should be Fig.2.2$', True),
+ (".//table/caption", '^Table 6 should be Table 2.2$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 6 should be List 2.2$', True),
],
}
@@ -496,6 +556,12 @@ def test_numbered_toctree_with_numfig(app, status, warning):
'^Fig.1 should be Fig.1$', True),
(".//div[@class='figure']/p[@class='caption']",
'^Fig.2 should be Fig.2$', True),
+ (".//table/caption", '^Table 1 should be Table 1$', True),
+ (".//table/caption", '^Table 2 should be Table 2$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 1 should be List 1$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 2 should be List 2$', True),
],
'foo.html': [
(".//div[@class='figure']/p[@class='caption']",
@@ -506,6 +572,18 @@ def test_numbered_toctree_with_numfig(app, status, warning):
'^Fig.1.3 should be Fig.1.3$', True),
(".//div[@class='figure']/p[@class='caption']",
'^Fig.1.4 should be Fig.1.4$', True),
+ (".//table/caption", '^Table 1.1 should be Table 1.1$', True),
+ (".//table/caption", '^Table 1.2 should be Table 1.2$', True),
+ (".//table/caption", '^Table 1.3 should be Table 1.3$', True),
+ (".//table/caption", '^Table 1.4 should be Table 1.4$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 1.1 should be List 1.1$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 1.2 should be List 1.2$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 1.3 should be List 1.3$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 1.4 should be List 1.4$', True),
],
'bar.html': [
(".//div[@class='figure']/p[@class='caption']",
@@ -514,10 +592,22 @@ def test_numbered_toctree_with_numfig(app, status, warning):
'^Fig.2.3 should be Fig.2.3$', True),
(".//div[@class='figure']/p[@class='caption']",
'^Fig.2.4 should be Fig.2.4$', True),
+ (".//table/caption", '^Table 2.1 should be Table 2.1$', True),
+ (".//table/caption", '^Table 2.3 should be Table 2.3$', True),
+ (".//table/caption", '^Table 2.4 should be Table 2.4$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 2.1 should be List 2.1$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 2.3 should be List 2.3$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 2.4 should be List 2.4$', True),
],
'baz.html': [
(".//div[@class='figure']/p[@class='caption']",
'^Fig.2.2 should be Fig.2.2$', True),
+ (".//table/caption", '^Table 2.2 should be Table 2.2$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 2.2 should be List 2.2$', True),
],
}
@@ -535,7 +625,7 @@ def test_numbered_toctree_with_numfig(app, status, warning):
@gen_with_app(buildername='html', testroot='tocdepth',
- confoverrides={'numfig': True, 'numfig_prefix': {'figure': 'Figure:'}})
+ confoverrides={'numfig': True, 'numfig_prefix': {'figure': 'Figure:', 'table': 'Tab_', 'code-block': 'Code-'}})
def test_numbered_toctree_with_numfig_prefix(app, status, warning):
app.builder.build_all()
@@ -545,6 +635,12 @@ def test_numbered_toctree_with_numfig_prefix(app, status, warning):
'^Figure:1 should be Fig.1$', True),
(".//div[@class='figure']/p[@class='caption']",
'^Figure:2 should be Fig.2$', True),
+ (".//table/caption", '^Tab_1 should be Table 1$', True),
+ (".//table/caption", '^Tab_2 should be Table 2$', True),
+ (".//div[@class='code-block-caption']",
+ '^Code-1 should be List 1$', True),
+ (".//div[@class='code-block-caption']",
+ '^Code-2 should be List 2$', True),
],
'foo.html': [
(".//div[@class='figure']/p[@class='caption']",
@@ -555,6 +651,18 @@ def test_numbered_toctree_with_numfig_prefix(app, status, warning):
'^Figure:1.3 should be Fig.1.3$', True),
(".//div[@class='figure']/p[@class='caption']",
'^Figure:1.4 should be Fig.1.4$', True),
+ (".//table/caption", '^Tab_1.1 should be Table 1.1$', True),
+ (".//table/caption", '^Tab_1.2 should be Table 1.2$', True),
+ (".//table/caption", '^Tab_1.3 should be Table 1.3$', True),
+ (".//table/caption", '^Tab_1.4 should be Table 1.4$', True),
+ (".//div[@class='code-block-caption']",
+ '^Code-1.1 should be List 1.1$', True),
+ (".//div[@class='code-block-caption']",
+ '^Code-1.2 should be List 1.2$', True),
+ (".//div[@class='code-block-caption']",
+ '^Code-1.3 should be List 1.3$', True),
+ (".//div[@class='code-block-caption']",
+ '^Code-1.4 should be List 1.4$', True),
],
'bar.html': [
(".//div[@class='figure']/p[@class='caption']",
@@ -563,10 +671,22 @@ def test_numbered_toctree_with_numfig_prefix(app, status, warning):
'^Figure:2.3 should be Fig.2.3$', True),
(".//div[@class='figure']/p[@class='caption']",
'^Figure:2.4 should be Fig.2.4$', True),
+ (".//table/caption", '^Tab_2.1 should be Table 2.1$', True),
+ (".//table/caption", '^Tab_2.3 should be Table 2.3$', True),
+ (".//table/caption", '^Tab_2.4 should be Table 2.4$', True),
+ (".//div[@class='code-block-caption']",
+ '^Code-2.1 should be List 2.1$', True),
+ (".//div[@class='code-block-caption']",
+ '^Code-2.3 should be List 2.3$', True),
+ (".//div[@class='code-block-caption']",
+ '^Code-2.4 should be List 2.4$', True),
],
'baz.html': [
(".//div[@class='figure']/p[@class='caption']",
'^Figure:2.2 should be Fig.2.2$', True),
+ (".//table/caption", '^Tab_2.2 should be Table 2.2$', True),
+ (".//div[@class='code-block-caption']",
+ '^Code-2.2 should be List 2.2$', True),
],
}
@@ -594,6 +714,12 @@ def test_numbered_toctree_with_numfig_secnum_depth(app, status, warning):
'^Fig.1 should be Fig.1$', True),
(".//div[@class='figure']/p[@class='caption']",
'^Fig.2 should be Fig.2$', True),
+ (".//table/caption", '^Table 1 should be Table 1$', True),
+ (".//table/caption", '^Table 2 should be Table 2$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 1 should be List 1$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 2 should be List 2$', True),
],
'foo.html': [
(".//div[@class='figure']/p[@class='caption']",
@@ -604,6 +730,18 @@ def test_numbered_toctree_with_numfig_secnum_depth(app, status, warning):
'^Fig.1.1.2 should be Fig.1.3$', True),
(".//div[@class='figure']/p[@class='caption']",
'^Fig.1.2.1 should be Fig.1.4$', True),
+ (".//table/caption", '^Table 1.1 should be Table 1.1$', True),
+ (".//table/caption", '^Table 1.1.1 should be Table 1.2$', True),
+ (".//table/caption", '^Table 1.1.2 should be Table 1.3$', True),
+ (".//table/caption", '^Table 1.2.1 should be Table 1.4$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 1.1 should be List 1.1$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 1.1.1 should be List 1.2$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 1.1.2 should be List 1.3$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 1.2.1 should be List 1.4$', True),
],
'bar.html': [
(".//div[@class='figure']/p[@class='caption']",
@@ -612,10 +750,22 @@ def test_numbered_toctree_with_numfig_secnum_depth(app, status, warning):
'^Fig.2.1.3 should be Fig.2.3$', True),
(".//div[@class='figure']/p[@class='caption']",
'^Fig.2.2.1 should be Fig.2.4$', True),
+ (".//table/caption", '^Table 2.1.1 should be Table 2.1$', True),
+ (".//table/caption", '^Table 2.1.3 should be Table 2.3$', True),
+ (".//table/caption", '^Table 2.2.1 should be Table 2.4$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 2.1.1 should be List 2.1$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 2.1.3 should be List 2.3$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 2.2.1 should be List 2.4$', True),
],
'baz.html': [
(".//div[@class='figure']/p[@class='caption']",
'^Fig.2.1.2 should be Fig.2.2$', True),
+ (".//table/caption", '^Table 2.1.2 should be Table 2.2$', True),
+ (".//div[@class='code-block-caption']",
+ '^List 2.1.2 should be List 2.2$', True),
],
}