#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) 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. * Don't consider contents of source comments for the search index.
* Set the default encoding to ``utf-8-sig`` to handle files with a * 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) res = "%.3f\\linewidth" % (float(amount) / 100.0)
return res 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): def visit_image(self, node):
attrs = node.attributes attrs = node.attributes
pre = [] # in reverse order pre = [] # in reverse order
post = [] post = []
include_graphics_options = [] include_graphics_options = []
inline = isinstance(node.parent, nodes.TextElement) is_inline = self.is_inline(node)
if attrs.has_key('scale'): if attrs.has_key('scale'):
# Could also be done with ``scale`` option to # Could also be done with ``scale`` option to
# ``\includegraphics``; doing it this way for consistency. # ``\includegraphics``; doing it this way for consistency.
@ -860,11 +864,11 @@ class LaTeXTranslator(nodes.NodeVisitor):
(0, 'left'): ('{', '\\hfill}'), (0, 'left'): ('{', '\\hfill}'),
(0, 'right'): ('{\\hfill', '}'),} (0, 'right'): ('{\\hfill', '}'),}
try: try:
pre.append(align_prepost[inline, attrs['align']][0]) pre.append(align_prepost[is_inline, attrs['align']][0])
post.append(align_prepost[inline, attrs['align']][1]) post.append(align_prepost[is_inline, attrs['align']][1])
except KeyError: except KeyError:
pass # XXX complain here? pass # XXX complain here?
if not inline: if not is_inline:
pre.append('\n') pre.append('\n')
post.append('\n') post.append('\n')
pre.reverse() pre.reverse()
@ -1022,7 +1026,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
self.context.append('') self.context.append('')
elif uri.startswith('mailto:') or uri.startswith('http:') or \ elif uri.startswith('mailto:') or uri.startswith('http:') or \
uri.startswith('https:') or uri.startswith('ftp:'): 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('}') self.context.append('}')
elif uri.startswith('#'): elif uri.startswith('#'):
self.body.append('\\hyperlink{%s}{' % uri[1:]) self.body.append('\\hyperlink{%s}{' % uri[1:])
@ -1323,6 +1327,10 @@ class LaTeXTranslator(nodes.NodeVisitor):
text = text.replace('--', u'-{-}') text = text.replace('--', u'-{-}')
return text 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): def visit_Text(self, node):
if self.verbatim is not None: if self.verbatim is not None:
self.verbatim += node.astext() self.verbatim += node.astext()

View File

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