@@ -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