shopware-twig-parser 0.1.0
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/LICENSE.md +21 -0
- package/README.md +25 -0
- package/dist/commonjs/index.d.ts +24 -0
- package/dist/commonjs/index.d.ts.map +1 -0
- package/dist/commonjs/index.js +87 -0
- package/dist/commonjs/index.js.map +1 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/esm/index.d.ts +24 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +81 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/package.json +50 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Nils Haberkamp
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# shopware-twig-parser
|
|
2
|
+
|
|
3
|
+
A parser for the Shopware 6 Twig admin markup language.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install shopware-twig-parser
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { parse } from "shopware-twig-parser";
|
|
15
|
+
|
|
16
|
+
const tree = parse("{% block my_block %}Hello, world!{% endblock %}");
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### Author
|
|
20
|
+
|
|
21
|
+
Hey, I'm Nils. In my spare time [I write about things](https://www.haberkamp.dev/) I learned or I create open source packages, that help me (and hopefully you) to build better apps.
|
|
22
|
+
|
|
23
|
+
## Feedback and Contributing
|
|
24
|
+
|
|
25
|
+
I highly appreceate your feedback! Please create an [issue](https://github.com/Haberkamp/shopware-twig-parser/issues/new), if you've found any bugs or want to request a feature.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
type Tree = {
|
|
2
|
+
rootNode: TemplateNode;
|
|
3
|
+
};
|
|
4
|
+
type TemplateNode = {
|
|
5
|
+
type: "template";
|
|
6
|
+
children: TwigStatementDirectiveNode[];
|
|
7
|
+
};
|
|
8
|
+
type TwigStatementDirectiveNode = {
|
|
9
|
+
type: "twig_statement_directive";
|
|
10
|
+
tag: TwigTagNode;
|
|
11
|
+
variable: TwigVariableNode;
|
|
12
|
+
children: TwigStatementDirectiveNode[];
|
|
13
|
+
};
|
|
14
|
+
type TwigTagNode = {
|
|
15
|
+
type: "twig_tag";
|
|
16
|
+
name: string;
|
|
17
|
+
};
|
|
18
|
+
type TwigVariableNode = {
|
|
19
|
+
type: "twig_variable";
|
|
20
|
+
content: string;
|
|
21
|
+
};
|
|
22
|
+
export declare function parse(content: string): Tree;
|
|
23
|
+
export {};
|
|
24
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAGA,KAAK,IAAI,GAAG;IACV,QAAQ,EAAE,YAAY,CAAC;CACxB,CAAC;AAEF,KAAK,YAAY,GAAG;IAClB,IAAI,EAAE,UAAU,CAAC;IACjB,QAAQ,EAAE,0BAA0B,EAAE,CAAC;CACxC,CAAC;AAEF,KAAK,0BAA0B,GAAG;IAChC,IAAI,EAAE,0BAA0B,CAAC;IACjC,GAAG,EAAE,WAAW,CAAC;IACjB,QAAQ,EAAE,gBAAgB,CAAC;IAC3B,QAAQ,EAAE,0BAA0B,EAAE,CAAC;CACxC,CAAC;AAEF,KAAK,WAAW,GAAG;IACjB,IAAI,EAAE,UAAU,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,KAAK,gBAAgB,GAAG;IACtB,IAAI,EAAE,eAAe,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAMF,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CA2F3C"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.parse = parse;
|
|
7
|
+
const tree_sitter_1 = __importDefault(require("tree-sitter"));
|
|
8
|
+
const tree_sitter_shopware_twig_1 = __importDefault(require("tree-sitter-shopware-twig"));
|
|
9
|
+
const parser = new tree_sitter_1.default();
|
|
10
|
+
// @ts-expect-error
|
|
11
|
+
parser.setLanguage(tree_sitter_shopware_twig_1.default);
|
|
12
|
+
function parse(content) {
|
|
13
|
+
const tree = parser.parse(content);
|
|
14
|
+
function convertNode(node) {
|
|
15
|
+
if (node.type === "statement_directive") {
|
|
16
|
+
const tagStatement = node.children.find((child) => child.type === "tag_statement");
|
|
17
|
+
if (tagStatement) {
|
|
18
|
+
const tagNode = tagStatement.children.find((child) => child.type === "tag");
|
|
19
|
+
const variableNode = tagStatement.children.find((child) => child.type === "variable");
|
|
20
|
+
if (tagNode && tagNode.text === "block" && variableNode) {
|
|
21
|
+
return {
|
|
22
|
+
type: "block",
|
|
23
|
+
name: variableNode.text,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
else if (tagNode && tagNode.text === "endblock") {
|
|
27
|
+
return {
|
|
28
|
+
type: "endblock",
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
// First pass: convert all nodes to intermediate format
|
|
36
|
+
const rawNodes = [];
|
|
37
|
+
for (const child of tree.rootNode.children) {
|
|
38
|
+
const converted = convertNode(child);
|
|
39
|
+
if (converted) {
|
|
40
|
+
rawNodes.push(converted);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// Second pass: build nested structure using a stack
|
|
44
|
+
function buildNestedStructure(nodes) {
|
|
45
|
+
const result = [];
|
|
46
|
+
const stack = [];
|
|
47
|
+
for (const node of nodes) {
|
|
48
|
+
if (node.type === "block") {
|
|
49
|
+
const blockNode = {
|
|
50
|
+
tag: {
|
|
51
|
+
type: "twig_tag",
|
|
52
|
+
name: "block",
|
|
53
|
+
},
|
|
54
|
+
type: "twig_statement_directive",
|
|
55
|
+
variable: {
|
|
56
|
+
type: "twig_variable",
|
|
57
|
+
content: node.name,
|
|
58
|
+
},
|
|
59
|
+
children: [],
|
|
60
|
+
};
|
|
61
|
+
if (stack.length > 0) {
|
|
62
|
+
// Add to the current parent's children
|
|
63
|
+
stack[stack.length - 1]?.children.push(blockNode);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
// Add to root level
|
|
67
|
+
result.push(blockNode);
|
|
68
|
+
}
|
|
69
|
+
// Push to stack for nesting
|
|
70
|
+
stack.push(blockNode);
|
|
71
|
+
}
|
|
72
|
+
else if (node.type === "endblock") {
|
|
73
|
+
// Pop from stack when we encounter an endblock
|
|
74
|
+
stack.pop();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
const children = buildNestedStructure(rawNodes);
|
|
80
|
+
return {
|
|
81
|
+
rootNode: {
|
|
82
|
+
type: "template",
|
|
83
|
+
children,
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;AAiCA,sBA2FC;AA5HD,8DAAiC;AACjC,0FAAqD;AA4BrD,MAAM,MAAM,GAAG,IAAI,qBAAM,EAAE,CAAC;AAC5B,mBAAmB;AACnB,MAAM,CAAC,WAAW,CAAC,mCAAY,CAAC,CAAC;AAEjC,SAAgB,KAAK,CAAC,OAAe;IACnC,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAEnC,SAAS,WAAW,CAClB,IAAS;QAET,IAAI,IAAI,CAAC,IAAI,KAAK,qBAAqB,EAAE,CAAC;YACxC,MAAM,YAAY,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CACrC,CAAC,KAAU,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,eAAe,CAC/C,CAAC;YACF,IAAI,YAAY,EAAE,CAAC;gBACjB,MAAM,OAAO,GAAG,YAAY,CAAC,QAAQ,CAAC,IAAI,CACxC,CAAC,KAAU,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,KAAK,CACrC,CAAC;gBACF,MAAM,YAAY,GA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AAE;iBACb,CAAC;gBAEF,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACrB,uCAAuC;oBACvC,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;gBACpD,CAAC;qBAAM,CAAC;oBACN,oBAAoB;oBACpB,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;gBACzB,CAAC;gBAED,4BAA4B;gBAC5B,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;YACxB,CAAC;iBAAM,IAAI,IAAI,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;gBACpC,+CAA+C;gBAC/C,KAAK,CAAC,GAAG,EAAE,CAAC;YACd,CAAC;QACH,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,MAAM,QAAQ,GAAG,oBAAoB,CAAC,QAAQ,CAAC,CAAC;IAEhD,OAAO;QACL,QAAQ,EAAE;YACR,IAAI,EAAE,UAAU;YAChB,QAAQ;SACT;KACF,CAAC;AACJ,CAAC","sourcesContent":["import Parser from \"tree-sitter\";\nimport ShopwareTwig from \"tree-sitter-shopware-twig\";\n\ntype Tree = {\n rootNode: TemplateNode;\n};\n\ntype TemplateNode = {\n type: \"template\";\n children: TwigStatementDirectiveNode[];\n};\n\ntype TwigStatementDirectiveNode = {\n type: \"twig_statement_directive\";\n tag: TwigTagNode;\n variable: TwigVariableNode;\n children: TwigStatementDirectiveNode[];\n};\n\ntype TwigTagNode = {\n type: \"twig_tag\";\n name: string;\n};\n\ntype TwigVariableNode = {\n type: \"twig_variable\";\n content: string;\n};\n\nconst parser = new Parser();\n// @ts-expect-error\nparser.setLanguage(ShopwareTwig);\n\nexport function parse(content: string): Tree {\n const tree = parser.parse(content);\n\n function convertNode(\n node: any\n ): { type: \"block\" | \"endblock\"; name?: string } | null {\n if (node.type === \"statement_directive\") {\n const tagStatement = node.children.find(\n (child: any) => child.type === \"tag_statement\"\n );\n if (tagStatement) {\n const tagNode = tagStatement.children.find(\n (child: any) => child.type === \"tag\"\n );\n const variableNode = tagStatement.children.find(\n (child: any) => child.type === \"variable\"\n );\n\n if (tagNode && tagNode.text === \"block\" && variableNode) {\n return {\n type: \"block\",\n name: variableNode.text,\n };\n } else if (tagNode && tagNode.text === \"endblock\") {\n return {\n type: \"endblock\",\n };\n }\n }\n }\n return null;\n }\n\n // First pass: convert all nodes to intermediate format\n const rawNodes: { type: \"block\" | \"endblock\"; name?: string }[] = [];\n for (const child of tree.rootNode.children) {\n const converted = convertNode(child);\n if (converted) {\n rawNodes.push(converted);\n }\n }\n\n // Second pass: build nested structure using a stack\n function buildNestedStructure(\n nodes: { type: \"block\" | \"endblock\"; name?: string }[]\n ): TwigStatementDirectiveNode[] {\n const result: TwigStatementDirectiveNode[] = [];\n const stack: TwigStatementDirectiveNode[] = [];\n\n for (const node of nodes) {\n if (node.type === \"block\") {\n const blockNode: TwigStatementDirectiveNode = {\n tag: {\n type: \"twig_tag\",\n name: \"block\",\n },\n type: \"twig_statement_directive\",\n variable: {\n type: \"twig_variable\",\n content: node.name!,\n },\n children: [],\n };\n\n if (stack.length > 0) {\n // Add to the current parent's children\n stack[stack.length - 1]?.children.push(blockNode);\n } else {\n // Add to root level\n result.push(blockNode);\n }\n\n // Push to stack for nesting\n stack.push(blockNode);\n } else if (node.type === \"endblock\") {\n // Pop from stack when we encounter an endblock\n stack.pop();\n }\n }\n\n return result;\n }\n\n const children = buildNestedStructure(rawNodes);\n\n return {\n rootNode: {\n type: \"template\",\n children,\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
type Tree = {
|
|
2
|
+
rootNode: TemplateNode;
|
|
3
|
+
};
|
|
4
|
+
type TemplateNode = {
|
|
5
|
+
type: "template";
|
|
6
|
+
children: TwigStatementDirectiveNode[];
|
|
7
|
+
};
|
|
8
|
+
type TwigStatementDirectiveNode = {
|
|
9
|
+
type: "twig_statement_directive";
|
|
10
|
+
tag: TwigTagNode;
|
|
11
|
+
variable: TwigVariableNode;
|
|
12
|
+
children: TwigStatementDirectiveNode[];
|
|
13
|
+
};
|
|
14
|
+
type TwigTagNode = {
|
|
15
|
+
type: "twig_tag";
|
|
16
|
+
name: string;
|
|
17
|
+
};
|
|
18
|
+
type TwigVariableNode = {
|
|
19
|
+
type: "twig_variable";
|
|
20
|
+
content: string;
|
|
21
|
+
};
|
|
22
|
+
export declare function parse(content: string): Tree;
|
|
23
|
+
export {};
|
|
24
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAGA,KAAK,IAAI,GAAG;IACV,QAAQ,EAAE,YAAY,CAAC;CACxB,CAAC;AAEF,KAAK,YAAY,GAAG;IAClB,IAAI,EAAE,UAAU,CAAC;IACjB,QAAQ,EAAE,0BAA0B,EAAE,CAAC;CACxC,CAAC;AAEF,KAAK,0BAA0B,GAAG;IAChC,IAAI,EAAE,0BAA0B,CAAC;IACjC,GAAG,EAAE,WAAW,CAAC;IACjB,QAAQ,EAAE,gBAAgB,CAAC;IAC3B,QAAQ,EAAE,0BAA0B,EAAE,CAAC;CACxC,CAAC;AAEF,KAAK,WAAW,GAAG;IACjB,IAAI,EAAE,UAAU,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,KAAK,gBAAgB,GAAG;IACtB,IAAI,EAAE,eAAe,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAMF,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CA2F3C"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import Parser from "tree-sitter";
|
|
2
|
+
import ShopwareTwig from "tree-sitter-shopware-twig";
|
|
3
|
+
const parser = new Parser();
|
|
4
|
+
// @ts-expect-error
|
|
5
|
+
parser.setLanguage(ShopwareTwig);
|
|
6
|
+
export function parse(content) {
|
|
7
|
+
const tree = parser.parse(content);
|
|
8
|
+
function convertNode(node) {
|
|
9
|
+
if (node.type === "statement_directive") {
|
|
10
|
+
const tagStatement = node.children.find((child) => child.type === "tag_statement");
|
|
11
|
+
if (tagStatement) {
|
|
12
|
+
const tagNode = tagStatement.children.find((child) => child.type === "tag");
|
|
13
|
+
const variableNode = tagStatement.children.find((child) => child.type === "variable");
|
|
14
|
+
if (tagNode && tagNode.text === "block" && variableNode) {
|
|
15
|
+
return {
|
|
16
|
+
type: "block",
|
|
17
|
+
name: variableNode.text,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
else if (tagNode && tagNode.text === "endblock") {
|
|
21
|
+
return {
|
|
22
|
+
type: "endblock",
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
// First pass: convert all nodes to intermediate format
|
|
30
|
+
const rawNodes = [];
|
|
31
|
+
for (const child of tree.rootNode.children) {
|
|
32
|
+
const converted = convertNode(child);
|
|
33
|
+
if (converted) {
|
|
34
|
+
rawNodes.push(converted);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
// Second pass: build nested structure using a stack
|
|
38
|
+
function buildNestedStructure(nodes) {
|
|
39
|
+
const result = [];
|
|
40
|
+
const stack = [];
|
|
41
|
+
for (const node of nodes) {
|
|
42
|
+
if (node.type === "block") {
|
|
43
|
+
const blockNode = {
|
|
44
|
+
tag: {
|
|
45
|
+
type: "twig_tag",
|
|
46
|
+
name: "block",
|
|
47
|
+
},
|
|
48
|
+
type: "twig_statement_directive",
|
|
49
|
+
variable: {
|
|
50
|
+
type: "twig_variable",
|
|
51
|
+
content: node.name,
|
|
52
|
+
},
|
|
53
|
+
children: [],
|
|
54
|
+
};
|
|
55
|
+
if (stack.length > 0) {
|
|
56
|
+
// Add to the current parent's children
|
|
57
|
+
stack[stack.length - 1]?.children.push(blockNode);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
// Add to root level
|
|
61
|
+
result.push(blockNode);
|
|
62
|
+
}
|
|
63
|
+
// Push to stack for nesting
|
|
64
|
+
stack.push(blockNode);
|
|
65
|
+
}
|
|
66
|
+
else if (node.type === "endblock") {
|
|
67
|
+
// Pop from stack when we encounter an endblock
|
|
68
|
+
stack.pop();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
const children = buildNestedStructure(rawNodes);
|
|
74
|
+
return {
|
|
75
|
+
rootNode: {
|
|
76
|
+
type: "template",
|
|
77
|
+
children,
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,YAAY,MAAM,2BAA2B,CAAC;AA4BrD,MAAM,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;AAC5B,mBAAmB;AACnB,MAAM,CAAC,WAAW,CAAC,YAAY,CAAC,CAAC;AAEjC,MAAM,UAAU,KAAK,CAAC,OAAe;IACnC,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAEnC,SAAS,WAAW,CAClB,IAAS;QAET,IAAI,IAAI,CAAC,IAAI,KAAK,qBAAqB,EAAE,CAAC;YACxC,MAAM,YAAY,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CACrC,CAAC,KAAU,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,eAAe,CAC/C,CAAC;YACF,IAAI,YAAY,EAAE,CAAC;gBACjB,MAAM,OAAO,GAAG,YAAY,CAAC,QAAQ,CAAC,IAAI,CACxC,CAAC,KAAU,EAAE,EAAE,CAAC,KAAK,CA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O,EAAE,IAAI,CAAC,IAAK;qBACpB;oBACD,QAAQ,EAAE,EAAE;iBACb,CAAC;gBAEF,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACrB,uCAAuC;oBACvC,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;gBACpD,CAAC;qBAAM,CAAC;oBACN,oBAAoB;oBACpB,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;gBACzB,CAAC;gBAED,4BAA4B;gBAC5B,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;YACxB,CAAC;iBAAM,IAAI,IAAI,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;gBACpC,+CAA+C;gBAC/C,KAAK,CAAC,GAAG,EAAE,CAAC;YACd,CAAC;QACH,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,MAAM,QAAQ,GAAG,oBAAoB,CAAC,QAAQ,CAAC,CAAC;IAEhD,OAAO;QACL,QAAQ,EAAE;YACR,IAAI,EAAE,UAAU;YAChB,QAAQ;SACT;KACF,CAAC;AACJ,CAAC","sourcesContent":["import Parser from \"tree-sitter\";\nimport ShopwareTwig from \"tree-sitter-shopware-twig\";\n\ntype Tree = {\n rootNode: TemplateNode;\n};\n\ntype TemplateNode = {\n type: \"template\";\n children: TwigStatementDirectiveNode[];\n};\n\ntype TwigStatementDirectiveNode = {\n type: \"twig_statement_directive\";\n tag: TwigTagNode;\n variable: TwigVariableNode;\n children: TwigStatementDirectiveNode[];\n};\n\ntype TwigTagNode = {\n type: \"twig_tag\";\n name: string;\n};\n\ntype TwigVariableNode = {\n type: \"twig_variable\";\n content: string;\n};\n\nconst parser = new Parser();\n// @ts-expect-error\nparser.setLanguage(ShopwareTwig);\n\nexport function parse(content: string): Tree {\n const tree = parser.parse(content);\n\n function convertNode(\n node: any\n ): { type: \"block\" | \"endblock\"; name?: string } | null {\n if (node.type === \"statement_directive\") {\n const tagStatement = node.children.find(\n (child: any) => child.type === \"tag_statement\"\n );\n if (tagStatement) {\n const tagNode = tagStatement.children.find(\n (child: any) => child.type === \"tag\"\n );\n const variableNode = tagStatement.children.find(\n (child: any) => child.type === \"variable\"\n );\n\n if (tagNode && tagNode.text === \"block\" && variableNode) {\n return {\n type: \"block\",\n name: variableNode.text,\n };\n } else if (tagNode && tagNode.text === \"endblock\") {\n return {\n type: \"endblock\",\n };\n }\n }\n }\n return null;\n }\n\n // First pass: convert all nodes to intermediate format\n const rawNodes: { type: \"block\" | \"endblock\"; name?: string }[] = [];\n for (const child of tree.rootNode.children) {\n const converted = convertNode(child);\n if (converted) {\n rawNodes.push(converted);\n }\n }\n\n // Second pass: build nested structure using a stack\n function buildNestedStructure(\n nodes: { type: \"block\" | \"endblock\"; name?: string }[]\n ): TwigStatementDirectiveNode[] {\n const result: TwigStatementDirectiveNode[] = [];\n const stack: TwigStatementDirectiveNode[] = [];\n\n for (const node of nodes) {\n if (node.type === \"block\") {\n const blockNode: TwigStatementDirectiveNode = {\n tag: {\n type: \"twig_tag\",\n name: \"block\",\n },\n type: \"twig_statement_directive\",\n variable: {\n type: \"twig_variable\",\n content: node.name!,\n },\n children: [],\n };\n\n if (stack.length > 0) {\n // Add to the current parent's children\n stack[stack.length - 1]?.children.push(blockNode);\n } else {\n // Add to root level\n result.push(blockNode);\n }\n\n // Push to stack for nesting\n stack.push(blockNode);\n } else if (node.type === \"endblock\") {\n // Pop from stack when we encounter an endblock\n stack.pop();\n }\n }\n\n return result;\n }\n\n const children = buildNestedStructure(rawNodes);\n\n return {\n rootNode: {\n type: \"template\",\n children,\n },\n };\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "shopware-twig-parser",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"main": "./dist/commonjs/index.js",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"keywords": [],
|
|
9
|
+
"author": "Nils Haberkamp",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"description": "A parser for Shopware Twig",
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@changesets/cli": "^2.29.5",
|
|
14
|
+
"@types/node": "^24.2.1",
|
|
15
|
+
"tshy": "^3.0.2",
|
|
16
|
+
"vite": "^7.1.2",
|
|
17
|
+
"vitest": "^3.2.4"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"tree-sitter": "^0.25.0",
|
|
21
|
+
"tree-sitter-shopware-twig": "^0.2.3"
|
|
22
|
+
},
|
|
23
|
+
"type": "module",
|
|
24
|
+
"tshy": {
|
|
25
|
+
"exports": {
|
|
26
|
+
"./package.json": "./package.json",
|
|
27
|
+
".": "./src/index.ts"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"exports": {
|
|
31
|
+
"./package.json": "./package.json",
|
|
32
|
+
".": {
|
|
33
|
+
"import": {
|
|
34
|
+
"types": "./dist/esm/index.d.ts",
|
|
35
|
+
"default": "./dist/esm/index.js"
|
|
36
|
+
},
|
|
37
|
+
"require": {
|
|
38
|
+
"types": "./dist/commonjs/index.d.ts",
|
|
39
|
+
"default": "./dist/commonjs/index.js"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"types": "./dist/commonjs/index.d.ts",
|
|
44
|
+
"module": "./dist/esm/index.js",
|
|
45
|
+
"scripts": {
|
|
46
|
+
"test": "vitest",
|
|
47
|
+
"build": "tshy",
|
|
48
|
+
"release": "changeset version && changeset publish"
|
|
49
|
+
}
|
|
50
|
+
}
|