html-component-engine 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -36
- package/bin/cli.js +529 -529
- package/package.json +37 -37
- package/src/engine/compiler.js +403 -312
- package/src/engine/utils.js +76 -74
package/src/engine/utils.js
CHANGED
|
@@ -1,74 +1,76 @@
|
|
|
1
|
-
// Utility functions for the HTML Component Engine
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Parse a self-closing Component tag to extract attributes
|
|
5
|
-
* @param {string} tag - The full component tag, e.g., <Component src="Button" text="Click" />
|
|
6
|
-
* @returns {object|null} - Object with attribute key-value pairs, or null if invalid
|
|
7
|
-
*/
|
|
8
|
-
export function parseSelfClosingComponentTag(tag) {
|
|
9
|
-
const regex = /<Component\s+([^>]+)\s*\/>/;
|
|
10
|
-
const match = tag.match(regex);
|
|
11
|
-
if (!match) return null;
|
|
12
|
-
|
|
13
|
-
const attrs = {};
|
|
14
|
-
// Support hyphenated attributes
|
|
15
|
-
const attrRegex = /([\w-]+)
|
|
16
|
-
let attrMatch;
|
|
17
|
-
while ((attrMatch = attrRegex.exec(match[1])) !== null) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
*
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
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
|
-
|
|
1
|
+
// Utility functions for the HTML Component Engine
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Parse a self-closing Component tag to extract attributes
|
|
5
|
+
* @param {string} tag - The full component tag, e.g., <Component src="Button" text="Click" />
|
|
6
|
+
* @returns {object|null} - Object with attribute key-value pairs, or null if invalid
|
|
7
|
+
*/
|
|
8
|
+
export function parseSelfClosingComponentTag(tag) {
|
|
9
|
+
const regex = /<Component\s+([^>]+)\s*\/>/;
|
|
10
|
+
const match = tag.match(regex);
|
|
11
|
+
if (!match) return null;
|
|
12
|
+
|
|
13
|
+
const attrs = {};
|
|
14
|
+
// Support hyphenated attributes with both quote styles and boolean attributes
|
|
15
|
+
const attrRegex = /([\w-]+)(?:=(?:"([^"]*)"|'([^']*)'))?/g;
|
|
16
|
+
let attrMatch;
|
|
17
|
+
while ((attrMatch = attrRegex.exec(match[1])) !== null) {
|
|
18
|
+
const key = attrMatch[1];
|
|
19
|
+
const value = attrMatch[2] ?? attrMatch[3] ?? 'true';
|
|
20
|
+
attrs[key] = value;
|
|
21
|
+
}
|
|
22
|
+
return attrs;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Parse a Component tag to extract attributes (legacy alias)
|
|
27
|
+
* @param {string} tag - The full component tag
|
|
28
|
+
* @returns {object|null} - Object with attribute key-value pairs, or null if invalid
|
|
29
|
+
*/
|
|
30
|
+
export function parseComponentTag(tag) {
|
|
31
|
+
return parseSelfClosingComponentTag(tag);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Parse variants from component HTML
|
|
36
|
+
* Looks for <!-- variants: primary=class1 class2, secondary=class3 -->
|
|
37
|
+
* @param {string} html - The component HTML
|
|
38
|
+
* @returns {object} - Map of variant name to classes
|
|
39
|
+
*/
|
|
40
|
+
export function parseVariants(html) {
|
|
41
|
+
const variants = {};
|
|
42
|
+
// Match HTML comment with variants - use non-greedy match
|
|
43
|
+
const regex = /<!--\s*variants:\s*(.+?)\s*-->/;
|
|
44
|
+
const match = html.match(regex);
|
|
45
|
+
if (match) {
|
|
46
|
+
const variantsStr = match[1];
|
|
47
|
+
const variantPairs = variantsStr.split(',');
|
|
48
|
+
for (const pair of variantPairs) {
|
|
49
|
+
const [name, ...classParts] = pair.split('=');
|
|
50
|
+
const classes = classParts.join('='); // Handle = in class names (unlikely but safe)
|
|
51
|
+
if (name && classes) {
|
|
52
|
+
variants[name.trim()] = classes.trim();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return variants;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Clean unused placeholders from compiled HTML
|
|
61
|
+
* @param {string} html - The compiled HTML
|
|
62
|
+
* @returns {string} - Cleaned HTML
|
|
63
|
+
*/
|
|
64
|
+
export function cleanUnusedPlaceholders(html) {
|
|
65
|
+
// Remove any remaining {{ ... }} placeholders
|
|
66
|
+
return html.replace(/\{\{\s*\w+\s*\}\}/g, '');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Normalize path separators to forward slashes
|
|
71
|
+
* @param {string} p - Path to normalize
|
|
72
|
+
* @returns {string} - Normalized path
|
|
73
|
+
*/
|
|
74
|
+
export function normalizePath(p) {
|
|
75
|
+
return p.replace(/\\/g, '/');
|
|
76
|
+
}
|