git.iko.soy/typeable/pandoc
on master help sign in
HTML reader: Handle tbody, thead in simple tables.

Closes #274.

commit934867f85829a75f38291cd694da32184252a305 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 15 Jul 2011, 21:14 (15 years ago) parentb30afc2
1 file changed+17−7
src/Text/Pandoc/Readers/HTML.hs+17−7
src/Text/Pandoc/Readers/HTML.hs+17−7
@@ -47,7 +47,7 @@ import Text.Pandoc.Parsing
4747import Data.Maybe ( fromMaybe, isJust )
4848import Data.List ( intercalate )
4949import Data.Char ( isSpace, isDigit )
50−import Control.Monad ( liftM, guard )
50+import Control.Monad ( liftM, guard, when )
5151
5252-- | Convert HTML-formatted string to 'Pandoc' document.
5353readHtml :: ParserState -- ^ Parser state
@@ -211,9 +211,9 @@ pSimpleTable :: TagParser [Block]
211211pSimpleTable = try $ do
212212 TagOpen _ _ <- pSatisfy (~== TagOpen "table" [])
213213 skipMany pBlank
214− head' <- option [] $ pInTags "th" pTd
215− rows <- many1 $ try $
216− skipMany pBlank >> pInTags "tr" pTd
214+ head' <- option [] $ pOptInTag "thead" $ pInTags "tr" (pCell "th")
215+ rows <- pOptInTag "tbody"
216+ $ many1 $ try $ skipMany pBlank >> pInTags "tr" (pCell "td")
217217 skipMany pBlank
218218 TagClose _ <- pSatisfy (~== TagClose "table")
219219 let cols = maximum $ map length rows
@@ -221,10 +221,10 @@ pSimpleTable = try $ do
221221 let widths = replicate cols 0
222222 return [Table [] aligns widths head' rows]
223223
224−pTd :: TagParser [TableCell]
225−pTd = try $ do
224+pCell :: String -> TagParser [TableCell]
225+pCell celltype = try $ do
226226 skipMany pBlank
227− res <- pInTags "td" pPlain
227+ res <- pInTags celltype pPlain
228228 skipMany pBlank
229229 return [res]
230230
@@ -378,6 +378,16 @@ pInTags tagtype parser = try $ do
378378 pSatisfy (~== TagOpen tagtype [])
379379 liftM concat $ manyTill parser (pCloses tagtype <|> eof)
380380
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+
381391pCloses :: String -> TagParser ()
382392pCloses tagtype = try $ do
383393 t <- lookAhead $ pSatisfy $ \tag -> isTagClose tag || isTagOpen tag