cssbeautify 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/cssbeautify.rb +331 -0
  3. metadata +57 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a0cd09cc6cfc1275c22e3cc0a5408bfeb1843ae6
4
+ data.tar.gz: 7ef79e72fb05f11ee932236c4f8aad9944521325
5
+ SHA512:
6
+ metadata.gz: 65e37293d86bd8d13bac6878b55a05e2a66972a94c5f17101412df82c4cb7ec8eef5bfd85b89f44c7c5424249dc0af9e2d4ff95a675ee488a73297e92efdfe33
7
+ data.tar.gz: 0526855081afd1e2fd7ad753482f6cd38357bb6c96ad734ffe24a7f5b45618a722865a85f28ddcfe1958467d9eeadf5321c192521e8178f52efcc71aba52156e
@@ -0,0 +1,331 @@
1
+ class CssBeautify
2
+ class << self
3
+ def whitespace?(c)
4
+ c == ' ' || c == "\n" || c == "\t" || c == "\r" || c == "\f"
5
+ end
6
+
7
+ def quote?(c)
8
+ c == "\"" || c == "'"
9
+ end
10
+
11
+ def name?(c)
12
+ (c >= 'a' && c <= 'z') ||
13
+ (c >= 'A' && c <= 'Z') ||
14
+ (c >= '0' && c <= '9') ||
15
+ '-_*.:#[]'.include?(c)
16
+ end
17
+
18
+ def trim_right(input)
19
+ input.sub /\s+\Z/, ''
20
+ end
21
+
22
+ STATE_START = 0
23
+ STATE_AT_RULE = 1
24
+ STATE_BLOCK = 2
25
+ STATE_SELECTOR = 3
26
+ STATE_RULESET = 4
27
+ STATE_PROPERTY = 5
28
+ STATE_SEPARATOR = 6
29
+ STATE_EXPRESSION = 7
30
+ STATE_URL = 8
31
+
32
+ def beautify(style, options = {})
33
+ openbracesuffix = (options[:openbrace] != 'end-of-line')
34
+ autosemicolon = !!options[:autosemicolon]
35
+ indent = options[:indent] || ' '
36
+
37
+ ch = ''
38
+ ch2 = ''
39
+ formatted = ''
40
+ index = 0
41
+ length = style.length
42
+ depth = 0
43
+ state = STATE_START
44
+ comment = false
45
+ blocks = []
46
+ style = style.gsub(/\r\n/, "\n")
47
+ quote = nil
48
+
49
+ append_indent = Proc.new { formatted << (indent * depth) }
50
+
51
+ open_block = Proc.new do
52
+ formatted = trim_right(formatted)
53
+ if openbracesuffix
54
+ formatted << " {"
55
+ else
56
+ formatted << "\n"
57
+ append_indent.call
58
+ formatted << '{'
59
+ end
60
+
61
+ formatted << "\n" unless ch2 == "\n"
62
+ depth = depth + 1
63
+ end
64
+
65
+ close_block = Proc.new do
66
+ depth = depth - 1
67
+ formatted = trim_right(formatted)
68
+
69
+ if formatted.length > 0 && autosemicolon
70
+ unless (formatted[-1] == ';' || formatted[-1] == '{')
71
+ formatted << ';'
72
+ end
73
+ end
74
+
75
+ formatted << "\n"
76
+ append_indent.call
77
+ formatted << '}'
78
+
79
+ blocks << formatted
80
+ formatted = ''
81
+ end
82
+
83
+ while index < length
84
+ ch = style[index]
85
+ ch2 = style[index + 1]
86
+ index = index + 1
87
+
88
+ if quote?(quote)
89
+ formatted << ch
90
+
91
+ quote = nil if ch == quote
92
+
93
+ if ch == "\\" && ch2 == quote
94
+ formatted << ch2
95
+ index = index + 1
96
+ end
97
+ next
98
+ end
99
+
100
+ if quote?(ch)
101
+ formatted << ch
102
+ quote = ch
103
+ next
104
+ end
105
+
106
+ if comment
107
+ formatted << ch
108
+ if ch == '*' && ch2 == '/'
109
+ comment = false
110
+ formatted << ch2
111
+ index = index + 1
112
+ end
113
+ next
114
+ end
115
+
116
+ if ch == '/' && ch2 == '*'
117
+ comment = true
118
+ formatted << ch << ch2
119
+ index = index + 1
120
+ next
121
+ end
122
+
123
+ if state == STATE_START
124
+ if blocks.empty?
125
+ if whitespace?(ch) && formatted.empty?
126
+ next
127
+ end
128
+ end
129
+
130
+ if (ch <= ' ') || ch.ord >= 128
131
+ state = STATE_START
132
+ formatted << ch
133
+ next
134
+ end
135
+
136
+ if name?(ch) || ch == '@'
137
+ str = trim_right(formatted)
138
+
139
+ if str.empty?
140
+ if blocks.length > 0
141
+ formatted = "\n\n"
142
+ end
143
+ else
144
+ if str[-1] == '}' || str[-1] == ';'
145
+ formatted = str + "\n\n"
146
+ else
147
+ while true
148
+ ch2 = formatted[-1]
149
+ break unless ch2 == ' ' || ch2.ord == 9
150
+ formatted = formatted.slice(0, formatted.length - 1)
151
+ end
152
+ end
153
+ end
154
+
155
+ formatted << ch
156
+ state = (ch == '@') ? STATE_AT_RULE : STATE_SELECTOR
157
+ next
158
+ end
159
+ end
160
+
161
+ if state == STATE_AT_RULE
162
+ if ch == ';'
163
+ formatted << ch
164
+ state = STATE_START
165
+ next
166
+ end
167
+
168
+ if ch == '{'
169
+ str = trim_right(formatted)
170
+ open_block.call
171
+ state = (str == '@font-face') ? STATE_RULESET : STATE_BLOCK
172
+ next
173
+ end
174
+
175
+ formatted << ch
176
+ next
177
+ end
178
+
179
+ if state == STATE_BLOCK
180
+ if name?(ch)
181
+ str = trim_right(formatted)
182
+ if str.empty?
183
+ if blocks.length > 0
184
+ formatted = "\n\n"
185
+ end
186
+ else
187
+ if str[-1] == '}'
188
+ formatted = str + "\n\n"
189
+ else
190
+ while true
191
+ ch2 = formatted[-1]
192
+ break unless ch2 == ' ' || ch2.ord == 9
193
+ formatted = formatted.slice(0, formatted.length - 1)
194
+ end
195
+ end
196
+ end
197
+
198
+ append_indent.call
199
+ formatted << ch
200
+ state = STATE_SELECTOR
201
+ next
202
+ end
203
+
204
+ if ch == '}'
205
+ close_block.call
206
+ state = STATE_START
207
+ next
208
+ end
209
+
210
+ formatted << ch
211
+ next
212
+ end
213
+
214
+ if state == STATE_SELECTOR
215
+ if ch == '{'
216
+ open_block.call
217
+ state = STATE_RULESET
218
+ next
219
+ end
220
+
221
+ if ch == '}'
222
+ close_block.call
223
+ state = STATE_START
224
+ next
225
+ end
226
+
227
+ formatted << ch
228
+ next
229
+ end
230
+
231
+ if state == STATE_RULESET
232
+ if ch == '}'
233
+ close_block.call
234
+ state = STATE_START
235
+ state = STATE_BLOCK if depth > 0
236
+ next
237
+ end
238
+
239
+ if ch == "\n"
240
+ formatted = trim_right(formatted)
241
+ formatted << "\n"
242
+ next
243
+ end
244
+
245
+ unless whitespace?(ch)
246
+ formatted = trim_right(formatted)
247
+ formatted << "\n"
248
+ append_indent.call
249
+ formatted << ch
250
+ state = STATE_PROPERTY
251
+ next
252
+ end
253
+
254
+ formatted << ch
255
+ next
256
+ end
257
+
258
+ if state == STATE_PROPERTY
259
+ if ch == ':'
260
+ formatted = trim_right(formatted)
261
+ formatted << ': '
262
+ state = STATE_EXPRESSION
263
+ state = STATE_SEPARATOR if whitespace?(ch2)
264
+ next
265
+ end
266
+
267
+ if ch == '}'
268
+ close_block.call
269
+ state = STATE_START
270
+ state = STATE_BLOCK if depth > 0
271
+ next
272
+ end
273
+
274
+ formatted << ch
275
+ next
276
+ end
277
+
278
+ if state == STATE_SEPARATOR
279
+ unless whitespace?(ch)
280
+ formatted << ch
281
+ state = STATE_EXPRESSION
282
+ next
283
+ end
284
+
285
+ state = STATE_EXPRESSION if quote?(ch)
286
+ next
287
+ end
288
+
289
+ if state == STATE_EXPRESSION
290
+ if ch == '}'
291
+ close_block.call
292
+ state = STATE_START
293
+ state = STATE_BLOCK if depth > 0
294
+ next
295
+ end
296
+
297
+ if ch == ';'
298
+ formatted = trim_right(formatted)
299
+ formatted << ";\n"
300
+ state = STATE_RULESET
301
+ next
302
+ end
303
+
304
+ formatted << ch
305
+
306
+ if ch == '('
307
+ if formatted[-4, 3] == 'url'
308
+ state = STATE_URL
309
+ next
310
+ end
311
+ end
312
+
313
+ next
314
+ end
315
+
316
+ if state == STATE_URL
317
+ if ch == ')' && formatted[-1] != "\\"
318
+ formatted << ch
319
+ state = STATE_EXPRESSION
320
+ next
321
+ end
322
+ end
323
+
324
+ formatted << ch
325
+ end
326
+
327
+ formatted = blocks.join('') + formatted
328
+ formatted.strip
329
+ end
330
+ end
331
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cssbeautify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Volodymyr Myskov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Beautify and format you css
28
+ email: jaromudr@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/cssbeautify.rb
34
+ homepage: https://github.com/Jaromudr/cssbeautify
35
+ licenses: []
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 2.4.5
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: CssBeautify
57
+ test_files: []