git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: Allow `%` characters in URLs.

This affects `\href` and `\url`. Closes #4832.

commit6d14f53bd96f123acb5d8030cf9402ddb2e41f01 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 12 Aug 2018, 16:45 (8 years ago) parent81131ef
2 files changed+52−13
src/Text/Pandoc/Readers/LaTeX.hs+31−13 test/command/4832.md+21
src/Text/Pandoc/Readers/LaTeX.hs+31−13
@@ -615,21 +615,28 @@ grouped parser = try $ do
615615 -- {{a,b}} should be parsed the same as {a,b}
616616 try (grouped parser <* egroup) <|> (mconcat <$> manyTill parser egroup)
617617
618−braced :: PandocMonad m => LP m [Tok]
619−braced = bgroup *> braced' 1
620− where braced' (n :: Int) =
621− handleEgroup n <|> handleBgroup n <|> handleOther n
622− handleEgroup n = do
618+braced' :: PandocMonad m => LP m Tok -> Int -> LP m [Tok]
619+braced' getTok n =
620+ handleEgroup <|> handleBgroup <|> handleOther
621+ where handleEgroup = do
623622 t <- egroup
624623 if n == 1
625624 then return []
626− else (t:) <$> braced' (n - 1)
627− handleBgroup n = do
625+ else (t:) <$> braced' getTok (n - 1)
626+ handleBgroup = do
628627 t <- bgroup
629− (t:) <$> braced' (n + 1)
630− handleOther n = do
631− t <- anyTok
632− (t:) <$> braced' n
628+ (t:) <$> braced' getTok (n + 1)
629+ handleOther = do
630+ t <- getTok
631+ (t:) <$> braced' getTok n
632+
633+braced :: PandocMonad m => LP m [Tok]
634+braced = bgroup *> braced' anyTok 1
635+
636+-- URLs require special handling, because they can contain %
637+-- characters. So we retonenize comments as we go...
638+bracedUrl :: PandocMonad m => LP m [Tok]
639+bracedUrl = bgroup *> braced' (retokenizeComment >> anyTok) 1
633640
634641bracketed :: PandocMonad m => Monoid a => LP m a -> LP m a
635642bracketed parser = try $ do
@@ -1290,6 +1297,17 @@ unescapeURL ('\\':x:xs) | isEscapable x = x:unescapeURL xs
12901297unescapeURL (x:xs) = x:unescapeURL xs
12911298unescapeURL [] = ""
12921299
1300+-- For handling URLs, which allow literal % characters...
1301+retokenizeComment :: PandocMonad m => LP m ()
1302+retokenizeComment = (do
1303+ Tok pos Comment txt <- satisfyTok isCommentTok
1304+ let updPos (Tok pos' toktype' txt') =
1305+ Tok (incSourceColumn (incSourceLine pos' (sourceLine pos - 1))
1306+ (sourceColumn pos)) toktype' txt'
1307+ let newtoks = map updPos $ tokenize (sourceName pos) $ T.tail txt
1308+ getInput >>= setInput . ((Tok pos Symbol "%" : newtoks) ++))
1309+ <|> return ()
1310+
12931311mathEnvWith :: PandocMonad m
12941312 => (Inlines -> a) -> Maybe Text -> Text -> LP m a
12951313mathEnvWith f innerEnv name = f . mathDisplay . inner <$> mathEnv name
@@ -1445,10 +1463,10 @@ inlineCommands = M.union inlineLanguageCommands $ M.fromList
14451463 , ("verb", doverb)
14461464 , ("lstinline", dolstinline)
14471465 , ("Verb", doverb)
1448− , ("url", ((unescapeURL . T.unpack . untokenize) <$> braced) >>= \url ->
1466+ , ("url", ((unescapeURL . T.unpack . untokenize) <$> bracedUrl) >>= \url ->
14491467 pure (link url "" (str url)))
14501468 , ("href", (unescapeURL . toksToString <$>
1451− braced <* optional sp) >>= \url ->
1469+ bracedUrl <* optional sp) >>= \url ->
14521470 tok >>= \lab -> pure (link url "" lab))
14531471 , ("includegraphics", do options <- option [] keyvals
14541472 src <- unescapeURL . T.unpack .
test/command/4832.mdnew file+21
@@ -0,0 +1,21 @@
1+```
2+% pandoc -f latex -t native
3+\url{http://example.com/foo%20bar.htm}
4+^D
5+[Para [Link ("",[],[]) [Str "http://example.com/foo%20bar.htm"] ("http://example.com/foo%20bar.htm","")]]
6+```
7+
8+```
9+% pandoc -f latex -t native
10+\url{http://example.com/foo{bar}.htm}
11+^D
12+[Para [Link ("",[],[]) [Str "http://example.com/foo{bar}.htm"] ("http://example.com/foo{bar}.htm","")]]
13+```
14+
15+```
16+% pandoc -f latex -t native
17+\href{http://example.com/foo%20bar}{Foobar}
18+^D
19+[Para [Link ("",[],[]) [Str "Foobar"] ("http://example.com/foo%20bar","")]]
20+```
21+