git.iko.soy/typeable/pandoc
on master help sign in
HTML reader: simplify list attribute handling

This removes the `foldOrElse` function from the internal Text.Pandoc.CSS module.

commitc6f2663a237897a7310d30b825dc5ad8c6bedd3e authorAlbert Krewinkel <albert@zeitkraut.de> dateWed 25 Nov 2020, 17:51 (6 years ago) parente26d31d
2 files changed+12−27
src/Text/Pandoc/CSS.hs+3−9 src/Text/Pandoc/Readers/HTML.hs+9−18
src/Text/Pandoc/CSS.hs+3−9
@@ -11,13 +11,13 @@ Portability : portable
1111
1212Tools for working with CSS.
1313-}
14−module Text.Pandoc.CSS ( foldOrElse
15− , pickStyleAttrProps
14+module Text.Pandoc.CSS ( pickStyleAttrProps
1615 , pickStylesToKVs
1716 )
1817where
1918
2019import qualified Data.Text as T
20+import Data.Maybe (mapMaybe, listToMaybe)
2121import Text.Pandoc.Shared (trim)
2222import Text.Parsec
2323import Text.Parsec.Text
@@ -31,12 +31,6 @@ ruleParser = do
3131styleAttrParser :: Parser [(T.Text, T.Text)]
3232styleAttrParser = many1 ruleParser
3333
34−orElse :: Eq a => a -> a -> a -> a
35−orElse v x y = if v == x then y else x
36−
37−foldOrElse :: Eq a => a -> [a] -> a
38−foldOrElse v xs = foldr (orElse v) v xs
39−
4034eitherToMaybe :: Either a b -> Maybe b
4135eitherToMaybe (Right x) = Just x
4236eitherToMaybe _ = Nothing
@@ -54,4 +48,4 @@ pickStylesToKVs props styleAttr =
5448pickStyleAttrProps :: [T.Text] -> T.Text -> Maybe T.Text
5549pickStyleAttrProps lookupProps styleAttr = do
5650 styles <- eitherToMaybe $ parse styleAttrParser "" styleAttr
57− foldOrElse Nothing $ map (`lookup` styles) lookupProps
51+ listToMaybe $ mapMaybe (`lookup` styles) lookupProps
src/Text/Pandoc/Readers/HTML.hs+9−18
@@ -46,7 +46,7 @@ import Text.HTML.TagSoup.Match
4646import Text.Pandoc.Builder (Blocks, Inlines, trimInlines)
4747import qualified Text.Pandoc.Builder as B
4848import Text.Pandoc.Class.PandocMonad (PandocMonad (..))
49−import Text.Pandoc.CSS (foldOrElse, pickStyleAttrProps)
49+import Text.Pandoc.CSS (pickStyleAttrProps)
5050import qualified Text.Pandoc.UTF8 as UTF8
5151import Text.Pandoc.Definition
5252import Text.Pandoc.Readers.HTML.Parsing
@@ -290,23 +290,14 @@ pOrderedList :: PandocMonad m => TagParser m Blocks
290290pOrderedList = try $ do
291291 TagOpen _ attribs' <- pSatisfy (matchTagOpen "ol" [])
292292 let attribs = toStringAttr attribs'
293− let (start, style) = (sta', sty')
294− where sta = fromMaybe "1" $
295− lookup "start" attribs
296− sta' = fromMaybe 1 $ safeRead sta
297−
298− pickListStyle = pickStyleAttrProps ["list-style-type", "list-style"]
299−
300− typeAttr = fromMaybe "" $ lookup "type" attribs
301− classAttr = fromMaybe "" $ lookup "class" attribs
302− styleAttr = fromMaybe "" $ lookup "style" attribs
303− listStyle = fromMaybe "" $ pickListStyle styleAttr
304−
305− sty' = foldOrElse DefaultStyle
306− [ parseTypeAttr typeAttr
307− , parseListStyleType classAttr
308− , parseListStyleType listStyle
309− ]
293+ let start = fromMaybe 1 $ lookup "start" attribs >>= safeRead
294+ let style = fromMaybe DefaultStyle
295+ $ (parseTypeAttr <$> lookup "type" attribs)
296+ <|> (parseListStyleType <$> lookup "class" attribs)
297+ <|> (parseListStyleType <$> (lookup "style" attribs >>= pickListStyle))
298+ where
299+ pickListStyle = pickStyleAttrProps ["list-style-type", "list-style"]
300+
310301 let nonItem = pSatisfy (\t ->
311302 not (tagOpen (`elem` ["li","ol","ul","dl"]) (const True) t) &&
312303 not (matchTagClose "ol" t))