git.iko.soy/typeable/pandoc
on master help sign in
HTML Reader: Parse <ol> type, class, and inline list-style(-type) CSS
commit7ef8700734ea8caae083e372b51cfe7bf2c51f9b authorOphir Lifshitz <hangfromthefloor@gmail.com> dateFri 24 Jul 2015, 02:53 (11 years ago) parent8390d93
3 files changed+67−18
pandoc.cabal+2−1 src/Text/Pandoc/CSS.hs+35 src/Text/Pandoc/Readers/HTML.hs+30−17
pandoc.cabal+2−1
@@ -358,7 +358,8 @@ Library
358358 Text.Pandoc.Templates,
359359 Text.Pandoc.XML,
360360 Text.Pandoc.SelfContained,
361− Text.Pandoc.Process
361+ Text.Pandoc.Process,
362+ Text.Pandoc.CSS
362363 Other-Modules: Text.Pandoc.Readers.Docx.Lists,
363364 Text.Pandoc.Readers.Docx.Reducible,
364365 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)
6464import Control.Monad.Reader (Reader,ask, asks, local, runReader)
6565import Network.URI (isURI)
6666import Text.Pandoc.Error
67+import Text.Pandoc.CSS (foldOrElse, pickStyleAttrProps)
6768
6869import Text.Parsec.Error
6970
@@ -252,6 +253,22 @@ pListItem nonItem = do
252253 let liDiv = maybe mempty (\x -> B.divWith (x, [], []) mempty) (lookup "id" attr)
253254 (liDiv <>) <$> pInTags "li" block <* skipMany nonItem
254255
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+
255272pOrderedList :: TagParser Blocks
256273pOrderedList = try $ do
257274 TagOpen _ attribs <- pSatisfy (~== TagOpen "ol" [])
@@ -261,23 +278,19 @@ pOrderedList = try $ do
261278 sta' = if all isDigit sta
262279 then read sta
263280 else 1
264− sty = fromMaybe (fromMaybe "" $
265− lookup "style" attribs) $
266− lookup "class" attribs
267− sty' = case sty of
268− "lower-roman" -> LowerRoman
269− "upper-roman" -> UpperRoman
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
281+
282+ pickListStyle = pickStyleAttrProps ["list-style-type", "list-style"]
283+
284+ typeAttr = fromMaybe "" $ lookup "type" attribs
285+ classAttr = fromMaybe "" $ lookup "class" attribs
286+ styleAttr = fromMaybe "" $ lookup "style" attribs
287+ listStyle = fromMaybe "" $ pickListStyle styleAttr
288+
289+ sty' = foldOrElse DefaultStyle
290+ [ parseTypeAttr typeAttr
291+ , parseListStyleType classAttr
292+ , parseListStyleType listStyle
293+ ]
281294 let nonItem = pSatisfy (\t ->
282295 not (tagOpen (`elem` ["li","ol","ul","dl"]) (const True) t) &&
283296 not (t ~== TagClose "ol"))