@@ -50,10 +50,10 @@ import Text.Pandoc.CSS (foldOrElse, pickStyleAttrProps)
5050import qualified Text.Pandoc.UTF8 as UTF8
5151import Text.Pandoc.Definition
5252import Text.Pandoc.Readers.HTML.Parsing
53+import Text.Pandoc.Readers.HTML.Table (pTable')
5354import Text.Pandoc.Readers.HTML.TagCategories
5455import Text.Pandoc.Readers.HTML.Types
5556import Text.Pandoc.Readers.LaTeX (rawLaTeXInline)
56−import Text.Pandoc.XML (html5Attributes, html4Attributes, rdfaAttributes)
5757import Text.Pandoc.Error
5858import Text.Pandoc.Logging
5959import Text.Pandoc.Options (
@@ -64,7 +64,7 @@ import Text.Pandoc.Options (
6464import Text.Pandoc.Parsing hiding ((<|>))
6565import Text.Pandoc.Shared (addMetaField, blocksToInlines', crFilter, escapeURI,
6666 extractSpaces, htmlSpanLikeElements, elemText, splitTextBy,
67− onlySimpleTableCells, safeRead, tshow)
67+ safeRead, tshow)
6868import Text.Pandoc.Walk
6969import Text.Parsec.Error
7070import Text.TeXMath (readMathML, writeTeX)
@@ -474,79 +474,7 @@ pHrule = do
474474 return B.horizontalRule
475475
476476pTable :: PandocMonad m => TagParser m Blocks
477−pTable = try $ do
478− TagOpen _ attribs' <- pSatisfy (matchTagOpen "table" [])
479− let attribs = toAttr attribs'
480− skipMany pBlank
481− caption <- option mempty $ pInTags "caption" inline <* skipMany pBlank
482− widths' <- (mconcat <$> many1 pColgroup) <|> many pCol
483− let pTh = option [] $ pInTags "tr" (pCell "th")
484− pTr = try $ skipMany pBlank >>
485− pInTags "tr" (pCell "td" <|> pCell "th")
486− pTBody = pInTag True "tbody" $ many1 pTr
487− head'' <- pInTag False "thead" (option [] pTr) <|> pInTag True "thead" pTh
488− head' <- map snd <$>
489− pInTag True "tbody"
490− (if null head'' then pTh else return head'')
491− topfoot <- option [] $ pInTag False "tfoot" $ many pTr
492− rowsLs <- many pTBody
493− bottomfoot <- option [] $ pInTag False "tfoot" $ many pTr
494− TagClose _ <- pSatisfy (matchTagClose "table")
495− let rows'' = concat rowsLs <> topfoot <> bottomfoot
496− let rows''' = map (map snd) rows''
497− -- fail on empty table
498− guard $ not $ null head' && null rows'''
499− let isSimple = onlySimpleTableCells $ fmap B.toList <$> head':rows'''
500− let cols = if null head'
501− then maximum (map length rows''')
502− else length head'
503− -- add empty cells to short rows
504− let addEmpties r = case cols - length r of
505− n | n > 0 -> r <> replicate n mempty
506− | otherwise -> r
507− let rows = map addEmpties rows'''
508− let aligns = case rows'' of
509− (cs:_) -> take cols $ map fst cs ++ repeat AlignDefault
510− _ -> replicate cols AlignDefault
511− let widths = if null widths'
512− then if isSimple
513− then replicate cols ColWidthDefault
514− else replicate cols (ColWidth (1.0 / fromIntegral cols))
515− else widths'
516− let toRow = Row nullAttr . map B.simpleCell
517− toHeaderRow l = [toRow l | not (null l)]
518− return $ B.tableWith attribs
519− (B.simpleCaption $ B.plain caption)
520− (zip aligns widths)
521− (TableHead nullAttr $ toHeaderRow head')
522− [TableBody nullAttr 0 [] $ map toRow rows]
523− (TableFoot nullAttr [])
524−
525−pCol :: PandocMonad m => TagParser m ColWidth
526−pCol = try $ do
527− TagOpen _ attribs' <- pSatisfy (matchTagOpen "col" [])
528− let attribs = toStringAttr attribs'
529− skipMany pBlank
530− optional $ pSatisfy (matchTagClose "col")
531− skipMany pBlank
532− let width = case lookup "width" attribs of
533− Nothing -> case lookup "style" attribs of
534− Just (T.stripPrefix "width:" -> Just xs) | T.any (== '%') xs ->
535− fromMaybe 0.0 $ safeRead (T.filter
536− (`notElem` (" \t\r\n%'\";" :: [Char])) xs)
537− _ -> 0.0
538− Just (T.unsnoc -> Just (xs, '%')) ->
539− fromMaybe 0.0 $ safeRead xs
540− _ -> 0.0
541− if width > 0.0
542− then return $ ColWidth $ width / 100.0
543− else return ColWidthDefault
544−
545−pColgroup :: PandocMonad m => TagParser m [ColWidth]
546−pColgroup = try $ do
547− pSatisfy (matchTagOpen "colgroup" [])
548− skipMany pBlank
549− manyTill pCol (pCloses "colgroup" <|> eof) <* skipMany pBlank
477+pTable = pTable' inline pCell
550478
551479noColOrRowSpans :: Tag Text -> Bool
552480noColOrRowSpans t = isNullOrOne "colspan" && isNullOrOne "rowspan"
@@ -847,16 +775,6 @@ pRawHtmlInline = do
847775mathMLToTeXMath :: Text -> Either Text Text
848776mathMLToTeXMath s = writeTeX <$> readMathML s
849777
850−toStringAttr :: [(Text, Text)] -> [(Text, Text)]
851−toStringAttr = map go
852− where
853− go (x,y) =
854− case T.stripPrefix "data-" x of
855− Just x' | x' `Set.notMember` (html5Attributes <>
856− html4Attributes <> rdfaAttributes)
857− -> (x',y)
858− _ -> (x,y)
859−
860778pScriptMath :: PandocMonad m => TagParser m Inlines
861779pScriptMath = try $ do
862780 TagOpen _ attr' <- pSatisfy $ tagOpen (=="script") (const True)
@@ -1151,16 +1069,6 @@ htmlTag f = try $ do
11511069 handleTag tagname
11521070 _ -> mzero
11531071
1154−mkAttr :: [(Text, Text)] -> Attr
1155−mkAttr attr = (attribsId, attribsClasses, attribsKV)
1156− where attribsId = fromMaybe "" $ lookup "id" attr
1157− attribsClasses = T.words (fromMaybe "" $ lookup "class" attr) <> epubTypes
1158− attribsKV = filter (\(k,_) -> k /= "class" && k /= "id") attr
1159− epubTypes = T.words $ fromMaybe "" $ lookup "epub:type" attr
1160−
1161−toAttr :: [(Text, Text)] -> Attr
1162−toAttr = mkAttr . toStringAttr
1163−
11641072-- Strip namespace prefixes
11651073stripPrefixes :: [Tag Text] -> [Tag Text]
11661074stripPrefixes = map stripPrefix