7
7
8
8
// to use this run `node ./convert.js`
9
9
10
- const postcss = require ( "postcss" ) ;
11
- const _ = require ( "lodash" ) ;
12
- const exportPath = "./src/Tailwind/Classes.elm" ;
10
+ const postcss = require ( 'postcss' ) ;
11
+ const _ = require ( 'lodash' ) ;
13
12
14
- fs = require ( "fs" ) ;
13
+ const exportPath = './src/Tailwind/Classes.elm' ;
15
14
16
- const css = fs . readFileSync ( "./scripts/tailwind.css" , "utf8" ) ; // minificated version introduces problems with compound selectors
15
+ fs = require ( 'fs' ) ;
16
+
17
+ const css = fs . readFileSync ( './scripts/tailwind.css' , 'utf8' ) ; // minificated version introduces problems with compound selectors
17
18
18
19
const root = postcss . parse ( css ) ;
19
20
20
21
const classObjs = { } ;
21
22
22
- const defaultIndentation = " " . repeat ( 4 ) ;
23
+ const defaultIndentation = ' ' . repeat ( 4 ) ;
23
24
24
- const ruleFormatter = rule => {
25
- let def = rule . toString ( ) . replace ( "{-" , " { -" ) ;
25
+ const ruleFormatter = ( rule ) => {
26
+ let def = rule . toString ( ) . replace ( '{-' , ' { -' ) ;
26
27
def = setCorrectIndentation ( def ) ;
27
28
return def ;
28
29
} ;
29
30
30
- const setCorrectIndentation = text => {
31
+ const setCorrectIndentation = ( text ) => {
31
32
// normalize indentation
32
33
if ( / { 4 } / . test ( text ) ) {
33
- text = text . replace ( / \n { 2 } / g, "\n" ) ;
34
+ text = text . replace ( / \n { 2 } / g, '\n' ) ;
34
35
}
35
36
// set indentation
36
- text = text . replace ( / \n / g, "\n" + defaultIndentation ) ;
37
+ text = text . replace ( / \n / g, `\n ${ defaultIndentation } ` ) ;
37
38
return text ;
38
39
} ;
39
40
40
41
/**
41
42
* This will walk through each of the css rules in tailwind
42
43
* and pull out the relevent information.
43
44
*/
44
- root . walkRules ( rule => {
45
+ root . walkRules ( ( rule ) => {
45
46
//
46
47
// Ignore anything that's not a class
47
48
//
48
- if ( ! rule . selector . startsWith ( "." ) ) return ;
49
+ if ( ! rule . selector . startsWith ( '.' ) ) return ;
49
50
50
51
//
51
52
// Ignore responsive variations in favor of using utility classes for that.
52
53
//
53
- if ( rule . selector . startsWith ( " .sm\\:" ) ) return ;
54
- if ( rule . selector . startsWith ( " .md\\:" ) ) return ;
55
- if ( rule . selector . startsWith ( " .lg\\:" ) ) return ;
56
- if ( rule . selector . startsWith ( " .xl\\:" ) ) return ;
54
+ if ( rule . selector . startsWith ( ' .sm\\:' ) ) return ;
55
+ if ( rule . selector . startsWith ( ' .md\\:' ) ) return ;
56
+ if ( rule . selector . startsWith ( ' .lg\\:' ) ) return ;
57
+ if ( rule . selector . startsWith ( ' .xl\\:' ) ) return ;
57
58
58
59
let name = rule . selector ;
59
60
60
61
//
61
62
// If we're dealing with a rule that's selecting an :after or :before, ignore the rule
62
63
//
63
- if ( name . indexOf ( " :after" ) != - 1 || name . indexOf ( " :before" ) != - 1 ) {
64
+ if ( name . indexOf ( ' :after' ) != - 1 || name . indexOf ( ' :before' ) != - 1 ) {
64
65
return ;
65
66
}
66
67
67
68
//
68
69
// Remove the leading dot
69
70
//
70
- name = name . replace ( / ^ \S * \. / , "" ) ;
71
+ name = name . replace ( / ^ \S * \. / , '' ) ;
71
72
72
73
//
73
74
// Throw away anything after a comma or a space
@@ -78,51 +79,59 @@ root.walkRules(rule => {
78
79
//
79
80
// Replace the \: with a __
80
81
//
81
- elmName = elmName . split ( " \\:" ) . join ( "__" ) ;
82
+ elmName = elmName . split ( ' \\:' ) . join ( '__' ) ;
82
83
83
84
//
84
85
// Replace the negative margin with a 'neg'
85
86
//
86
- elmName = elmName . replace ( / - m ( [ l r t b x y ] ) / g, " neg_m$1" ) ;
87
+ elmName = elmName . replace ( / - m ( [ l r t b x y ] ) / g, ' neg_m$1' ) ;
87
88
88
89
//
89
90
// Change a leading dash to a 'neg'
90
91
//
91
- elmName = elmName . replace ( / ^ - / , " neg" ) ;
92
+ elmName = elmName . replace ( / ^ - / , ' neg' ) ;
92
93
93
94
//
94
95
// Replace dashes with underscores
95
96
//
96
- elmName = elmName . replace ( / - / g, "_" ) ;
97
+ elmName = elmName . replace ( / - / g, '_' ) ;
97
98
98
99
//
99
100
// Change the \/ in fractions to `over`
100
101
//
101
- elmName = elmName . replace ( / \\ \/ / g, "over" ) ;
102
+ elmName = elmName . replace ( / \\ \/ / g, 'over' ) ;
103
+
104
+ //
105
+ // Remove :focus
106
+ //
107
+ elmName = elmName . replace ( ':focus' , '' ) ;
108
+
109
+ //
110
+ // Remove :hover
111
+ //
112
+ elmName = elmName . replace ( ':hover' , '' ) ;
102
113
103
114
//
104
115
// Before using "name", but after basing elmName on it, escape the backslack in the Elm string
105
116
//
106
- name = name . replace ( / \\ / g, " \\\\" ) ;
117
+ name = name . replace ( / \\ / g, ' \\\\' ) ;
107
118
108
119
const obj = {
109
120
name,
110
121
elmName,
111
- def : ruleFormatter ( rule )
122
+ def : ruleFormatter ( rule ) ,
112
123
} ;
113
124
114
125
console . log ( obj ) ;
115
126
116
- if ( name in classObjs )
117
- classObjs [ name ] . def += "\n" + defaultIndentation + obj . def ; // class has been already registered, only append new def
127
+ if ( name in classObjs ) { classObjs [ name ] . def += `\n${ defaultIndentation } ${ obj . def } ` ; } // class has been already registered, only append new def
118
128
else classObjs [ name ] = obj ;
119
129
} ) ;
120
130
121
- const classes = _ ( classObjs ) . sortBy ( " name" ) ;
131
+ const classes = _ ( classObjs ) . sortBy ( ' name' ) ;
122
132
123
133
// creates an elm variable for each class
124
- const elmify = cl => {
125
- return `
134
+ const elmify = cl => `
126
135
{-| This class maps to this CSS definition:
127
136
128
137
${ cl . def }
@@ -134,7 +143,6 @@ ${cl.elmName} =
134
143
135
144
136
145
` ;
137
- } ;
138
146
139
147
// // string of the elm file
140
148
const elmString = `
@@ -160,7 +168,7 @@ They do however show the minifed css definition as their comment.
160
168
161
169
# Classes and their Definitions
162
170
163
- @docs ${ classes . map ( ( { elmName } ) => elmName ) . join ( ", " ) }
171
+ @docs ${ classes . map ( ( { elmName } ) => elmName ) . join ( ', ' ) }
164
172
165
173
-}
166
174
@@ -173,24 +181,24 @@ type TailwindClass = TailwindClass String
173
181
sm : TailwindClass -> TailwindClass
174
182
sm (TailwindClass c) =
175
183
TailwindClass ("sm:" ++ c)
176
-
184
+
177
185
178
186
{-| Add a size prefix to the tailwind rule, making it only apply to that screen size and above.
179
187
Notice, doesn't make sure the class being passed in is going to work with a responsive prefix.. Not all tailwind rules are responsive-capable.
180
188
-}
181
189
md : TailwindClass -> TailwindClass
182
190
md (TailwindClass c) =
183
191
TailwindClass ("md:" ++ c)
184
-
185
-
192
+
193
+
186
194
{-| Add a size prefix to the tailwind rule, making it only apply to that screen size and above.
187
195
Notice, doesn't make sure the class being passed in is going to work with a responsive prefix.. Not all tailwind rules are responsive-capable.
188
196
-}
189
197
lg : TailwindClass -> TailwindClass
190
198
lg (TailwindClass c) =
191
199
TailwindClass ("lg:" ++ c)
192
200
193
-
201
+
194
202
{-| Add a size prefix to the tailwind rule, making it only apply to that screen size and above.
195
203
Notice, doesn't make sure the class being passed in is going to work with a responsive prefix.. Not all tailwind rules are responsive-capable.
196
204
-}
@@ -199,13 +207,13 @@ xl (TailwindClass c) =
199
207
TailwindClass ("xl:" ++ c)
200
208
201
209
202
- ${ classes . map ( elmify ) . join ( "" ) } `;
210
+ ${ classes . map ( elmify ) . join ( '' ) } `;
203
211
204
212
// writing the string to the file
205
- fs . writeFile ( exportPath , elmString , function ( err ) {
213
+ fs . writeFile ( exportPath , elmString , ( err ) => {
206
214
if ( err ) {
207
215
return console . log ( err ) ;
208
216
}
209
217
210
- console . log ( exportPath , " was saved!" ) ;
218
+ console . log ( exportPath , ' was saved!' ) ;
211
219
} ) ;
0 commit comments