git.iko.soy/typeable/pandoc
on master help sign in
Markdown reader: properly escape URIs.

+ Resolves Issue #220. + Added escapeURI function to Markdown reader. This escapes links in a way that makes sense for markdown. If they've used URI escapes like %20 in their link, these will be preserved. But if they've used a special character or space without escaping it, it will be escaped. This should make sense in most cases. + Previously pandoc collapsed adjacent spaces and replaced these sequences of spaces with + characters. That isn't correct for a URI path (+ is to be used only in the query part). We've also removed the space-collapsing behavior. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1847 788f1e2b-df1e-0410-8736-df70ead52e1b

commit07ae5bc264d1c280dc8c7e0798a0df4bd560ab74 authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateSat 27 Feb 2010, 03:06 (17 years ago) parentd3f1ddf
2 files changed+12−4
src/Text/Pandoc/Readers/Markdown.hs+10−2 tests/markdown-reader-more.native+2−2
src/Text/Pandoc/Readers/Markdown.hs+10−2
@@ -45,6 +45,7 @@ import Text.Pandoc.Readers.HTML ( rawHtmlBlock, anyHtmlBlockTag,
4545import Text.Pandoc.CharacterReferences ( decodeCharacterReferences )
4646import Text.ParserCombinators.Parsec
4747import Control.Monad (when, liftM, unless)
48+import Network.URI ( unEscapeString, escapeURIString, isUnescapedInURI )
4849
4950-- | Read markdown from an input string and return a Pandoc document.
5051readMarkdown :: ParserState -- ^ Parser state, including options for parser
@@ -73,6 +74,13 @@ specialChars = "\\[]*_~`<>$!^-.&'\"\8216\8217\8220\8221;"
7374-- auxiliary functions
7475--
7576
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
80+-- escape the result.
81+escapeURI :: String -> String
82+escapeURI = escapeURIString isUnescapedInURI . unEscapeString
83+
7684indentSpaces :: GenParser Char ParserState [Char]
7785indentSpaces = try $ do
7886 state <- getState
@@ -194,7 +202,7 @@ referenceKey = try $ do
194202 tit <- option "" referenceTitle
195203 blanklines
196204 endPos <- getPosition
197− let newkey = (lab, (intercalate "+" $ words $ removeTrailingSpace src, tit))
205+ let newkey = (lab, (escapeURI $ removeTrailingSpace src, tit))
198206 st <- getState
199207 let oldkeys = stateKeys st
200208 updateState $ \s -> s { stateKeys = newkey : oldkeys }
@@ -1173,7 +1181,7 @@ source' = do
11731181 tit <- option "" linkTitle
11741182 skipSpaces
11751183 eof
1176− return (intercalate "+" $ words $ removeTrailingSpace src, tit)
1184+ return (escapeURI $ removeTrailingSpace src, tit)
11771185
11781186linkTitle :: GenParser Char st String
11791187linkTitle = try $ do
tests/markdown-reader-more.native+2−2
@@ -6,8 +6,8 @@ Pandoc (Meta [] [] [])
66, Para [TeX "\\placeformula",Space,TeX "\\startformula\n L_{1} = L_{2}\n \\stopformula"]
77, Para [TeX "\\start[a2]\n\\start[a2]\n\\stop[a2]\n\\stop[a2]"]
88, Header 2 [Str "URLs",Space,Str "with",Space,Str "spaces"]
9−, Para [Link [Str "foo"] ("/bar+and+baz",""),Space,Link [Str "foo"] ("/bar+and+baz",""),Space,Link [Str "foo"] ("/bar+and+baz",""),Space,Link [Str "foo"] ("bar+baz","title")]
10−, Para [Link [Str "baz"] ("/foo+foo",""),Space,Link [Str "bam"] ("/foo+fee",""),Space,Link [Str "bork"] ("/foo/zee+zob","title")]
9+, Para [Link [Str "foo"] ("/bar%20and%20baz",""),Space,Link [Str "foo"] ("/bar%20and%20baz",""),Space,Link [Str "foo"] ("/bar%20%20and%20%20baz",""),Space,Link [Str "foo"] ("bar%20baz","title")]
10+, Para [Link [Str "baz"] ("/foo%20foo",""),Space,Link [Str "bam"] ("/foo%20fee",""),Space,Link [Str "bork"] ("/foo/zee%20zob","title")]
1111, Header 2 [Str "Horizontal",Space,Str "rules",Space,Str "with",Space,Str "spaces",Space,Str "at",Space,Str "end"]
1212, HorizontalRule
1313, HorizontalRule