diff --git a/CHANGES b/CHANGES index 1bc081f76..ba5c97429 100644 --- a/CHANGES +++ b/CHANGES @@ -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 diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 4cd6070e2..2ef92e244 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -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() diff --git a/tests/test_markup.py b/tests/test_markup.py index 77f2016ed..d65b798e8 100644 --- a/tests/test_markup.py +++ b/tests/test_markup.py @@ -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 `_', None, + u'\\href{http://example.com/~me/}{test}')