diff --git a/README.md b/README.md
index a724abe..f4cef68 100644
--- a/README.md
+++ b/README.md
@@ -15,10 +15,8 @@ json-markup will take a JSON document and add markup to it so it can be styled i
## Usage
``` js
-var jsonMarkup = require('json-markup');
-
-var html = jsonMarkup({hello:'world'});
-
+var jsonMarkup = require('json-markup')
+var html = jsonMarkup({hello:'world'})
console.log(html);
```
@@ -26,7 +24,28 @@ The above example will print the following HTML
``` html
{
- hello: "world"
+ hello: "world"
+}
+```
+
+If you provide an object map with CSS style then style will be applied inline::
+
+```js
+var jsonMarkup = require('json-markup')
+var css2json = require('css2json')
+var fs = require('fs')
+
+var styleFile = css2json(fs.readFileSync('style.css', 'utf8'))
+var html = jsonMarkup({hello:'world', foo: 'bar'}, styleFile)
+console.log(html)
+```
+
+Now outputs looks like:
+
+```html
+{
+ hello: "world",
+ foo: "bar"
}
```
diff --git a/index.js b/index.js
index 4302911..94fb122 100644
--- a/index.js
+++ b/index.js
@@ -1,68 +1,92 @@
-var INDENT = ' ';
+'use strict'
-var type = function(doc) {
- if (doc === null) return 'null';
- if (Array.isArray(doc)) return 'array';
- if (typeof doc === 'string' && /^https?:/.test(doc)) return 'link';
+var INDENT = ' '
- return typeof doc;
-};
+function inlineRule (objRule) {
+ var str = ''
+ Object.keys(objRule).forEach(function (rule) {
+ str += rule + ':' + objRule[rule] + ';'
+ })
+ return str
+}
-var escape = function(str) {
- return str.replace(//g, '>');
-};
+function Stylize (styleFile) {
+ function styleClass (cssClass) {
+ return 'class="' + cssClass + '"'
+ }
-module.exports = function(doc) {
- var indent = '';
+ function styleInline (cssClass) {
+ return 'style="' + inlineRule(styleFile['.' + cssClass]) + '"'
+ }
- var forEach = function(list, start, end, fn) {
- if (!list.length) return start+' '+end;
+ if (!styleFile) return styleClass
+ return styleInline
+}
- var out = start+'\n';
+function type (doc) {
+ if (doc === null) return 'null'
+ if (Array.isArray(doc)) return 'array'
+ if (typeof doc === 'string' && /^https?:/.test(doc)) return 'link'
- indent += INDENT;
- list.forEach(function(key, i) {
- out += indent+fn(key)+(i < list.length-1 ? ',' : '')+'\n';
- });
- indent = indent.slice(0, -INDENT.length);
+ return typeof doc
+}
- return out + indent+end;
- };
+function escape (str) {
+ return str.replace(//g, '>')
+}
- var visit = function(obj) {
- if (obj === undefined) return '';
+module.exports = function (doc, styleFile) {
+ var indent = ''
+ var style = Stylize(styleFile)
- switch (type(obj)) {
- case 'boolean':
- return ''+obj+'';
+ var forEach = function (list, start, end, fn) {
+ if (!list.length) return start + ' ' + end
- case 'number':
- return ''+obj+'';
+ var out = start + '\n'
- case 'null':
- return 'null';
+ indent += INDENT
+ list.forEach(function (key, i) {
+ out += indent + fn(key) + (i < list.length - 1 ? ',' : '') + '\n'
+ })
+ indent = indent.slice(0, -INDENT.length)
- case 'string':
- return '"'+escape(obj.replace(/\n/g, '\n'+indent))+'"';
+ return out + indent + end
+ }
- case 'link':
- return '"'+escape(obj)+'"';
+ function visit (obj) {
+ if (obj === undefined) return ''
- case 'array':
- return forEach(obj, '[', ']', visit);
+ switch (type(obj)) {
+ case 'boolean':
+ return '' + obj + ''
- case 'object':
- var keys = Object.keys(obj).filter(function(key) {
- return obj[key] !== undefined;
- });
+ case 'number':
+ return '' + obj + ''
- return forEach(keys, '{', '}', function(key) {
- return ''+key + ': '+visit(obj[key]);
- });
- }
+ case 'null':
+ return 'null'
- return '';
- };
+ case 'string':
+ return '"' + escape(obj.replace(/\n/g, '\n' + indent)) + '"'
- return ''+visit(doc)+'
';
-};
+ case 'link':
+ return '"' + escape(obj) + '"'
+
+ case 'array':
+ return forEach(obj, '[', ']', visit)
+
+ case 'object':
+ var keys = Object.keys(obj).filter(function (key) {
+ return obj[key] !== undefined
+ })
+
+ return forEach(keys, '{', '}', function (key) {
+ return '' + key + ': ' + visit(obj[key])
+ })
+ }
+
+ return ''
+ }
+
+ return '' + visit(doc) + '
'
+}