-
Notifications
You must be signed in to change notification settings - Fork 1
/
program.js
73 lines (59 loc) · 1.91 KB
/
program.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"use strict";
var Document = require("wizdom");
module.exports = Program;
function Program() {
Document.call(this);
this.documentElement = this.createElement("body");
}
Program.prototype = Object.create(Document.prototype);
Program.prototype.constructor = Program;
Program.prototype.Element = Section;
Program.prototype.digest = function digest() {
return digestNode(this.documentElement, '');
};
function Section(ownerDocument, tagName) {
Document.prototype.Element.call(this, ownerDocument, tagName);
}
Section.prototype = Object.create(Document.prototype.Element.prototype);
Section.prototype.constructor = Section;
Section.prototype.add = function add(text) {
var child = this.ownerDocument.createTextNode(text);
this.appendChild(child);
return child;
};
Section.prototype.addSection = function addSection(type, name) {
var section = this.ownerDocument.createElement(type);
section.name = name;
this.appendChild(section);
return section;
};
Section.prototype.indent = function indent(name) {
return this.addSection("indent", name);
};
Section.prototype.push = function () {
this.add("parents[parents.length] = parent; parent = node;\n");
};
Section.prototype.pop = function () {
this.add("node = parent; parent = parents[parents.length - 1]; parents.length--;\n");
};
function digestNode(node, prefix) {
if (node.nodeType === 1) {
var string = '';
if (node.tagName === 'indent') {
if (node.name) {
string += prefix + '// ' + node.name + '\n';
}
prefix = prefix + ' ';
}
var currentNode = node.firstChild;
while (currentNode) {
string += digestNode(currentNode, prefix);
currentNode = currentNode.nextSibling;
}
return string;
} else if (node.nodeType === 3) {
return prefix + node.data;
} else {
return "";
}
};