Skip to content
This repository was archived by the owner on Feb 23, 2025. It is now read-only.

Commit fb99db0

Browse files
committed
v0.0.3
1 parent ee070b6 commit fb99db0

File tree

10 files changed

+517
-348
lines changed

10 files changed

+517
-348
lines changed

README.md

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
1-
# handlebars-react
2-
> Compile Handlebars templates to [React](https://facebook.github.io/react/).
1+
# handlebars-html-parser
2+
> Parse Handlebars and HTML.
3+
4+
Parse this:
5+
```handlebars
6+
<{{tag}}>value</{{tag}}>
7+
```
8+
into this:
9+
```js
10+
[
11+
{ type:"htmlTagStart", closing:false },
12+
{ type:"htmlTagNameStart" },
13+
{ type:"hbsVariable", id:"tag" },
14+
{ type:"htmlTagNameEnd" },
15+
{ type:"text", text:"value" },
16+
{ type:"htmlTagStart", closing:true },
17+
{ type:"htmlTagNameStart" },
18+
{ type:"hbsVariable", id:"tag" },
19+
{ type:"htmlTagNameEnd" },
20+
{ type:"htmlTagEnd" }
21+
]
22+
```
23+
…for use in compiling to something like a virtual DOM, such that of [handlebars-react](https://github.com/stevenvachon/handlebars-react).
24+
25+
For more information on the strengths, weaknesses and implementation details of this library, check out the [wiki](https://github.com/stevenvachon/handlebars-html-parser/wiki).
326

427
## FAQ
5-
1. **Why not use [parse5](https://npmjs.com/package/parse5)?**
28+
1. **How is this different from [HTMLBars](https://github.com/tildeio/htmlbars)?**
29+
HTMLBars *builds* a DOM whereas this library *allows* you to build a DOM and even a virtual DOM.
30+
31+
2. **Why not use [parse5](https://npmjs.com/package/parse5)?**
632
As of v1.2.0, it is not forgiving enough, in that it will parse `<{{tag}}>asdf</{{tag}}>` as text instead of HTML.
733

834
## Roadmap Features
@@ -11,12 +37,5 @@ As of v1.2.0, it is not forgiving enough, in that it will parse `<{{tag}}>asdf</
1137
* `options.ignoreComments` to omit html comments from compiling
1238
* `options.mustacheOnly` that disables helpers
1339

14-
## api idea
15-
https://github.com/isaacs/sax-js#usage
16-
17-
## ast type ideas
18-
https://github.com/blakeembrey/dombars/blob/master/lib/compiler/base.js
19-
https://github.com/mikepb/jade-react-compiler/blob/master/lib/builder.js
20-
21-
## template idea
22-
https://github.com/duncanbeevers/jade-react
40+
## Changelog
41+
* 0.0.1–0.0.3 pre-releases

lib/NodeTypes.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
module.exports =
3+
{
4+
HBS_COMMENT_END: "hbsCommentEnd",
5+
HBS_COMMENT_START: "hbsCommentStart",
6+
HBS_HELPER_END: "hbsHelperEnd",
7+
HBS_HELPER_START: "hbsHelperStart",
8+
HBS_SECTION_END: "hbsSectionEnd",
9+
HBS_SECTION_START: "hbsSectionStart",
10+
HBS_VARIABLE: "hbsVariable",
11+
12+
HTML_ATTR_END: "htmlAttrEnd",
13+
HTML_ATTR_NAME_END: "htmlAttrNameEnd",
14+
HTML_ATTR_NAME_START: "htmlAttrNameStart",
15+
HTML_ATTR_START: "htmlAttrStart",
16+
HTML_ATTR_VALUE_END: "htmlAttrValueEnd",
17+
HTML_ATTR_VALUE_START: "htmlAttrValueStart",
18+
HTML_COMMENT_END: "htmlCommentEnd",
19+
HTML_COMMENT_START: "htmlCommentStart",
20+
HTML_TAG_END: "htmlTagEnd",
21+
HTML_TAG_NAME_END: "htmlTagNameEnd",
22+
HTML_TAG_NAME_START: "htmlTagNameStart",
23+
HTML_TAG_START: "htmlTagStart",
24+
25+
TEXT: "text"
26+
};

lib/Nodes.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
//"use strict";
2+
var NodeTypes = require("./NodeTypes");
3+
var Nodes = {};
4+
5+
for (var i in NodeTypes)
6+
{
7+
if (NodeTypes.hasOwnProperty(i) === false) continue;
8+
9+
switch (NodeTypes[i])
10+
{
11+
case NodeTypes.HBS_HELPER_END:
12+
{
13+
Nodes[i] = function(statement)
14+
{
15+
return {
16+
type: NodeTypes.HBS_HELPER_END,
17+
id: statement.mustache.id.string,
18+
//statement: statement
19+
};
20+
};
21+
break;
22+
}
23+
24+
25+
case NodeTypes.HBS_HELPER_START:
26+
{
27+
Nodes[i] = function(statement)
28+
{
29+
return {
30+
type: NodeTypes.HBS_HELPER_START,
31+
id: statement.mustache.id.string,
32+
params: statement.mustache.params.map( function(param){ return param.string }),
33+
//statement: statement
34+
};
35+
};
36+
break;
37+
}
38+
39+
40+
case NodeTypes.HBS_SECTION_END:
41+
{
42+
Nodes[i] = function(statement)
43+
{
44+
return {
45+
type: NodeTypes.HBS_SECTION_END,
46+
id: statement.mustache.id.string,
47+
//statement: statement
48+
};
49+
};
50+
break;
51+
}
52+
53+
54+
case NodeTypes.HBS_SECTION_START:
55+
{
56+
Nodes[i] = function(statement)
57+
{
58+
return {
59+
type: NodeTypes.HBS_SECTION_START,
60+
id: statement.mustache.id.string,
61+
params: statement.mustache.params.parts,// ?
62+
//statement: statement
63+
};
64+
};
65+
break;
66+
}
67+
68+
69+
case NodeTypes.HBS_VARIABLE:
70+
{
71+
Nodes[i] = function(statement)
72+
{
73+
return {
74+
type: NodeTypes.HBS_VARIABLE,
75+
id: statement.id.string,
76+
//statement: statement
77+
};
78+
};
79+
break;
80+
}
81+
82+
83+
case NodeTypes.HTML_TAG_START:
84+
{
85+
Nodes[i] = function(closing)
86+
{
87+
return {
88+
type: NodeTypes.HTML_TAG_START,
89+
closing: !!closing
90+
};
91+
};
92+
break;
93+
}
94+
95+
96+
case NodeTypes.TEXT:
97+
{
98+
Nodes[i] = function(text)
99+
{
100+
return {
101+
type:NodeTypes.TEXT,
102+
text:text
103+
};
104+
};
105+
break;
106+
}
107+
108+
109+
default:
110+
{
111+
// Simple types are references for speed
112+
Nodes[i] = { type:NodeTypes[i] };
113+
}
114+
}
115+
}
116+
117+
118+
119+
module.exports = Nodes;

lib/devlog.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
"use strict";
2+
// TODO :: call this debug-inspect? a plugin for debug package?
3+
var util = require("util");
4+
5+
var trimPattern = new RegExp([
6+
"^(\\[) ?",// group $1, must start with "[" or "[ "
7+
"([\\s\\S]*?)",// group $2, any char(s) 0 or more times (matching the least amount possible)
8+
"(?:",// non-capturing group
9+
",?\\s*",// optional ", " or ", " etc
10+
"\\[length]: ",// array length for `all()`
11+
"(?:\\x1B\\[\\d+m)?",// optional ansi color start
12+
"\\d+",// number(s)
13+
"(?:\\x1B\\[\\d+m)?",// optional ansi color end
14+
")?",// optional
15+
" ?(])$"// group $3, must end in "]" or " ]"
16+
].join(""));
17+
18+
19+
20+
function log(args, options)
21+
{
22+
var data = parseArgs(args);
23+
var str = util.inspect(data.args, options);
24+
str = trim(str, data.trimLength, data.unencapsulate);
25+
26+
console.log(str);
27+
}
28+
29+
30+
31+
function parseArgs(args)
32+
{
33+
var result, trimLength=false, unencapsulate=false;
34+
35+
switch (args.length)
36+
{
37+
case 0:
38+
{
39+
result = "";
40+
break;
41+
}
42+
case 1:
43+
{
44+
result = args[0];
45+
46+
if (Object.prototype.toString.call(result) === "[object Array]")
47+
{
48+
trimLength = true;
49+
unencapsulate = result.length > 0;
50+
}
51+
break;
52+
}
53+
default:
54+
{
55+
// Return regular array instead of special arguments object
56+
var argsArray = [];
57+
for (var i=0; i<args.length; i++)
58+
{
59+
argsArray.push( args[i] );
60+
}
61+
62+
result = argsArray;
63+
trimLength = true;
64+
unencapsulate = true;
65+
}
66+
}
67+
68+
return { args:result, trimLength:trimLength, unencapsulate:unencapsulate };
69+
}
70+
71+
72+
73+
function trim(str, trimLength, unencapsulate)
74+
{
75+
if (trimLength===true || unencapsulate===true)
76+
{
77+
var groups = (unencapsulate===true) ? "$2" : "$1$2$3";
78+
79+
return str.replace(trimPattern, groups);
80+
}
81+
else
82+
{
83+
return str;
84+
}
85+
}
86+
87+
88+
89+
// Public API
90+
91+
92+
93+
function inspect()
94+
{
95+
log.apply(null, [arguments, {depth:null, colors:true}]);
96+
}
97+
98+
99+
100+
inspect.all = function()
101+
{
102+
log.apply(null, [arguments, {depth:null, showHidden:true, colors:true}]);
103+
};
104+
105+
106+
107+
inspect.basic = function()
108+
{
109+
console.log.apply(null, arguments);
110+
};
111+
112+
113+
114+
/*inspect("asdf");
115+
inspect("asdf", 123);
116+
inspect("asdf");
117+
118+
console.log("==========");
119+
120+
inspect.all( { asdf1:{asdf:{asdf:["asdf"]}}, asdf2:"asdf" }, ["asdf"] );
121+
console.log( { asdf1:{asdf:{asdf:["asdf"]}}, asdf2:"asdf" }, ["asdf"] );
122+
123+
console.log("==========");
124+
125+
inspect.all([]);
126+
inspect([]);
127+
console.log( util.inspect([], {depth:null, colors:true}) );*/
128+
129+
130+
131+
module.exports = inspect;

0 commit comments

Comments
 (0)