Skip to content

Commit

Permalink
LaTeX writer: Improve escaping of URIs in href, url.
Browse files Browse the repository at this point in the history
Closes #8992.
  • Loading branch information
jgm committed Aug 9, 2023
1 parent cc371a9 commit cbb33fe
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
33 changes: 19 additions & 14 deletions src/Text/Pandoc/Writers/LaTeX/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Text.Pandoc.Writers.LaTeX.Types (LW, WriterState(..))
import Text.Pandoc.Writers.LaTeX.Lang (toBabel)
import Text.Pandoc.Highlighting (toListingsLanguage)
import Text.DocLayout
import Network.URI (escapeURIString)
import Text.Pandoc.Definition
import Text.Pandoc.ImageSize (showFl)
import Control.Monad.State.Strict (gets, modify)
Expand Down Expand Up @@ -91,6 +92,13 @@ stringToLaTeX context zs = do
'\'':_ -> cs <> "\\," <> xs -- add thin space
_ -> cs <> xs
in case x of
'\\'| isUrl -> emitc '/' -- NB. / works as path sep even on Windows
c | isUrl ->
if c `elem` ['{', '}', '|', '^', '~', '[', ']', '`']
then emits (escapeURIString (const False) [c])
else emitc c
'{' -> emits "\\{"
'}' -> emits "\\}"
'?' | ligatures -> -- avoid ?` ligature
case xs of
'`':_ -> emits "?{}"
Expand All @@ -99,27 +107,24 @@ stringToLaTeX context zs = do
case xs of
'`':_ -> emits "!{}"
_ -> emitc x
'{' -> emits "\\{"
'}' -> emits "\\}"
'`' | ctx == CodeString -> emitcseq "\\textasciigrave"
'$' | not isUrl -> emits "\\$"
'$' -> emits "\\$"
'%' -> emits "\\%"
'&' | not isUrl -> emits "\\&"
'_' | not isUrl -> emits "\\_"
'&' -> emits "\\&"
'_' -> emits "\\_"
'#' -> emits "\\#"
'-' | not isUrl -> case xs of
'-' -> case xs of
-- prevent adjacent hyphens from forming ligatures
('-':_) -> emits "-\\/"
_ -> emitc '-'
'~' | not isUrl -> emitcseq "\\textasciitilde"
'~' -> emitcseq "\\textasciitilde"
'^' -> emits "\\^{}"
'\\'| isUrl -> emitc '/' -- NB. / works as path sep even on Windows
| otherwise -> emitcseq "\\textbackslash"
'|' | not isUrl -> emitcseq "\\textbar"
'<' -> emitcseq "\\textless"
'>' -> emitcseq "\\textgreater"
'[' -> emits "{[}" -- to avoid interpretation as
']' -> emits "{]}" -- optional arguments
'\\' -> emitcseq "\\textbackslash"
'|' -> emitcseq "\\textbar"
'<' -> emitcseq "\\textless"
'>' -> emitcseq "\\textgreater"
'[' -> emits "{[}" -- to avoid interpretation as
']' -> emits "{]}" -- optional arguments
'\'' -> emitcseq "\\textquotesingle"
'\160' -> emits "~"
'\x200B' -> emits "\\hspace{0pt}" -- zero-width space
Expand Down
6 changes: 3 additions & 3 deletions test/command/5340.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
^D
\url{https://example.com/foo-bar}
\url{https://example.com/foo--bar}
\url{https://example.com/foo\%2Dbar}
\url{https://example.com/foo\%2D\%2Dbar}
\url{https://example.com/foo\%2D\%2Dbar}
\url{https://example.com/foo%2Dbar}
\url{https://example.com/foo%2D%2Dbar}
\url{https://example.com/foo%2D%2Dbar}
```
6 changes: 6 additions & 0 deletions test/command/8992.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```
% pandoc -t latex
[A theorem](https://en.wikipedia.org/wiki/Rice's_theorem).
^D
\href{https://en.wikipedia.org/wiki/Rice's_theorem}{A theorem}.
```

0 comments on commit cbb33fe

Please sign in to comment.