git.iko.soy/typeable/pandoc
on master help sign in
HTML Reader: Parse <ol> type, class, and inline list-style(-type) CSS
7ef8700Ophir Lifshitz
pandoc.cabal+2−1
@@ -358,7 +358,8 @@ Library
358 Text.Pandoc.Templates,
358 Text.Pandoc.Templates,
359 Text.Pandoc.XML,
359 Text.Pandoc.XML,
360 Text.Pandoc.SelfContained,
360 Text.Pandoc.SelfContained,
361− Text.Pandoc.Process
361+ Text.Pandoc.Process,
362+ Text.Pandoc.CSS
362 Other-Modules: Text.Pandoc.Readers.Docx.Lists,
363 Other-Modules: Text.Pandoc.Readers.Docx.Lists,
363 Text.Pandoc.Readers.Docx.Reducible,
364 Text.Pandoc.Readers.Docx.Reducible,
364 Text.Pandoc.Readers.Docx.Parse,
365 Text.Pandoc.Readers.Docx.Parse,
src/Text/Pandoc/CSS.hsnew file+35
@@ -0,0 +1,35 @@
1+module Text.Pandoc.CSS ( foldOrElse,
2+ pickStyleAttrProps
3+ )
4+where
5+
6+import Text.Pandoc.Shared (trim)
7+import Text.Parsec
8+import Text.Parsec.String
9+import Control.Applicative ((<*))
10+
11+ruleParser :: Parser (String, String)
12+ruleParser = do
13+ p <- many1 (noneOf ":") <* char ':'
14+ v <- many1 (noneOf ":;") <* char ';' <* spaces
15+ return (trim p, trim v)
16+
17+styleAttrParser :: Parser [(String, String)]
18+styleAttrParser = do
19+ p <- many1 ruleParser
20+ return p
21+
22+orElse :: Eq a => a -> a -> a -> a
23+orElse v x y = if v == x then y else x
24+
25+foldOrElse :: Eq a => a -> [a] -> a
26+foldOrElse v xs = foldr (orElse v) v xs
27+
28+eitherToMaybe :: Either a b -> Maybe b
29+eitherToMaybe (Right x) = Just x
30+eitherToMaybe _ = Nothing
31+
32+pickStyleAttrProps :: [String] -> String -> Maybe String
33+pickStyleAttrProps lookupProps styleAttr = do
34+ styles <- eitherToMaybe $ parse styleAttrParser "" styleAttr
35+ foldOrElse Nothing $ map (flip lookup styles) lookupProps
src/Text/Pandoc/Readers/HTML.hs+30−17
@@ -64,6 +64,7 @@ import Data.Default (Default (..), def)
64import Control.Monad.Reader (Reader,ask, asks, local, runReader)
64import Control.Monad.Reader (Reader,ask, asks, local, runReader)
65import Network.URI (isURI)
65import Network.URI (isURI)
66import Text.Pandoc.Error
66import Text.Pandoc.Error
67+import Text.Pandoc.CSS (foldOrElse, pickStyleAttrProps)
67
68
68import Text.Parsec.Error
69import Text.Parsec.Error
69
70
@@ -252,6 +253,22 @@ pListItem nonItem = do
252 let liDiv = maybe mempty (\x -> B.divWith (x, [], []) mempty) (lookup "id" attr)
253 let liDiv = maybe mempty (\x -> B.divWith (x, [], []) mempty) (lookup "id" attr)
253 (liDiv <>) <$> pInTags "li" block <* skipMany nonItem
254 (liDiv <>) <$> pInTags "li" block <* skipMany nonItem
254
255
256+parseListStyleType :: String -> ListNumberStyle
257+parseListStyleType "lower-roman" = LowerRoman
258+parseListStyleType "upper-roman" = UpperRoman
259+parseListStyleType "lower-alpha" = LowerAlpha
260+parseListStyleType "upper-alpha" = UpperAlpha
261+parseListStyleType "decimal" = Decimal
262+parseListStyleType _ = DefaultStyle
263+
264+parseTypeAttr :: String -> ListNumberStyle
265+parseTypeAttr "i" = LowerRoman
266+parseTypeAttr "I" = UpperRoman
267+parseTypeAttr "a" = LowerAlpha
268+parseTypeAttr "A" = UpperAlpha
269+parseTypeAttr "1" = Decimal
270+parseTypeAttr _ = DefaultStyle
271+
255pOrderedList :: TagParser Blocks
272pOrderedList :: TagParser Blocks
256pOrderedList = try $ do
273pOrderedList = try $ do
257 TagOpen _ attribs <- pSatisfy (~== TagOpen "ol" [])
274 TagOpen _ attribs <- pSatisfy (~== TagOpen "ol" [])
@@ -261,23 +278,19 @@ pOrderedList = try $ do
261 sta' = if all isDigit sta
278 sta' = if all isDigit sta
262 then read sta
279 then read sta
263 else 1
280 else 1
264− sty = fromMaybe (fromMaybe "" $
281+
282+ pickListStyle = pickStyleAttrProps ["list-style-type", "list-style"]
283+
265− lookup "style" attribs) $
284+ typeAttr = fromMaybe "" $ lookup "type" attribs
266− lookup "class" attribs
285+ classAttr = fromMaybe "" $ lookup "class" attribs
267− sty' = case sty of
286+ styleAttr = fromMaybe "" $ lookup "style" attribs
268− "lower-roman" -> LowerRoman
287+ listStyle = fromMaybe "" $ pickListStyle styleAttr
269− "upper-roman" -> UpperRoman
288+
270− "lower-alpha" -> LowerAlpha
271− "upper-alpha" -> UpperAlpha
272− "decimal" -> Decimal
273− _ ->
274− case lookup "type" attribs of
275− Just "1" -> Decimal
276− Just "I" -> UpperRoman
277− Just "i" -> LowerRoman
278− Just "A" -> UpperAlpha
279− Just "a" -> LowerAlpha
280− _ -> DefaultStyle
289+ sty' = foldOrElse DefaultStyle
290+ [ parseTypeAttr typeAttr
291+ , parseListStyleType classAttr
292+ , parseListStyleType listStyle
293+ ]
281 let nonItem = pSatisfy (\t ->
294 let nonItem = pSatisfy (\t ->
282 not (tagOpen (`elem` ["li","ol","ul","dl"]) (const True) t) &&
295 not (tagOpen (`elem` ["li","ol","ul","dl"]) (const True) t) &&
283 not (t ~== TagClose "ol"))
296 not (t ~== TagClose "ol"))