@@ -55,9 +55,10 @@ import Text.Pandoc.Walk
5555import qualified Data.Map as M
5656import Data.Foldable ( for_ )
5757import Data.Maybe ( fromMaybe, isJust, isNothing )
58+import Data.List.Split ( wordsBy )
5859import Data.List ( intercalate, isPrefixOf )
5960import Data.Char ( isDigit, isLetter, isAlphaNum )
60−import Control.Monad ( guard, mzero, void, unless )
61+import Control.Monad ( guard, mzero, void, unless, mplus )
6162import Control.Arrow ((***))
6263import Control.Applicative ( (<|>) )
6364import Data.Monoid (First (..))
@@ -472,31 +473,35 @@ pTable = try $ do
472473 caption <- option mempty $ pInTags "caption" inline <* skipMany pBlank
473474 widths' <- (mconcat <$> many1 pColgroup) <|> many pCol
474475 let pTh = option [] $ pInTags "tr" (pCell "th")
475− pTr = try $ skipMany pBlank >> pInTags "tr" (pCell "td" <|> pCell "th")
476+ pTr = try $ skipMany pBlank >>
477+ pInTags "tr" (pCell "td" <|> pCell "th")
476478 pTBody = do pOptInTag "tbody" $ many1 pTr
477479 head'' <- pOptInTag "thead" pTh
478− head' <- pOptInTag "tbody" $ do
479− if null head''
480− then pTh
481− else return head''
480+ head' <- map snd <$>
481+ (pOptInTag "tbody" $
482+ if null head'' then pTh else return head'')
482483 rowsLs <- many pTBody
483484 rows' <- pOptInTag "tfoot" $ many pTr
484485 TagClose _ <- pSatisfy (matchTagClose "table")
485486 let rows'' = (concat rowsLs) <> rows'
487+ let rows''' = map (map snd) rows''
488+ -- let rows''' = map (map snd) rows''
486489 -- fail on empty table
487− guard $ not $ null head' && null rows''
490+ guard $ not $ null head' && null rows'''
488491 let isSinglePlain x = case B.toList x of
489492 [] -> True
490493 [Plain _] -> True
491494 _ -> False
492− let isSimple = all isSinglePlain $ concat (head':rows'')
493− let cols = length $ if null head' then head rows'' else head'
495+ let isSimple = all isSinglePlain $ concat (head':rows''')
496+ let cols = length $ if null head' then head rows''' else head'
494497 -- add empty cells to short rows
495498 let addEmpties r = case cols - length r of
496499 n | n > 0 -> r <> replicate n mempty
497500 | otherwise -> r
498− let rows = map addEmpties rows''
499− let aligns = replicate cols AlignDefault
501+ let rows = map addEmpties rows'''
502+ let aligns = case rows'' of
503+ (cs:_) -> map fst cs
504+ _ -> replicate cols AlignDefault
500505 let widths = if null widths'
501506 then if isSimple
502507 then replicate cols 0
@@ -534,12 +539,24 @@ noColOrRowSpans t = isNullOrOne "colspan" && isNullOrOne "rowspan"
534539 "1" -> True
535540 _ -> False
536541
537−pCell :: PandocMonad m => Text -> TagParser m [Blocks]
542+pCell :: PandocMonad m => Text -> TagParser m [(Alignment, Blocks)]
538543pCell celltype = try $ do
539544 skipMany pBlank
545+ tag <- lookAhead $
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
556+ _ -> AlignDefault
540557 res <- pInTags' celltype noColOrRowSpans block
541558 skipMany pBlank
542− return [res]
559+ return [(align, res)]
543560
544561pBlockQuote :: PandocMonad m => TagParser m Blocks
545562pBlockQuote = do