html-code-gen
Advanced tools
Comparing version 0.0.1 to 0.1.0
/** | ||
* @file print-element.js | ||
* @file print method for element | ||
* @author nighca<nighca@live.cn> | ||
@@ -15,5 +15,9 @@ */ | ||
var indent = function(opt){ | ||
return util.indent(opt.level, opt['indent-char'], opt['indent-size']); | ||
}; | ||
var printAttribute = function(attribute){ | ||
// boolean attribute | ||
if(booleanAttributes.indexOf(attribute.name) >= 0){ | ||
if(util.isIn(attribute.name, booleanAttributes)){ | ||
return attribute.name; | ||
@@ -28,11 +32,71 @@ } | ||
return attributes.map(printAttribute).join(' '); | ||
return array.map.call(attributes, printAttribute).join(' '); | ||
}; | ||
var printVoidElementNode = function(info, node, condition){ | ||
return util.format('<${tag}${attributes}>', info); | ||
}; | ||
var printRawTextElementNode = function(info, node, condition){ | ||
var content = node.childNodes[0].textContent.replace(/(^\s*\n)|(\n\s*$)/g, ''); | ||
return [ | ||
info.start, | ||
content, | ||
info.end | ||
].join(info.sep); | ||
}; | ||
var printNormalElementNode = function(info, node, condition){ | ||
// children | ||
var children = array.map.call(node.childNodes, function(childNode, i){ | ||
return print(childNode, newOpt); | ||
}); | ||
// inner content | ||
var content = ( | ||
condition.noFormat ? | ||
children : | ||
children.filter(function(child){ | ||
return child.trim() | ||
}).map(function(child){ | ||
return info.innerIndent + child; | ||
}) | ||
).join(info.sep); | ||
// format & output | ||
return ( | ||
content ? | ||
[ | ||
info.start, | ||
content, | ||
info.end | ||
] : | ||
[ | ||
info.start, | ||
info.end | ||
] | ||
).join(info.sep); | ||
}; | ||
// format method for general element | ||
var printElementNode = function(node, opt){ | ||
// print method for node | ||
var print = require('./print'); | ||
var tag = node.tagName.toLowerCase(), | ||
attributesStr = printAttributes(node.attributes); | ||
// conditions | ||
var condition = { | ||
isVoid: util.isIn(tag, tagTypeMap['void']), | ||
isHtml: tag === 'html', | ||
noFormat: opt['no-format'] || util.isIn(tag, opt['no-format-tag']), | ||
isRawText: util.isIn(tag, tagTypeMap['raw-text']) | ||
}; | ||
// node info | ||
var info = { | ||
indent: indent(opt), | ||
tag: tag, | ||
@@ -43,15 +107,26 @@ attributes: attributesStr ? (' ' + attributesStr) : '' | ||
// void elements | ||
if(tagTypeMap['void'].indexOf(tag) >= 0){ | ||
return util.format('<${tag}${attributes}>', info); | ||
} | ||
if(condition.isVoid) return printVoidElementNode(info, node, condition); | ||
var print = require('./print'); | ||
info.content = array.map.call(node.childNodes, function(childNode){ | ||
return print(childNode, opt); | ||
}).join(''); | ||
// new opt for next-level (child) nodes | ||
var newOpt = util.extend({}, opt); | ||
return util.format('<${tag}${attributes}>${content}</${tag}>', info); | ||
// increase level | ||
// do not indent 'head' & 'body' (under 'html') | ||
if(!condition.isHtml) newOpt.level++; | ||
// tag start & end | ||
util.extend(info, { | ||
start: util.format('<${tag}${attributes}>', info), | ||
end: (condition.noFormat ? '' : info.indent) + util.format('</${tag}>', info), | ||
sep: condition.noFormat ? '' : '\n', | ||
// indent for child nodes | ||
innerIndent: indent(newOpt) | ||
}); | ||
// raw text ( 'script' / 'style' ) | ||
if(condition.isRawText) return printRawTextElementNode(info, node, condition); | ||
return printNormalElementNode(info, node, condition); | ||
}; | ||
module.exports = printElementNode; |
/** | ||
* @file print.js | ||
* @file print methods for different kinds of node | ||
* @author nighca<nighca@live.cn> | ||
@@ -8,2 +8,4 @@ */ | ||
var util = require('./util'); | ||
var array = Array.prototype; | ||
@@ -13,3 +15,3 @@ | ||
var printTextNode = function(node, opt){ | ||
return node.textContent; | ||
return node.textContent.replace(/[\s\n\r]+/g, ' '); | ||
}; | ||
@@ -36,3 +38,5 @@ | ||
return print(childNode, opt); | ||
}).join(''); | ||
}).filter(function(content){ | ||
return content.trim(); | ||
}).join(opt['no-format'] ? '' : '\n'); | ||
}; | ||
@@ -46,5 +50,23 @@ | ||
// default options | ||
opt = util.extend({ | ||
// size of indent | ||
'indent-size': 4, | ||
// char of indent ( space / tab ) | ||
'indent-char': 'space', | ||
// max char num in one line | ||
'max-char': 80, | ||
// tags whose content should not be formatted | ||
'no-format-tag': spec.tagTypeMap.inline, | ||
// no format | ||
'no-format': false, | ||
// special formatters { tagName ( script / style ) : formatter } | ||
'formatter': {}, | ||
// current level | ||
'level': 0 | ||
}, opt); | ||
var typeMap = spec.nodeType; | ||
var output = ''; | ||
var output; | ||
@@ -51,0 +73,0 @@ switch(node.nodeType){ |
/** | ||
* @file spec.js | ||
* @file some spec info | ||
* @author nighca<nighca@live.cn> | ||
@@ -24,3 +24,4 @@ */ | ||
'raw-text': ['script', 'style'], | ||
'escapable-raw-text': ['textarea', 'title'] | ||
'escapable-raw-text': ['textarea', 'title'], | ||
'inline': ['a', 'span', 'img', 'bdo', 'em', 'strong', 'dfn', 'code', 'samp', 'kbd', 'var', 'cite', 'abbr', 'acronym', 'q', 'sub', 'sup', 'tt', 'i', 'b', 'big', 'small', 'u', 's', 'strike', 'font', 'ins', 'del', 'pre', 'address', 'dt', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'] | ||
}; | ||
@@ -27,0 +28,0 @@ |
/** | ||
* @file util.js | ||
* @file util methods | ||
* @author nighca<nighca@live.cn> | ||
*/ | ||
// copy properties from src to target | ||
var extend = function(target, src){ | ||
for(var key in src){ | ||
if(src.hasOwnProperty(key)){ | ||
target[key] = src[key]; | ||
} | ||
} | ||
return target; | ||
}; | ||
// 'a${x}c', {x:'b'} -> 'abc' | ||
@@ -14,4 +24,23 @@ var format = function(template, vars) { | ||
// repeat a string in given times | ||
var repeat = function(str, num){ | ||
return Array.prototype.join.call({ length: num + 1 }, str); | ||
}; | ||
// generate indent content | ||
var indent = function(level, type, size){ | ||
return repeat(type === 'tab' ? '\t' : repeat(' ', size), level); | ||
}; | ||
// is in an array | ||
var isIn = function(obj, arr){ | ||
return arr.indexOf(obj) >= 0; | ||
}; | ||
module.exports = { | ||
format: format | ||
extend: extend, | ||
format: format, | ||
repeat: repeat, | ||
indent: indent, | ||
isIn: isIn | ||
}; |
{ | ||
"name": "html-code-gen", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "", | ||
"main": "index.js", | ||
"bin": { | ||
"html-code-gen": "./bin/html-code-gen" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"coveralls": "^2.11.2", | ||
"fecs": "*", | ||
"htmlcs": "0.0.3", | ||
@@ -18,5 +14,3 @@ "istanbul": "^0.3.2", | ||
"scripts": { | ||
"lint": "fecs lib cli test/**/*.spec.js --type=js", | ||
"coverage": "istanbul cover jasmine-node --captureExceptions test/**/*.spec.js", | ||
"test": "npm run lint && npm run coverage", | ||
"test": "istanbul cover jasmine-node --captureExceptions test/**/*.spec.js", | ||
"coveralls": "cat ./coverage/lcov.info | coveralls" | ||
@@ -23,0 +17,0 @@ }, |
@@ -6,3 +6,3 @@ html-code-gen | ||
[![NPM version](https://badge.fury.io/js/html-code-gen.svg)](http://badge.fury.io/js/html-code-gen) | ||
[![Coverage Status](https://coveralls.io/repos/nighca/html-code-gen/badge.png)](https://coveralls.io/r/nighca/html-code-gen) | ||
[![Coverage Status](https://coveralls.io/repos/nighca/html-code-gen/badge.svg?branch=master)](https://coveralls.io/r/nighca/html-code-gen?branch=master) | ||
[![Dependencies](http://img.shields.io/david/nighca/html-code-gen.svg?style=flat-square)](https://david-dm.org/nighca/html-code-gen) | ||
@@ -19,6 +19,34 @@ [![DevDependencies](http://img.shields.io/david/dev/nighca/html-code-gen.svg?style=flat-square)](https://david-dm.org/nighca/html-code-gen) | ||
```javascript | ||
var genner = require('html-code-gen'); | ||
```javascript | ||
var genner = require('html-code-gen'), | ||
output = genner.print(dom, opt); | ||
``` | ||
### Options | ||
var output = genner.print(dom, opt); | ||
``` | ||
* `indent-size`: size of indent | ||
default: `4` | ||
* `indent-char`: char of indent ( space / tab ) | ||
default: `'space'` | ||
* `max-char`: max char num in one line | ||
default: `80` (TODO) | ||
* `no-format-tag`: tags whose content should not be formatted | ||
default: [`spec.tagTypeMap.inline`](./lib/spec.js#L25) | ||
* `no-format`: no format | ||
default: `false` | ||
* `formatter`: special formatters { tagName ( script / style ) : formater ) | ||
default: `{}` | ||
* `level`: current level | ||
default: `0` |
72623
4
15
332
51