Skip to content

Commit

Permalink
basic
Browse files Browse the repository at this point in the history
  • Loading branch information
voischev committed Oct 20, 2015
1 parent 3daf4bc commit 2e38720
Show file tree
Hide file tree
Showing 15 changed files with 319 additions and 12 deletions.
22 changes: 22 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This file is for unifying the coding style for different editors and IDEs.
# More information at http://EditorConfig.org

# No .editorconfig files above the root directory
root = true

[*]
charset = utf-8
indent_size = 4
end_of_line = lf
indent_style = space
trim_trailing_whitespace = true
insert_final_newline = true

[*.{bemjson.js,deps.js}]
indent_size = 4

[{bower,package}.json]
indent_size = 2

[*.md]
trim_trailing_whitespace = false
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# OS
.DS_Store
._*

# NODEJS
node_modules
npm-debug.log

coverage
61 changes: 61 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"excludeFiles": [
".git/**",
"node_modules/**",
"coverage/**"
],
"fileExtensions": [".js"],
"validateParameterSeparator": ", ",
"disallowSpacesInCallExpression": true,
"disallowSpaceAfterObjectKeys": true,
"disallowSpaceBeforeSemicolon": true,
"disallowSpaceBeforePostfixUnaryOperators": true,
"disallowNewlineBeforeBlockStatements": true,
"disallowMultipleLineBreaks": true,
"disallowTrailingComma": true,
"disallowTrailingWhitespace": true,
"disallowNamedUnassignedFunctions": true,
"disallowSpacesInFunctionDeclaration": {
"beforeOpeningRoundBrace": true
},
"disallowSpacesInFunctionExpression": {
"beforeOpeningRoundBrace": true
},
"disallowSpacesInConditionalExpression": {
"afterTest": true,
"afterConsequent": true
},
"requireSpaceBeforeBlockStatements": 1,
"requireSpaceAfterKeywords": [
"do",
"for",
"if",
"else",
"switch",
"case",
"try",
"catch",
"void",
"while",
"with",
"return",
"typeof"
],
"requireSpaceAfterBinaryOperators": true,
"requireSpacesInForStatement": true,
"requireSpaceBetweenArguments": true,
"requireSemicolons": true,
"requireFunctionDeclarations": true,
"requireCommaBeforeLineBreak": true,
"requireSpacesInConditionalExpression": {
"afterTest": true,
"afterConsequent": true
},
"requireSpacesInFunctionDeclaration": {
"beforeOpeningCurlyBrace": true
},
"requireSpacesInConditionalExpression": {
"beforeConsequent": true,
"beforeAlternate": true
}
}
2 changes: 2 additions & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
coverage
8 changes: 8 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"eqeqeq": true,
"expr": true,
"maxlen": 120,
"undef": true,
"unused": true,
"node": true
}
7 changes: 7 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules/
test/
npm-debug.log
.editorconfig
.jscsrc
.jshintignore
.jshintrc
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
sudo: false
language: node_js
node_js:
- "4.1"
- "0.12"
- "0.10"

env:
global:
- ISTANBUL_COVERAGE: yes

after_success:
- npm i coveralls
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && echo "Coverage data was sent to coveralls!"
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(The MIT License)

Copyright (c) 2015 Ivan Voischev <voischev.ivan@ya.ru>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
# posthtml-parser
[![npm version](https://badge.fury.io/js/posthtml-parser.svg)](http://badge.fury.io/js/posthtml-parser)
[![Build Status](https://travis-ci.org/posthtml/posthtml-parser.svg?branch=master)](https://travis-ci.org/posthtml/posthtml-parser?branch=master)
[![Coverage Status](https://coveralls.io/repos/posthtml/posthtml-parser/badge.svg?branch=master)](https://coveralls.io/r/posthtml/posthtml-parser?branch=master)

Parse HTML/XML to [PostHTMLTree](https://github.com/posthtml/posthtml#posthtml-json-tree-example)

## License

[MIT](LICENSE)
81 changes: 81 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*jshint -W082 */
var htmlparser = require('htmlparser2');

/**
* Parse html to PostHTMLTree
* @param {String} html
* @return {Object}
*/
module.exports = function postHTMLParser(html) {
var bufArray = [],
results = [];

bufArray.last = function() {
return this[this.length - 1];
};

var parser = new htmlparser.Parser({
onprocessinginstruction: function(name, data) {
name === '!doctype' && results.push('<' + data + '>');
},
oncomment: function(data) {
var comment = '<!--' + data + '-->',
last = bufArray.last();

if (!last) {
results.push(comment);
return;
}

last.content || (last.content = []);
last.content.push(comment);
},
onopentag: function(tag, attrs) {
var buf = {};

buf.tag = tag;

if (!isEmpty(attrs)) buf.attrs = attrs;

bufArray.push(buf);
},
onclosetag: function() {
var buf = bufArray.pop();

if (bufArray.length === 0) {
results.push(buf);
return;
}

var last = bufArray.last();
if (!(last.content instanceof Array)) {
last.content = [];
}
last.content.push(buf);
},
ontext: function(text) {
var last = bufArray.last();
if (!last) {
results.push(text);
return;
}

last.content || (last.content = []);
last.content.push(text);
}
});

parser.write(html);
parser.end();

return results;
};

function isEmpty(obj) {
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
return false;
}
}
return true;
}
32 changes: 20 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
{
"name": "posthtml-parser",
"version": "0.0.0",
"description": "Parser for PostHTML",
"version": "0.1.0",
"description": "Parse HTML/XML to PostHTMLTree",
"keywords": [
"html",
"xml",
"postproccessor",
"parser",
"transform",
"transformations",
"preprocessor",
"processor",
"posthtml"
"posthtml",
"posthtmltree"
],
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "npm run lint && npm run coverage",
"lint": "jshint . && jscs -v .",
"coverage": "istanbul cover --report text --report html --report lcov node_modules/mocha/bin/_mocha"
},
"repository": {
"type": "git",
"url": "git+https://github.com/voischev/posthtml-parser.git"
"url": "git+https://github.com/posthtml/posthtml-parser.git"
},
"author": "Ivan Voischev <voischev.ivan@ya.ru>",
"license": "MIT",
"bugs": {
"url": "https://github.com/voischev/posthtml-parser/issues"
"url": "https://github.com/posthtml/posthtml-parser/issues"
},
"homepage": "https://github.com/voischev/posthtml-parser#readme"
"homepage": "https://github.com/posthtml/posthtml-parser#readme",
"dependencies": {
"htmlparser2": "^3.8.3"
},
"devDependencies": {
"chai": "^3.3.0",
"istanbul": "^0.4.0",
"jscs": "^2.3.5",
"jshint": "^2.8.0",
"mocha": "^2.3.3"
}
}
2 changes: 2 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--inline-diffs
--reporter spec
2 changes: 2 additions & 0 deletions test/templates/parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions test/templates/render.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta charset="utf-8">
<title>Html</title>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7, IE=9">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Description">

<meta property="og:url" content="http://github.com/posthtml">
<meta property="og:type" content="website">
<meta property="og:site_name" content="PostHTML">

<link rel="stylesheet" type="text/css" href="path/to/file.css">
<script src="path/to/file.js" type="text/javascript" charset="utf-8"></script>

<script>
console.log('PostHTML!');
</script>
</head>
<body onload="try{if(!google.j.b){document.f&&document.f.q.focus();document.gbqf&&document.gbqf.q.focus();}}catch(e){}if(document.images)new Image().src='/images/nav_logo231.png'">

<h1>Title</h1>
<p>Lorem ipsum dolor sit amet...</p>

<section class="foo" style="color: red;">
<header class="foo bar" style="color: blue; border: 1px solid" id="id">
<div class="foo bar baz" id="idd" data-url="url/to/">
<span id="idd" data-data="{ foo: 'bar' }">
<a href="#">
<img src="path/to/img">
Link
</a>
</span>
</div>
</header>
</section>

<script type="text/javascript">
(function(){function k(a){++b;a=a||window.event;google.iTick(a.target||a.srcElement)}if(google.timers&&google.timers.load.t){var c,b,f;google.c.c.a&&(google.startTick("aft"),google.afte=!1);var g=document.getElementsByTagName("img");c=g.length;for(var d=b=0,a;d<c;++d)if(a=g[d],google.c.c.i&&"none"==a.style.display)++b;else{var h="string"!=typeof a.src||!a.src,e=h||a.complete;google.c.c.d?a.getAttribute("data-deferred")&&(e=!1,a.removeAttribute("data-deferred")):google.c.c.m&&h&&a.getAttribute("data-bsrc")&&
(e=!1);e?++b:google.rll(a,!0,k)}f=c-b;google.rll(window,!1,function(){google.tick("load","ol");google.c.e("load","imc",String(b));google.c.e("load","imn",String(c));google.c.e("load","imp",String(f));google.unblockCSI("load","ol")});google.tick("load",["prt","iml"])}})();
</script>

<script src="path/to/file2.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>
14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var parser = require('..');
var describe = require('mocha').describe;
var it = require('mocha').it;
var expect = require('chai').expect;
var path = require('path');
var fs = require('fs');
var html = fs.readFileSync(path.resolve(__dirname, 'templates/render.html'), 'utf8').toString();
var tree = require('./templates/parser.js');

describe('PostHTML-Parser test', function() {
it('html to tree', function() {
expect(tree).to.eql(parser(html));
});
});

0 comments on commit 2e38720

Please sign in to comment.