-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David Björklund
committed
Sep 6, 2011
0 parents
commit fae4f9c
Showing
11 changed files
with
187 additions
and
0 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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
/node_modules |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,48 @@ | |||
# Css2json | |||
|
|||
Css2json transform CSS to JSON. | |||
|
|||
## Installation | |||
|
|||
``` | |||
npm install css2json | |||
``` | |||
|
|||
## Usage | |||
|
|||
``` | |||
var css2json = require('css2json'); | |||
var json = css2json.parse(css); | |||
``` | |||
|
|||
## Example | |||
|
|||
The css | |||
|
|||
``` | |||
h1#header { | |||
font-family: 'Times New Roman'; | |||
} | |||
p { | |||
margin-right: 100px; | |||
} | |||
h1#header { | |||
color: #ff0000 | |||
} | |||
``` | |||
|
|||
would transform into | |||
|
|||
``` | |||
{ | |||
"h1#header": { | |||
"font-family": "Times New Roman", | |||
"color": "#ff0000" | |||
}, | |||
"p": { | |||
"margin-right": "100px" | |||
} | |||
} | |||
``` |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,46 @@ | |||
var _ = require('underscore')._ | |||
|
|||
// | |||
// Parse a css-string to a json-object. | |||
// | |||
exports.parse = parse = function(css) { | |||
|
|||
// @json is the return value | |||
var json = {}; | |||
|
|||
// Each instance gets parsed and then removed from the input @css, until the | |||
// length is 0 | |||
while(css.length > 0) { | |||
// save index to the left bracket | |||
var lbracket = css.indexOf('{'); | |||
// save index to the right bracket | |||
var rbracket = css.indexOf('}'); | |||
|
|||
css.substr(0, lbracket).split(",").forEach(function(s) { | |||
var selectors = s.trim(); | |||
// initialize standard value if it isn't set | |||
if (!json[selectors]) json[selectors] = {}; | |||
// Collect the attribute to the selectors | |||
var attributes = css.substring(lbracket + 1, rbracket).split(";"); | |||
// Make the attributes chainable | |||
_(attributes).chain() | |||
// Trim each attribute from unneccesary white space | |||
.map(function(elm){ return elm.trim() }) | |||
// Remove all empty values, "" for example | |||
.compact() | |||
// Get the value back from the chaining | |||
.value() | |||
// Go through each attribute and insert the value in the css-object | |||
.forEach(function(attribute){ | |||
var index = attribute.indexOf(":"); | |||
json[selectors][attribute.substring(0, index)] = | |||
attribute.substring(index + 1).trim().replace(/'/g, ""); | |||
}); | |||
|
|||
}); | |||
// Continue to next instance | |||
css = css.slice(rbracket + 1).trim() | |||
} | |||
// return the json data | |||
return json; | |||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,20 @@ | |||
{ | |||
"author": "David Björklund <david.bjorklund@gmail.com> (http://davidbjorklund.se)", | |||
"name": "css2json", | |||
"description": "Parse css to json.", | |||
"version": "0.0.1", | |||
"repository": { | |||
"type": "git", | |||
"url": "git://github.com/kesla/node-snappy.git" | |||
}, | |||
"main": "./lib/css2json.js", | |||
"engines": { | |||
"node": "~v0.4.11" | |||
}, | |||
"dependencies": { | |||
"underscore": "~1.1.7" | |||
}, | |||
"devDependencies": { | |||
"json-san": "0.0.2" | |||
} | |||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,11 @@ | |||
h1#header { | |||
font-family: 'Times New Roman'; | |||
} | |||
|
|||
p { | |||
margin-right: 100px; | |||
} | |||
|
|||
h1#header { | |||
color: #ff0000 | |||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,9 @@ | |||
{ | |||
"h1#header": { | |||
"font-family": "Times New Roman", | |||
"color": "#ff0000" | |||
}, | |||
"p": { | |||
"margin-right": "100px" | |||
} | |||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,11 @@ | |||
h1#header { | |||
font-family: 'Times New Roman'; | |||
} | |||
|
|||
p { | |||
margin-right: 100px; | |||
} | |||
|
|||
h1#header, h2#footer { | |||
color: #ff0000 | |||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,12 @@ | |||
{ | |||
"h1#header": { | |||
"font-family": "Times New Roman", | |||
"color": "#ff0000" | |||
}, | |||
"p": { | |||
"margin-right": "100px" | |||
}, | |||
"h2#footer": { | |||
"color": "#ff0000" | |||
} | |||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,16 @@ | |||
var assert = require('assert'), | |||
css2json = require('../lib/css2json'), | |||
fs = require('fs'), | |||
JSON2 = require("json-san"), | |||
path = require('path'); | |||
|
|||
var bases = ['simple', 'advanced', 'advanced2']; | |||
|
|||
bases.forEach(function(base){ | |||
var basePath = path.join(__dirname, base); | |||
var css = fs.readFileSync(basePath + '.css', 'utf8'); | |||
json = JSON2.parse(fs.readFileSync(basePath + '.json', 'utf8')); | |||
|
|||
assert.deepEqual(css2json.parse(css), json); | |||
|
|||
}); |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
day.weekday hour.7am minute.30 { | |||
event-name: 'cleanup'; | |||
module: 'clean.js'; | |||
broadcast: '127.0.0.1:1337'; | |||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,8 @@ | |||
{ | |||
"day.weekday hour.7am minute.30": | |||
{ | |||
"event-name": "cleanup", | |||
"module": "clean.js", | |||
"broadcast": "127.0.0.1:1337" | |||
} | |||
} |