git.iko.soy/typeable/pandoc
on master help sign in
Shared: Removed unescapeURI, modified escapeURI.

escapeURI now only escapes space characters, leaving unicode characters as they are, instead of converting them to octets and URL-encoding them, as before. This gives more readable URIs. User agents now do the percent-encoding themselves. URIs are no longer unescaped at all on conversion to markdown, asciidoc, rst, org. Closes #349.

commitbdec07bac9efb737dbc60e6985d8ba1500a5eeb9 authorJohn MacFarlane <fiddlosopher@gmail.com> dateFri 2 Dec 2011, 19:39 (15 years ago) parentc6456ef
6 files changed+18−33
src/Text/Pandoc/Shared.hs+8−17 src/Text/Pandoc/Writers/AsciiDoc.hs+2−4 src/Text/Pandoc/Writers/Markdown.hs+1−2 src/Text/Pandoc/Writers/Org.hs+1−2 src/Text/Pandoc/Writers/RST.hs+3−5 tests/markdown-reader-more.native+3−3
src/Text/Pandoc/Shared.hs+8−17
@@ -44,7 +44,6 @@ module Text.Pandoc.Shared (
4444 camelCaseToHyphenated,
4545 toRomanNumeral,
4646 escapeURI,
47− unescapeURI,
4847 tabFilter,
4948 -- * Pandoc block and inline list processing
5049 orderedListMarkers,
@@ -73,11 +72,10 @@ module Text.Pandoc.Shared (
7372import Text.Pandoc.Definition
7473import Text.Pandoc.Generic
7574import qualified Text.Pandoc.UTF8 as UTF8 (readFile)
76−import Data.Char ( toLower, isLower, isUpper, isAlpha, isAscii,
77− isLetter, isDigit )
75+import Data.Char ( toLower, isLower, isUpper, isAlpha,
76+ isLetter, isDigit, isSpace )
7877import Data.List ( find, isPrefixOf, intercalate )
79−import Network.URI ( isAllowedInURI, escapeURIString, unEscapeString )
80−import Codec.Binary.UTF8.String ( encodeString, decodeString )
78+import Network.URI ( escapeURIString )
8179import System.Directory
8280import System.FilePath ( (</>) )
8381import Data.Generics (Typeable, Data)
@@ -181,16 +179,9 @@ toRomanNumeral x =
181179 _ | x >= 1 -> "I" ++ toRomanNumeral (x - 1)
182180 _ -> ""
183181
184−-- | Escape unicode characters in a URI. Characters that are
185−-- already valid in a URI, including % and ?, are left alone.
182+-- | Escape whitespace in URI.
186183escapeURI :: String -> String
187−escapeURI = escapeURIString isAllowedInURI . encodeString
188−
189−-- | Unescape unicode and some special characters in a URI, but
190−-- without introducing spaces.
191−unescapeURI :: String -> String
192−unescapeURI = escapeURIString (\c -> isAllowedInURI c || not (isAscii c)) .
193− decodeString . unEscapeString
184+escapeURI = escapeURIString (not . isSpace)
194185
195186-- | Convert tabs to spaces and filter out DOS line endings.
196187-- Tabs will be preserved if tab stop is set to 0.
@@ -304,9 +295,9 @@ consolidateInlines (Str x : ys) =
304295 fromStr (Str z) = z
305296 fromStr _ = error "consolidateInlines - fromStr - not a Str"
306297consolidateInlines (Space : ys) = Space : rest
307− where isSpace Space = True
308− isSpace _ = False
309− rest = consolidateInlines $ dropWhile isSpace ys
298+ where isSp Space = True
299+ isSp _ = False
300+ rest = consolidateInlines $ dropWhile isSp ys
310301consolidateInlines (Emph xs : Emph ys : zs) = consolidateInlines $
311302 Emph (xs ++ ys) : zs
312303consolidateInlines (Strong xs : Strong ys : zs) = consolidateInlines $
src/Text/Pandoc/Writers/AsciiDoc.hs+2−4
@@ -336,12 +336,11 @@ inlineToAsciiDoc _ (RawInline _ _) = return empty
336336inlineToAsciiDoc _ (LineBreak) = return $ " +" <> cr
337337inlineToAsciiDoc _ Space = return space
338338inlineToAsciiDoc opts (Cite _ lst) = inlineListToAsciiDoc opts lst
339−inlineToAsciiDoc opts (Link txt (src', _tit)) = do
339+inlineToAsciiDoc opts (Link txt (src, _tit)) = do
340340-- relative: link:downloads/foo.zip[download foo.zip]
341341-- abs: http://google.cod[Google]
342342-- or my@email.com[email john]
343343 linktext <- inlineListToAsciiDoc opts txt
344− let src = unescapeURI src'
345344 let isRelative = ':' `notElem` src
346345 let prefix = if isRelative
347346 then text "link:"
@@ -353,7 +352,7 @@ inlineToAsciiDoc opts (Link txt (src', _tit)) = do
353352 return $ if useAuto
354353 then text srcSuffix
355354 else prefix <> text src <> "[" <> linktext <> "]"
356−inlineToAsciiDoc opts (Image alternate (src', tit)) = do
355+inlineToAsciiDoc opts (Image alternate (src, tit)) = do
357356-- image:images/logo.png[Company logo, title="blah"]
358357 let txt = if (null alternate) || (alternate == [Str ""])
359358 then [Str "image"]
@@ -362,7 +361,6 @@ inlineToAsciiDoc opts (Image alternate (src', tit)) = do
362361 let linktitle = if null tit
363362 then empty
364363 else text $ ",title=\"" ++ tit ++ "\""
365− let src = unescapeURI src'
366364 return $ "image:" <> text src <> "[" <> linktext <> linktitle <> "]"
367365inlineToAsciiDoc opts (Note [Para inlines]) =
368366 inlineToAsciiDoc opts (Note [Plain inlines])
src/Text/Pandoc/Writers/Markdown.hs+1−2
@@ -493,12 +493,11 @@ inlineToMarkdown opts (Cite (c:cs) lst)
493493 modekey SuppressAuthor = "-"
494494 modekey _ = ""
495495inlineToMarkdown _ (Cite _ _) = return $ text ""
496−inlineToMarkdown opts (Link txt (src', tit)) = do
496+inlineToMarkdown opts (Link txt (src, tit)) = do
497497 linktext <- inlineListToMarkdown opts txt
498498 let linktitle = if null tit
499499 then empty
500500 else text $ " \"" ++ tit ++ "\""
501− let src = unescapeURI src'
502501 let srcSuffix = if isPrefixOf "mailto:" src then drop 7 src else src
503502 let useRefLinks = writerReferenceLinks opts
504503 let useAuto = case (tit,txt) of
src/Text/Pandoc/Writers/Org.hs+1−2
@@ -272,8 +272,7 @@ inlineToOrg (Link txt (src, _)) = do
272272 _ -> do contents <- inlineListToOrg txt
273273 modify $ \s -> s{ stLinks = True }
274274 return $ "[[" <> text src <> "][" <> contents <> "]]"
275−inlineToOrg (Image _ (source', _)) = do
276− let source = unescapeURI source'
275+inlineToOrg (Image _ (source, _)) = do
277276 modify $ \s -> s{ stImages = True }
278277 return $ "[[" <> text source <> "]]"
279278inlineToOrg (Note contents) = do
src/Text/Pandoc/Writers/RST.hs+3−5
@@ -298,9 +298,8 @@ inlineToRST Space = return space
298298inlineToRST (Link [Code _ str] (src, _)) | src == str ||
299299 src == "mailto:" ++ str = do
300300 let srcSuffix = if isPrefixOf "mailto:" src then drop 7 src else src
301− return $ text $ unescapeURI srcSuffix
302−inlineToRST (Link txt (src', tit)) = do
303− let src = unescapeURI src'
301+ return $ text srcSuffix
302+inlineToRST (Link txt (src, tit)) = do
304303 useReferenceLinks <- get >>= return . writerReferenceLinks . stOptions
305304 linktext <- inlineListToRST $ normalizeSpaces txt
306305 if useReferenceLinks
@@ -311,8 +310,7 @@ inlineToRST (Link txt (src', tit)) = do
311310 modify $ \st -> st { stLinks = refs' }
312311 return $ "`" <> linktext <> "`_"
313312 else return $ "`" <> linktext <> " <" <> text src <> ">`_"
314−inlineToRST (Image alternate (source', tit)) = do
315− let source = unescapeURI source'
313+inlineToRST (Image alternate (source, tit)) = do
316314 pics <- get >>= return . stImages
317315 let labelsUsed = map fst pics
318316 let txt = if null alternate || alternate == [Str ""] ||
tests/markdown-reader-more.native+3−3
@@ -29,9 +29,9 @@
2929,Para [Str "`",Str "hi"]
3030,Para [Str "there",Str "`"]
3131,Header 2 [Str "Multilingual",Space,Str "URLs"]
32−,Para [Link [Code ("",["url"],[]) "http://\27979.com?\27979=\27979"] ("http://%E6%B5%8B.com?%E6%B5%8B=%E6%B5%8B","")]
33−,Para [Link [Str "foo"] ("/bar/%E6%B5%8B?x=%E6%B5%8B","title")]
34−,Para [Link [Code ("",["url"],[]) "\27979@foo.\27979.baz"] ("mailto:%E6%B5%8B@foo.%E6%B5%8B.baz","")]
32+,Plain [RawInline "html" "<http://\27979.com?\27979=\27979>"]
33+,Para [Link [Str "foo"] ("/bar/\27979?x=\27979","title")]
34+,Para [Link [Code ("",["url"],[]) "\27979@foo.\27979.baz"] ("mailto:\27979@foo.\27979.baz","")]
3535,Header 2 [Str "Numbered",Space,Str "examples"]
3636,OrderedList (1,Example,TwoParens)
3737 [[Plain [Str "First",Space,Str "example",Str "."]]