-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSS.hs
167 lines (139 loc) · 4.03 KB
/
CSS.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
--
-- This file is part of FactorCSS
-- Licensed under the MIT License,
-- http://www.opensource.org/licenses/mit-license
-- Copyright 2005 James Bursa <[email protected]>
--
-- Datatypes for CSS
module CSS where
import Data.List
-- import Numeric
data Stylesheet
= Stylesheet
(Maybe String) -- charset
[(String, [String])] -- imports (url, media)
[Statement]
deriving Show
data Statement
= Ruleset [Selector] [Declaration]
| Media [String] [Statement]
| Page (Maybe String) [Declaration]
deriving Show
type Selector
= [(SimpleSelector, Combinator)]
type SimpleSelector
= (Maybe String, [Detail])
data Detail
= Id String
| Class String
| Attrib String
| AttribEq String String
| AttribInc String String
| AttribDM String String
| Pseudo String
| PseudoFunc String String
deriving Show
data Combinator
= Ancestor
| Preceded
| Parent
| NoMore
deriving Show
type Declaration
= (String, [Value], Bool)
data Value
= Number Float
| Percentage Float
| Length String
| Ems Float
| Exs Float
| Angle String
| Time String
| Freq String
| StringV String
| Ident String
| Uri String
| HexColour String
| Slash
| Comma
| Function String [Value]
deriving Show
is_ruleset :: Statement -> Bool
is_ruleset (Ruleset sels decls) = True
is_ruleset _ = False
is_media :: Statement -> Bool
is_media (Media media stmts) = True
is_media _ = False
is_page :: Statement -> Bool
is_page (Page page decls) = True
is_page _ = False
show_stylesheet :: Stylesheet -> [String]
show_stylesheet (Stylesheet c is ss) = show_charset c ++
map show_import (reverse is) ++
map show_statement ss
show_charset :: Maybe String -> [String]
show_charset Nothing = []
show_charset (Just c) = ["@charset " ++ c ++ ";"]
show_import :: (String, [String]) -> String
show_import (url, media) = "@import " ++ url ++ " " ++
(concat . intersperse ", ") media ++ ";"
show_statement :: Statement -> String
show_statement (Ruleset ss ds) =
show_selectors ss ++
" { " ++
(concat . intersperse "; " . map show_declaration) ds ++
" }"
show_statement (Media media stmts) =
"@media " ++ (concat . intersperse ", ") media ++
" {\n" ++
(unlines . map ('\t':) . map show_statement) stmts ++
"}"
show_statement z = "(statement)"
show_selectors :: [Selector] -> String
show_selectors = concat . intersperse ", " . map show_selector
show_selector :: Selector -> String
show_selector [] = ""
show_selector ((ss, c):zs) = show_simple_selector ss ++
show_combinator c ++
show_selector zs
show_simple_selector :: SimpleSelector -> String
show_simple_selector (Just s, ds) = s ++ (concat . map show_detail) ds
show_simple_selector (Nothing, []) = "*"
show_simple_selector (Nothing, ds) = (concat . map show_detail) ds
show_detail :: Detail -> String
show_detail (Id i) = i
show_detail (Class c) = "." ++ c
show_detail (Attrib a) = "[" ++ a ++ "]"
show_detail (AttribEq a v) = "[" ++ a ++ "=" ++ v ++ "]"
show_detail (AttribInc a v) = "[" ++ a ++ "~=" ++ v ++ "]"
show_detail (AttribDM a v) = "[" ++ a ++ "|=" ++ v ++ "]"
show_detail (Pseudo p) = ":" ++ p
show_detail (PseudoFunc f p) = ":" ++ f ++ p ++ ")"
show_combinator :: Combinator -> String
show_combinator Ancestor = " "
show_combinator Preceded = " + "
show_combinator Parent = " > "
show_combinator NoMore = ""
show_declaration :: Declaration -> String
show_declaration (p, vs, i) = p ++ ": " ++ show_values vs ++
if i then " !important" else ""
show_values :: [Value] -> String
show_values = concat . intersperse " " . map show_value
showFFloat :: (Num n, Show n) => n -> String -> String
showFFloat n s = show n ++ s
show_value :: Value -> String
show_value (Number n) = showFFloat n ""
show_value (Percentage p) = showFFloat p "%"
show_value (Length l) = l
show_value (Ems e) = showFFloat e "em"
show_value (Exs x) = showFFloat x "ex"
show_value (Angle a) = a
show_value (Time t) = t
show_value (Freq f) = f
show_value (StringV s) = s
show_value (Ident i) = i
show_value (Uri u) = u
show_value (HexColour c) = c
show_value Slash = "/"
show_value Comma = ","
show_value (Function f vs) = f ++ show_values vs ++ ")"