@@ -45,6 +45,7 @@ import Text.Pandoc.Readers.HTML ( rawHtmlBlock, anyHtmlBlockTag,
45import Text.Pandoc.CharacterReferences ( decodeCharacterReferences )
45import Text.Pandoc.CharacterReferences ( decodeCharacterReferences )
46import Text.ParserCombinators.Parsec
46import Text.ParserCombinators.Parsec
47import Control.Monad (when, liftM, unless)
47import Control.Monad (when, liftM, unless)
48+import Network.URI ( unEscapeString, escapeURIString, isUnescapedInURI )
49-- | Read markdown from an input string and return a Pandoc document.
50-- | Read markdown from an input string and return a Pandoc document.
50readMarkdown :: ParserState -- ^ Parser state, including options for parser
51readMarkdown :: ParserState -- ^ Parser state, including options for parser
@@ -73,6 +74,13 @@ specialChars = "\\[]*_~`<>$!^-.&'\"\8216\8217\8220\8221;"
73-- auxiliary functions
74-- auxiliary functions
77+-- | Escape a URI in a markdown-appropriate way. First,
78+-- we unescape the string, since we don't want to screw things
79+-- up if they've entered a properly encoded URI. Then, we
81+escapeURI :: String -> String
82+escapeURI = escapeURIString isUnescapedInURI . unEscapeString
76indentSpaces :: GenParser Char ParserState [Char]
84indentSpaces :: GenParser Char ParserState [Char]
77indentSpaces = try $ do
85indentSpaces = try $ do
78 state <- getState
86 state <- getState
@@ -194,7 +202,7 @@ referenceKey = try $ do
194 tit <- option "" referenceTitle
202 tit <- option "" referenceTitle
195 blanklines
203 blanklines
196 endPos <- getPosition
204 endPos <- getPosition
197− let newkey = (lab, (intercalate "+" $ words $ removeTrailingSpace src, tit))
205+ let newkey = (lab, (escapeURI $ removeTrailingSpace src, tit))
198 st <- getState
206 st <- getState
199 let oldkeys = stateKeys st
207 let oldkeys = stateKeys st
200 updateState $ \s -> s { stateKeys = newkey : oldkeys }
208 updateState $ \s -> s { stateKeys = newkey : oldkeys }
@@ -1173,7 +1181,7 @@ source' = do
1173 tit <- option "" linkTitle
1181 tit <- option "" linkTitle
1174 skipSpaces
1182 skipSpaces
1176− return (intercalate "+" $ words $ removeTrailingSpace src, tit)
1184+ return (escapeURI $ removeTrailingSpace src, tit)
1178linkTitle :: GenParser Char st String
1186linkTitle :: GenParser Char st String
1179linkTitle = try $ do
1187linkTitle = try $ do