-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.coffee
44 lines (34 loc) · 1.33 KB
/
parser.coffee
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
parse = require('css-parse')
stringify = require('css-stringify')
fs = require('fs')
any = (x) -> return true in (!!n for n in x)
# process.stdin.resume()
# process.stdin.setEncoding('utf8')
# process.stdin.on 'data', (chunk) -> css += chunk
# process.stdin.on 'end', ->
css = fs.readFileSync('demo.css', 'utf8')
out = parse(css)
{stylesheet: {rules}} = out
js_rules = []
remaining_rules = []
for rule in rules
continue if rule.type isnt 'rule'
if any(sel.match(/\:\:(before|after|first\-letter|first\-line)/) for sel in rule.selectors)
# these aren't injectable by solitude
console.log rule
for decl in rule.declarations when decl.type is 'declaration'
if decl.value.indexOf('!important') == -1
decl.value += ' !important'
decl.value = decl.value.replace(/\s+/g, ' ').trim()
remaining_rules.push rule
else
# for sel in rule.selectors
# js_rules.push sel
js_rules.push [
rule.selectors,
[decl.property, decl.value.replace(/\s+/g, ' ').trim()] for decl in rule.declarations when decl.type is 'declaration'
]
console.log rule.selectors, rule.declarations
console.log JSON.stringify(js_rules)
fs.writeFileSync('rules.js', 'rules = ' + JSON.stringify(js_rules, null, '\t'))
fs.writeFileSync('remainder.css', stringify({stylesheet: rules: remaining_rules}))