-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
54 lines (42 loc) · 1.24 KB
/
main.rb
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
# Inputs:
# html file
# scss file
# final file
# Process:
# Get list of classes is use
# Filter for uniq
# Append scss import for each uniq class (using standard string for path)
# Compile scss
require 'nokogiri'
htmlfile = ARGV[0] # full path and filename for source html file
scssfile = ARGV[1] # full path and filename for skeleton scss file
scsspath = ARGV[2] # path to the component SCSS files
finalcss = ARGV[3] # full path and filename for final compiled CSS file
# Get a list of all classes used in the HTML
def getClasses(arr, html)
page = Nokogiri::HTML(open(html))
elements = page.css("*")
elements.each{ |el| arr << el['class'] }
end
# Based on the list of classes,
# add imports to the corresponding scss module
# for every class in use in the HTML.
def addImports(arr, file, path)
arr.each do |myclass|
scssimport = "@import " + path + "_" + myclass + ".scss';"
File.open(file, "a+") do |output|
output.write scssimport
end
end
end
# compile the scss
def compileSCSS(scss, css)
`sass #{scss} #{finalcss}`
end
# PROCESSES
classes = []
getClasses(classes, htmlfile)
# filter the array of classes for unique values only
classes = classes.uniq
addImports(classes, scssfile, scsspath)
compileSCSS(scssfile, finalcss)