-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroost.js
183 lines (174 loc) · 7.33 KB
/
roost.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
function beautify(htmlString) {
let indentation = 2, output = "";
const regex = /(<\/?\w+(?:\s+\w+(?:\s*=\s*(?:".*?"|'.*?'|[\^'">\s]+))?)*\s*\/?>)|([^<]+)/g, matches = htmlString.matchAll(regex);
for (const match of matches) {
const tag = match[1], text = match[2];
if (tag) {
const isClosingTag = tag.startsWith("</");
if (isClosingTag) indentation--;
output += " ".repeat(indentation) + tag + "\n";
if (!isClosingTag) indentation++;
} else {
output += " ".repeat(indentation) + text.trim() + "\n";
}
}
return output.trim();
}
function generateElement(tag, attributes, content) {
let element = `<${tag}`;
for (let attribute in attributes) {
if (attributes.hasOwnProperty(attribute)) {
let value = attributes[attribute];
if (typeof value === "string") {
value = value.replace(/"/g, """);
}
element += ` ${attribute}="${value}"`;
}
}
if (content === undefined || content === null) {
element += `>`;
} else {
element += `>${content}</${tag}>`;
}
return element;
}
function JHTMLtoHTML(jhtml, level = 0) {
let html = "";
for (let element in jhtml) {
if (jhtml.hasOwnProperty(element)) {
let tag = element.split("-")[0], id = element.split("-")[1];
if (id === undefined) {
id = "0";
}
let attributes = jhtml[element], content = "";
if (typeof attributes === "object") {
if (attributes.hasOwnProperty("content")) {
if (typeof attributes["content"] === "object") {
content = JHTMLtoHTML(attributes["content"], level + 1);
} else {
content = attributes["content"];
}
delete attributes["content"];
}
html += `${generateElement(element, attributes, content)}`;
} else if (tag !== "img") {
content = attributes;
let indent = " ".repeat(level);
if (tag === "head" && attributes.hasOwnProperty("content")) {
html += `${indent}<${tag}>\n`;
let headContent = attributes["content"];
for (let contentElement in headContent) {
if (headContent.hasOwnProperty(contentElement)) {
let contentTag = contentElement.split("-")[0], contentId = contentElement.split("-")[1];
if (contentId === undefined) {
contentId = "0";
}
let contentAttributes = headContent[contentElement], contentContent = "";
if (typeof contentAttributes === "object") {
if (contentAttributes.hasOwnProperty("content")) {
if (typeof contentAttributes["content"] === "object") {
contentContent = JHTMLtoHTML(contentAttributes["content"], level + 2);
} else {
contentContent = contentAttributes["content"];
}
delete contentAttributes["content"];
}
html += `${indent} ${generateElement(contentElement, contentAttributes, contentContent)}`;
}
}
}
html += `${indent}</${tag}>\n`;
} else {
html += `\n${indent}<${tag} ${stringifyAttributes(attributes)}>${content}`;
if (tag !== "br") {
html += `${JHTMLtoHTML({ [`/${element}`]: {} }, level + 1)}`;
}
}
} else {
let indent = " ".repeat(level);
html += `\n${indent}<${tag} ${stringifyAttributes(attributes)}>`;
}
}
}
html = html.replace(/-\d+/g, "");
if (level === 0) {
html += "\n";
}
const tml = beautify(html);
return tml;
}
function stringifyAttributes(attributes) {
let attrStr = "", value = ""
for (let attr in attributes) {
if (attributes.hasOwnProperty(attr)) {
value = attributes[attr];
if (typeof value === "string") {
value = value.replace(/"/g, '\\"');
attrStr += `${attr}="${value}" `;
} else {
attrStr += `${attr}=${value} `;
}
}
}
return attrStr.trim();
}
const roost = {
alerts: [],
compile: (js) => {
let html = "";
if (js.doctype === true || js.doctype === "html" && !js.lang === undefined) {
html = `<!DOCTYPE HTML>\n<html lang="${js.lang}">\n`;
} else if (js.lang === undefined) {
err("[roost] no lang selected, selecting en");
html = `<!DOCTYPE HTML>\n<html lang="en">\n`;
} else {
err("[roost] quirks mode detected");
}
if (js.head === undefined) {
err("[roost] no head detected");
html = `${html} <head>\n`;
} else {
html = `${html} <head>\n`;
if (js.head.metaTags === undefined || true || false) {
if (js.head.metaTags === undefined) {
console.log("[roost] No meta tags template detected");
}
if (!js.head.metaTags === false) {
html = `${html} <meta charset="UTF-8">\n <meta http-equiv="X-UA-Compatible" content="IE=edge">\n <meta name="viewport" content="width=device-width, initial-scale=1.0">\n`;
}
}
if (js.head.title === undefined) {
err("[roost] no title detected");
} else {
html = `${html} <title>${js.head.title}</title>\n`;
}
if (js.head.content === !undefined) {
html = `${html} ${JHTMLtoHTML(js.head.content)}\n`
}
if (js.stylesheets) {
js.stylesheets.forEach(element => {
html = `${html} <link rel="stylesheet" href="${element}">\n`
});
}
if (js.scripts) {
js.scripts.forEach(element => {
html = `${html} <script type="module" src="${element}"></script>\n`
});
}
html = `${html} <!-- HTML Document generated by Roost. -->\n </head>\n`
}
if (js.body === undefined) {
err("[roost] no body detected");
html = `${html} <body>\n <h1>Website generated by Roost</h1>\n </body>`;
} else {
html = `${html} <body>\n ${JHTMLtoHTML(js.body)}\n </body>\n`;
}
html = `${html}</html>`;
return html;
function err(err) {
console.log(err)
roost.alerts.push(err);
}
}
};
export default roost;