git.iko.soy/typeable/pandoc
on master help sign in
HTML reader: Handle tbody, thead in simple tables.
934867fJohn MacFarlane
src/Text/Pandoc/Readers/HTML.hs+17−7
@@ -47,7 +47,7 @@ import Text.Pandoc.Parsing
47import Data.Maybe ( fromMaybe, isJust )
47import Data.Maybe ( fromMaybe, isJust )
48import Data.List ( intercalate )
48import Data.List ( intercalate )
49import Data.Char ( isSpace, isDigit )
49import Data.Char ( isSpace, isDigit )
50−import Control.Monad ( liftM, guard )
50+import Control.Monad ( liftM, guard, when )
51
51
52-- | Convert HTML-formatted string to 'Pandoc' document.
52-- | Convert HTML-formatted string to 'Pandoc' document.
53readHtml :: ParserState -- ^ Parser state
53readHtml :: ParserState -- ^ Parser state
@@ -211,9 +211,9 @@ pSimpleTable :: TagParser [Block]
211pSimpleTable = try $ do
211pSimpleTable = try $ do
212 TagOpen _ _ <- pSatisfy (~== TagOpen "table" [])
212 TagOpen _ _ <- pSatisfy (~== TagOpen "table" [])
213 skipMany pBlank
213 skipMany pBlank
214− head' <- option [] $ pInTags "th" pTd
214+ head' <- option [] $ pOptInTag "thead" $ pInTags "tr" (pCell "th")
215− rows <- many1 $ try $
215+ rows <- pOptInTag "tbody"
216− skipMany pBlank >> pInTags "tr" pTd
216+ $ many1 $ try $ skipMany pBlank >> pInTags "tr" (pCell "td")
217 skipMany pBlank
217 skipMany pBlank
218 TagClose _ <- pSatisfy (~== TagClose "table")
218 TagClose _ <- pSatisfy (~== TagClose "table")
219 let cols = maximum $ map length rows
219 let cols = maximum $ map length rows
@@ -221,10 +221,10 @@ pSimpleTable = try $ do
221 let widths = replicate cols 0
221 let widths = replicate cols 0
222 return [Table [] aligns widths head' rows]
222 return [Table [] aligns widths head' rows]
223
223
224−pTd :: TagParser [TableCell]
224+pCell :: String -> TagParser [TableCell]
225−pTd = try $ do
225+pCell celltype = try $ do
226 skipMany pBlank
226 skipMany pBlank
227− res <- pInTags "td" pPlain
227+ res <- pInTags celltype pPlain
228 skipMany pBlank
228 skipMany pBlank
229 return [res]
229 return [res]
230
230
@@ -378,6 +378,16 @@ pInTags tagtype parser = try $ do
378 pSatisfy (~== TagOpen tagtype [])
378 pSatisfy (~== TagOpen tagtype [])
379 liftM concat $ manyTill parser (pCloses tagtype <|> eof)
379 liftM concat $ manyTill parser (pCloses tagtype <|> eof)
380
380
381+pOptInTag :: String -> TagParser a
382+ -> TagParser a
383+pOptInTag tagtype parser = try $ do
384+ open <- option False (pSatisfy (~== TagOpen tagtype []) >> return True)
385+ skipMany pBlank
386+ x <- parser
387+ skipMany pBlank
388+ when open $ pCloses tagtype
389+ return x
390+
381pCloses :: String -> TagParser ()
391pCloses :: String -> TagParser ()
382pCloses tagtype = try $ do
392pCloses tagtype = try $ do
383 t <- lookAhead $ pSatisfy $ \tag -> isTagClose tag || isTagOpen tag
393 t <- lookAhead $ pSatisfy $ \tag -> isTagClose tag || isTagOpen tag