Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit aka first version!

  • Loading branch information...
commit fae4f9c59b48cdf6db48f131b684795eef80893c 0 parents
David Björklund authored
1  .gitignore
@@ -0,0 +1 @@
+/node_modules
48 Readme.md
@@ -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 lib/css2json.js
@@ -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 package.json
@@ -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 test/advanced.css
@@ -0,0 +1,11 @@
+h1#header {
+ font-family: 'Times New Roman';
+}
+
+p {
+ margin-right: 100px;
+}
+
+h1#header {
+ color: #ff0000
+}
9 test/advanced.json
@@ -0,0 +1,9 @@
+{
+ "h1#header": {
+ "font-family": "Times New Roman",
+ "color": "#ff0000"
+ },
+ "p": {
+ "margin-right": "100px"
+ }
+}
11 test/advanced2.css
@@ -0,0 +1,11 @@
+h1#header {
+ font-family: 'Times New Roman';
+}
+
+p {
+ margin-right: 100px;
+}
+
+h1#header, h2#footer {
+ color: #ff0000
+}
12 test/advanced2.json
@@ -0,0 +1,12 @@
+{
+ "h1#header": {
+ "font-family": "Times New Roman",
+ "color": "#ff0000"
+ },
+ "p": {
+ "margin-right": "100px"
+ },
+ "h2#footer": {
+ "color": "#ff0000"
+ }
+}
16 test/all.js
@@ -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 test/simple.css
@@ -0,0 +1,5 @@
+day.weekday hour.7am minute.30 {
+ event-name: 'cleanup';
+ module: 'clean.js';
+ broadcast: '127.0.0.1:1337';
+}
8 test/simple.json
@@ -0,0 +1,8 @@
+{
+ "day.weekday hour.7am minute.30":
+ {
+ "event-name": "cleanup",
+ "module": "clean.js",
+ "broadcast": "127.0.0.1:1337"
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.