From a740de05086960eb6d926f68acd43fedb4599f6c Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Thu, 9 Aug 2012 08:11:28 -0700 Subject: Use safeRead instead of using reads directly (various modules). --- src/Text/Pandoc.hs | 8 ++++---- src/Text/Pandoc/Highlighting.hs | 7 ++++--- src/Text/Pandoc/Readers/Native.hs | 31 ++++++++++++++++--------------- src/Text/Pandoc/Writers/EPUB.hs | 6 +++--- src/pandoc.hs | 32 ++++++++++++++++---------------- 5 files changed, 43 insertions(+), 41 deletions(-) diff --git a/src/Text/Pandoc.hs b/src/Text/Pandoc.hs index b65e7130d..6a019bc3f 100644 --- a/src/Text/Pandoc.hs +++ b/src/Text/Pandoc.hs @@ -135,6 +135,7 @@ import Text.Pandoc.Writers.Org import Text.Pandoc.Writers.AsciiDoc import Text.Pandoc.Templates import Text.Pandoc.Options +import Text.Pandoc.Shared (safeRead) import Data.ByteString.Lazy (ByteString) import Data.Version (showVersion) import Text.JSON.Generic @@ -158,10 +159,9 @@ parseFormatSpec = parse formatSpec "" extMod = do polarity <- oneOf "-+" name <- many1 $ noneOf "-+" - ext <- case reads name of - ((n,[]):_) -> return n - _ -> unexpected $ "Unknown extension: " ++ - name + ext <- case safeRead name of + Just n -> return n + Nothing -> unexpected $ "Unknown extension: " ++ name return $ case polarity of '-' -> Set.delete ext _ -> Set.insert ext diff --git a/src/Text/Pandoc/Highlighting.hs b/src/Text/Pandoc/Highlighting.hs index 080acebee..95df88099 100644 --- a/src/Text/Pandoc/Highlighting.hs +++ b/src/Text/Pandoc/Highlighting.hs @@ -47,6 +47,7 @@ module Text.Pandoc.Highlighting ( languages , Style ) where import Text.Pandoc.Definition +import Text.Pandoc.Shared (safeRead) import Text.Highlighting.Kate import Data.List (find) import Data.Maybe (fromMaybe) @@ -60,9 +61,9 @@ highlight :: (FormatOptions -> [SourceLine] -> a) -- ^ Formatter -> String -- ^ Raw contents of the CodeBlock -> Maybe a -- ^ Maybe the formatted result highlight formatter (_, classes, keyvals) rawCode = - let firstNum = case reads (fromMaybe "1" $ lookup "startFrom" keyvals) of - ((n,_):_) -> n - [] -> 1 + let firstNum = case safeRead (fromMaybe "1" $ lookup "startFrom" keyvals) of + Just n -> n + Nothing -> 1 fmtOpts = defaultFormatOpts{ startNumber = firstNum, numberLines = any (`elem` diff --git a/src/Text/Pandoc/Readers/Native.hs b/src/Text/Pandoc/Readers/Native.hs index 410c44a37..a0e5a0635 100644 --- a/src/Text/Pandoc/Readers/Native.hs +++ b/src/Text/Pandoc/Readers/Native.hs @@ -31,6 +31,7 @@ Conversion of a string representation of a pandoc type (@Pandoc@, module Text.Pandoc.Readers.Native ( readNative ) where import Text.Pandoc.Definition +import Text.Pandoc.Shared (safeRead) nullMeta :: Meta nullMeta = Meta{ docTitle = [] @@ -51,31 +52,31 @@ nullMeta = Meta{ docTitle = [] readNative :: String -- ^ String to parse (assuming @'\n'@ line endings) -> Pandoc readNative s = - case reads s of - (d,_):_ -> d - [] -> Pandoc nullMeta $ readBlocks s + case safeRead s of + Just d -> d + Nothing -> Pandoc nullMeta $ readBlocks s readBlocks :: String -> [Block] readBlocks s = - case reads s of - (d,_):_ -> d - [] -> [readBlock s] + case safeRead s of + Just d -> d + Nothing -> [readBlock s] readBlock :: String -> Block readBlock s = - case reads s of - (d,_):_ -> d - [] -> Plain $ readInlines s + case safeRead s of + Just d -> d + Nothing -> Plain $ readInlines s readInlines :: String -> [Inline] readInlines s = - case reads s of - (d,_):_ -> d - [] -> [readInline s] + case safeRead s of + Just d -> d + Nothing -> [readInline s] readInline :: String -> Inline readInline s = - case reads s of - (d,_):_ -> d - [] -> error "Cannot parse document" + case safeRead s of + Just d -> d + Nothing -> error "Cannot parse document" diff --git a/src/Text/Pandoc/Writers/EPUB.hs b/src/Text/Pandoc/Writers/EPUB.hs index 9a6f9670f..1a2764cb6 100644 --- a/src/Text/Pandoc/Writers/EPUB.hs +++ b/src/Text/Pandoc/Writers/EPUB.hs @@ -309,9 +309,9 @@ ppTopElement = ("\n" ++) . unEntity . unEntity ('&':'#':xs) = let (ds,ys) = break (==';') xs rest = drop 1 ys - in case reads ('\'':'\\':ds ++ "'") of - ((x,_):_) -> x : unEntity rest - _ -> '&':'#':unEntity xs + in case safeRead ('\'':'\\':ds ++ "'") of + Just x -> x : unEntity rest + Nothing -> '&':'#':unEntity xs unEntity (x:xs) = x : unEntity xs imageTypeOf :: FilePath -> Maybe String diff --git a/src/pandoc.hs b/src/pandoc.hs index e7a378826..094be39eb 100644 --- a/src/pandoc.hs +++ b/src/pandoc.hs @@ -33,7 +33,7 @@ module Main where import Text.Pandoc import Text.Pandoc.PDF (tex2pdf) import Text.Pandoc.Readers.LaTeX (handleIncludes) -import Text.Pandoc.Shared ( tabFilter, readDataFile, +import Text.Pandoc.Shared ( tabFilter, readDataFile, safeRead, headerShift, findDataFile, normalize, err, warn ) import Text.Pandoc.XML ( toEntities, fromEntities ) import Text.Pandoc.SelfContained ( makeSelfContained ) @@ -279,13 +279,13 @@ options = , Option "" ["base-header-level"] (ReqArg (\arg opt -> - case reads arg of - [(t,"")] | t > 0 -> do + case safeRead arg of + Just t | t > 0 -> do let oldTransforms = optTransforms opt let shift = t - 1 return opt{ optTransforms = headerShift shift : oldTransforms } - _ -> err 19 + _ -> err 19 "base-header-level must be a number > 0") "NUMBER") "" -- "Headers base level" @@ -311,9 +311,9 @@ options = , Option "" ["tab-stop"] (ReqArg (\arg opt -> - case reads arg of - [(t,"")] | t > 0 -> return opt { optTabStop = t } - _ -> err 31 + case safeRead arg of + Just t | t > 0 -> return opt { optTabStop = t } + _ -> err 31 "tab-stop must be a number greater than 0") "NUMBER") "" -- "Tab stop (default 4)" @@ -360,9 +360,9 @@ options = , Option "" ["columns"] (ReqArg (\arg opt -> - case reads arg of - [(t,"")] | t > 0 -> return opt { optColumns = t } - _ -> err 33 $ + case safeRead arg of + Just t | t > 0 -> return opt { optColumns = t } + _ -> err 33 $ "columns must be a number greater than 0") "NUMBER") "" -- "Length of line in characters" @@ -494,10 +494,10 @@ options = , Option "" ["slide-level"] (ReqArg (\arg opt -> do - case reads arg of - [(t,"")] | t >= 1 && t <= 6 -> + case safeRead arg of + Just t | t >= 1 && t <= 6 -> return opt { optSlideLevel = Just t } - _ -> err 39 $ + _ -> err 39 $ "slide level must be a number between 1 and 6") "NUMBER") "" -- "Force header level for slides" @@ -713,9 +713,9 @@ options = ] readExtension :: String -> IO Extension -readExtension s = case reads ('E':'x':'t':'_':map toLower s) of - ((ext,""):_) -> return ext - _ -> err 59 $ "Unknown extension: " ++ s +readExtension s = case safeRead ('E':'x':'t':'_':map toLower s) of + Just ext -> return ext + Nothing -> err 59 $ "Unknown extension: " ++ s -- Returns usage message usageMessage :: String -> [OptDescr (Opt -> IO Opt)] -> String -- cgit v1.2.3