-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync
executable file
·75 lines (65 loc) · 2.09 KB
/
sync
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
#!/usr/bin/env ruby
require 'open-uri'
# Attempt to load the 'listen' gem
listen_loaded = begin
require 'listen'
true
rescue LoadError
false
end
# URL of the text file and the base CSS file
url = 'https://raw.githubusercontent.com/easylist/easylist/master/fanboy-addon/fanboy_annoyance_general_hide.txt'
base_file = 'base.css'
output_file = 'adblock.css'
# Function to minify CSS content
def minify_css(content)
content.gsub(/\/\*.*?\*\//m, '')
.gsub(/\s+/, ' ')
.gsub(/\s*{\s*/, '{')
.gsub(/\s*}\s*/, '}')
.gsub(/\s*;\s*/, ';')
.gsub(/;\s*}/, '}')
end
def generate_css(url, base_file, output_file)
base_content = File.read(base_file)
minified_base_content = minify_css(base_content)
selectors = []
OpenURI.open_uri(url) do |file|
file.each_line do |line|
next if line.start_with?('!') || line.strip.empty?
selector = line.strip.gsub(/^#+/, '')
selectors << selector
end
end
combined_selectors = selectors.join(', ')
minified_css_rule = "#{combined_selectors} {display: none !important;visibility: invisible !important;}"
File.open(output_file, 'w') do |output|
output.puts "/* auto generated with ./sync - do not edit */"
output.puts minified_base_content
output.puts minify_css(minified_css_rule)
end
puts "CSS file generated: #{output_file}"
end
def watch_file(url, base_file, output_file, listen_loaded)
if listen_loaded
current_dir = File.dirname(__FILE__)
listener = Listen.to(current_dir) do |modified, added, removed|
if (modified + added).any? { |n| n.include?(base_file) }
puts "#{base_file} has changed. Regenerating #{output_file}."
generate_css(url, base_file, output_file)
end
end
listener.start
puts "Listening for changes in #{base_file}..."
sleep
else
generate_css(url, base_file, output_file)
puts "CSS file generated: #{output_file}"
puts "Notice: to use --sync you must gem install listen"
end
end
if ARGV.include?('--watch')
watch_file(url, base_file, output_file, listen_loaded)
else
generate_css(url, base_file, output_file)
end