-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdjango.mjs
More file actions
155 lines (143 loc) · 4.13 KB
/
django.mjs
File metadata and controls
155 lines (143 loc) · 4.13 KB
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
const autoSelfClosingTags = [
"area",
"base",
"br",
"col",
"embed",
"hr",
"img",
"input",
"keygen",
"link",
"meta",
"param",
"source",
"track",
"wbr",
];
export function replaceDjangoTags(text) {
const djangoTags = {};
let index = 0;
let currentNesting = 0;
let nesting = [0];
const stateStack = [
{
mode: "default",
data: {},
},
];
// Parse the html, and calculate the nesting level of each row + find each django tag
for (let i = 0; i < text.length; i++) {
const char = text[i];
const nextChar = text[i + 1];
const prevChar = text[i - 1];
const state = stateStack[stateStack.length - 1];
if (char === "\n") {
nesting.push(currentNesting);
}
const isDjangoTagOpening =
char === "{" && (nextChar === "%" || nextChar === "{");
const pushDjangoTagState = (replaceType) =>
stateStack.push({
mode: "djangoTag",
data: { type: nextChar, replaceType },
start: i,
});
const isStringOpening = char === '"' || char === "'";
const pushStringState = () =>
stateStack.push({
mode: "string",
data: { type: char },
start: i,
});
switch (state.mode) {
case "default":
if (isDjangoTagOpening) {
pushDjangoTagState("comment");
}
// Match HTML tags
else if (char === "<" && nextChar !== "!" && nextChar !== "?") {
const isClosing = nextChar === "/";
// use regex to find the tag name
const tagName = text.match(/<\s*\/?\s*(\w+)/)?.[1].toLowerCase();
stateStack.push({
mode: "htmlTag",
data: {
isClosing,
isAutoSelfClosing: autoSelfClosingTags.includes(tagName),
},
start: i,
});
if (!isClosing) nesting[nesting.length - 1] = ++currentNesting;
}
break;
case "djangoTag":
// First check if we are entering a string
if (isStringOpening) {
pushStringState();
}
// Then check if we are exiting the tag
else if (
(state.data.type === "%" && char === "%" && nextChar === "}") ||
(state.data.type === "{" && char === "}" && nextChar === "}")
) {
const start = state.start;
const end = i + 2;
const tag = text.slice(start, end);
const key =
state.data.replaceType === "comment"
? `<!--__DJANGO_TAG_${index++}__-->`
: `__DJANGO_TAG_${index++}__`;
djangoTags[key] = { tag, start, end, key };
stateStack.pop();
i++;
}
break;
case "string":
// If the previous stack's state is djangoTag, then don't match new django tags, otherwise do
if (
stateStack[stateStack.length - 2]?.mode !== "djangoTag" &&
isDjangoTagOpening
) {
pushDjangoTagState("attribute");
} else if (char === state.data.type && prevChar !== "\\") {
stateStack.pop();
}
break;
case "htmlTag":
// Match django tags and attribute names
if (isDjangoTagOpening) {
pushDjangoTagState("attribute");
}
// Match strings
else if (isStringOpening) {
pushStringState();
}
// Match closing of the tag
else if (char === ">") {
// If it was self closing or closing, then we need to go back a nesting level
if (
state.data.isAutoSelfClosing ||
state.data.isClosing ||
prevChar === "/"
) {
currentNesting--;
}
stateStack.pop();
}
break;
}
}
// Build the string with placeholders and the html text between them
let newString = [];
let lastIndex = 0;
for (const tag of Object.values(djangoTags)) {
newString.push(text.slice(lastIndex, tag.start));
newString.push(tag.key);
lastIndex = tag.end;
}
// Add the last part of the string
newString.push(text.slice(lastIndex));
// console.log(newString);
return { newString: newString.join(""), djangoTags, nesting };
}