Skip to content

Commit cd9d763

Browse files
committed
update docs
1 parent 01f4358 commit cd9d763

23 files changed

Lines changed: 366 additions & 168 deletions

config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"debug": false
2+
"debug": true
33
}

docs/Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ GEM
212212
minitest (5.19.0)
213213
nokogiri (1.18.9-arm64-darwin)
214214
racc (~> 1.4)
215+
nokogiri (1.18.9-x86_64-darwin)
216+
racc (~> 1.4)
215217
nokogiri (1.18.9-x86_64-linux-gnu)
216218
racc (~> 1.4)
217219
octokit (4.25.1)
@@ -251,6 +253,7 @@ GEM
251253

252254
PLATFORMS
253255
arm64-darwin-22
256+
x86_64-darwin-20
254257
x86_64-linux
255258

256259
DEPENDENCIES
-176 Bytes
Binary file not shown.
531 KB
Loading

docs/code-api/generator.md

Lines changed: 79 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -7,129 +7,128 @@ parent: API
77

88
# `Generator` class
99

10-
This code provides a `Generator` class for parsing, generating, and manipulating PHP files. It handles various PHP constructs such as namespaces, classes, interfaces, traits, and more. Additionally, it can detect reserved keywords and make names safe for use in generated code.
10+
{: .warning }
11+
> This page documents the modern `Generator` class for parsing templates of any file format. For legacy PHP generation from JSON/YAML/NEON templates, see the `ClassicGenerator` class.
12+
13+
The `Generator` class provides functionality for parsing modern templates (any file format) and generating files. It uses [Pars'Em](https://github.com/matronator/parsem) for template parsing and variable substitution.
1114

1215
- [Important Classes](#important-classes)
13-
- [FileObject](#fileobject)
14-
- [PhpFile](#phpfile)
16+
- [GenericFileObject](#genericfileobject)
17+
- [TemplateHeader](#templateheader)
1518
- [Constants](#constants)
1619
- [Methods](#methods)
17-
- [`isBundle`](#isbundlestring-path-string-contents--null-bool)
18-
- [`parse`](#parsestring-filename-string-contents-array-arguments-fileobject)
19-
- [`parseFile`](#parsefilestring-path-array-arguments-fileobject)
20-
- [`generateFile`](#generatefileobject-parsed-array-arguments-fileobject)
21-
- [`getName`](#getnamestring-path-string-contents--null-string)
22-
- [`generate`](#generateobject-body-array-args-phpfile)
23-
- [`is`](#ismixed-subject-bool)
24-
- [`isReservedKeyword`](#isreservedkeywordstring-name-bool)
25-
- [`makeNameSafe`](#makenamesafestring-name-string)
20+
- [`parseAnyFile`](#parseanyfilestring-path-array-arguments--bool-usec commentsyntax--false-genericfileobject)
21+
- [`writeFiles`](#writefilesgenericfileobject--genericfileobject-files-void)
22+
- [`getName`](#getnamestring-content--null-string-path--null-string)
23+
- [`getTemplateHeader`](#gettemplateheaderstring-content-templateheader)
24+
- [`removeHeader`](#removeheaderstring-content-string)
2625

2726
## Important Classes
2827

29-
### FileObject
28+
### GenericFileObject
3029

31-
`Matronator\Mtrgen\FileObject` is the main data structure for the parsed templates. It contains all the information about the file that is needed for writing it to the disk.
30+
`Matronator\Mtrgen\GenericFileObject` is the main data structure for parsed modern templates. It contains all the information about the file that is needed for writing it to the disk.
3231

3332
```php
34-
class FileObject
35-
{
36-
public PhpFile $contents;
37-
38-
public string $filename;
33+
namespace Matronator\Mtrgen;
3934

40-
public string $directory;
41-
42-
public ?string $entity = null;
35+
class GenericFileObject
36+
{
37+
public string $contents; // The file contents as a string
38+
public string $filename; // The output filename (including extension)
39+
public string $directory; // The output directory path
4340

44-
public function __construct(string $directory, string $filename, PhpFile $contents, ?string $entity = null) {
45-
$this->filename = $filename . '.php';
41+
public function __construct(string $directory, string $filename, string $contents) {
42+
$this->filename = $filename;
4643
$this->contents = $contents;
4744
$this->directory = $directory;
48-
$this->entity = $entity;
4945
}
5046
}
5147
```
5248

53-
### PhpFile
54-
55-
`Nette\PhpGenerator\PhpFile` is a class from the [Nette](https://nette.org) [PHP Generator library](https://github.com/nette/php-generator). It is used to represent a PHP file and its contents. It is used by the `Generator` class for generating the parsed templates.
56-
57-
Constants
58-
---------
59-
60-
- `RESERVED_KEYWORDS`: An array of reserved keywords in PHP.
61-
- `RESERVED_CONSTANTS`: An array of reserved constants in PHP.
62-
63-
Methods
64-
-------
49+
### TemplateHeader
6550

66-
### `isBundle(string $path, ?string $contents = null): bool`
51+
`Matronator\Mtrgen\Template\TemplateHeader` represents the metadata header of a template file.
6752

68-
Checks if a given template is a bundle.
53+
```php
54+
namespace Matronator\Mtrgen\Template;
6955

70-
- `$path`: The path of the template file.
71-
- `$contents`: (optional) The contents of the template file.
72-
- Returns: A boolean indicating whether the template is a bundle.
56+
class TemplateHeader
57+
{
58+
public string $name; // Template name (for storage/identification)
59+
public string $filename; // Output filename (can use template variables)
60+
public string $path; // Output directory path (can use template variables)
7361

74-
### `parse(string $filename, string $contents, array $arguments): FileObject`
62+
public function __construct(string $name, string $filename, string $path);
63+
public static function fromArray(array $array): static;
64+
}
65+
```
7566

76-
Parses and generates a `FileObject` from string content and a filename.
67+
## Constants
7768

78-
- `$filename`: The filename of the file to parse.
79-
- `$contents`: The contents of the file to parse.
80-
- `$arguments`: An array of arguments for parsing.
81-
- Returns: A `FileObject` representing the parsed file.
69+
- `HEADER_PATTERN`: Regular expression pattern for matching the template header block (`/^--- MTRGEN ---(.+)--- \/MTRGEN ---/ms`).
70+
- `COMMENT_PATTERN`: Regular expression pattern for matching comment-based template variables (for use with `useCommentSyntax` option).
8271

83-
### `parseFile(string $path, array $arguments): FileObject`
72+
## Methods
8473

85-
Parses and generates a `FileObject` from a file.
74+
### `parseAnyFile(string $path, array $arguments = [], bool $useCommentSyntax = false): GenericFileObject`
8675

87-
- `$path`: The path to the file to parse.
88-
- `$arguments`: An array of arguments for parsing.
89-
- Returns: A `FileObject` representing the parsed file.
76+
Parses a template file of any format and returns a `GenericFileObject` ready to be written to disk.
9077

91-
### `generateFile(object $parsed, array $arguments): FileObject`
78+
- `$path`: The path to the template file.
79+
- `$arguments`: (optional) An array of arguments to pass to the template variables.
80+
- `$useCommentSyntax`: (optional) If `true`, uses comment-based syntax (`/*variable*/`) instead of `<%variable%>` syntax.
81+
- Returns: A `GenericFileObject` representing the parsed file.
9282

93-
Generates a `FileObject` from a parsed object.
83+
**Example:**
9484

95-
- `$parsed`: The parsed object.
96-
- `$arguments`: An array of arguments for generating the file.
97-
- Returns: A `FileObject` representing the generated file.
85+
```php
86+
$file = Generator::parseAnyFile('component.js.mtr', [
87+
'name' => 'MyComponent',
88+
'event' => 'click'
89+
]);
90+
// $file->filename will be "MyComponent.js" (from header)
91+
// $file->directory will be the path from header
92+
// $file->contents will be the parsed template content
93+
```
9894

99-
### `getName(string $path, ?string $contents = null): string`
95+
### `writeFiles(GenericFileObject|GenericFileObject[] $files): void`
10096

101-
Gets the name of the template from a file.
97+
Writes one or more parsed file objects to disk. Creates directories if they don't exist.
10298

103-
- `$path`: The path of the template file.
104-
- `$contents`: (optional) The contents of the template file.
105-
- Returns: The name of the template.
99+
- `$files`: A single `GenericFileObject` or an array of `GenericFileObject` instances.
106100

107-
### `generate(object $body, array $args): PhpFile`
101+
**Example:**
108102

109-
Generates a `PhpFile` from a parsed object.
103+
```php
104+
// Write a single file
105+
Generator::writeFiles($file);
110106

111-
- `$body`: The parsed object.
112-
- `$args`: An array of arguments for generating the file.
113-
- Returns: A `PhpFile` representing the generated file.
107+
// Write multiple files
108+
Generator::writeFiles([$file1, $file2, $file3]);
109+
```
114110

115-
### `is(mixed &$subject): bool`
111+
### `getName(?string $content = null, ?string $path = null): string`
116112

117-
Shorthand for checking if a variable is set and not empty.
113+
Gets the template name from the header. Either `$content` or `$path` must be provided.
118114

119-
- `$subject`: The variable to check.
120-
- Returns: A boolean indicating whether the variable is set and not empty.
115+
- `$content`: (optional) The template file contents.
116+
- `$path`: (optional) The path to the template file.
117+
- Returns: The template name from the header.
118+
- Throws: `RuntimeException` if neither `$content` nor `$path` is provided, or if the header is missing required fields.
121119

122-
### `isReservedKeyword(string $name): bool`
120+
### `getTemplateHeader(string $content): TemplateHeader`
123121

124-
Checks if the given name is a reserved keyword.
122+
Extracts and parses the template header from the file content.
125123

126-
- `$name`: The name to check.
127-
- Returns: A boolean indicating whether the name is a reserved keyword.
124+
- `$content`: The template file contents (including the header).
125+
- Returns: A `TemplateHeader` object with `name`, `filename`, and `path` properties.
126+
- Throws: `RuntimeException` if the header is missing required properties.
128127

129-
### `makeNameSafe(string $name): string`
128+
### `removeHeader(string $content): string`
130129

131-
Makes the given name safe by adding an underscore if it is a reserved keyword.
130+
Removes the template header block from the file content, returning only the template body.
132131

133-
- `$name`: The name to make safe.
134-
- Returns: A safe name that doesn't conflict with reserved keywords or constants.
132+
- `$content`: The template file contents (including the header).
133+
- Returns: The template content without the header block.
135134

docs/index.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ title: Introduction
77

88
![MTRGen Logo](assets/images/logo.png)
99

10-
File generator engine that can generate PHP files from JSON/YAML/NEON templates.
10+
File generator engine that can generate files of any format from templates. Templates can be any file format (JavaScript, PHP, TypeScript, Python, etc.) and will generate files in the same format. MTRGen also supports legacy JSON/YAML/NEON templates for generating PHP files.
1111

1212
## Requirements
1313

@@ -48,19 +48,19 @@ vendor/bin/mtrgen list
4848
vendor/bin/mtrgen generate --help
4949
vendor/bin/mtrgen gen -h
5050
51-
# Generate from file
52-
vendor/bin/mtrgen generate --path=my/folder/template.json
53-
vendor/bin/mtrgen gen -p my/folder/template.json
51+
# Generate from file (any format)
52+
vendor/bin/mtrgen generate --path=my/folder/component.js.mtr
53+
vendor/bin/mtrgen gen -p my/folder/template.php.mtr
5454
5555
# Generate from the local store
5656
vendor/bin/mtrgen generate TemplateName
5757
5858
# Save a template to the local store
59-
vendor/bin/mtrgen save path/to/template.json
60-
vendor/bin/mtrgen s path/to/template.json
59+
vendor/bin/mtrgen save path/to/template.js.mtr
60+
vendor/bin/mtrgen s path/to/template.php.mtr
6161
6262
# Optionally provide an alias to save the template under
63-
vendor/bin/mtrgen save path/to/template.json --alias=NewName
63+
vendor/bin/mtrgen save path/to/template.js.mtr --alias=NewName
6464
6565
# Remove a template from the local store
6666
vendor/bin/mtrgen remove TemplateName
@@ -69,7 +69,7 @@ vendor/bin/mtrgen r TemplateName
6969

7070
## Acknowledgement
7171

72-
This project would not be possible without [Nette](https://nette.org)'s [`php-generator`](https://github.com/nette/php-generator) package, which is used for the final code generation itself to output the finished PHP file.
72+
This project uses [Pars'Em](https://github.com/matronator/parsem) for template parsing and variable substitution. For legacy PHP file generation, it uses [Nette](https://nette.org)'s [`php-generator`](https://github.com/nette/php-generator) package.
7373

7474
## License
7575

docs/online-registry/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ vendor/bin/mtrgen generate vendor/template
3131

3232
If you just want to generate a file from a template in the online registry one-time only and don't want to add it to your local store, you can use the `use` command. This command works just like the `generate` command, except it takes an identifier as an argument and searches the online registry instead of your local store.
3333

34-
When it finds the template, it will prompt you to provide arguments for the template (if there are any) and generates the PHP file without saving the template in your local store. This is useful if you just want to use a template one time and know you won't be needing it in the future again.
34+
When it finds the template, it will prompt you to provide arguments for the template (if there are any) and generates the file without saving the template in your local store. This is useful if you just want to use a template one time and know you won't be needing it in the future again. The generated file format depends on the template (modern templates can generate any format, legacy templates generate PHP files).
3535

3636
### Using the `use` command
3737

docs/online-registry/publishing-templates.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,14 @@ Now that your account is created and you've successfully logged in, it's time yo
6464
Publishing a template is as simple as just running a single command:
6565

6666
```bash
67-
vendor/bin/mtrgen publish --path=path/to/your/template.json
67+
vendor/bin/mtrgen publish --path=path/to/your/template.js.mtr
6868
# Or a shorter alias
69-
vendor/bin/mtrgen pub -p path/to/your/template.json
69+
vendor/bin/mtrgen pub -p path/to/your/template.php.mtr
7070
```
7171

72+
{: .note }
73+
> You can publish templates of any format (modern templates) or legacy JSON/YAML/NEON templates. The template format doesn't matter - as long as it has a valid header, it can be published.
74+
7275
Alternatively if you want to publish a template you already have saved in your local store, just provide the template name instead of the `--path` option, like this:
7376

7477
```bash

docs/templates/index.md

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ has_children: true
99

1010
## Introduction
1111

12-
Generator templates (or just templates) describe the structure of the generated file. Templates can be either YAML, JSON or NEON files and they have to conform to a JSON schema. We will talk more about the schema in the next chapter (*see [mtrgen-template-schema.json](template-structure.md#mtrgen-template-schema)*).
12+
Generator templates (or just templates) describe the structure of the generated file. MTRGen supports two types of templates:
13+
14+
1. **Modern templates**: Any file format (JavaScript, PHP, TypeScript, Python, etc.) that can generate files in the same format. These templates use a header block to define metadata and use template variables for dynamic content.
15+
16+
2. **Legacy templates**: JSON/YAML/NEON files that generate PHP files. These templates conform to a JSON schema and are used for generating PHP classes, interfaces, and traits. (*See [Template Structure](template-structure.md) for more details on legacy templates.*)
1317

1418
{: .tip }
15-
> It is recommended to name your templates with a `.mtr` suffix after the name right after the extension (eg. `entity.yaml.mtr`). If you do that, the [MTRGen Templates Syntax Highlighting](https://marketplace.visualstudio.com/items?itemName=matronator.mtrgen-yaml-templates) VSCode extension, will add syntax highlighting and snippets to all `*.mtr` files.
19+
> It is recommended to name your templates with a `.mtr` extension (eg. `component.js.mtr`, `entity.php.mtr`). The `.mtr` extension helps identify template files and enables syntax highlighting in editors. The [MTRGen Templates Syntax Highlighting](https://marketplace.visualstudio.com/items?itemName=matronator.mtrgen-yaml-templates) VSCode extension provides syntax highlighting and snippets for `*.mtr` files.
1620
1721
## Template syntax
1822

@@ -27,15 +31,18 @@ The header block looks like this:
2731
```
2832
--- MTRGEN ---
2933
name: template-name
30-
filename: <% name|firstUpper %>.php
31-
path: ./app/Controllers
32-
--- MTRGEN ---
34+
filename: <% name|upperFirst %>.js
35+
path: ./src/components
36+
--- /MTRGEN ---
3337
34-
// Rest of the template
38+
// Rest of the template content
3539
```
3640

3741
You can use template variables for the header values, except for the `name` field, which must be known beforehand to be able to correctly save and use the template later.
3842

43+
{: .note }
44+
> The output filename is defined in the `filename` field of the header. The generated file will have exactly the name you specify here, including the extension. For example, if your template is `component.js.mtr` and the header specifies `filename: <% name %>.js`, the generated file will be `MyComponent.js` (assuming `name` is "MyComponent").
45+
3946
#### Header fields
4047

4148
##### `name`
@@ -44,7 +51,7 @@ A unique name of the template to be saved under in the local store. If published
4451

4552
##### `filename`
4653

47-
The filename of the generated file. Can use template variables to make the filename dynamic.
54+
The filename of the generated file, including the extension. Can use template variables to make the filename dynamic. The generated file will have exactly this name - for example, if you specify `filename: <% name %>.js`, the output will be a JavaScript file with that name.
4855

4956
##### `path`
5057

@@ -154,6 +161,31 @@ There are a few built-in filters that you can use:
154161
155162
`truncate` - Truncates the variable to the specified length
156163
164+
## Examples
165+
166+
### Modern Template Example
167+
168+
Here's an example of a modern JavaScript template (`component.js.mtr`):
169+
170+
```javascript
171+
--- MTRGEN ---
172+
name: js-component
173+
filename: <% name|upperFirst %>.js
174+
path: src/components
175+
--- /MTRGEN ---
176+
177+
document.addEventListener('<% event="click" %>', function() {
178+
var component = document.querySelector('#<% id="myId" %>');
179+
component.classList.add('<% className="active"|lower %>');
180+
});
181+
```
182+
183+
When you generate this template with arguments like `name=Button event=click id=btn1 className=ACTIVE`, it will create `Button.js` in the `src/components` directory with the variables replaced.
184+
185+
### Legacy Template Example
186+
187+
Legacy templates are JSON/YAML/NEON files that generate PHP files. See the [Template Structure](template-structure.md) page for details on legacy template format.
188+
157189
## VSCode Extension
158190
159191
To get syntax highlighting for template files (highlight/colorize `<% variable|filter %>` and `<% if %><% endif %>` even inside strings) and some helper snippets (like `---` to insert the template header), you can download the [MTRGen Templates Syntax Highlighting](https://marketplace.visualstudio.com/items?itemName=matronator.mtrgen-yaml-templates) extension for VS Code.

docs/templates/template-structure.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ parent: Templates
77

88
# Template structure
99

10-
As mentioned earlier, the template parser supports YAML, JSON and NEON files, so you can write your templates using any of these formats. All templates must follow the same structure, which is defined in a JSON schema. You can find the full schema here: [mtrgen-template-schema.json](https://www.mtrgen.com/storage/schemas/template/latest/mtrgen-template-schema.json)
10+
{: .warning }
11+
> This page describes **legacy templates** - JSON/YAML/NEON files that generate PHP files. For modern templates (any file format), see the [Templates Overview](../templates/index.md).
12+
13+
Legacy templates are YAML, JSON or NEON files that generate PHP files. All legacy templates must follow the same structure, which is defined in a JSON schema. You can find the full schema here: [mtrgen-template-schema.json](https://www.mtrgen.com/storage/schemas/template/latest/mtrgen-template-schema.json)
14+
15+
{: .note }
16+
> Modern templates (any file format) don't use this schema. They simply use a header block and template variables directly in the file content. See the [Templates Overview](../templates/index.md) for more information.
1117
1218
Here is a simplified version of the schema in YAML format for better readability.
1319

0 commit comments

Comments
 (0)