git.iko.soy/typeable/pandoc
on master help sign in
Properly escape URIs in all readers.
commitc87d52223ad97ae7f21c69b09ec7b1467bd35670 authorJohn MacFarlane <jgm@berkeley.edu> dateTue 23 Mar 2010, 15:07 (17 years ago) parentc3384bb
4 files changed+37−44
src/Text/Pandoc/Readers/HTML.hs+3−3 src/Text/Pandoc/Readers/LaTeX.hs+6−7 src/Text/Pandoc/Readers/Markdown.hs+9−16 src/Text/Pandoc/Readers/RST.hs+19−18
src/Text/Pandoc/Readers/HTML.hs+3−3
@@ -182,7 +182,7 @@ unsanitaryURI u =
182182 "ldaps:", "magnet:", "mms:", "msnim:", "notes:", "rsync:",
183183 "secondlife:", "skype:", "ssh:", "sftp:", "smb:", "sms:",
184184 "snews:", "webcal:", "ymsgr:"]
185− in case parseURIReference (stringToURI u) of
185+ in case parseURIReference (escapeURI u) of
186186 Just p -> (map toLower $ uriScheme p) `notElem` safeURISchemes
187187 Nothing -> True
188188
@@ -746,7 +746,7 @@ link = try $ do
746746 Nothing -> fail "no href"
747747 let title = fromMaybe "" $ extractAttribute "title" attributes
748748 lab <- inlinesTilEnd "a"
749− return $ Link (normalizeSpaces lab) (url, title)
749+ return $ Link (normalizeSpaces lab) (escapeURI url, title)
750750
751751image :: GenParser Char ParserState Inline
752752image = try $ do
@@ -756,5 +756,5 @@ image = try $ do
756756 Nothing -> fail "no src"
757757 let title = fromMaybe "" $ extractAttribute "title" attributes
758758 let alt = fromMaybe "" (extractAttribute "alt" attributes)
759− return $ Image [Str alt] (url, title)
759+ return $ Image [Str alt] (escapeURI url, title)
760760
src/Text/Pandoc/Readers/LaTeX.hs+6−7
@@ -738,7 +738,7 @@ url :: GenParser Char ParserState Inline
738738url = try $ do
739739 string "\\url"
740740 url' <- charsInBalanced '{' '}'
741− return $ Link [Code url'] (url', "")
741+ return $ Link [Code url'] (escapeURI url', "")
742742
743743link :: GenParser Char ParserState Inline
744744link = try $ do
@@ -746,17 +746,16 @@ link = try $ do
746746 url' <- manyTill anyChar (char '}')
747747 char '{'
748748 label' <- manyTill inline (char '}')
749− return $ Link (normalizeSpaces label') (url', "")
749+ return $ Link (normalizeSpaces label') (escapeURI url', "")
750750
751751image :: GenParser Char ParserState Inline
752752image = try $ do
753753 ("includegraphics", _, args) <- command
754754 let args' = filter isArg args -- filter out options
755− let src = if null args' then
756− ("", "")
757− else
758− (stripFirstAndLast (head args'), "")
759− return $ Image [Str "image"] src
755+ let (src,tit) = case args' of
756+ [] -> ("", "")
757+ (x:_) -> (stripFirstAndLast x, "")
758+ return $ Image [Str "image"] (escapeURI src, tit)
760759
761760footnote :: GenParser Char ParserState Inline
762761footnote = try $ do
src/Text/Pandoc/Readers/Markdown.hs+9−16
@@ -31,7 +31,7 @@ module Text.Pandoc.Readers.Markdown (
3131 readMarkdown
3232 ) where
3333
34−import Data.List ( transpose, isPrefixOf, isSuffixOf, sortBy, findIndex, intercalate )
34+import Data.List ( transpose, isSuffixOf, sortBy, findIndex, intercalate )
3535import Data.Ord ( comparing )
3636import Data.Char ( isAlphaNum )
3737import Data.Maybe
@@ -73,10 +73,6 @@ specialChars = "\\[]*_~`<>$!^-.&'\"\8216\8217\8220\8221;"
7373-- auxiliary functions
7474--
7575
76−-- | Replace spaces with %20
77−uriEscapeSpaces :: String -> String
78−uriEscapeSpaces = substitute " " "%20"
79−
8076indentSpaces :: GenParser Char ParserState [Char]
8177indentSpaces = try $ do
8278 state <- getState
@@ -206,7 +202,7 @@ referenceKey = try $ do
206202 tit <- option "" referenceTitle
207203 blanklines
208204 endPos <- getPosition
209− let newkey = (lab, (uriEscapeSpaces $ removeTrailingSpace src, tit))
205+ let newkey = (lab, (escapeURI $ removeTrailingSpace src, tit))
210206 st <- getState
211207 let oldkeys = stateKeys st
212208 updateState $ \s -> s { stateKeys = newkey : oldkeys }
@@ -1194,7 +1190,7 @@ source' = do
11941190 tit <- option "" linkTitle
11951191 skipSpaces
11961192 eof
1197− return (uriEscapeSpaces $ removeTrailingSpace src, tit)
1193+ return (escapeURI $ removeTrailingSpace src, tit)
11981194
11991195linkTitle :: GenParser Char st String
12001196linkTitle = try $ do
@@ -1208,11 +1204,11 @@ linkTitle = try $ do
12081204link :: GenParser Char ParserState Inline
12091205link = try $ do
12101206 lab <- reference
1211− src <- source <|> referenceLink lab
1207+ (src, tit) <- source <|> referenceLink lab
12121208 sanitize <- getState >>= return . stateSanitizeHTML
1213− if sanitize && unsanitaryURI (fst src)
1209+ if sanitize && unsanitaryURI src
12141210 then fail "Unsanitary URI"
1215− else return $ Link lab src
1211+ else return $ Link lab (src, tit)
12161212
12171213-- a link like [this][ref] or [this][] or [this]
12181214referenceLink :: [Inline]
@@ -1229,18 +1225,15 @@ referenceLink lab = do
12291225autoLink :: GenParser Char ParserState Inline
12301226autoLink = try $ do
12311227 char '<'
1232− src <- uri <|> (emailAddress >>= (return . ("mailto:" ++)))
1228+ (orig, src) <- uri <|> emailAddress
12331229 char '>'
1234− let src' = if "mailto:" `isPrefixOf` src
1235− then drop 7 src
1236− else src
12371230 st <- getState
12381231 let sanitize = stateSanitizeHTML st
12391232 if sanitize && unsanitaryURI src
12401233 then fail "Unsanitary URI"
12411234 else return $ if stateStrict st
1242− then Link [Str src'] (src, "")
1243− else Link [Code src'] (src, "")
1235+ then Link [Str orig] (src, "")
1236+ else Link [Code orig] (src, "")
12441237
12451238image :: GenParser Char ParserState Inline
12461239image = try $ do
src/Text/Pandoc/Readers/RST.hs+19−18
@@ -556,7 +556,7 @@ targetURI = do
556556 contents <- many1 (try (many spaceChar >> newline >>
557557 many1 spaceChar >> noneOf " \t\n") <|> noneOf "\n")
558558 blanklines
559− return contents
559+ return $ escapeURI $ removeLeadingTrailingSpace $ contents
560560
561561imageKey :: GenParser Char ParserState ([Inline], (String, [Char]))
562562imageKey = try $ do
@@ -565,13 +565,13 @@ imageKey = try $ do
565565 skipSpaces
566566 string "image::"
567567 src <- targetURI
568− return (normalizeSpaces ref, (removeLeadingTrailingSpace src, ""))
568+ return (normalizeSpaces ref, (src, ""))
569569
570570anonymousKey :: GenParser Char st ([Inline], (String, [Char]))
571571anonymousKey = try $ do
572572 oneOfStrings [".. __:", "__"]
573573 src <- targetURI
574− return ([Str "_"], (removeLeadingTrailingSpace src, ""))
574+ return ([Str "_"], (src, ""))
575575
576576regularKey :: GenParser Char ParserState ([Inline], (String, [Char]))
577577regularKey = try $ do
@@ -579,7 +579,7 @@ regularKey = try $ do
579579 ref <- referenceName
580580 char ':'
581581 src <- targetURI
582− return (normalizeSpaces ref, (removeLeadingTrailingSpace src, ""))
582+ return (normalizeSpaces ref, (src, ""))
583583
584584--
585585-- tables
@@ -883,7 +883,8 @@ explicitLink = try $ do
883883 src <- manyTill (noneOf ">\n") (char '>')
884884 skipSpaces
885885 string "`_"
886− return $ Link (normalizeSpaces label') (removeLeadingTrailingSpace src, "")
886+ return $ Link (normalizeSpaces label')
887+ (escapeURI $ removeLeadingTrailingSpace src, "")
887888
888889referenceLink :: GenParser Char ParserState Inline
889890referenceLink = try $ do
@@ -891,25 +892,25 @@ referenceLink = try $ do
891892 key <- option label' (do{char '_'; return [Str "_"]}) -- anonymous link
892893 state <- getState
893894 let keyTable = stateKeys state
894− src <- case lookupKeySrc keyTable key of
895− Nothing -> fail "no corresponding key"
896− Just target -> return target
895+ (src,tit) <- case lookupKeySrc keyTable key of
896+ Nothing -> fail "no corresponding key"
897+ Just target -> return target
897898 -- if anonymous link, remove first anon key so it won't be used again
898899 let keyTable' = if (key == [Str "_"]) -- anonymous link?
899− then delete ([Str "_"], src) keyTable -- remove first anon key
900+ then delete ([Str "_"], (src,tit)) keyTable -- remove first anon key
900901 else keyTable
901902 setState $ state { stateKeys = keyTable' }
902− return $ Link (normalizeSpaces label') src
903+ return $ Link (normalizeSpaces label') (src, tit)
903904
904905autoURI :: GenParser Char ParserState Inline
905906autoURI = do
906− src <- uri
907− return $ Link [Str src] (src, "")
907+ (orig, src) <- uri
908+ return $ Link [Str orig] (src, "")
908909
909910autoEmail :: GenParser Char ParserState Inline
910911autoEmail = do
911− src <- emailAddress
912− return $ Link [Str src] ("mailto:" ++ src, "")
912+ (orig, src) <- emailAddress
913+ return $ Link [Str orig] (src, "")
913914
914915autoLink :: GenParser Char ParserState Inline
915916autoLink = autoURI <|> autoEmail
@@ -921,7 +922,7 @@ image = try $ do
921922 ref <- manyTill inline (char '|')
922923 state <- getState
923924 let keyTable = stateKeys state
924− src <- case lookupKeySrc keyTable ref of
925− Nothing -> fail "no corresponding key"
926− Just target -> return target
927− return $ Image (normalizeSpaces ref) src
925+ (src,tit) <- case lookupKeySrc keyTable ref of
926+ Nothing -> fail "no corresponding key"
927+ Just target -> return target
928+ return $ Image (normalizeSpaces ref) (src, tit)