Skip to content

Commit

Permalink
Consider comments as whitespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
romildo committed Apr 14, 2017
1 parent a189d65 commit 53269ec
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/Text/Megaparsec/TagSoup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import Data.Semigroup ((<>))
import qualified Data.Set as Set
import Text.HTML.TagSoup
import Text.StringLike
import Text.Megaparsec ((<|>))
import Text.Megaparsec.Combinator
import Text.Megaparsec.Error
import Text.Megaparsec.Pos
Expand Down Expand Up @@ -71,9 +72,16 @@ space = satisfy (\tag -> case tag of
TagText x | all isSpace (toString x) -> True
_ -> False)

-- | Parses any whitespace. Whitespace consists of zero or more ocurrences of 'space'.
-- | Parses a comment.
comment :: (StringLike str, MonadParsec e s m, Token s ~ Tag str) => m (Tag str)
comment = satisfy (\tag -> case tag of
TagComment _ -> True
_ -> False)

-- | Parses any whitespace. Whitespace consists of zero or more
-- ocurrences of 'space' or 'comment'.
whitespace :: (StringLike str, MonadParsec e s m, Token s ~ Tag str) => m ()
whitespace = skipMany space
whitespace = skipMany (space <|> comment)

-- | @lexeme p@ first applies parser @p@ and then 'whitespace', returning the value of @p@.
--
Expand Down
5 changes: 3 additions & 2 deletions test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ type StringTagParser = TagParser String

testDoc = [r|
<ul>
<li>Item 1</li>
<li>Item 2</li>
<!-- comments are ignored -->
<li>Item 1</li>
<li>Item 2</li>
</ul>
|]

Expand Down

0 comments on commit 53269ec

Please sign in to comment.