Replace read with safeRead. Closes #5162.

This commit is contained in:
John MacFarlane 2018-12-17 10:24:09 -08:00
parent 90c820dc4e
commit 404e96761a
3 changed files with 8 additions and 11 deletions

View file

@ -39,21 +39,21 @@ import Data.List
import Data.Maybe
import Text.Pandoc.Generic (bottomUp)
import Text.Pandoc.JSON
import Text.Pandoc.Shared (trim)
import Text.Pandoc.Shared (trim, safeRead)
isListItem :: Block -> Bool
isListItem (Div (_, classes, _) _) | "list-item" `elem` classes = True
isListItem _ = False
getLevel :: Block -> Maybe Integer
getLevel (Div (_, _, kvs) _) = read <$> lookup "level" kvs
getLevel (Div (_, _, kvs) _) = lookup "level" kvs >>= safeRead
getLevel _ = Nothing
getLevelN :: Block -> Integer
getLevelN b = fromMaybe (-1) (getLevel b)
getNumId :: Block -> Maybe Integer
getNumId (Div (_, _, kvs) _) = read <$> lookup "num-id" kvs
getNumId (Div (_, _, kvs) _) = lookup "num-id" kvs >>= safeRead
getNumId _ = Nothing
getNumIdN :: Block -> Integer
@ -89,7 +89,7 @@ getListType b@(Div (_, _, kvs) _) | isListItem b =
Just f ->
case txt of
Just t -> Just $ Enumerated (
read (fromMaybe "1" start) :: Int,
fromMaybe 1 (start >>= safeRead) :: Int,
fromMaybe DefaultStyle (lookup f listStyleMap),
fromMaybe DefaultDelim (lookup t listDelimMap))
Nothing -> Nothing

View file

@ -62,7 +62,6 @@ import Prelude
import Control.Applicative hiding (liftA, liftA2, liftA3)
import Control.Arrow
import Data.Char (isDigit)
import Data.Default
import qualified Data.Foldable as F
import Data.List (unfoldr)
@ -72,6 +71,8 @@ import qualified Data.Set as S
import qualified Text.XML.Light as XML
import Text.Pandoc.Shared (safeRead)
import Text.Pandoc.Readers.Odt.Arrows.Utils
import Text.Pandoc.Readers.Odt.Generic.Fallible
@ -576,11 +577,7 @@ readListLevelStyle levelType = readAttr NsText "level"
toListLevelStyle _ p s LinfNone b = ListLevelStyle LltBullet p s LinfNone (startValue b)
toListLevelStyle _ p s f@(LinfString _) b = ListLevelStyle LltBullet p s f (startValue b)
toListLevelStyle t p s f b = ListLevelStyle t p s f (startValue b)
startValue (Just "") = 1
startValue (Just v) = if all isDigit v
then read v
else 1
startValue Nothing = 1
startValue mbx = fromMaybe 1 (mbx >>= safeRead)
--
chooseMostSpecificListLevelStyle :: S.Set ListLevelStyle -> Maybe ListLevelStyle

View file

@ -265,7 +265,7 @@ macroDefinition = try $ do
return (macroName, expander)
where
placeholder :: Monad m => OrgParser m Int
placeholder = try . fmap read $ char '$' *> many1 digit
placeholder = try . fmap (fromMaybe 1 . safeRead) $ char '$' *> many1 digit
expansionPart :: Monad m => OrgParser m String
expansionPart = try $ many (notFollowedBy placeholder *> noneOf "\n\r")