@@ -11,7 +11,8 @@
11Parsing functions and utilities.
11Parsing functions and utilities.
13module Text.Pandoc.Readers.HTML.Parsing
13module Text.Pandoc.Readers.HTML.Parsing
14− ( pInTags
15+ , pInTags
15 , pInTags'
16 , pInTags'
@@ -43,6 +44,13 @@ import Text.Pandoc.XML (html5Attributes, html4Attributes, rdfaAttributes)
43import qualified Data.Set as Set
44import qualified Data.Set as Set
44import qualified Data.Text as T
45import qualified Data.Text as T
47+-- | Whether no tag, the closing tag, or both tags can be omitted.
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.
46pInTags :: (PandocMonad m, Monoid a) => Text -> TagParser m a -> TagParser m a
54pInTags :: (PandocMonad m, Monoid a) => Text -> TagParser m a -> TagParser m a
47pInTags tagtype parser = pInTags' tagtype (const True) parser
55pInTags tagtype parser = pInTags' tagtype (const True) parser
@@ -57,14 +65,18 @@ pInTags' tagtype tagtest parser = try $ do
58-- parses p, preceded by an opening tag (optional if tagsOptional)
66-- parses p, preceded by an opening tag (optional if tagsOptional)
59-- and followed by a closing tag (optional if tagsOptional)
67-- and followed by a closing tag (optional if tagsOptional)
60−pInTag :: PandocMonad m => Bool -> Text -> TagParser m a -> TagParser m a
68+pInTag :: PandocMonad m => TagOmission -> Text -> TagParser m a -> TagParser m a
61−pInTag tagsOptional tagtype p = try $ do
69+pInTag tagOmission tagtype p = try $ do
62 skipMany pBlank
70 skipMany pBlank
71+ let openingOptional = tagOmission == TagsOmittable
72+ let closingOptional = tagOmission /= TagsRequired
73+ (if openingOptional then optional else void) $
63− (if tagsOptional then optional else void) $ pSatisfy (matchTagOpen tagtype [])
74+ pSatisfy (matchTagOpen tagtype [])
64 skipMany pBlank
75 skipMany pBlank
66 skipMany pBlank
77 skipMany pBlank
78+ (if closingOptional then optional else void) $
67− (if tagsOptional then optional else void) $ pSatisfy (matchTagClose tagtype)
79+ pSatisfy (matchTagClose tagtype)
68 skipMany pBlank
80 skipMany pBlank