Skip to content

Commit 4268bcf

Browse files
author
soliury
committed
init
0 parents  commit 4268bcf

File tree

9 files changed

+11008
-0
lines changed

9 files changed

+11008
-0
lines changed

.editorconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 4
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.md]
13+
trim_trailing_whitespace = false
14+
15+
[test/fixtures/*]
16+
insert_final_newline = false
17+
trim_trailing_whitespace = false

.eslintrc

Lines changed: 379 additions & 0 deletions
Large diffs are not rendered by default.

.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# OSX
2+
#
3+
.DS_Store
4+
5+
# Xcode
6+
#
7+
build/
8+
*.pbxuser
9+
!default.pbxuser
10+
*.mode1v3
11+
!default.mode1v3
12+
*.mode2v3
13+
!default.mode2v3
14+
*.perspectivev3
15+
!default.perspectivev3
16+
xcuserdata
17+
*.xccheckout
18+
*.moved-aside
19+
DerivedData
20+
*.hmap
21+
*.ipa
22+
*.xcuserstate
23+
project.xcworkspace
24+
25+
# node.js
26+
#
27+
node_modules/
28+
npm-debug.log
29+
30+
.idea
31+
32+
app/mock/user.js
33+
34+
react-packager-cache**
35+
.rnplay
36+
tmp
37+
38+

README.md

Whitespace-only changes.

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib/htmlRender')

lib/htmlParse.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
var htmlparser = require('./htmlparser2')
2+
3+
4+
var blockTagArr = ['div', 'p', 'img', 'address',
5+
'blockquote', 'dir', 'dl',
6+
'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
7+
'menu', 'ol', 'pre', 'table', 'ul', 'li', 'hr']
8+
9+
var inlineTagArr = ['a', 'abbr', 'b', 'big',
10+
'br', 'cite', 'code', 'em', 'label', 'span', 'strong']
11+
12+
13+
function indexOf(item, arr) {
14+
for (var i = 0; i < arr.length; i++) {
15+
if (item == arr[i]) return true
16+
}
17+
return false
18+
}
19+
20+
21+
var parseHtml = function (html, done) {
22+
var rootStack = [{
23+
name: 'div'
24+
}]
25+
var tagStack = [rootStack[0]]
26+
27+
var opts = {
28+
recognizeSelfClosing: true,
29+
lowerCaseAttributeNames: true,
30+
lowerCaseTags: true,
31+
decodeEntities: true
32+
}
33+
34+
var parser = new htmlparser.Parser({
35+
onopentag: function (name, attribs) {
36+
var parent = tagStack[tagStack.length - 1]
37+
if (!parent.children) parent.children = []
38+
39+
if (indexOf(name, blockTagArr) == 1) {
40+
parent.children.push({
41+
name: name,
42+
attribs: attribs,
43+
type: 'block',
44+
parent: parent
45+
})
46+
tagStack.push(parent.children[parent.children.length - 1])
47+
}
48+
else {
49+
var type = 'inline'
50+
if (parent.name == 'pre') type = 'block'
51+
52+
53+
parent.children.push({
54+
name: name,
55+
attribs: attribs,
56+
type: type,
57+
parent: parent
58+
})
59+
tagStack.push(parent.children[parent.children.length - 1])
60+
}
61+
},
62+
ontext: function (text) {
63+
if (text == '\n') return;
64+
var parent = tagStack[tagStack.length - 1]
65+
if (!parent.children) parent.children = []
66+
67+
parent.children.push({
68+
name: 'text',
69+
text: text,
70+
parent: parent,
71+
type: 'inline'
72+
});
73+
},
74+
onclosetag: function (name) {
75+
tagStack.pop()
76+
},
77+
onend: function () {
78+
done(rootStack[0].children)
79+
}
80+
}, opts)
81+
82+
parser.write(html)
83+
parser.end()
84+
}
85+
86+
87+
module.exports = parseHtml
88+
89+
90+

0 commit comments

Comments
 (0)