@@ -55,9 +55,10 @@ import Text.Pandoc.Walk
55import qualified Data.Map as M
55import qualified Data.Map as M
56import Data.Foldable ( for_ )
56import Data.Foldable ( for_ )
57import Data.Maybe ( fromMaybe, isJust, isNothing )
57import Data.Maybe ( fromMaybe, isJust, isNothing )
58+import Data.List.Split ( wordsBy )
58import Data.List ( intercalate, isPrefixOf )
59import Data.List ( intercalate, isPrefixOf )
59import Data.Char ( isDigit, isLetter, isAlphaNum )
60import Data.Char ( isDigit, isLetter, isAlphaNum )
60−import Control.Monad ( guard, mzero, void, unless )
61+import Control.Monad ( guard, mzero, void, unless, mplus )
61import Control.Arrow ((***))
62import Control.Arrow ((***))
62import Control.Applicative ( (<|>) )
63import Control.Applicative ( (<|>) )
63import Data.Monoid (First (..))
64import Data.Monoid (First (..))
@@ -472,31 +473,35 @@ pTable = try $ do
472 caption <- option mempty $ pInTags "caption" inline <* skipMany pBlank
473 caption <- option mempty $ pInTags "caption" inline <* skipMany pBlank
473 widths' <- (mconcat <$> many1 pColgroup) <|> many pCol
474 widths' <- (mconcat <$> many1 pColgroup) <|> many pCol
474 let pTh = option [] $ pInTags "tr" (pCell "th")
475 let pTh = option [] $ pInTags "tr" (pCell "th")
476+ pTr = try $ skipMany pBlank >>
475− pTr = try $ skipMany pBlank >> pInTags "tr" (pCell "td" <|> pCell "th")
477+ pInTags "tr" (pCell "td" <|> pCell "th")
476 pTBody = do pOptInTag "tbody" $ many1 pTr
478 pTBody = do pOptInTag "tbody" $ many1 pTr
477 head'' <- pOptInTag "thead" pTh
479 head'' <- pOptInTag "thead" pTh
480+ head' <- map snd <$>
478− head' <- pOptInTag "tbody" $ do
481+ (pOptInTag "tbody" $
481− else return head''
482+ if null head'' then pTh else return head'')
482 rowsLs <- many pTBody
483 rowsLs <- many pTBody
483 rows' <- pOptInTag "tfoot" $ many pTr
484 rows' <- pOptInTag "tfoot" $ many pTr
484 TagClose _ <- pSatisfy (matchTagClose "table")
485 TagClose _ <- pSatisfy (matchTagClose "table")
485 let rows'' = (concat rowsLs) <> rows'
486 let rows'' = (concat rowsLs) <> rows'
487+ let rows''' = map (map snd) rows''
488+ -- let rows''' = map (map snd) rows''
486 -- fail on empty table
489 -- fail on empty table
487− guard $ not $ null head' && null rows''
490+ guard $ not $ null head' && null rows'''
488 let isSinglePlain x = case B.toList x of
491 let isSinglePlain x = case B.toList x of
489 [] -> True
492 [] -> True
490 [Plain _] -> True
493 [Plain _] -> True
491 _ -> False
494 _ -> False
492− let isSimple = all isSinglePlain $ concat (head':rows'')
495+ let isSimple = all isSinglePlain $ concat (head':rows''')
493− let cols = length $ if null head' then head rows'' else head'
496+ let cols = length $ if null head' then head rows''' else head'
494 -- add empty cells to short rows
497 -- add empty cells to short rows
495 let addEmpties r = case cols - length r of
498 let addEmpties r = case cols - length r of
496 n | n > 0 -> r <> replicate n mempty
499 n | n > 0 -> r <> replicate n mempty
497 | otherwise -> r
500 | otherwise -> r
498− let rows = map addEmpties rows''
501+ let rows = map addEmpties rows'''
502+ let aligns = case rows'' of
503+ (cs:_) -> map fst cs
499− let aligns = replicate cols AlignDefault
504+ _ -> replicate cols AlignDefault
500 let widths = if null widths'
505 let widths = if null widths'
501 then if isSimple
506 then if isSimple
502 then replicate cols 0
507 then replicate cols 0
@@ -534,12 +539,24 @@ noColOrRowSpans t = isNullOrOne "colspan" && isNullOrOne "rowspan"
534 "1" -> True
539 "1" -> True
535 _ -> False
540 _ -> False
537−pCell :: PandocMonad m => Text -> TagParser m [Blocks]
542+pCell :: PandocMonad m => Text -> TagParser m [(Alignment, Blocks)]
538pCell celltype = try $ do
543pCell celltype = try $ do
539 skipMany pBlank
544 skipMany pBlank
546+ pSatisfy (\t -> t ~== TagOpen celltype [] && noColOrRowSpans t)
547+ let extractAlign' [] = ""
548+ extractAlign' ("text-align":x:_) = x
549+ extractAlign' (_:xs) = extractAlign' xs
550+ let extractAlign = extractAlign' . wordsBy (`elem` [' ','\t',';',':'])
551+ let align = case maybeFromAttrib "align" tag `mplus`
552+ (extractAlign <$> maybeFromAttrib "style" tag) of
553+ Just "left" -> AlignLeft
554+ Just "right" -> AlignRight
555+ Just "center" -> AlignCenter
540 res <- pInTags' celltype noColOrRowSpans block
557 res <- pInTags' celltype noColOrRowSpans block
541 skipMany pBlank
558 skipMany pBlank
542− return [res]
559+ return [(align, res)]
544pBlockQuote :: PandocMonad m => TagParser m Blocks
561pBlockQuote :: PandocMonad m => TagParser m Blocks
545pBlockQuote = do
562pBlockQuote = do