Show warnings if invalid dimension units found

This commit is contained in:
Takeshi KOMIYA committed 2016-06-26 22:32:34 +09:00
1 parent efd74a9e14
commit 4bc5a242ce
3 files changed
+52 -19

No files matched your search

+2
View File
@@ -49,6 +49,8 @@ Features added
lines wrapped to fit table cell (ref: #2704)
* #2597: Show warning messages as darkred
* latex, allow image dimensions using px unit (default is 96px=1in)
* Show warnings if invalid dimension units found
Bugs fixed
----------
+22 -19
View File
@@ -275,6 +275,24 @@ def escape_abbr(text):
return re.sub('\.(?=\s|$)', '.\\@', text)
def rstdim_to_latexdim(width_str):
"""Convert `width_str` with rst length to LaTeX length."""
match = re.match('(\d*\.?\d*)\s*(\S*)', width_str)
if not match:
raise ValueError
res = width_str
amount, unit = match.groups()[:2]
if not unit:
return None
elif unit == 'pt':
res = '%sbp' % amount # convert to 'bp'
elif unit == "px":
res = "%.3f\\sphinxpxdimen" % (float(amount))
elif unit == "%":
res = "%.3f\\linewidth" % (float(amount) / 100.0)
return res
class LaTeXTranslator(nodes.NodeVisitor):
sectionnames = ["part", "chapter", "section", "subsection",
"subsubsection", "paragraph", "subparagraph"]
@@ -1360,25 +1378,10 @@ class LaTeXTranslator(nodes.NodeVisitor):
pass
def latex_image_length(self, width_str):
"""Convert `width_str` with rst length to LaTeX length.
This function is copied from docutils' latex writer
"""
match = re.match('(\d*\.?\d*)\s*(\S*)', width_str)
if not match:
# fallback
return width_str
res = width_str
amount, unit = match.groups()[:2]
if not unit:
return None
elif unit == 'pt':
res = '%sbp' % amount # convert to 'bp'
elif unit == "px":
res = "%.3f\\sphinxpxdimen" % (float(amount))
elif unit == "%":
res = "%.3f\\linewidth" % (float(amount) / 100.0)
return res
try:
return rstdim_to_latexdim(width_str)
except ValueError:
self.builder.warn('dimension unit %s is invalid. Ignored.' % width_str)
def is_inline(self, node):
"""Check whether a node represents an inline element."""
+28
View File
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
"""
test_writer_latex
~~~~~~~~~~~~~~~~
Test the LaTeX writer
:copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from __future__ import print_function
from sphinx.writers.latex import rstdim_to_latexdim
def test_rstdim_to_latexdim():
# Length units docutils supported
# http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#length-units
assert rstdim_to_latexdim('160em') == '160em'
assert rstdim_to_latexdim('160px') == '160.000\\sphinxpxdimen'
assert rstdim_to_latexdim('160in') == '160in'
assert rstdim_to_latexdim('160cm') == '160cm'
assert rstdim_to_latexdim('160mm') == '160mm'
assert rstdim_to_latexdim('160pt') == '160bp'
assert rstdim_to_latexdim('160pc') == '160pc'
assert rstdim_to_latexdim('30%') == '0.300\\linewidth'
assert rstdim_to_latexdim('160') is None
assert rstdim_to_latexdim('160.0em') == '160.0em'