git.iko.soy/typeable/pandoc
on master help sign in
Use `stripPrefix` where appropriate.
82118b3Artyom Kazak
src/Text/Pandoc/Readers/Docx.hs+7−7
@@ -86,7 +86,7 @@ import Text.Pandoc.Readers.Docx.TexChar
86import Text.Pandoc.Shared
86import Text.Pandoc.Shared
87import Text.Pandoc.MediaBag (insertMedia, MediaBag)
87import Text.Pandoc.MediaBag (insertMedia, MediaBag)
88import Data.Maybe (mapMaybe, fromMaybe)
88import Data.Maybe (mapMaybe, fromMaybe)
89−import Data.List (delete, isPrefixOf, (\\), intercalate, intersect)
89+import Data.List (delete, stripPrefix, (\\), intercalate, intersect)
90import Data.Monoid
90import Data.Monoid
91import qualified Data.ByteString.Lazy as B
91import qualified Data.ByteString.Lazy as B
92import qualified Data.Map as M
92import qualified Data.Map as M
@@ -455,8 +455,8 @@ oMathElemToTexString (LowerLimit base limElems) = do
455 -- we want to make sure to replace the `\rightarrow` with `\to`
455 -- we want to make sure to replace the `\rightarrow` with `\to`
456 let arrowToTo :: String -> String
456 let arrowToTo :: String -> String
457 arrowToTo "" = ""
457 arrowToTo "" = ""
458− arrowToTo s | "\\rightarrow" `isPrefixOf` s =
458+ arrowToTo s | Just s' <- stripPrefix "\\rightarrow" s =
459− "\\to" ++ (arrowToTo $ drop (length "\\rightarrow") s)
459+ "\\to" ++ arrowToTo s'
460 arrowToTo (c:cs) = c : arrowToTo cs
460 arrowToTo (c:cs) = c : arrowToTo cs
461 lim' = arrowToTo lim
461 lim' = arrowToTo lim
462 return $ case baseString of
462 return $ case baseString of
@@ -470,8 +470,8 @@ oMathElemToTexString (UpperLimit base limElems) = do
470 -- we want to make sure to replace the `\rightarrow` with `\to`
470 -- we want to make sure to replace the `\rightarrow` with `\to`
471 let arrowToTo :: String -> String
471 let arrowToTo :: String -> String
472 arrowToTo "" = ""
472 arrowToTo "" = ""
473− arrowToTo s | "\\rightarrow" `isPrefixOf` s =
473+ arrowToTo s | Just s' <- stripPrefix "\\rightarrow" s =
474− "\\to" ++ (arrowToTo $ drop (length "\\rightarrow") s)
474+ "\\to" ++ arrowToTo s'
475 arrowToTo (c:cs) = c : arrowToTo cs
475 arrowToTo (c:cs) = c : arrowToTo cs
476 lim' = arrowToTo lim
476 lim' = arrowToTo lim
477 return $ case baseString of
477 return $ case baseString of
@@ -698,8 +698,8 @@ ilToCode Space = " "
698ilToCode _ = ""
698ilToCode _ = ""
699
699
700isHeaderClass :: String -> Maybe Int
700isHeaderClass :: String -> Maybe Int
701−isHeaderClass s | "Heading" `isPrefixOf` s =
701+isHeaderClass s | Just s' <- stripPrefix "Heading" s =
702− case reads (drop (length "Heading") s) :: [(Int, String)] of
702+ case reads s' :: [(Int, String)] of
703 [] -> Nothing
703 [] -> Nothing
704 ((n, "") : []) -> Just n
704 ((n, "") : []) -> Just n
705 _ -> Nothing
705 _ -> Nothing
src/Text/Pandoc/Shared.hs+7−7
@@ -99,7 +99,7 @@ import System.Environment (getProgName)
99import System.Exit (exitWith, ExitCode(..))
99import System.Exit (exitWith, ExitCode(..))
100import Data.Char ( toLower, isLower, isUpper, isAlpha,
100import Data.Char ( toLower, isLower, isUpper, isAlpha,
101 isLetter, isDigit, isSpace )
101 isLetter, isDigit, isSpace )
102−import Data.List ( find, isPrefixOf, intercalate )
102+import Data.List ( find, stripPrefix, intercalate )
103import qualified Data.Map as M
103import qualified Data.Map as M
104import Network.URI ( escapeURIString, isURI, nonStrictRelativeTo,
104import Network.URI ( escapeURIString, isURI, nonStrictRelativeTo,
105 unEscapeString, parseURIReference, isAllowedInURI )
105 unEscapeString, parseURIReference, isAllowedInURI )
@@ -183,9 +183,9 @@ substitute :: (Eq a) => [a] -> [a] -> [a] -> [a]
183substitute _ _ [] = []
183substitute _ _ [] = []
184substitute [] _ xs = xs
184substitute [] _ xs = xs
185substitute target replacement lst@(x:xs) =
185substitute target replacement lst@(x:xs) =
186− if target `isPrefixOf` lst
186+ case stripPrefix target lst of
187− then replacement ++ substitute target replacement (drop (length target) lst)
187+ Just lst' -> replacement ++ substitute target replacement lst'
188− else x : substitute target replacement xs
188+ Nothing -> x : substitute target replacement xs
189
189
190ordNub :: (Ord a) => [a] -> [a]
190ordNub :: (Ord a) => [a] -> [a]
191ordNub l = go Set.empty l
191ordNub l = go Set.empty l
@@ -808,9 +808,9 @@ fetchItem' media sourceURL s = do
808-- | Read from a URL and return raw data and maybe mime type.
808-- | Read from a URL and return raw data and maybe mime type.
809openURL :: String -> IO (Either E.SomeException (BS.ByteString, Maybe String))
809openURL :: String -> IO (Either E.SomeException (BS.ByteString, Maybe String))
810openURL u
810openURL u
811− | "data:" `isPrefixOf` u =
811+ | Just u' <- stripPrefix "data:" u =
812− let mime = takeWhile (/=',') $ drop 5 u
812+ let mime = takeWhile (/=',') u'
813− contents = B8.pack $ unEscapeString $ drop 1 $ dropWhile (/=',') u
813+ contents = B8.pack $ unEscapeString $ drop 1 $ dropWhile (/=',') u'
814 in return $ Right (decodeLenient contents, Just mime)
814 in return $ Right (decodeLenient contents, Just mime)
815#ifdef HTTP_CLIENT
815#ifdef HTTP_CLIENT
816 | otherwise = withSocketsDo $ E.try $ do
816 | otherwise = withSocketsDo $ E.try $ do
src/Text/Pandoc/Writers/AsciiDoc.hs+3−2
@@ -43,7 +43,8 @@ import Text.Pandoc.Shared
43import Text.Pandoc.Writers.Shared
43import Text.Pandoc.Writers.Shared
44import Text.Pandoc.Options
44import Text.Pandoc.Options
45import Text.Pandoc.Parsing hiding (blankline, space)
45import Text.Pandoc.Parsing hiding (blankline, space)
46+import Data.Maybe (fromMaybe)
46−import Data.List ( isPrefixOf, intersperse, intercalate )
47+import Data.List ( stripPrefix, intersperse, intercalate )
47import Text.Pandoc.Pretty
48import Text.Pandoc.Pretty
48import Control.Monad.State
49import Control.Monad.State
49import qualified Data.Map as M
50import qualified Data.Map as M
@@ -401,7 +402,7 @@ inlineToAsciiDoc opts (Link txt (src, _tit)) = do
401 let prefix = if isRelative
402 let prefix = if isRelative
402 then text "link:"
403 then text "link:"
403 else empty
404 else empty
404− let srcSuffix = if isPrefixOf "mailto:" src then drop 7 src else src
405+ let srcSuffix = fromMaybe src (stripPrefix "mailto:" src)
405 let useAuto = case txt of
406 let useAuto = case txt of
406 [Str s] | escapeURI s == srcSuffix -> True
407 [Str s] | escapeURI s == srcSuffix -> True
407 _ -> False
408 _ -> False
src/Text/Pandoc/Writers/Docbook.hs+14−14
@@ -37,7 +37,7 @@ import Text.Pandoc.Writers.Shared
37import Text.Pandoc.Options
37import Text.Pandoc.Options
38import Text.Pandoc.Templates (renderTemplate')
38import Text.Pandoc.Templates (renderTemplate')
39import Text.Pandoc.Readers.TeXMath
39import Text.Pandoc.Readers.TeXMath
40−import Data.List ( isPrefixOf, intercalate, isSuffixOf )
40+import Data.List ( stripPrefix, isPrefixOf, intercalate, isSuffixOf )
41import Data.Char ( toLower )
41import Data.Char ( toLower )
42import Data.Monoid ( Any(..) )
42import Data.Monoid ( Any(..) )
43import Text.Pandoc.Highlighting ( languages, languagesByExtension )
43import Text.Pandoc.Highlighting ( languages, languagesByExtension )
@@ -312,19 +312,19 @@ inlineToDocbook _ (RawInline f x) | f == "html" || f == "docbook" = text x
312 | otherwise = empty
312 | otherwise = empty
313inlineToDocbook _ LineBreak = text "\n"
313inlineToDocbook _ LineBreak = text "\n"
314inlineToDocbook _ Space = space
314inlineToDocbook _ Space = space
315−inlineToDocbook opts (Link txt (src, _)) =
315+inlineToDocbook opts (Link txt (src, _))
316− if isPrefixOf "mailto:" src
316+ | Just email <- stripPrefix "mailto:" src =
317− then let src' = drop 7 src
318− emailLink = inTagsSimple "email" $ text $
317+ let emailLink = inTagsSimple "email" $ text $
319− escapeStringForXML $ src'
318+ escapeStringForXML $ email
320− in case txt of
319+ in case txt of
321− [Str s] | escapeURI s == src' -> emailLink
320+ [Str s] | escapeURI s == email -> emailLink
322− _ -> inlinesToDocbook opts txt <+>
321+ _ -> inlinesToDocbook opts txt <+>
323− char '(' <> emailLink <> char ')'
322+ char '(' <> emailLink <> char ')'
323+ | otherwise =
324− else (if isPrefixOf "#" src
324+ (if isPrefixOf "#" src
325− then inTags False "link" [("linkend", drop 1 src)]
325+ then inTags False "link" [("linkend", drop 1 src)]
326− else inTags False "ulink" [("url", src)]) $
326+ else inTags False "ulink" [("url", src)]) $
327− inlinesToDocbook opts txt
327+ inlinesToDocbook opts txt
328inlineToDocbook _ (Image _ (src, tit)) =
328inlineToDocbook _ (Image _ (src, tit)) =
329 let titleDoc = if null tit
329 let titleDoc = if null tit
330 then empty
330 then empty
src/Text/Pandoc/Writers/FB2.hs+14−15
@@ -29,7 +29,7 @@ import Control.Monad.State (StateT, evalStateT, get, modify)
29import Control.Monad.State (liftM, liftM2, liftIO)
29import Control.Monad.State (liftM, liftM2, liftIO)
30import Data.ByteString.Base64 (encode)
30import Data.ByteString.Base64 (encode)
31import Data.Char (toLower, isSpace, isAscii, isControl)
31import Data.Char (toLower, isSpace, isAscii, isControl)
32−import Data.List (intersperse, intercalate, isPrefixOf)
32+import Data.List (intersperse, intercalate, isPrefixOf, stripPrefix)
33import Data.Either (lefts, rights)
33import Data.Either (lefts, rights)
34import Network.Browser (browse, request, setAllowRedirects, setOutHandler)
34import Network.Browser (browse, request, setAllowRedirects, setOutHandler)
35import Network.HTTP (catchIO_, getRequest, getHeaders, getResponseBody)
35import Network.HTTP (catchIO_, getRequest, getHeaders, getResponseBody)
@@ -252,22 +252,21 @@ readDataURI :: String -- ^ URI
252 -> Maybe (String,String,Bool,String)
252 -> Maybe (String,String,Bool,String)
253 -- ^ Maybe (mime,charset,isBase64,data)
253 -- ^ Maybe (mime,charset,isBase64,data)
254readDataURI uri =
254readDataURI uri =
255− let prefix = "data:"
255+ case stripPrefix "data:" uri of
256− in if not (prefix `isPrefixOf` uri)
257− then Nothing
256+ Nothing -> Nothing
258− else
257+ Just rest ->
259− let rest = drop (length prefix) uri
260− meta = takeWhile (/= ',') rest -- without trailing ','
258+ let meta = takeWhile (/= ',') rest -- without trailing ','
261− uridata = drop (length meta + 1) rest
259+ uridata = drop (length meta + 1) rest
262− parts = split (== ';') meta
260+ parts = split (== ';') meta
263− (mime,cs,enc)=foldr upd ("text/plain","US-ASCII",False) parts
261+ (mime,cs,enc)=foldr upd ("text/plain","US-ASCII",False) parts
264− in Just (mime,cs,enc,uridata)
262+ in Just (mime,cs,enc,uridata)
263+
265 where
264 where
266 upd str m@(mime,cs,enc)
265 upd str m@(mime,cs,enc)
267− | isMimeType str = (str,cs,enc)
266+ | isMimeType str = (str,cs,enc)
268− | "charset=" `isPrefixOf` str = (mime,drop (length "charset=") str,enc)
267+ | Just str' <- stripPrefix "charset=" str = (mime,str',enc)
269− | str == "base64" = (mime,cs,True)
268+ | str == "base64" = (mime,cs,True)
270− | otherwise = m
269+ | otherwise = m
271
270
272-- Without parameters like ;charset=...; see RFC 2045, 5.1
271-- Without parameters like ;charset=...; see RFC 2045, 5.1
273isMimeType :: String -> Bool
272isMimeType :: String -> Bool
src/Text/Pandoc/Writers/LaTeX.hs+3−3
@@ -37,7 +37,7 @@ import Text.Pandoc.Options
37import Text.Pandoc.Templates
37import Text.Pandoc.Templates
38import Text.Printf ( printf )
38import Text.Printf ( printf )
39import Network.URI ( isURI, unEscapeString )
39import Network.URI ( isURI, unEscapeString )
40−import Data.List ( (\\), isSuffixOf, isInfixOf,
40+import Data.List ( (\\), isSuffixOf, isInfixOf, stripPrefix,
41 isPrefixOf, intercalate, intersperse )
41 isPrefixOf, intercalate, intersperse )
42import Data.Char ( toLower, isPunctuation, isAscii, isLetter, isDigit, ord )
42import Data.Char ( toLower, isPunctuation, isAscii, isLetter, isDigit, ord )
43import Data.Maybe ( fromMaybe )
43import Data.Maybe ( fromMaybe )
@@ -761,8 +761,8 @@ inlineToLaTeX (Link txt (src, _)) =
761 do modify $ \s -> s{ stUrl = True }
761 do modify $ \s -> s{ stUrl = True }
762 src' <- stringToLaTeX URLString src
762 src' <- stringToLaTeX URLString src
763 return $ text $ "\\url{" ++ src' ++ "}"
763 return $ text $ "\\url{" ++ src' ++ "}"
764− [Str x] | "mailto:" `isPrefixOf` src &&
764+ [Str x] | Just rest <- stripPrefix "mailto:" src,
765− escapeURI x == drop 7 src -> -- email autolink
765+ escapeURI x == rest -> -- email autolink
766 do modify $ \s -> s{ stUrl = True }
766 do modify $ \s -> s{ stUrl = True }
767 src' <- stringToLaTeX URLString src
767 src' <- stringToLaTeX URLString src
768 contents <- inlineListToLaTeX txt
768 contents <- inlineListToLaTeX txt
src/Text/Pandoc/Writers/Man.hs+3−2
@@ -36,7 +36,8 @@ import Text.Pandoc.Writers.Shared
36import Text.Pandoc.Options
36import Text.Pandoc.Options
37import Text.Pandoc.Readers.TeXMath
37import Text.Pandoc.Readers.TeXMath
38import Text.Printf ( printf )
38import Text.Printf ( printf )
39−import Data.List ( isPrefixOf, intersperse, intercalate )
39+import Data.List ( stripPrefix, intersperse, intercalate )
40+import Data.Maybe (fromMaybe)
40import Text.Pandoc.Pretty
41import Text.Pandoc.Pretty
41import Text.Pandoc.Builder (deleteMeta)
42import Text.Pandoc.Builder (deleteMeta)
42import Control.Monad.State
43import Control.Monad.State
@@ -343,7 +344,7 @@ inlineToMan _ (LineBreak) = return $
343inlineToMan _ Space = return space
344inlineToMan _ Space = return space
344inlineToMan opts (Link txt (src, _)) = do
345inlineToMan opts (Link txt (src, _)) = do
345 linktext <- inlineListToMan opts txt
346 linktext <- inlineListToMan opts txt
346− let srcSuffix = if isPrefixOf "mailto:" src then drop 7 src else src
347+ let srcSuffix = fromMaybe src (stripPrefix "mailto:" src)
347 return $ case txt of
348 return $ case txt of
348 [Str s]
349 [Str s]
349 | escapeURI s == srcSuffix ->
350 | escapeURI s == srcSuffix ->
src/Text/Pandoc/Writers/Markdown.hs+3−2
@@ -38,7 +38,8 @@ import Text.Pandoc.Shared
38import Text.Pandoc.Writers.Shared
38import Text.Pandoc.Writers.Shared
39import Text.Pandoc.Options
39import Text.Pandoc.Options
40import Text.Pandoc.Parsing hiding (blankline, blanklines, char, space)
40import Text.Pandoc.Parsing hiding (blankline, blanklines, char, space)
41+import Data.Maybe (fromMaybe)
41−import Data.List ( group, isPrefixOf, find, intersperse, transpose, sortBy )
42+import Data.List ( group, stripPrefix, find, intersperse, transpose, sortBy )
42import Data.Char ( isSpace, isPunctuation )
43import Data.Char ( isSpace, isPunctuation )
43import Data.Ord ( comparing )
44import Data.Ord ( comparing )
44import Text.Pandoc.Pretty
45import Text.Pandoc.Pretty
@@ -815,7 +816,7 @@ inlineToMarkdown opts (Link txt (src, tit)) = do
815 let linktitle = if null tit
816 let linktitle = if null tit
816 then empty
817 then empty
817 else text $ " \"" ++ tit ++ "\""
818 else text $ " \"" ++ tit ++ "\""
818− let srcSuffix = if isPrefixOf "mailto:" src then drop 7 src else src
819+ let srcSuffix = fromMaybe src (stripPrefix "mailto:" src)
819 let useAuto = isURI src &&
820 let useAuto = isURI src &&
820 case txt of
821 case txt of
821 [Str s] | escapeURI s == srcSuffix -> True
822 [Str s] | escapeURI s == srcSuffix -> True
src/Text/Pandoc/Writers/RST.hs+3−2
@@ -37,7 +37,8 @@ import Text.Pandoc.Shared
37import Text.Pandoc.Writers.Shared
37import Text.Pandoc.Writers.Shared
38import Text.Pandoc.Templates (renderTemplate')
38import Text.Pandoc.Templates (renderTemplate')
39import Text.Pandoc.Builder (deleteMeta)
39import Text.Pandoc.Builder (deleteMeta)
40+import Data.Maybe (fromMaybe)
40−import Data.List ( isPrefixOf, intersperse, transpose )
41+import Data.List ( isPrefixOf, stripPrefix, intersperse, transpose )
41import Network.URI (isURI)
42import Network.URI (isURI)
42import Text.Pandoc.Pretty
43import Text.Pandoc.Pretty
43import Control.Monad.State
44import Control.Monad.State
@@ -401,7 +402,7 @@ inlineToRST (Link [Str str] (src, _))
401 if "mailto:" `isPrefixOf` src
402 if "mailto:" `isPrefixOf` src
402 then src == escapeURI ("mailto:" ++ str)
403 then src == escapeURI ("mailto:" ++ str)
403 else src == escapeURI str = do
404 else src == escapeURI str = do
404− let srcSuffix = if isPrefixOf "mailto:" src then drop 7 src else src
405+ let srcSuffix = fromMaybe src (stripPrefix "mailto:" src)
405 return $ text srcSuffix
406 return $ text srcSuffix
406inlineToRST (Link [Image alt (imgsrc,imgtit)] (src, _tit)) = do
407inlineToRST (Link [Image alt (imgsrc,imgtit)] (src, _tit)) = do
407 label <- registerImage alt (imgsrc,imgtit) (Just src)
408 label <- registerImage alt (imgsrc,imgtit) (Just src)