Skip to content

Commit

Permalink
Fix #4550: html: Centering tables by default using CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed May 6, 2019
1 parent 035d550 commit 107c20a
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 66 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Incompatible changes
API directly
* #6230: The anchor of term in glossary directive is changed if it is consisted
by non-ASCII characters
* #4550: html: Centering tables by default using CSS

Deprecated
----------
Expand Down
2 changes: 1 addition & 1 deletion sphinx/templates/latex/longtable.tex_t
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
\begin{savenotes}\sphinxatlongtablestart\begin{longtable}
<%- if table.align == 'center' -%>
<%- if table.align in ('center', 'default') -%>
[c]
<%- elif table.align == 'left' -%>
[l]
Expand Down
2 changes: 1 addition & 1 deletion sphinx/templates/latex/tabular.tex_t
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
\begin{savenotes}\sphinxattablestart
<% if table.align -%>
<%- if table.align == 'center' -%>
<%- if table.align in ('center', 'default') -%>
\centering
<%- elif table.align == 'left' -%>
\raggedright
Expand Down
2 changes: 1 addition & 1 deletion sphinx/templates/latex/tabulary.tex_t
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
\begin{savenotes}\sphinxattablestart
<% if table.align -%>
<%- if table.align == 'center' -%>
<%- if table.align in ('center', 'default') -%>
\centering
<%- elif table.align == 'left' -%>
\raggedright
Expand Down
15 changes: 15 additions & 0 deletions sphinx/themes/basic/static/basic.css_t
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ img.align-center, .figure.align-center, object.align-center {
margin-right: auto;
}

img.align-default, .figure.align-default {
display: block;
margin-left: auto;
margin-right: auto;
}

.align-left {
text-align: left;
}
Expand All @@ -297,6 +303,10 @@ img.align-center, .figure.align-center, object.align-center {
text-align: center;
}

.align-default {
text-align: center;
}

.align-right {
text-align: right;
}
Expand Down Expand Up @@ -368,6 +378,11 @@ table.align-center {
margin-right: auto;
}

table.align-default {
margin-left: auto;
margin-right: auto;
}

table caption span.caption-number {
font-style: italic;
}
Expand Down
2 changes: 1 addition & 1 deletion sphinx/transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def apply(self, **kwargs):
# type: (Any) -> None
matcher = NodeMatcher(nodes.table, nodes.figure)
for node in self.document.traverse(matcher): # type: nodes.Element
node.setdefault('align', 'center')
node.setdefault('align', 'default')


class FilterSystemMessages(SphinxTransform):
Expand Down
1 change: 1 addition & 0 deletions sphinx/writers/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,7 @@ def visit_image(self, node):
(1, 'middle'): ('\\raisebox{-0.5\\height}{', '}'),
(1, 'bottom'): ('\\raisebox{-\\height}{', '}'),
(0, 'center'): ('{\\hspace*{\\fill}', '\\hspace*{\\fill}}'),
(0, 'default'): ('{\\hspace*{\\fill}', '\\hspace*{\\fill}}'),
# These 2 don't exactly do the right thing. The image should
# be floated alongside the paragraph. See
# https://www.w3.org/TR/html4/struct/objects.html#adef-align-IMG
Expand Down
114 changes: 57 additions & 57 deletions tests/test_build_html.py

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/test_ext_graphviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_graphviz_png_html(app, status, warning):
app.builder.build_all()

content = (app.outdir / 'index.html').text()
html = (r'<div class="figure align-center" .*?>\s*'
html = (r'<div class="figure align-default" .*?>\s*'
r'<div class="graphviz"><img .*?/></div>\s*<p class="caption">'
r'<span class="caption-text">caption of graph</span>.*</p>\s*</div>')
assert re.search(html, content, re.S)
Expand Down Expand Up @@ -52,7 +52,7 @@ def test_graphviz_svg_html(app, status, warning):

content = (app.outdir / 'index.html').text()

html = (r'<div class=\"figure align-center\" .*?>\n'
html = (r'<div class=\"figure align-default\" .*?>\n'
r'<div class="graphviz"><object data=\".*\.svg\".*>\n'
r'\s*<p class=\"warning\">digraph foo {\n'
r'bar -&gt; baz\n'
Expand Down
6 changes: 3 additions & 3 deletions tests/test_ext_inheritance_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_inheritance_diagram_png_html(app, status, warning):

content = (app.outdir / 'index.html').text()

pattern = ('<div class="figure align-center" id="id1">\n'
pattern = ('<div class="figure align-default" id="id1">\n'
'<div class="graphviz">'
'<img src="_images/inheritance-\\w+.png" alt="Inheritance diagram of test.Foo" '
'class="inheritance graphviz" /></div>\n<p class="caption">'
Expand All @@ -40,7 +40,7 @@ def test_inheritance_diagram_svg_html(app, status, warning):

content = (app.outdir / 'index.html').text()

pattern = ('<div class="figure align-center" id="id1">\n'
pattern = ('<div class="figure align-default" id="id1">\n'
'<div class="graphviz">'
'<object data="_images/inheritance-\\w+.svg" '
'type="image/svg\\+xml" class="inheritance graphviz">\n'
Expand Down Expand Up @@ -80,7 +80,7 @@ def test_inheritance_diagram_latex_alias(app, status, warning):

content = (app.outdir / 'index.html').text()

pattern = ('<div class="figure align-center" id="id1">\n'
pattern = ('<div class="figure align-default" id="id1">\n'
'<div class="graphviz">'
'<img src="_images/inheritance-\\w+.png" alt="Inheritance diagram of test.Foo" '
'class="inheritance graphviz" /></div>\n<p class="caption">'
Expand Down

0 comments on commit 107c20a

Please sign in to comment.