Skip to content

Commit

Permalink
Initial commit aka first version!
Browse files Browse the repository at this point in the history
  • Loading branch information
David Björklund committed Sep 6, 2011
0 parents commit fae4f9c
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
48 changes: 48 additions & 0 deletions Readme.md
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"
}
}
```
46 changes: 46 additions & 0 deletions lib/css2json.js
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;
}
20 changes: 20 additions & 0 deletions package.json
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"
}
}
11 changes: 11 additions & 0 deletions test/advanced.css
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
}
9 changes: 9 additions & 0 deletions test/advanced.json
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"
}
}
11 changes: 11 additions & 0 deletions test/advanced2.css
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
}
12 changes: 12 additions & 0 deletions test/advanced2.json
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"
}
}
16 changes: 16 additions & 0 deletions test/all.js
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);

});
5 changes: 5 additions & 0 deletions test/simple.css
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';
}
8 changes: 8 additions & 0 deletions test/simple.json
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"
}
}

0 comments on commit fae4f9c

Please sign in to comment.