#191: Don't escape the tilde in URIs in LaTeX.

This commit is contained in:
Georg Brandl 2009-06-04 18:11:17 +02:00
parent 42d12d8683
commit 501adbdb6f
3 changed files with 18 additions and 5 deletions

View File

@ -1,6 +1,8 @@
Release 0.6.2 (in development)
==============================
* #191: Don't escape the tilde in URIs in LaTeX.
* Don't consider contents of source comments for the search index.
* Set the default encoding to ``utf-8-sig`` to handle files with a

View File

@ -828,12 +828,16 @@ class LaTeXTranslator(nodes.NodeVisitor):
res = "%.3f\\linewidth" % (float(amount) / 100.0)
return res
def is_inline(self, node):
"""Check whether a node represents an inline element."""
return isinstance(node.parent, nodes.TextElement)
def visit_image(self, node):
attrs = node.attributes
pre = [] # in reverse order
post = []
include_graphics_options = []
inline = isinstance(node.parent, nodes.TextElement)
is_inline = self.is_inline(node)
if attrs.has_key('scale'):
# Could also be done with ``scale`` option to
# ``\includegraphics``; doing it this way for consistency.
@ -860,11 +864,11 @@ class LaTeXTranslator(nodes.NodeVisitor):
(0, 'left'): ('{', '\\hfill}'),
(0, 'right'): ('{\\hfill', '}'),}
try:
pre.append(align_prepost[inline, attrs['align']][0])
post.append(align_prepost[inline, attrs['align']][1])
pre.append(align_prepost[is_inline, attrs['align']][0])
post.append(align_prepost[is_inline, attrs['align']][1])
except KeyError:
pass # XXX complain here?
if not inline:
if not is_inline:
pre.append('\n')
post.append('\n')
pre.reverse()
@ -1022,7 +1026,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
self.context.append('')
elif uri.startswith('mailto:') or uri.startswith('http:') or \
uri.startswith('https:') or uri.startswith('ftp:'):
self.body.append('\\href{%s}{' % self.encode(uri))
self.body.append('\\href{%s}{' % self.encode_uri(uri))
self.context.append('}')
elif uri.startswith('#'):
self.body.append('\\hyperlink{%s}{' % uri[1:])
@ -1323,6 +1327,10 @@ class LaTeXTranslator(nodes.NodeVisitor):
text = text.replace('--', u'-{-}')
return text
def encode_uri(self, text):
# in \href, the tilde is allowed and must be represented literally
return self.encode(text).replace('\\textasciitilde{}', '~')
def visit_Text(self, node):
if self.verbatim is not None:
self.verbatim += node.astext()

View File

@ -118,3 +118,6 @@ def test_latex_escaping():
u'\\begin{Verbatim}[commandchars=@\\[\\]]\n'
u'@PYGZat[]@(@Gamma@)\\@(@infty@)@$@PYGZlb[]@PYGZrb[]\n'
u'\\end{Verbatim}')
# in URIs
yield (verify, u'`test <http://example.com/~me/>`_', None,
u'\\href{http://example.com/~me/}{test}')