forked from andrewtimberlake/css
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added license and readme along with ability to parse file, file name …
…or style string
- Loading branch information
1 parent
6620549
commit c378445
Showing
8 changed files
with
146 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
tags | ||
*.gem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (C) 2010 by Andrew Timberlake | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
=CSS | ||
|
||
A Ruby gem that allows the parsing and creation of css files. | ||
|
||
==Quick Start | ||
|
||
style = "body { background: #FFF url('image.png') no-repeat; }" | ||
css = CSS::Parser.new.parse(style) | ||
puts css['body'].background.color.to_s #=> #FFF | ||
css['body'].background.position = 'top center' | ||
puts css['body'].to_s #=> 'background: #FFF url('image.png') top center no-repeat' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,88 @@ | ||
module CSS | ||
class Parser | ||
def parse(file_name) | ||
ruleset = RuleSet.new | ||
# Parse a css style by string, file or file name | ||
# | ||
# ==Examples | ||
# parser = CSS::Parser.new | ||
# css = parser.parse("body { background: #FFF }") | ||
# | ||
# css = parser.parse("/home/andrew/style.css") | ||
# | ||
# File.open("style.css") do |file| | ||
# css = parser.parse(file) | ||
# end | ||
def parse(file_file_name_or_style_string) | ||
css_style = file_file_name_or_style_string | ||
reset | ||
lineno = 1 | ||
if css_style.respond_to?(:size) && css_style.size < 255 && File.exists?(css_style) | ||
File.open(css_style) do |file| | ||
file.each_line do |line| | ||
parse_line(line, lineno) | ||
lineno += 1 | ||
end | ||
end | ||
elsif css_style.respond_to?(:each_line) | ||
css_style.each_line do |line| | ||
parse_line(line, lineno) | ||
lineno += 1 | ||
end | ||
end | ||
|
||
state = :selector | ||
previous_state = nil | ||
previous_char = nil | ||
buffer = [] | ||
selector = nil | ||
@ruleset | ||
end | ||
|
||
File.open(file_name) do |file| | ||
file.each do |line| | ||
char_count = 1 | ||
line.each_char do |char| | ||
buffer << char unless state == :comment | ||
private | ||
def reset | ||
@ruleset = RuleSet.new | ||
|
||
case char | ||
when '{' | ||
if state == :selector | ||
buffer.pop | ||
selector = buffer.join.strip | ||
buffer.clear | ||
state = :ruleset | ||
else | ||
unless state == :comment | ||
raise CSSError.new(file.lineno, char_count, "Unexpected '{'") | ||
end | ||
end | ||
when '}' | ||
unless state == :comment | ||
buffer.pop | ||
ruleset << Rule.new(selector, buffer.join.strip) | ||
state = :selector | ||
buffer.clear | ||
end | ||
when '/' | ||
if previous_char == '*' | ||
buffer.pop | ||
state = previous_state | ||
end | ||
when '*' | ||
if previous_char == '/' | ||
buffer.pop | ||
previous_state = state | ||
state = :comment | ||
@state = :selector | ||
@previous_state = nil | ||
@previous_char = nil | ||
@buffer = [] | ||
@selector = nil | ||
end | ||
|
||
def parse_line(line, lineno) | ||
char_count = 1 | ||
line.each_char do |char| | ||
@buffer << char unless @state == :comment | ||
|
||
case char | ||
when '{' | ||
if @state == :selector | ||
@buffer.pop | ||
@selector = @buffer.join.strip | ||
@buffer.clear | ||
@state = :ruleset | ||
else | ||
unless @state == :comment | ||
raise CSSError.new(lineno, char_count, "Unexpected '{'") | ||
end | ||
end | ||
|
||
previous_char = char unless char == ' ' | ||
char_count += 1 | ||
when '}' | ||
unless @state == :comment | ||
@buffer.pop | ||
@ruleset << Rule.new(@selector, @buffer.join.strip) | ||
@state = :selector | ||
@buffer.clear | ||
end | ||
when '/' | ||
if @previous_char == '*' | ||
@buffer.pop | ||
@state = @previous_state | ||
end | ||
when '*' | ||
if @previous_char == '/' | ||
@buffer.pop | ||
@previous_state = @state | ||
@state = :comment | ||
end | ||
end | ||
|
||
@previous_char = char unless char == ' ' | ||
char_count += 1 | ||
end | ||
end | ||
|
||
ruleset | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters