Skip to content

Commit

Permalink
Add inline styling support (#7)
Browse files Browse the repository at this point in the history
Updated source code using standard format as well.
  • Loading branch information
Kikobeats authored and mafintosh committed Jul 30, 2016
1 parent c7168b1 commit 3bdc45d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 54 deletions.
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,37 @@ 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);
```

The above example will print the following HTML

``` html
<div class="json-markup">{
<span class="json-markup-key">hello:</span> <span class="json-markup-string">"world"</span>
<span class="json-markup-key">hello:</span> <span class="json-markup-string">"world"</span>
}</div>
```

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
<div style="line-height:17px;font-size:13px;font-family:monospace;white-space:pre;">{
<span style="font-weight:bold;">hello:</span> <span style="color:green;">"world"</span>,
<span style="font-weight:bold;">foo:</span> <span style="color:green;">"bar"</span>
}</div>
```

Expand Down
122 changes: 73 additions & 49 deletions index.js
Original file line number Diff line number Diff line change
@@ -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, '&lt;').replace(/>/g, '&gt;');
};
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, '&lt;').replace(/>/g, '&gt;')
}

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 '<span class="json-markup-bool">'+obj+'</span>';
var forEach = function (list, start, end, fn) {
if (!list.length) return start + ' ' + end

case 'number':
return '<span class="json-markup-number">'+obj+'</span>';
var out = start + '\n'

case 'null':
return '<span class="json-markup-null">null</span>';
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 '<span class="json-markup-string">"'+escape(obj.replace(/\n/g, '\n'+indent))+'"</span>';
return out + indent + end
}

case 'link':
return '<span class="json-markup-string">"<a href="'+escape(obj)+'">'+escape(obj)+'</a>"</span>';
function visit (obj) {
if (obj === undefined) return ''

case 'array':
return forEach(obj, '[', ']', visit);
switch (type(obj)) {
case 'boolean':
return '<span ' + style('json-markup-bool') + '>' + obj + '</span>'

case 'object':
var keys = Object.keys(obj).filter(function(key) {
return obj[key] !== undefined;
});
case 'number':
return '<span ' + style('json-markup-number') + '>' + obj + '</span>'

return forEach(keys, '{', '}', function(key) {
return '<span class="json-markup-key">'+key + ':</span> '+visit(obj[key]);
});
}
case 'null':
return '<span ' + style('json-markup-null') + '>null</span>'

return '';
};
case 'string':
return '<span ' + style('json-markup-string') + '>"' + escape(obj.replace(/\n/g, '\n' + indent)) + '"</span>'

return '<div class="json-markup">'+visit(doc)+'</div>';
};
case 'link':
return '<span ' + style('json-markup-string') + '>"<a href="' + escape(obj) + '">' + escape(obj) + '</a>"</span>'

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 '<span ' + style('json-markup-key') + '>' + key + ':</span> ' + visit(obj[key])
})
}

return ''
}

return '<div ' + style('json-markup') + '>' + visit(doc) + '</div>'
}

0 comments on commit 3bdc45d

Please sign in to comment.