Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix regexp #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 54 additions & 59 deletions parses.rb
Original file line number Diff line number Diff line change
@@ -1,80 +1,75 @@
require 'open-uri'

class Parses
BLOCK_COMMENT = /\/\*.*?\*\//m
LINE_COMMENT = /\/\/.*/

attr_accessor :in_file, :out_file, :out_hash
def initialize(args)
@in_file, @out_file = args
class Rule
def initialize(str)
@body = str
end

def run
self.remove_comments
self.remove_newlines
self.construct_hash
self.construct_file
self
def selectors
@body.match(/[^{]*/).to_s.split(/,/).map{|s| s.strip}.select{|s| !s.empty? }
end

def lines
@lines ||= open( @in_file ).read
def props
begin
p = @body.match(/\{([^}]*)/)[1].split(';').select{|s| !s.strip.empty? }
p.map{|s| "{ " + s.strip + " }"}
rescue
[]
end
end

def remove_comments
lines.gsub! /\/\*.*?\*\//m, ''
lines.gsub! /\/\/[^www\.].*/, ''
self
def body
@body
end

def remove_newlines
lines.gsub! /;\s*$\n/, "; "
lines.gsub! /,\s*$\n/, ", "
lines.gsub! /{\s*$\n/, "{ "
lines.gsub! /^\s*\}\n/, " }"
self
end
end

def construct_hash
@out_hash = {}
lines.split(/\n/).each do |line|
/(.+)\{(.+)\}/ =~ line
next if $1.nil?
next if $2.nil?
selectors, attributes = $1, $2

attributes.split( ';' ).each do |attr|
next if attr.strip.empty?
@out_hash["{ #{attr.strip}; }"] ||=[]
@out_hash["{ #{attr.strip}; }"] += selectors.split(',').map{ |s| s.strip }
end
end
end

def construct_file
sorted_keys = @out_hash.keys.sort
in_file, out_file = ARGV

#file = File.open(in_file, "r")
#rules = file.read.split('}')

File.open(out_file, 'w+') do |writer|
sorted_keys.each do |attr|
selectors = @out_hash[attr]
text = ""
file = File.open(in_file)
file.each{ |line| text += line }
text = text.gsub( /[\n\r\t]/m, " " )
text = text.gsub( / +/m, " " )
text = text.gsub( /\/\*[^*]*\*\//m, " " )
rules = text.split('}')

selectors.each do |selector|
unless selectors.last == selector
selector = selector + ','
end
rules = rules.map{|r| Rule.new(r) }

writer.puts selector
end
writer.puts attr
writer.puts
end
hash = {}

##BUILD
rules.each do |r|
r.props.each do |p|

#add all the keys
if !( hash.has_key? p) then
hash[p] = []
end

#add the selector
r.selectors.each do |s|
hash[p].push s
end

end
end

parses = Parses.new ARGV
parses.run
##PRINT
output = ""
hash.keys.sort.each do |key|
output += "\n"
output += hash[key].join(",\n") + "\n"
output += key + "\n"
end

puts output

out_file = File.new(out_file, "w")
out_file.write(output)
out_file.close

__END__