diff options
| author | John MacFarlane <[email protected]> | 2023-08-09 12:06:35 -0700 |
|---|---|---|
| committer | John MacFarlane <[email protected]> | 2023-08-09 12:09:41 -0700 |
| commit | cbb33fefb758250e745d9795ce97cc91e293626a (patch) | |
| tree | 8c45eb18a5f98110d4641c86e1c28fa7efa3f8a6 /src | |
| parent | cc371a9485241db3e0101180432e2122d023a9bd (diff) | |
LaTeX writer: Improve escaping of URIs in href, url.
Closes #8992.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Text/Pandoc/Writers/LaTeX/Util.hs | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/src/Text/Pandoc/Writers/LaTeX/Util.hs b/src/Text/Pandoc/Writers/LaTeX/Util.hs index 118709568..98bb2a01f 100644 --- a/src/Text/Pandoc/Writers/LaTeX/Util.hs +++ b/src/Text/Pandoc/Writers/LaTeX/Util.hs @@ -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) @@ -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 "?{}" @@ -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 |
