git.iko.soy/typeable/pandoc
on master help sign in
Use safeRead instead of using reads directly (various modules).
a740de0John MacFarlane
src/Text/Pandoc.hs+4−4
@@ -135,6 +135,7 @@ import Text.Pandoc.Writers.Org
135import Text.Pandoc.Writers.AsciiDoc
135import Text.Pandoc.Writers.AsciiDoc
136import Text.Pandoc.Templates
136import Text.Pandoc.Templates
137import Text.Pandoc.Options
137import Text.Pandoc.Options
138+import Text.Pandoc.Shared (safeRead)
138import Data.ByteString.Lazy (ByteString)
139import Data.ByteString.Lazy (ByteString)
139import Data.Version (showVersion)
140import Data.Version (showVersion)
140import Text.JSON.Generic
141import Text.JSON.Generic
@@ -158,10 +159,9 @@ parseFormatSpec = parse formatSpec ""
158 extMod = do
159 extMod = do
159 polarity <- oneOf "-+"
160 polarity <- oneOf "-+"
160 name <- many1 $ noneOf "-+"
161 name <- many1 $ noneOf "-+"
161− ext <- case reads name of
162+ ext <- case safeRead name of
162− ((n,[]):_) -> return n
163+ Just n -> return n
163− _ -> unexpected $ "Unknown extension: " ++
164+ Nothing -> unexpected $ "Unknown extension: " ++ name
164− name
165 return $ case polarity of
165 return $ case polarity of
166 '-' -> Set.delete ext
166 '-' -> Set.delete ext
167 _ -> Set.insert ext
167 _ -> Set.insert ext
src/Text/Pandoc/Highlighting.hs+4−3
@@ -47,6 +47,7 @@ module Text.Pandoc.Highlighting ( languages
47 , Style
47 , Style
48 ) where
48 ) where
49import Text.Pandoc.Definition
49import Text.Pandoc.Definition
50+import Text.Pandoc.Shared (safeRead)
50import Text.Highlighting.Kate
51import Text.Highlighting.Kate
51import Data.List (find)
52import Data.List (find)
52import Data.Maybe (fromMaybe)
53import Data.Maybe (fromMaybe)
@@ -60,9 +61,9 @@ highlight :: (FormatOptions -> [SourceLine] -> a) -- ^ Formatter
60 -> String -- ^ Raw contents of the CodeBlock
61 -> String -- ^ Raw contents of the CodeBlock
61 -> Maybe a -- ^ Maybe the formatted result
62 -> Maybe a -- ^ Maybe the formatted result
62highlight formatter (_, classes, keyvals) rawCode =
63highlight formatter (_, classes, keyvals) rawCode =
63− let firstNum = case reads (fromMaybe "1" $ lookup "startFrom" keyvals) of
64+ let firstNum = case safeRead (fromMaybe "1" $ lookup "startFrom" keyvals) of
64− ((n,_):_) -> n
65+ Just n -> n
65− [] -> 1
66+ Nothing -> 1
66 fmtOpts = defaultFormatOpts{
67 fmtOpts = defaultFormatOpts{
67 startNumber = firstNum,
68 startNumber = firstNum,
68 numberLines = any (`elem`
69 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@,
31module Text.Pandoc.Readers.Native ( readNative ) where
31module Text.Pandoc.Readers.Native ( readNative ) where
32
32
33import Text.Pandoc.Definition
33import Text.Pandoc.Definition
34+import Text.Pandoc.Shared (safeRead)
34
35
35nullMeta :: Meta
36nullMeta :: Meta
36nullMeta = Meta{ docTitle = []
37nullMeta = Meta{ docTitle = []
@@ -51,31 +52,31 @@ nullMeta = Meta{ docTitle = []
51readNative :: String -- ^ String to parse (assuming @'\n'@ line endings)
52readNative :: String -- ^ String to parse (assuming @'\n'@ line endings)
52 -> Pandoc
53 -> Pandoc
53readNative s =
54readNative s =
54− case reads s of
55+ case safeRead s of
55− (d,_):_ -> d
56+ Just d -> d
56− [] -> Pandoc nullMeta $ readBlocks s
57+ Nothing -> Pandoc nullMeta $ readBlocks s
57
58
58readBlocks :: String -> [Block]
59readBlocks :: String -> [Block]
59readBlocks s =
60readBlocks s =
60− case reads s of
61+ case safeRead s of
61− (d,_):_ -> d
62+ Just d -> d
62− [] -> [readBlock s]
63+ Nothing -> [readBlock s]
63
64
64readBlock :: String -> Block
65readBlock :: String -> Block
65readBlock s =
66readBlock s =
66− case reads s of
67+ case safeRead s of
67− (d,_):_ -> d
68+ Just d -> d
68− [] -> Plain $ readInlines s
69+ Nothing -> Plain $ readInlines s
69
70
70readInlines :: String -> [Inline]
71readInlines :: String -> [Inline]
71readInlines s =
72readInlines s =
72− case reads s of
73+ case safeRead s of
73− (d,_):_ -> d
74+ Just d -> d
74− [] -> [readInline s]
75+ Nothing -> [readInline s]
75
76
76readInline :: String -> Inline
77readInline :: String -> Inline
77readInline s =
78readInline s =
78− case reads s of
79+ case safeRead s of
79− (d,_):_ -> d
80+ Just d -> d
80− [] -> error "Cannot parse document"
81+ Nothing -> error "Cannot parse document"
81
82
src/Text/Pandoc/Writers/EPUB.hs+3−3
@@ -309,9 +309,9 @@ ppTopElement = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ++) . unEntity .
309 unEntity ('&':'#':xs) =
309 unEntity ('&':'#':xs) =
310 let (ds,ys) = break (==';') xs
310 let (ds,ys) = break (==';') xs
311 rest = drop 1 ys
311 rest = drop 1 ys
312− in case reads ('\'':'\\':ds ++ "'") of
312+ in case safeRead ('\'':'\\':ds ++ "'") of
313− ((x,_):_) -> x : unEntity rest
313+ Just x -> x : unEntity rest
314− _ -> '&':'#':unEntity xs
314+ Nothing -> '&':'#':unEntity xs
315 unEntity (x:xs) = x : unEntity xs
315 unEntity (x:xs) = x : unEntity xs
316
316
317imageTypeOf :: FilePath -> Maybe String
317imageTypeOf :: FilePath -> Maybe String
src/pandoc.hs+16−16
@@ -33,7 +33,7 @@ module Main where
33import Text.Pandoc
33import Text.Pandoc
34import Text.Pandoc.PDF (tex2pdf)
34import Text.Pandoc.PDF (tex2pdf)
35import Text.Pandoc.Readers.LaTeX (handleIncludes)
35import Text.Pandoc.Readers.LaTeX (handleIncludes)
36−import Text.Pandoc.Shared ( tabFilter, readDataFile,
36+import Text.Pandoc.Shared ( tabFilter, readDataFile, safeRead,
37 headerShift, findDataFile, normalize, err, warn )
37 headerShift, findDataFile, normalize, err, warn )
38import Text.Pandoc.XML ( toEntities, fromEntities )
38import Text.Pandoc.XML ( toEntities, fromEntities )
39import Text.Pandoc.SelfContained ( makeSelfContained )
39import Text.Pandoc.SelfContained ( makeSelfContained )
@@ -279,13 +279,13 @@ options =
279 , Option "" ["base-header-level"]
279 , Option "" ["base-header-level"]
280 (ReqArg
280 (ReqArg
281 (\arg opt ->
281 (\arg opt ->
282− case reads arg of
282+ case safeRead arg of
283− [(t,"")] | t > 0 -> do
283+ Just t | t > 0 -> do
284 let oldTransforms = optTransforms opt
284 let oldTransforms = optTransforms opt
285 let shift = t - 1
285 let shift = t - 1
286 return opt{ optTransforms =
286 return opt{ optTransforms =
287 headerShift shift : oldTransforms }
287 headerShift shift : oldTransforms }
288− _ -> err 19
288+ _ -> err 19
289 "base-header-level must be a number > 0")
289 "base-header-level must be a number > 0")
290 "NUMBER")
290 "NUMBER")
291 "" -- "Headers base level"
291 "" -- "Headers base level"
@@ -311,9 +311,9 @@ options =
311 , Option "" ["tab-stop"]
311 , Option "" ["tab-stop"]
312 (ReqArg
312 (ReqArg
313 (\arg opt ->
313 (\arg opt ->
314− case reads arg of
314+ case safeRead arg of
315− [(t,"")] | t > 0 -> return opt { optTabStop = t }
315+ Just t | t > 0 -> return opt { optTabStop = t }
316− _ -> err 31
316+ _ -> err 31
317 "tab-stop must be a number greater than 0")
317 "tab-stop must be a number greater than 0")
318 "NUMBER")
318 "NUMBER")
319 "" -- "Tab stop (default 4)"
319 "" -- "Tab stop (default 4)"
@@ -360,9 +360,9 @@ options =
360 , Option "" ["columns"]
360 , Option "" ["columns"]
361 (ReqArg
361 (ReqArg
362 (\arg opt ->
362 (\arg opt ->
363− case reads arg of
363+ case safeRead arg of
364− [(t,"")] | t > 0 -> return opt { optColumns = t }
364+ Just t | t > 0 -> return opt { optColumns = t }
365− _ -> err 33 $
365+ _ -> err 33 $
366 "columns must be a number greater than 0")
366 "columns must be a number greater than 0")
367 "NUMBER")
367 "NUMBER")
368 "" -- "Length of line in characters"
368 "" -- "Length of line in characters"
@@ -494,10 +494,10 @@ options =
494 , Option "" ["slide-level"]
494 , Option "" ["slide-level"]
495 (ReqArg
495 (ReqArg
496 (\arg opt -> do
496 (\arg opt -> do
497− case reads arg of
497+ case safeRead arg of
498− [(t,"")] | t >= 1 && t <= 6 ->
498+ Just t | t >= 1 && t <= 6 ->
499 return opt { optSlideLevel = Just t }
499 return opt { optSlideLevel = Just t }
500− _ -> err 39 $
500+ _ -> err 39 $
501 "slide level must be a number between 1 and 6")
501 "slide level must be a number between 1 and 6")
502 "NUMBER")
502 "NUMBER")
503 "" -- "Force header level for slides"
503 "" -- "Force header level for slides"
@@ -713,9 +713,9 @@ options =
713 ]
713 ]
714
714
715readExtension :: String -> IO Extension
715readExtension :: String -> IO Extension
716−readExtension s = case reads ('E':'x':'t':'_':map toLower s) of
716+readExtension s = case safeRead ('E':'x':'t':'_':map toLower s) of
717− ((ext,""):_) -> return ext
717+ Just ext -> return ext
718− _ -> err 59 $ "Unknown extension: " ++ s
718+ Nothing -> err 59 $ "Unknown extension: " ++ s
719
719
720-- Returns usage message
720-- Returns usage message
721usageMessage :: String -> [OptDescr (Opt -> IO Opt)] -> String
721usageMessage :: String -> [OptDescr (Opt -> IO Opt)] -> String