/[james]/factorcss/FactorCSS.hs
ViewVC logotype

Contents of /factorcss/FactorCSS.hs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 67 - (show annotations) (download) (as text)
Thu Dec 30 23:22:02 2004 UTC (19 years, 10 months ago) by james
File MIME type: text/x-haskell
File size: 6908 byte(s)
Add identity, tree, and statistics modes.

1 --
2 -- This file is part of FactorCSS
3 -- Licensed under the MIT License,
4 -- http://www.opensource.org/licenses/mit-license
5 -- Copyright 2004 James Bursa <james@semichrome.net>
6 --
7
8 -- Factor a CSS stylesheet.
9 --
10 -- This tool takes a CSS stylesheet on input and produces an almost equivalent
11 -- stylesheet on output, but with rulesets split, combined, and reordered to
12 -- "factor out" common declarations. This helps reveal shared components. The
13 -- resulting stylesheet may also be smaller.
14 --
15 -- The only known case where the output is not equivalent to the input is when
16 -- the stylesheet depends on the order of rules (see CSS 2.1 6.4.1).
17
18 import List
19 import System
20 import Tokeniser
21 import Parser
22 import CSS
23
24 -- Program entry function.
25 main :: IO ()
26 main = do
27 args <- getArgs
28 interact (run args)
29
30 -- Produce output from arguments and input.
31 run :: [String] -> String -> String
32 run args input
33 | argument "help" args = usage
34 | null args = go factor
35 | argument "factor" args = go factor
36 | argument "explode" args = go (concatMap explode)
37 | argument "identity" args = go id
38 | argument "tree" args = show stylesheet ++ "\n"
39 | argument "statistics" args = stats stylesheet ++ "\n"
40 | otherwise = usage
41 where go f = (unlines . show_stylesheet . process f) stylesheet
42 stylesheet = (parser . alexScanTokens) input
43
44 -- Check an argument list for an argument in short or long form.
45 argument :: String -> [String] -> Bool
46 argument long args = elem short shorts || elem ("--" ++ long) args
47 where short = head long
48 shorts = (concat . filter is_short) args
49 is_short ('-':'-':_) = False
50 is_short ('-':_) = True
51 is_short _ = False
52
53 -- Usage help string.
54 usage :: String
55 usage = unlines ["Usage: factorcss [OPTION]... <FILE",
56 "\"Factor out\" common declarations in a CSS stylesheet by splitting, reordering,",
57 "and combining rulesets.",
58 "The stylesheet is read from standard input, and the result is produced on",
59 "standard output.",
60 "",
61 "Output mode:",
62 " -f, --factor factor out common declarations (default)",
63 " -e, --explode produce rulesets with one selector and declaration each",
64 " -i, --identity just parse and output unmodified",
65 " -t, --tree parse and display parse tree",
66 " -s, --statistics count rulesets, selectors, and declarations",
67 "",
68 " -h, --help display this help and exit"]
69
70 -- Process a Stylesheet.
71 process :: ([Statement] -> [Statement]) -> Stylesheet -> Stylesheet
72 process f (Stylesheet charset imports stmts) =
73 Stylesheet charset imports (process_stmts f stmts)
74
75 -- Process a list of Statements.
76 process_stmts :: ([Statement] -> [Statement]) -> [Statement] -> [Statement]
77 process_stmts f stmts = (f . filter is_ruleset) stmts ++
78 (map (process_media f) . filter is_media) stmts ++
79 filter is_page stmts
80
81 -- Process a Media Statement.
82 process_media :: ([Statement] -> [Statement]) -> Statement -> Statement
83 process_media f (Media media stmts) = Media media (f stmts)
84
85 -- Factor a list of Ruleset Statements.
86 factor :: [Statement] -> [Statement]
87 factor = map implode_sel . groupBy eq_sel .
88 sortBy cmp_sel .
89 map implode_decl . groupBy eq_decl .
90 sortBy cmp_decl . sortBy cmp_sel .
91 concat . map explode
92
93 -- Explode a Ruleset Statement into an equivalent list of Ruleset Statements
94 -- with one Selector and one Declaration per Ruleset.
95 --
96 -- For example (in CSS syntax),
97 -- explode "h1, em { color: red; background-color: blue }" =
98 -- "h1 { color: red }
99 -- h1 { background-color: blue }
100 -- em { color: red }
101 -- em { background-color: blue }"
102 --
103 -- [length (explode (Ruleset sels decls)) == length sels * length decls]
104 explode :: Statement -> [Statement]
105 explode (Ruleset sels decls) =
106 concat (map (\s -> map (\d -> Ruleset [s] [d]) decls) sels)
107
108 -- Compare two Ruleset Statements by the Selectors in each.
109 cmp_sel :: Statement -> Statement -> Ordering
110 cmp_sel (Ruleset sels0 decls0) (Ruleset sels1 decls1)
111 | a < b = LT
112 | a == b = EQ
113 | a > b = GT
114 where a = show_selectors sels0
115 b = show_selectors sels1
116
117 -- Compare two Ruleset Statements by the first Declaration in each.
118 cmp_decl :: Statement -> Statement -> Ordering
119 cmp_decl (Ruleset sels0 (decl0:decls0)) (Ruleset sels1 (decl1:decls1))
120 | a < b = LT
121 | a == b = EQ
122 | a > b = GT
123 where a = show_declaration decl0
124 b = show_declaration decl1
125
126 -- Compare two Ruleset Statements for equality by the Selectors in each.
127 eq_sel :: Statement -> Statement -> Bool
128 eq_sel z = (== EQ) . cmp_sel z
129
130 -- Compare two Ruleset Statements for equality by the first declaration in
131 -- each.
132 eq_decl :: Statement -> Statement -> Bool
133 eq_decl z = (== EQ) . cmp_decl z
134
135 -- Implode a list of Ruleset Statements with equal lists of Selectors to a
136 -- single equivalent Ruleset Statement.
137 --
138 -- For example,
139 -- implode_sel "h1, em { color: red }
140 -- h1, em { background-color: blue }" =
141 -- "h1, em { color: red; background-color: blue }"
142 implode_sel :: [Statement] -> Statement
143 implode_sel ((Ruleset sels decls):stmts) =
144 Ruleset sels (concat (decls:(map declarations stmts)))
145
146 -- Implode a list of Ruleset Statements with equal lists of Declarations to a
147 -- single equivalent Ruleset Statement.
148 --
149 -- For example,
150 -- implode_decl "h1 { color: red }
151 -- em { color: red }" =
152 -- "h1, em { color: red }"
153 implode_decl :: [Statement] -> Statement
154 implode_decl ((Ruleset sels decls):stmts) =
155 Ruleset (concat (sels:(map selectors stmts))) decls
156
157 -- Count rulesets, selectors, and declarations in a Stylesheet.
158 stats :: Stylesheet -> String
159 stats (Stylesheet charset imports stmts)
160 | n == 0 = "0 rulesets"
161 | otherwise = "rulesets " ++ show n ++
162 ", selectors min " ++ show s0 ++
163 " max " ++ show s1 ++
164 " mean " ++ take 5 (
165 show (fromIntegral sn / fromIntegral n)) ++
166 ", declarations min " ++ show d0 ++
167 " max " ++ show d1 ++
168 " mean " ++ take 5 (
169 show (fromIntegral dn / fromIntegral n))
170 where [n, s0, s1, sn, d0, d1, dn] = stats_stmts stmts
171
172 -- Count rulesets, selectors, and declarations in a list of Statements.
173 stats_stmts :: [Statement] -> [Int]
174 stats_stmts stmts = stats_rulesets (filter is_ruleset stmts ++
175 (concatMap get_stmts (filter is_media stmts)))
176 where get_stmts (Media media stmts) = stmts
177
178 -- Count rulesets, selectors, and declarations in a list of Ruleset Statements.
179 stats_rulesets :: [Statement] -> [Int]
180 stats_rulesets stmts = [length stmts,
181 minimum sel_lengths, maximum sel_lengths, sum sel_lengths,
182 minimum decl_lengths, maximum decl_lengths, sum decl_lengths]
183 where sel_lengths = map (length . selectors) stmts
184 decl_lengths = map (length . declarations) stmts
185
186 -- Extract the list of Selectors from a Ruleset Statement.
187 selectors :: Statement -> [Selector]
188 selectors (Ruleset s d) = s
189
190 -- Extract the list of Declarations from a Ruleset Statement.
191 declarations :: Statement -> [Declaration]
192 declarations (Ruleset s d) = d

  ViewVC Help
Powered by ViewVC 1.1.26