mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Close #8487: csv-table now considers abspath as relpath from srcdir
To make directives' behavior consistent, the :file: option for csv-table directive now recognizes an absolute path as a relative path from source directory.
This commit is contained in:
parent
ae413e95ed
commit
fb4220d0a2
2
CHANGES
2
CHANGES
@ -40,6 +40,8 @@ Incompatible changes
|
|||||||
* #8769: LaTeX refactoring: split sphinx.sty into multiple files and rename
|
* #8769: LaTeX refactoring: split sphinx.sty into multiple files and rename
|
||||||
some auxiliary files created in ``latex`` build output repertory
|
some auxiliary files created in ``latex`` build output repertory
|
||||||
* #8937: Use explicit title instead of <no title>
|
* #8937: Use explicit title instead of <no title>
|
||||||
|
* #8487: The :file: option for csv-table directive now recognizes an absolute
|
||||||
|
path as a relative path from source directory
|
||||||
|
|
||||||
Deprecated
|
Deprecated
|
||||||
----------
|
----------
|
||||||
|
@ -32,11 +32,6 @@ The following is a list of deprecated interfaces.
|
|||||||
- TBD
|
- TBD
|
||||||
- ``logo_url``
|
- ``logo_url``
|
||||||
|
|
||||||
* - ``sphinx.directives.patches.CSVTable``
|
|
||||||
- 4.0
|
|
||||||
- 6.0
|
|
||||||
- ``docutils.parsers.rst.diretives.tables.CSVTable``
|
|
||||||
|
|
||||||
* - ``sphinx.directives.patches.ListTable``
|
* - ``sphinx.directives.patches.ListTable``
|
||||||
- 4.0
|
- 4.0
|
||||||
- 6.0
|
- 6.0
|
||||||
|
@ -6,7 +6,9 @@
|
|||||||
:license: BSD, see LICENSE for details.
|
:license: BSD, see LICENSE for details.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
import warnings
|
import warnings
|
||||||
|
from os import path
|
||||||
from typing import TYPE_CHECKING, Any, Dict, List, Tuple, cast
|
from typing import TYPE_CHECKING, Any, Dict, List, Tuple, cast
|
||||||
|
|
||||||
from docutils import nodes
|
from docutils import nodes
|
||||||
@ -18,13 +20,19 @@ from sphinx import addnodes
|
|||||||
from sphinx.deprecation import RemovedInSphinx60Warning
|
from sphinx.deprecation import RemovedInSphinx60Warning
|
||||||
from sphinx.directives import optional_int
|
from sphinx.directives import optional_int
|
||||||
from sphinx.domains.math import MathDomain
|
from sphinx.domains.math import MathDomain
|
||||||
|
from sphinx.locale import __
|
||||||
|
from sphinx.util import logging
|
||||||
from sphinx.util.docutils import SphinxDirective
|
from sphinx.util.docutils import SphinxDirective
|
||||||
from sphinx.util.nodes import set_source_info
|
from sphinx.util.nodes import set_source_info
|
||||||
|
from sphinx.util.osutil import SEP, os_path, relpath
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from sphinx.application import Sphinx
|
from sphinx.application import Sphinx
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Figure(images.Figure):
|
class Figure(images.Figure):
|
||||||
"""The figure directive which applies `:name:` option to the figure node
|
"""The figure directive which applies `:name:` option to the figure node
|
||||||
instead of the image node.
|
instead of the image node.
|
||||||
@ -87,22 +95,26 @@ class RSTTable(tables.RSTTable):
|
|||||||
|
|
||||||
|
|
||||||
class CSVTable(tables.CSVTable):
|
class CSVTable(tables.CSVTable):
|
||||||
"""The csv-table directive which sets source and line information to its caption.
|
"""The csv-table directive which searches a CSV file from Sphinx project's source
|
||||||
|
directory when an absolute path is given via :file: option.
|
||||||
Only for docutils-0.13 or older version."""
|
"""
|
||||||
|
|
||||||
def run(self) -> List[Node]:
|
def run(self) -> List[Node]:
|
||||||
warnings.warn('CSVTable is deprecated.',
|
if 'file' in self.options and self.options['file'].startswith((SEP, os.sep)):
|
||||||
RemovedInSphinx60Warning)
|
env = self.state.document.settings.env
|
||||||
|
filename = self.options['file']
|
||||||
|
if path.exists(filename):
|
||||||
|
logger.warning(__('":file:" option for csv-table directive now recognizes '
|
||||||
|
'an absolute path as a relative path from source directory. '
|
||||||
|
'Please update your document.'),
|
||||||
|
location=(env.docname, self.lineno))
|
||||||
|
else:
|
||||||
|
abspath = path.join(env.srcdir, os_path(self.options['file'][1:]))
|
||||||
|
docdir = path.dirname(env.doc2path(env.docname))
|
||||||
|
self.options['file'] = relpath(abspath, docdir)
|
||||||
|
|
||||||
return super().run()
|
return super().run()
|
||||||
|
|
||||||
def make_title(self) -> Tuple[nodes.title, List[system_message]]:
|
|
||||||
title, message = super().make_title()
|
|
||||||
if title:
|
|
||||||
set_source_info(self, title)
|
|
||||||
|
|
||||||
return title, message
|
|
||||||
|
|
||||||
|
|
||||||
class ListTable(tables.ListTable):
|
class ListTable(tables.ListTable):
|
||||||
"""The list-table directive which sets source and line information to its caption.
|
"""The list-table directive which sets source and line information to its caption.
|
||||||
@ -224,6 +236,7 @@ class MathDirective(SphinxDirective):
|
|||||||
def setup(app: "Sphinx") -> Dict[str, Any]:
|
def setup(app: "Sphinx") -> Dict[str, Any]:
|
||||||
directives.register_directive('figure', Figure)
|
directives.register_directive('figure', Figure)
|
||||||
directives.register_directive('meta', Meta)
|
directives.register_directive('meta', Meta)
|
||||||
|
directives.register_directive('csv-table', CSVTable)
|
||||||
directives.register_directive('code', Code)
|
directives.register_directive('code', Code)
|
||||||
directives.register_directive('math', MathDirective)
|
directives.register_directive('math', MathDirective)
|
||||||
|
|
||||||
|
0
tests/roots/test-directive-csv-table/conf.py
Normal file
0
tests/roots/test-directive-csv-table/conf.py
Normal file
1
tests/roots/test-directive-csv-table/example.csv
Normal file
1
tests/roots/test-directive-csv-table/example.csv
Normal file
@ -0,0 +1 @@
|
|||||||
|
foo,bar,baz
|
|
1
tests/roots/test-directive-csv-table/subdir/example.csv
Normal file
1
tests/roots/test-directive-csv-table/subdir/example.csv
Normal file
@ -0,0 +1 @@
|
|||||||
|
FOO,BAR,BAZ
|
|
@ -8,6 +8,7 @@
|
|||||||
:license: BSD, see LICENSE for details.
|
:license: BSD, see LICENSE for details.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import pytest
|
||||||
from docutils import nodes
|
from docutils import nodes
|
||||||
|
|
||||||
from sphinx.testing import restructuredtext
|
from sphinx.testing import restructuredtext
|
||||||
@ -54,6 +55,37 @@ def test_code_directive(app):
|
|||||||
assert_node(doctree[0], language="python", linenos=True, highlight_args={'linenostart': 5})
|
assert_node(doctree[0], language="python", linenos=True, highlight_args={'linenostart': 5})
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.sphinx(testroot='directive-csv-table')
|
||||||
|
def test_csv_table_directive(app):
|
||||||
|
# relative path from current document
|
||||||
|
text = ('.. csv-table::\n'
|
||||||
|
' :file: example.csv\n')
|
||||||
|
doctree = restructuredtext.parse(app, text, docname="subdir/index")
|
||||||
|
assert_node(doctree,
|
||||||
|
([nodes.table, nodes.tgroup, (nodes.colspec,
|
||||||
|
nodes.colspec,
|
||||||
|
nodes.colspec,
|
||||||
|
[nodes.tbody, nodes.row])],))
|
||||||
|
assert_node(doctree[0][0][3][0],
|
||||||
|
([nodes.entry, nodes.paragraph, "FOO"],
|
||||||
|
[nodes.entry, nodes.paragraph, "BAR"],
|
||||||
|
[nodes.entry, nodes.paragraph, "BAZ"]))
|
||||||
|
|
||||||
|
# absolute path from source directory
|
||||||
|
text = ('.. csv-table::\n'
|
||||||
|
' :file: /example.csv\n')
|
||||||
|
doctree = restructuredtext.parse(app, text, docname="subdir/index")
|
||||||
|
assert_node(doctree,
|
||||||
|
([nodes.table, nodes.tgroup, (nodes.colspec,
|
||||||
|
nodes.colspec,
|
||||||
|
nodes.colspec,
|
||||||
|
[nodes.tbody, nodes.row])],))
|
||||||
|
assert_node(doctree[0][0][3][0],
|
||||||
|
([nodes.entry, nodes.paragraph, "foo"],
|
||||||
|
[nodes.entry, nodes.paragraph, "bar"],
|
||||||
|
[nodes.entry, nodes.paragraph, "baz"]))
|
||||||
|
|
||||||
|
|
||||||
def test_math_directive(app):
|
def test_math_directive(app):
|
||||||
# normal case
|
# normal case
|
||||||
text = '.. math:: E = mc^2'
|
text = '.. math:: E = mc^2'
|
||||||
|
Loading…
Reference in New Issue
Block a user