git.iko.soy/typeable/pandoc
on master help sign in
Use safeRead instead of using reads directly (various modules).
commita740de05086960eb6d926f68acd43fedb4599f6c authorJohn MacFarlane <jgm@berkeley.edu> dateThu 9 Aug 2012, 08:11 (14 years ago) parent10cdd59
5 files changed+43−41
src/Text/Pandoc.hs+4−4 src/Text/Pandoc/Highlighting.hs+4−3 src/Text/Pandoc/Readers/Native.hs+16−15 src/Text/Pandoc/Writers/EPUB.hs+3−3 src/pandoc.hs+16−16
src/Text/Pandoc.hs+4−4
@@ -135,6 +135,7 @@ import Text.Pandoc.Writers.Org
135135import Text.Pandoc.Writers.AsciiDoc
136136import Text.Pandoc.Templates
137137import Text.Pandoc.Options
138+import Text.Pandoc.Shared (safeRead)
138139import Data.ByteString.Lazy (ByteString)
139140import Data.Version (showVersion)
140141import Text.JSON.Generic
@@ -158,10 +159,9 @@ parseFormatSpec = parse formatSpec ""
158159 extMod = do
159160 polarity <- oneOf "-+"
160161 name <- many1 $ noneOf "-+"
161− ext <- case reads name of
162− ((n,[]):_) -> return n
163− _ -> unexpected $ "Unknown extension: " ++
164− name
162+ ext <- case safeRead name of
163+ Just n -> return n
164+ Nothing -> unexpected $ "Unknown extension: " ++ name
165165 return $ case polarity of
166166 '-' -> Set.delete ext
167167 _ -> Set.insert ext
src/Text/Pandoc/Highlighting.hs+4−3
@@ -47,6 +47,7 @@ module Text.Pandoc.Highlighting ( languages
4747 , Style
4848 ) where
4949import Text.Pandoc.Definition
50+import Text.Pandoc.Shared (safeRead)
5051import Text.Highlighting.Kate
5152import Data.List (find)
5253import Data.Maybe (fromMaybe)
@@ -60,9 +61,9 @@ highlight :: (FormatOptions -> [SourceLine] -> a) -- ^ Formatter
6061 -> String -- ^ Raw contents of the CodeBlock
6162 -> Maybe a -- ^ Maybe the formatted result
6263highlight formatter (_, classes, keyvals) rawCode =
63− let firstNum = case reads (fromMaybe "1" $ lookup "startFrom" keyvals) of
64− ((n,_):_) -> n
65− [] -> 1
64+ let firstNum = case safeRead (fromMaybe "1" $ lookup "startFrom" keyvals) of
65+ Just n -> n
66+ Nothing -> 1
6667 fmtOpts = defaultFormatOpts{
6768 startNumber = firstNum,
6869 numberLines = any (`elem`
src/Text/Pandoc/Readers/Native.hs+16−15
@@ -31,6 +31,7 @@ Conversion of a string representation of a pandoc type (@Pandoc@,
3131module Text.Pandoc.Readers.Native ( readNative ) where
3232
3333import Text.Pandoc.Definition
34+import Text.Pandoc.Shared (safeRead)
3435
3536nullMeta :: Meta
3637nullMeta = Meta{ docTitle = []
@@ -51,31 +52,31 @@ nullMeta = Meta{ docTitle = []
5152readNative :: String -- ^ String to parse (assuming @'\n'@ line endings)
5253 -> Pandoc
5354readNative s =
54− case reads s of
55− (d,_):_ -> d
56− [] -> Pandoc nullMeta $ readBlocks s
55+ case safeRead s of
56+ Just d -> d
57+ Nothing -> Pandoc nullMeta $ readBlocks s
5758
5859readBlocks :: String -> [Block]
5960readBlocks s =
60− case reads s of
61− (d,_):_ -> d
62− [] -> [readBlock s]
61+ case safeRead s of
62+ Just d -> d
63+ Nothing -> [readBlock s]
6364
6465readBlock :: String -> Block
6566readBlock s =
66− case reads s of
67− (d,_):_ -> d
68− [] -> Plain $ readInlines s
67+ case safeRead s of
68+ Just d -> d
69+ Nothing -> Plain $ readInlines s
6970
7071readInlines :: String -> [Inline]
7172readInlines s =
72− case reads s of
73− (d,_):_ -> d
74− [] -> [readInline s]
73+ case safeRead s of
74+ Just d -> d
75+ Nothing -> [readInline s]
7576
7677readInline :: String -> Inline
7778readInline s =
78− case reads s of
79− (d,_):_ -> d
80− [] -> error "Cannot parse document"
79+ case safeRead s of
80+ Just d -> d
81+ Nothing -> error "Cannot parse document"
8182
src/Text/Pandoc/Writers/EPUB.hs+3−3
@@ -309,9 +309,9 @@ ppTopElement = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ++) . unEntity .
309309 unEntity ('&':'#':xs) =
310310 let (ds,ys) = break (==';') xs
311311 rest = drop 1 ys
312− in case reads ('\'':'\\':ds ++ "'") of
313− ((x,_):_) -> x : unEntity rest
314− _ -> '&':'#':unEntity xs
312+ in case safeRead ('\'':'\\':ds ++ "'") of
313+ Just x -> x : unEntity rest
314+ Nothing -> '&':'#':unEntity xs
315315 unEntity (x:xs) = x : unEntity xs
316316
317317imageTypeOf :: FilePath -> Maybe String
src/pandoc.hs+16−16
@@ -33,7 +33,7 @@ module Main where
3333import Text.Pandoc
3434import Text.Pandoc.PDF (tex2pdf)
3535import Text.Pandoc.Readers.LaTeX (handleIncludes)
36−import Text.Pandoc.Shared ( tabFilter, readDataFile,
36+import Text.Pandoc.Shared ( tabFilter, readDataFile, safeRead,
3737 headerShift, findDataFile, normalize, err, warn )
3838import Text.Pandoc.XML ( toEntities, fromEntities )
3939import Text.Pandoc.SelfContained ( makeSelfContained )
@@ -279,13 +279,13 @@ options =
279279 , Option "" ["base-header-level"]
280280 (ReqArg
281281 (\arg opt ->
282− case reads arg of
283− [(t,"")] | t > 0 -> do
282+ case safeRead arg of
283+ Just t | t > 0 -> do
284284 let oldTransforms = optTransforms opt
285285 let shift = t - 1
286286 return opt{ optTransforms =
287287 headerShift shift : oldTransforms }
288− _ -> err 19
288+ _ -> err 19
289289 "base-header-level must be a number > 0")
290290 "NUMBER")
291291 "" -- "Headers base level"
@@ -311,9 +311,9 @@ options =
311311 , Option "" ["tab-stop"]
312312 (ReqArg
313313 (\arg opt ->
314− case reads arg of
315− [(t,"")] | t > 0 -> return opt { optTabStop = t }
316− _ -> err 31
314+ case safeRead arg of
315+ Just t | t > 0 -> return opt { optTabStop = t }
316+ _ -> err 31
317317 "tab-stop must be a number greater than 0")
318318 "NUMBER")
319319 "" -- "Tab stop (default 4)"
@@ -360,9 +360,9 @@ options =
360360 , Option "" ["columns"]
361361 (ReqArg
362362 (\arg opt ->
363− case reads arg of
364− [(t,"")] | t > 0 -> return opt { optColumns = t }
365− _ -> err 33 $
363+ case safeRead arg of
364+ Just t | t > 0 -> return opt { optColumns = t }
365+ _ -> err 33 $
366366 "columns must be a number greater than 0")
367367 "NUMBER")
368368 "" -- "Length of line in characters"
@@ -494,10 +494,10 @@ options =
494494 , Option "" ["slide-level"]
495495 (ReqArg
496496 (\arg opt -> do
497− case reads arg of
498− [(t,"")] | t >= 1 && t <= 6 ->
497+ case safeRead arg of
498+ Just t | t >= 1 && t <= 6 ->
499499 return opt { optSlideLevel = Just t }
500− _ -> err 39 $
500+ _ -> err 39 $
501501 "slide level must be a number between 1 and 6")
502502 "NUMBER")
503503 "" -- "Force header level for slides"
@@ -713,9 +713,9 @@ options =
713713 ]
714714
715715readExtension :: String -> IO Extension
716−readExtension s = case reads ('E':'x':'t':'_':map toLower s) of
717− ((ext,""):_) -> return ext
718− _ -> err 59 $ "Unknown extension: " ++ s
716+readExtension s = case safeRead ('E':'x':'t':'_':map toLower s) of
717+ Just ext -> return ext
718+ Nothing -> err 59 $ "Unknown extension: " ++ s
719719
720720-- Returns usage message
721721usageMessage :: String -> [OptDescr (Opt -> IO Opt)] -> String