git.iko.soy/typeable/pandoc
on master help sign in
Use `stripPrefix` where appropriate.
commit82118b332899a90a60a0e95e8ae416d7f5398071 authorArtyom Kazak <yom@artyom.me> dateSun 3 Aug 2014, 14:44 (12 years ago) parentfeebab9
9 files changed+57−54
src/Text/Pandoc/Readers/Docx.hs+7−7 src/Text/Pandoc/Shared.hs+7−7 src/Text/Pandoc/Writers/AsciiDoc.hs+3−2 src/Text/Pandoc/Writers/Docbook.hs+14−14 src/Text/Pandoc/Writers/FB2.hs+14−15 src/Text/Pandoc/Writers/LaTeX.hs+3−3 src/Text/Pandoc/Writers/Man.hs+3−2 src/Text/Pandoc/Writers/Markdown.hs+3−2 src/Text/Pandoc/Writers/RST.hs+3−2
src/Text/Pandoc/Readers/Docx.hs+7−7
@@ -86,7 +86,7 @@ import Text.Pandoc.Readers.Docx.TexChar
8686import Text.Pandoc.Shared
8787import Text.Pandoc.MediaBag (insertMedia, MediaBag)
8888import Data.Maybe (mapMaybe, fromMaybe)
89−import Data.List (delete, isPrefixOf, (\\), intercalate, intersect)
89+import Data.List (delete, stripPrefix, (\\), intercalate, intersect)
9090import Data.Monoid
9191import qualified Data.ByteString.Lazy as B
9292import qualified Data.Map as M
@@ -455,8 +455,8 @@ oMathElemToTexString (LowerLimit base limElems) = do
455455 -- we want to make sure to replace the `\rightarrow` with `\to`
456456 let arrowToTo :: String -> String
457457 arrowToTo "" = ""
458− arrowToTo s | "\\rightarrow" `isPrefixOf` s =
459− "\\to" ++ (arrowToTo $ drop (length "\\rightarrow") s)
458+ arrowToTo s | Just s' <- stripPrefix "\\rightarrow" s =
459+ "\\to" ++ arrowToTo s'
460460 arrowToTo (c:cs) = c : arrowToTo cs
461461 lim' = arrowToTo lim
462462 return $ case baseString of
@@ -470,8 +470,8 @@ oMathElemToTexString (UpperLimit base limElems) = do
470470 -- we want to make sure to replace the `\rightarrow` with `\to`
471471 let arrowToTo :: String -> String
472472 arrowToTo "" = ""
473− arrowToTo s | "\\rightarrow" `isPrefixOf` s =
474− "\\to" ++ (arrowToTo $ drop (length "\\rightarrow") s)
473+ arrowToTo s | Just s' <- stripPrefix "\\rightarrow" s =
474+ "\\to" ++ arrowToTo s'
475475 arrowToTo (c:cs) = c : arrowToTo cs
476476 lim' = arrowToTo lim
477477 return $ case baseString of
@@ -698,8 +698,8 @@ ilToCode Space = " "
698698ilToCode _ = ""
699699
700700isHeaderClass :: String -> Maybe Int
701−isHeaderClass s | "Heading" `isPrefixOf` s =
702− case reads (drop (length "Heading") s) :: [(Int, String)] of
701+isHeaderClass s | Just s' <- stripPrefix "Heading" s =
702+ case reads s' :: [(Int, String)] of
703703 [] -> Nothing
704704 ((n, "") : []) -> Just n
705705 _ -> Nothing
src/Text/Pandoc/Shared.hs+7−7
@@ -99,7 +99,7 @@ import System.Environment (getProgName)
9999import System.Exit (exitWith, ExitCode(..))
100100import Data.Char ( toLower, isLower, isUpper, isAlpha,
101101 isLetter, isDigit, isSpace )
102−import Data.List ( find, isPrefixOf, intercalate )
102+import Data.List ( find, stripPrefix, intercalate )
103103import qualified Data.Map as M
104104import Network.URI ( escapeURIString, isURI, nonStrictRelativeTo,
105105 unEscapeString, parseURIReference, isAllowedInURI )
@@ -183,9 +183,9 @@ substitute :: (Eq a) => [a] -> [a] -> [a] -> [a]
183183substitute _ _ [] = []
184184substitute [] _ xs = xs
185185substitute target replacement lst@(x:xs) =
186− if target `isPrefixOf` lst
187− then replacement ++ substitute target replacement (drop (length target) lst)
188− else x : substitute target replacement xs
186+ case stripPrefix target lst of
187+ Just lst' -> replacement ++ substitute target replacement lst'
188+ Nothing -> x : substitute target replacement xs
189189
190190ordNub :: (Ord a) => [a] -> [a]
191191ordNub l = go Set.empty l
@@ -808,9 +808,9 @@ fetchItem' media sourceURL s = do
808808-- | Read from a URL and return raw data and maybe mime type.
809809openURL :: String -> IO (Either E.SomeException (BS.ByteString, Maybe String))
810810openURL u
811− | "data:" `isPrefixOf` u =
812− let mime = takeWhile (/=',') $ drop 5 u
813− contents = B8.pack $ unEscapeString $ drop 1 $ dropWhile (/=',') u
811+ | Just u' <- stripPrefix "data:" u =
812+ let mime = takeWhile (/=',') u'
813+ contents = B8.pack $ unEscapeString $ drop 1 $ dropWhile (/=',') u'
814814 in return $ Right (decodeLenient contents, Just mime)
815815#ifdef HTTP_CLIENT
816816 | otherwise = withSocketsDo $ E.try $ do
src/Text/Pandoc/Writers/AsciiDoc.hs+3−2
@@ -43,7 +43,8 @@ import Text.Pandoc.Shared
4343import Text.Pandoc.Writers.Shared
4444import Text.Pandoc.Options
4545import Text.Pandoc.Parsing hiding (blankline, space)
46−import Data.List ( isPrefixOf, intersperse, intercalate )
46+import Data.Maybe (fromMaybe)
47+import Data.List ( stripPrefix, intersperse, intercalate )
4748import Text.Pandoc.Pretty
4849import Control.Monad.State
4950import qualified Data.Map as M
@@ -401,7 +402,7 @@ inlineToAsciiDoc opts (Link txt (src, _tit)) = do
401402 let prefix = if isRelative
402403 then text "link:"
403404 else empty
404− let srcSuffix = if isPrefixOf "mailto:" src then drop 7 src else src
405+ let srcSuffix = fromMaybe src (stripPrefix "mailto:" src)
405406 let useAuto = case txt of
406407 [Str s] | escapeURI s == srcSuffix -> True
407408 _ -> False
src/Text/Pandoc/Writers/Docbook.hs+14−14
@@ -37,7 +37,7 @@ import Text.Pandoc.Writers.Shared
3737import Text.Pandoc.Options
3838import Text.Pandoc.Templates (renderTemplate')
3939import Text.Pandoc.Readers.TeXMath
40−import Data.List ( isPrefixOf, intercalate, isSuffixOf )
40+import Data.List ( stripPrefix, isPrefixOf, intercalate, isSuffixOf )
4141import Data.Char ( toLower )
4242import Data.Monoid ( Any(..) )
4343import Text.Pandoc.Highlighting ( languages, languagesByExtension )
@@ -312,19 +312,19 @@ inlineToDocbook _ (RawInline f x) | f == "html" || f == "docbook" = text x
312312 | otherwise = empty
313313inlineToDocbook _ LineBreak = text "\n"
314314inlineToDocbook _ Space = space
315−inlineToDocbook opts (Link txt (src, _)) =
316− if isPrefixOf "mailto:" src
317− then let src' = drop 7 src
318− emailLink = inTagsSimple "email" $ text $
319− escapeStringForXML $ src'
320− in case txt of
321− [Str s] | escapeURI s == src' -> emailLink
322− _ -> inlinesToDocbook opts txt <+>
323− char '(' <> emailLink <> char ')'
324− else (if isPrefixOf "#" src
325− then inTags False "link" [("linkend", drop 1 src)]
326− else inTags False "ulink" [("url", src)]) $
327− inlinesToDocbook opts txt
315+inlineToDocbook opts (Link txt (src, _))
316+ | Just email <- stripPrefix "mailto:" src =
317+ let emailLink = inTagsSimple "email" $ text $
318+ escapeStringForXML $ email
319+ in case txt of
320+ [Str s] | escapeURI s == email -> emailLink
321+ _ -> inlinesToDocbook opts txt <+>
322+ char '(' <> emailLink <> char ')'
323+ | otherwise =
324+ (if isPrefixOf "#" src
325+ then inTags False "link" [("linkend", drop 1 src)]
326+ else inTags False "ulink" [("url", src)]) $
327+ inlinesToDocbook opts txt
328328inlineToDocbook _ (Image _ (src, tit)) =
329329 let titleDoc = if null tit
330330 then empty
src/Text/Pandoc/Writers/FB2.hs+14−15
@@ -29,7 +29,7 @@ import Control.Monad.State (StateT, evalStateT, get, modify)
2929import Control.Monad.State (liftM, liftM2, liftIO)
3030import Data.ByteString.Base64 (encode)
3131import Data.Char (toLower, isSpace, isAscii, isControl)
32−import Data.List (intersperse, intercalate, isPrefixOf)
32+import Data.List (intersperse, intercalate, isPrefixOf, stripPrefix)
3333import Data.Either (lefts, rights)
3434import Network.Browser (browse, request, setAllowRedirects, setOutHandler)
3535import Network.HTTP (catchIO_, getRequest, getHeaders, getResponseBody)
@@ -252,22 +252,21 @@ readDataURI :: String -- ^ URI
252252 -> Maybe (String,String,Bool,String)
253253 -- ^ Maybe (mime,charset,isBase64,data)
254254readDataURI uri =
255− let prefix = "data:"
256− in if not (prefix `isPrefixOf` uri)
257− then Nothing
258− else
259− let rest = drop (length prefix) uri
260− meta = takeWhile (/= ',') rest -- without trailing ','
261− uridata = drop (length meta + 1) rest
262− parts = split (== ';') meta
263− (mime,cs,enc)=foldr upd ("text/plain","US-ASCII",False) parts
264− in Just (mime,cs,enc,uridata)
255+ case stripPrefix "data:" uri of
256+ Nothing -> Nothing
257+ Just rest ->
258+ let meta = takeWhile (/= ',') rest -- without trailing ','
259+ uridata = drop (length meta + 1) rest
260+ parts = split (== ';') meta
261+ (mime,cs,enc)=foldr upd ("text/plain","US-ASCII",False) parts
262+ in Just (mime,cs,enc,uridata)
263+
265264 where
266265 upd str m@(mime,cs,enc)
267− | isMimeType str = (str,cs,enc)
268− | "charset=" `isPrefixOf` str = (mime,drop (length "charset=") str,enc)
269− | str == "base64" = (mime,cs,True)
270− | otherwise = m
266+ | isMimeType str = (str,cs,enc)
267+ | Just str' <- stripPrefix "charset=" str = (mime,str',enc)
268+ | str == "base64" = (mime,cs,True)
269+ | otherwise = m
271270
272271-- Without parameters like ;charset=...; see RFC 2045, 5.1
273272isMimeType :: String -> Bool
src/Text/Pandoc/Writers/LaTeX.hs+3−3
@@ -37,7 +37,7 @@ import Text.Pandoc.Options
3737import Text.Pandoc.Templates
3838import Text.Printf ( printf )
3939import Network.URI ( isURI, unEscapeString )
40−import Data.List ( (\\), isSuffixOf, isInfixOf,
40+import Data.List ( (\\), isSuffixOf, isInfixOf, stripPrefix,
4141 isPrefixOf, intercalate, intersperse )
4242import Data.Char ( toLower, isPunctuation, isAscii, isLetter, isDigit, ord )
4343import Data.Maybe ( fromMaybe )
@@ -761,8 +761,8 @@ inlineToLaTeX (Link txt (src, _)) =
761761 do modify $ \s -> s{ stUrl = True }
762762 src' <- stringToLaTeX URLString src
763763 return $ text $ "\\url{" ++ src' ++ "}"
764− [Str x] | "mailto:" `isPrefixOf` src &&
765− escapeURI x == drop 7 src -> -- email autolink
764+ [Str x] | Just rest <- stripPrefix "mailto:" src,
765+ escapeURI x == rest -> -- email autolink
766766 do modify $ \s -> s{ stUrl = True }
767767 src' <- stringToLaTeX URLString src
768768 contents <- inlineListToLaTeX txt
src/Text/Pandoc/Writers/Man.hs+3−2
@@ -36,7 +36,8 @@ import Text.Pandoc.Writers.Shared
3636import Text.Pandoc.Options
3737import Text.Pandoc.Readers.TeXMath
3838import Text.Printf ( printf )
39−import Data.List ( isPrefixOf, intersperse, intercalate )
39+import Data.List ( stripPrefix, intersperse, intercalate )
40+import Data.Maybe (fromMaybe)
4041import Text.Pandoc.Pretty
4142import Text.Pandoc.Builder (deleteMeta)
4243import Control.Monad.State
@@ -343,7 +344,7 @@ inlineToMan _ (LineBreak) = return $
343344inlineToMan _ Space = return space
344345inlineToMan opts (Link txt (src, _)) = do
345346 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)
347348 return $ case txt of
348349 [Str s]
349350 | escapeURI s == srcSuffix ->
src/Text/Pandoc/Writers/Markdown.hs+3−2
@@ -38,7 +38,8 @@ import Text.Pandoc.Shared
3838import Text.Pandoc.Writers.Shared
3939import Text.Pandoc.Options
4040import Text.Pandoc.Parsing hiding (blankline, blanklines, char, space)
41−import Data.List ( group, isPrefixOf, find, intersperse, transpose, sortBy )
41+import Data.Maybe (fromMaybe)
42+import Data.List ( group, stripPrefix, find, intersperse, transpose, sortBy )
4243import Data.Char ( isSpace, isPunctuation )
4344import Data.Ord ( comparing )
4445import Text.Pandoc.Pretty
@@ -815,7 +816,7 @@ inlineToMarkdown opts (Link txt (src, tit)) = do
815816 let linktitle = if null tit
816817 then empty
817818 else text $ " \"" ++ tit ++ "\""
818− let srcSuffix = if isPrefixOf "mailto:" src then drop 7 src else src
819+ let srcSuffix = fromMaybe src (stripPrefix "mailto:" src)
819820 let useAuto = isURI src &&
820821 case txt of
821822 [Str s] | escapeURI s == srcSuffix -> True
src/Text/Pandoc/Writers/RST.hs+3−2
@@ -37,7 +37,8 @@ import Text.Pandoc.Shared
3737import Text.Pandoc.Writers.Shared
3838import Text.Pandoc.Templates (renderTemplate')
3939import Text.Pandoc.Builder (deleteMeta)
40−import Data.List ( isPrefixOf, intersperse, transpose )
40+import Data.Maybe (fromMaybe)
41+import Data.List ( isPrefixOf, stripPrefix, intersperse, transpose )
4142import Network.URI (isURI)
4243import Text.Pandoc.Pretty
4344import Control.Monad.State
@@ -401,7 +402,7 @@ inlineToRST (Link [Str str] (src, _))
401402 if "mailto:" `isPrefixOf` src
402403 then src == escapeURI ("mailto:" ++ str)
403404 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)
405406 return $ text srcSuffix
406407inlineToRST (Link [Image alt (imgsrc,imgtit)] (src, _tit)) = do
407408 label <- registerImage alt (imgsrc,imgtit) (Just src)