Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider comments as whitespaces #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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