-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcss.reb
More file actions
132 lines (126 loc) · 3.49 KB
/
css.reb
File metadata and controls
132 lines (126 loc) · 3.49 KB
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
Rebol [
title: "CSS utilities"
type: module
name: css
Date: 14-Mar-2025
Version: 0.1.1
Author: @Oldes
Home: https://github.com/Oldes/Rebol-CSS
Rights: MIT
Purpose: {Tokenize CSS content and minify it}
Exports: [css-tokenize css-minify]
]
rules: context [
;; bitsets
*nonascii: complement charset [0 - 177]
*hexa: charset [#"a"-#"f" #"A"-#"F" #"0"-#"9"]
*notesc: complement charset [#"a"-#"f" #"A"-#"F" #"0"-#"9" "^-^/^M^L"]
*nmstart: charset [#"_" #"a"-#"z" #"A"-#"Z"]
*nmchar: charset [#"_" #"a"-#"z" #"A"-#"Z" #"0"-#"9" #"-"]
*num: charset [#"0"-#"9"]
*str: complement charset "^/^M^L\"
*n: charset "nN"
*o: charset "oO"
*t: charset "tT"
*combinator: charset "+>~"
*token-char: charset "{}:@;(),"
*whitespace: charset " ^-^/^M^L"
;; rules
=escape: [#"\" [*notesc | 1 6 *hexa]]
=nmchar: [some [*nmchar | *nonascii | =escape]]
=number: [opt [#"+" | #"-"] any *num #"." some *num | some *num]
=newline: [lf | crlf | cr | 12] ;= 12 = form feed char
=str: [*str | *nonascii | #"\" =newline | =escape]
=string1: [#"^"" some [#"^"" break | =str]]
=string2: [#"'" some [#"'" break | =str]]
=string: [=string1 | =string2]
=invalid1: [#"^"" any str]
=invalid2: [#"'" any str]
=invalid: [=invalid1 | =invalid2]
=ws: [any WHITESPACE]
=nmstart: [*nmstart | *nonascii | =escape]
=name: [some *nmchar]
=ident: [opt #"-" =nmstart any =nmchar]
=hash: [#"#" =name]
=namespace_prefix: [opt [=ident | #"*"] #"|"] ;; e.g. svg| in: svg|circle {...}
=type_selector: [opt =namespace_prefix =ident]
=universal: [opt =namespace_prefix #"*"]
=class: [#"." =ident]
]
css-tokenize: function/with [
;@@ https://www.w3.org/TR/css-syntax-3/#tokenizer-algorithms
css [string! binary! url! file!]
][
case [
any [file? css url? css] [css: read/string css]
binary? css [css: to string! css]
]
parse/case css [
any *whitespace
collect any [
"<!--" thru "-->" any *whitespace
| "/*" thru "*/" any *whitespace
| keep [
*token-char
| *combinator
| =string
| =type_selector
| =universal
| =hash
| =class
| =ident
]
| copy tmp: =number keep (transcode/one tmp) opt [keep ["%" | =name] ]
| some *whitespace keep (SP)
| keep skip
]
]
] :rules
css-minify: function [tokens][
unless block? tokens [tokens: css-tokenize tokens]
ajoin parse tokens [collect any [
#";" opt #" " ahead #"}" ;== removes ; in front of }
| #"{" any [#" " | #";"] keep (#"{")
| #"}" #" " keep (#"}")
| #":" #" " keep (#":")
| #";" any [#" " | #";"] [ahead #"}" | keep (#";")]
| #"(" (expr?: on ) #" " keep (#"(")
| #")" (expr?: off) #" " keep (#")")
| #"," #" " keep (#",")
| #" " [
ahead [#"{" | #"}" | #"(" | #")" | #":" | #";" | #","]
| keep [#">" | #"~"] opt #" "
| if (not expr?) keep #"+" opt #" "
| end
]
| quote 0 [
"ms" keep ("0s") | ;== 0ms -> 0s
[#"%" | string!] not #"," keep (0) ;== zero percent/dimension
]
| #"+" ahead number!
| #"-" ahead quote 0
;= and in a media query must be separated with a space
| "and" #" " ahead #"(" keep ("and ")
| if (expr?) [
#" " #"/" #" " keep (#"/")
| #" " #"*" #" " keep (#"*")
]
| "black" keep ("#000")
| "white" keep ("#fff")
| keep skip
]]
]
register-codec [
name: 'css
type: 'text
title: "Cascading Style Sheets"
suffixes: [%.css]
decode: function [
data [binary! file! url!]
][
css-tokenize data
]
encode: function[data [block!]][
css-minify data
]
]