-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.hs
66 lines (57 loc) · 1.64 KB
/
main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
{-# LANGUAGE OverloadedStrings #-}
import Data.List hiding (take)
import Prelude hiding (take)
import Control.Monad
import Control.Applicative hiding (many)
import Data.Monoid
import Data.Attoparsec.Char8
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as S
import qualified Data.Enumerator as E
import qualified Data.Enumerator.List as EL
-- takeTill' :: (Char -> Bool) -> Parser ByteString
-- takeTill' p = do
-- (h, t) <- span p <$> get
-- put t
-- return h
data Token' s = TagOpen s
| TagClose s
| PartialToken s
| Text s
deriving (Show)
type Token = Token' ByteString
tag :: Parser Token
tag = do
string "<"
s <- takeTill (=='>')
end <- atEnd
if end
then return $ PartialToken $ "<" `mappend` s
else do
take 1
return $ case S.uncons s of
Just ('/', s') -> TagClose s'
Nothing -> TagOpen S.empty
_ -> TagOpen s
text :: Parser Token
text = do
s <- takeTill (=='<')
if S.null s
then empty
else return $ Text s
token = tag <|> text
html :: Parser [Token]
html = many token
accum :: ByteString -> ByteString -> Either String (ByteString, [Token])
accum acc input = handlePartial <$> (parseOnly html $ acc `mappend` input)
where
handlePartial tokens =
if (null tokens)
then (S.empty, [])
else case last tokens of
PartialToken s
-> (s, init tokens)
_ -> (S.empty, tokens)
main = do
let enum = E.enumList 1 [" <a href=", "\"x", "x\">x", "x</a>"] E.$= EL.concatMapAccumM accum S.empty
print $ (E.run_ $ enum E.$$ EL.consume)