git.iko.soy/typeable/pandoc
on master help sign in
HTML reader: allow finer grained options for tag omission
commit3e01ae405f9bf5f40e1b8e519029825aa4880602 authorAlbert Krewinkel <albert@zeitkraut.de> dateThu 26 Nov 2020, 07:22 (6 years ago) parentce606cb
3 files changed+26−13
src/Text/Pandoc/Readers/HTML.hs+1−1 src/Text/Pandoc/Readers/HTML/Parsing.hs+17−5 src/Text/Pandoc/Readers/HTML/Table.hs+8−7
src/Text/Pandoc/Readers/HTML.hs+1−1
@@ -514,7 +514,7 @@ pFigure = try $ do
514514 TagOpen _ _ <- pSatisfy (matchTagOpen "figure" [])
515515 skipMany pBlank
516516 let pImg = (\x -> (Just x, Nothing)) <$>
517− (pInTag True "p" pImage <* skipMany pBlank)
517+ (pInTag TagsOmittable "p" pImage <* skipMany pBlank)
518518 pCapt = (\x -> (Nothing, Just x)) <$> do
519519 bs <- pInTags "figcaption" block
520520 return $ blocksToInlines' $ B.toList bs
src/Text/Pandoc/Readers/HTML/Parsing.hs+17−5
@@ -11,7 +11,8 @@
1111Parsing functions and utilities.
1212-}
1313module Text.Pandoc.Readers.HTML.Parsing
14− ( pInTags
14+ ( TagOmission (..)
15+ , pInTags
1516 , pInTags'
1617 , pInTag
1718 , pAny
@@ -43,6 +44,13 @@ import Text.Pandoc.XML (html5Attributes, html4Attributes, rdfaAttributes)
4344import qualified Data.Set as Set
4445import qualified Data.Text as T
4546
47+-- | Whether no tag, the closing tag, or both tags can be omitted.
48+data TagOmission
49+ = TagsRequired -- ^ Opening and closing tags are both required
50+ | ClosingTagOptional -- ^ The closing tag can be omitted
51+ | TagsOmittable -- ^ Both tags, opening and closing, can be omitted.
52+ deriving (Eq)
53+
4654pInTags :: (PandocMonad m, Monoid a) => Text -> TagParser m a -> TagParser m a
4755pInTags tagtype parser = pInTags' tagtype (const True) parser
4856
@@ -57,14 +65,18 @@ pInTags' tagtype tagtest parser = try $ do
5765
5866-- parses p, preceded by an opening tag (optional if tagsOptional)
5967-- and followed by a closing tag (optional if tagsOptional)
60−pInTag :: PandocMonad m => Bool -> Text -> TagParser m a -> TagParser m a
61−pInTag tagsOptional tagtype p = try $ do
68+pInTag :: PandocMonad m => TagOmission -> Text -> TagParser m a -> TagParser m a
69+pInTag tagOmission tagtype p = try $ do
6270 skipMany pBlank
63− (if tagsOptional then optional else void) $ pSatisfy (matchTagOpen tagtype [])
71+ let openingOptional = tagOmission == TagsOmittable
72+ let closingOptional = tagOmission /= TagsRequired
73+ (if openingOptional then optional else void) $
74+ pSatisfy (matchTagOpen tagtype [])
6475 skipMany pBlank
6576 x <- p
6677 skipMany pBlank
67− (if tagsOptional then optional else void) $ pSatisfy (matchTagClose tagtype)
78+ (if closingOptional then optional else void) $
79+ pSatisfy (matchTagClose tagtype)
6880 skipMany pBlank
6981 return x
7082
src/Text/Pandoc/Readers/HTML/Table.hs+8−7
@@ -69,15 +69,16 @@ pTable' block pCell = try $ do
6969 caption <- option mempty $ pInTags "caption" block <* skipMany pBlank
7070 widths' <- (mconcat <$> many1 pColgroup) <|> many pCol
7171 let pTh = option [] $ pInTags "tr" (pCell "th")
72− pTr = try $ skipMany pBlank >>
73− pInTags "tr" (pCell "td" <|> pCell "th")
74− pTBody = pInTag True "tbody" $ many1 pTr
75− head'' <- pInTag False "thead" (option [] pTr) <|> pInTag True "thead" pTh
76− head' <- pInTag True "tbody"
72+ pTr = try $ skipMany pBlank
73+ *> pInTags "tr" (pCell "td" <|> pCell "th")
74+ pTBody = pInTag TagsOmittable "tbody" $ many1 pTr
75+ head'' <- pInTag ClosingTagOptional "thead" (option [] pTr)
76+ <|> pInTag TagsOmittable "thead" pTh
77+ head' <- pInTag TagsOmittable "tbody"
7778 (if null head'' then pTh else return head'')
78− topfoot <- option [] $ pInTag False "tfoot" $ many pTr
79+ topfoot <- option [] $ pInTag TagsRequired "tfoot" $ many pTr
7980 rowsLs <- many pTBody
80− bottomfoot <- option [] $ pInTag False "tfoot" $ many pTr
81+ bottomfoot <- option [] $ pInTag ClosingTagOptional "tfoot" $ many pTr
8182 TagClose _ <- pSatisfy (matchTagClose "table")
8283 let rows = concat rowsLs <> topfoot <> bottomfoot
8384 rows''' = map (map cellContents) rows