-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.hs
273 lines (249 loc) · 5.8 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Main where
import Control.Applicative
import Data.Attoparsec.Text
import Data.Char
import Data.List hiding (takeWhile)
import Data.Map (Map)
import qualified Data.Map as M
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Prelude hiding (takeWhile)
import System.Exit
import Text.Pretty.Simple
data HTML
= Branch TagName [ Attr ] [ HTML ]
| Leaf Text
deriving (Eq)
newtype CSS = CSS (Map Text Text)
deriving (Eq)
deriving newtype (Monoid, Semigroup)
instance Show CSS where
show (CSS hmap) =
mconcat
[ "M.fromList [ "
, intercalate "," (go <$> M.assocs hmap)
, " ]"
]
where
go (k,v) = "(" <> "\"" <>
T.unpack k <> "\" ," <> "\"" <>
T.unpack v <> "\" )"
data Attr = Attr Text (Maybe Text)
deriving (Eq)
instance Show HTML where
show (Leaf x) = "\"" <> T.unpack x <> "\""
show (Branch t as cs) =
mconcat $
[ T.unpack t
, "_ "
, show as
] ++ [ show cs | not (isEmpty t) ]
instance Show Attr where
show (Attr "style" (Just v)) =
mconcat
[ "style_ $ "
, T.unpack v
]
show (Attr k (Just v))
| T.any (=='-') k =
mconcat
[ "textProp \""
, T.unpack k
, "\""
, " \""
, T.unpack v
, "\""
]
| otherwise =
mconcat
[ T.unpack k
, "_ "
, "\""
, T.unpack v
, "\""
]
show (Attr "checked" Nothing) =
"checked_ True"
show (Attr k Nothing) =
mconcat
[ "textProp \""
, T.unpack k
, "\" \"\""
]
type TagName = Text
tag :: Parser (TagName, [Attr])
tag = do
_ <- char '<'
t <- takeWhile1 isAlphaNum
_ <- char '>'
pure (t, [])
tagWithAttrs :: Parser (TagName, [Attr])
tagWithAttrs = do
_ <- char '<'
t <- takeWhile1 (/=' ')
_ <- char ' '
as <- attrs `sepBy` char ' '
skipSpace
_ <- char '/' <|> char '>'
pure (t, as)
attrs :: Parser Attr
attrs = kvAttr <|> attr
where
predicate x = isAlpha x || x == '-'
kvAttr = Attr <$> key <*> do Just <$> value
attr = flip Attr Nothing <$> justKey
justKey = takeWhile1 predicate
key = do
k <- takeWhile1 predicate
_ <- char '='
pure k
value = do
_ <- char '"'
v <- takeWhile (/= '"')
_ <- char '"'
pure v
children :: Parser [HTML]
children = many htmlOrLeaf
htmlOrLeaf :: Parser HTML
htmlOrLeaf = html <|> leaf
leaf :: Parser HTML
leaf = Leaf <$> do
strip . T.filter (/='\n') <$>
takeWhile1 (/='<')
where
strip = T.reverse
. T.dropWhile (==' ')
. T.reverse
. T.dropWhile (==' ')
dropFluff :: Parser ()
dropFluff = do
_ <- takeWhile (`elem` ("\n " :: String))
pure ()
html :: Parser HTML
html = do
(openTag, as) <-
tag <|> tagWithAttrs
dropFluff
cs <-
if isEmpty openTag
then pure []
else do
cs <- children
closeTag
pure cs
dropFluff
let hasStyle (Attr k _) = k == "style"
pure $ case find hasStyle as of
Just (Attr key (Just cssText)) -> do
let parsedCss = T.pack $ show (parseCss cssText)
newAttr = Attr key (Just parsedCss)
oldAttrs = filter (not . hasStyle) as
Branch openTag (newAttr : oldAttrs) cs
_ ->
Branch openTag as cs
parseCss :: Text -> CSS
parseCss cssText = CSS cssMap
where
cssMap
= M.fromList
[ (k,v)
| [k,v] <- T.splitOn ":" <$> T.splitOn ";" cssText
]
isEmpty :: Text -> Bool
isEmpty =
flip elem
[ "area"
, "base"
, "br"
, "col"
, "embed"
, "hr"
, "img"
, "input"
, "link"
, "meta"
, "param"
, "source"
, "track"
, "wbr"
]
closeTag :: Parser ()
closeTag = do
_ <- string "</"
_ <- takeWhile1 isAlphaNum
_ <- char '>'
pure ()
main :: IO ()
main = do
file <- stripDoctype . removeComments <$> T.getContents
case parseOnly html file of
Right r ->
pPrint r
Left e -> do
print e
exitFailure
-- | Layered lexer
data Mode
= InComment
| Normal
deriving (Show, Eq)
stripDoctype :: Text -> Text
stripDoctype t = do
let firstLine = T.takeWhile (/='\n') t
if "<!doctype html>" `T.isPrefixOf` (T.toLower firstLine)
then T.drop 1 (T.dropWhile (/='\n') t)
else t
-- | Remove HTML comments using a layered lexer
--
-- @
-- > removeComments "<a><!-- hey --></a>"
-- > <a></a>
-- @
--
removeComments :: Text -> Text
removeComments = go Normal Nothing
where
go Normal Nothing txt =
case T.uncons txt of
Nothing ->
mempty
Just (c, next) ->
T.singleton c <>
go Normal (Just c) next
go Normal (Just _) txt =
case T.uncons txt of
Nothing ->
mempty
Just (c,next) ->
case T.uncons next of
Just (c',next') ->
if [c,c'] == "<!"
then
go InComment (Just c') next'
else
T.singleton c <>
go Normal (Just c) next
Nothing ->
T.singleton c <>
go Normal (Just c) next
go InComment Nothing txt =
case T.uncons txt of
Nothing ->
error "Comment not terminated"
Just (c,next) ->
go InComment (Just c) next
go InComment (Just prev) txt =
case T.uncons txt of
Nothing ->
error "Comment not terminated"
Just (c,next) ->
if [prev,c] == "->"
then go Normal (Just c) next
else go InComment (Just c) next