git.iko.soy/typeable/pandoc
on master help sign in
HTML reader: parse attributes into table attributes.
commit37cadd5a025503b280b6328d523a57efea8c6256 authorJohn MacFarlane <jgm@berkeley.edu> dateSat 18 Apr 2020, 10:52 (7 years ago) parent9a809d4
1 file changed+18−14
src/Text/Pandoc/Readers/HTML.hs+18−14
src/Text/Pandoc/Readers/HTML.hs+18−14
@@ -389,9 +389,8 @@ pDiv = try $ do
389389 isDivLike "main" = True
390390 isDivLike _ = False
391391 TagOpen tag attr' <- lookAhead $ pSatisfy $ tagOpen isDivLike (const True)
392− let attr = toStringAttr attr'
392+ let (ident, classes, kvs) = toAttr attr'
393393 contents <- pInTags tag block
394− let (ident, classes, kvs) = mkAttr attr
395394 let classes' = if tag == "section"
396395 then "section":classes
397396 else classes
@@ -479,7 +478,8 @@ pHrule = do
479478
480479pTable :: PandocMonad m => TagParser m Blocks
481480pTable = try $ do
482− TagOpen _ _ <- pSatisfy (matchTagOpen "table" [])
481+ TagOpen _ attribs' <- pSatisfy (matchTagOpen "table" [])
482+ let attribs = toAttr attribs'
483483 skipMany pBlank
484484 caption <- option mempty $ pInTags "caption" inline <* skipMany pBlank
485485 widths' <- (mconcat <$> many1 pColgroup) <|> many pCol
@@ -518,7 +518,8 @@ pTable = try $ do
518518 else widths'
519519 let toRow = Row nullAttr . map B.simpleCell
520520 toHeaderRow l = if null l then [] else [toRow l]
521− return $ B.table (B.simpleCaption $ B.plain caption)
521+ return $ B.tableWith attribs
522+ (B.simpleCaption $ B.plain caption)
522523 (zip aligns widths)
523524 (TableHead nullAttr $ toHeaderRow head')
524525 [TableBody nullAttr 0 [] $ map toRow rows]
@@ -619,7 +620,7 @@ pFigure = try $ do
619620pCodeBlock :: PandocMonad m => TagParser m Blocks
620621pCodeBlock = try $ do
621622 TagOpen _ attr' <- pSatisfy (matchTagOpen "pre" [])
622− let attr = toStringAttr attr'
623+ let attr = toAttr attr'
623624 contents <- manyTill pAny (pCloses "pre" <|> eof)
624625 let rawText = T.concat $ map tagToText contents
625626 -- drop leading newline if any
@@ -630,7 +631,7 @@ pCodeBlock = try $ do
630631 let result = case T.unsnoc result' of
631632 Just (result'', '\n') -> result''
632633 _ -> result'
633− return $ B.codeBlockWith (mkAttr attr) result
634+ return $ B.codeBlockWith attr result
634635
635636tagToText :: Tag Text -> Text
636637tagToText (TagText s) = s
@@ -731,7 +732,7 @@ pSpanLike =
731732 where
732733 parseTag tagName = do
733734 TagOpen _ attrs <- pSatisfy $ tagOpenLit tagName (const True)
734− let (ids, cs, kvs) = mkAttr . toStringAttr $ attrs
735+ let (ids, cs, kvs) = toAttr attrs
735736 content <- mconcat <$> manyTill inline (pCloses tagName <|> eof)
736737 return $ B.spanWith (ids, tagName : cs, kvs) content
737738
@@ -797,7 +798,7 @@ pCodeWithClass elemToClass = try $ do
797798 let tagTest = flip elem . fmap fst $ elemToClass
798799 TagOpen open attr' <- pSatisfy $ tagOpen tagTest (const True)
799800 result <- manyTill pAny (pCloses open)
800− let (ids,cs,kvs) = mkAttr . toStringAttr $ attr'
801+ let (ids,cs,kvs) = toAttr attr'
801802 cs' = maybe cs (:cs) . lookup open $ elemToClass
802803 return . B.codeWith (ids,cs',kvs) .
803804 T.unwords . T.lines . innerText $ result
@@ -805,9 +806,9 @@ pCodeWithClass elemToClass = try $ do
805806pCode :: PandocMonad m => TagParser m Inlines
806807pCode = try $ do
807808 (TagOpen open attr') <- pSatisfy $ tagOpen (`elem` ["code","tt"]) (const True)
808− let attr = toStringAttr attr'
809+ let attr = toAttr attr'
809810 result <- manyTill pAny (pCloses open)
810− return $ B.codeWith (mkAttr attr) $ T.unwords $ T.lines $ innerText result
811+ return $ B.codeWith attr $ T.unwords $ T.lines $ innerText result
811812
812813-- https://developer.mozilla.org/en-US/docs/Web/HTML/Element/bdo
813814-- Bidirectional Text Override
@@ -825,14 +826,14 @@ pSpan :: PandocMonad m => TagParser m Inlines
825826pSpan = try $ do
826827 guardEnabled Ext_native_spans
827828 TagOpen _ attr' <- lookAhead $ pSatisfy $ tagOpen (=="span") (const True)
828− let attr = toStringAttr attr'
829+ let attr = toAttr attr'
829830 contents <- pInTags "span" inline
830831 let isSmallCaps = fontVariant == "small-caps" || "smallcaps" `elem` classes
831− where styleAttr = fromMaybe "" $ lookup "style" attr
832+ where styleAttr = fromMaybe "" $ lookup "style" attr'
832833 fontVariant = fromMaybe "" $ pickStyleAttrProps ["font-variant"] styleAttr
833834 classes = maybe []
834− T.words $ lookup "class" attr
835− let tag = if isSmallCaps then B.smallcaps else B.spanWith (mkAttr attr)
835+ T.words $ lookup "class" attr'
836+ let tag = if isSmallCaps then B.smallcaps else B.spanWith attr
836837 return $ tag contents
837838
838839pRawHtmlInline :: PandocMonad m => TagParser m Inlines
@@ -1298,6 +1299,9 @@ mkAttr attr = (attribsId, attribsClasses, attribsKV)
12981299 attribsKV = filter (\(k,_) -> k /= "class" && k /= "id") attr
12991300 epubTypes = T.words $ fromMaybe "" $ lookup "epub:type" attr
13001301
1302+toAttr :: [(Text, Text)] -> Attr
1303+toAttr = mkAttr . toStringAttr
1304+
13011305-- Strip namespace prefixes
13021306stripPrefixes :: [Tag Text] -> [Tag Text]
13031307stripPrefixes = map stripPrefix