Skip to content

Commit

Permalink
Updated the VaporHX preset
Browse files Browse the repository at this point in the history
  • Loading branch information
RussBaz committed Mar 4, 2024
1 parent 663793d commit 1e8be77
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 31 deletions.
7 changes: 6 additions & 1 deletion Plugins/ReparsePlugin/ReparsePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct ReparsePlugin: CommandPlugin {

let providedImports = argExtractor.extractOption(named: "imports")
let providedParameters = argExtractor.extractOption(named: "parameters")
let providedProtocols = argExtractor.extractOption(named: "protocols")

let preset: Preset = if providedPreset == "vapor" { .vapor } else if providedPreset == "vaporhx" { .vaporHX } else { .none }

Expand Down Expand Up @@ -94,14 +95,15 @@ struct ReparsePlugin: CommandPlugin {

var imports: [String] = []
var parameters: [String] = []
let protocols = argExtractor.extractOption(named: "protocols")
var protocols: [String] = []

switch preset {
case .vapor:
imports.append("Vapor")
imports.append(contentsOf: providedImports)
parameters.append("req:Request")
parameters.append(contentsOf: providedParameters)
protocols.append(contentsOf: providedProtocols)
case .vaporHX:
imports.append("Vapor")
imports.append("VHX")
Expand All @@ -110,9 +112,12 @@ struct ReparsePlugin: CommandPlugin {
parameters.append("isPage:Bool")
parameters.append("?context:EmptyContext=EmptyContext()")
parameters.append(contentsOf: providedParameters)
protocols.append("HXTemplateable")
protocols.append(contentsOf: providedProtocols)
case .none:
imports.append(contentsOf: providedImports)
parameters.append(contentsOf: providedParameters)
protocols.append(contentsOf: providedProtocols)
}

let dryRun = argExtractor.extractFlag(named: "dryRun") > 0
Expand Down
73 changes: 43 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,33 @@ NOTE: The master branch is way ahead of the latest release as it includes many f
2. HTML Templating (and not a text templating)
3. Templates are Compiled into the Swift Code

## Example
## How to Use

You can find a `Vapor` example in the `Sources/Example` folder.
If you would like to use it in your own project, you would need to add the `ReparseRuntime` as a dependency to your project.

`Sources/Example/pages.swift` file was generated by the following command (from the project root):
Here is the help for the tool when it is run on it own:

```
swift run reparse $(pwd)/Resources/Pages $(pwd)/Sources/Example --file-name=pages.swift --imports=Vapor --parameters=req:Request
USAGE: reparse <location> <destination> [--file-name <file-name>] [--file-extension <file-extension>] [--enum-name <enum-name>] [--imports <imports> ...] [--parameters <parameters> ...] [--protocols <protocols> ...] [--dry-run]
ARGUMENTS:
<location> The target data folder location.
<destination> The destination folder for the output file.
OPTIONS:
--file-name <file-name> Output file name (default: pages.swift)
--file-extension <file-extension>
The file extension to be searched for (default: html)
--enum-name <enum-name> The name of the generated enum (default: Pages)
--imports <imports> List of global imports
--parameters <parameters>
List of shared parameters (parameters to be added to every 'include' function) in a form of '[?][label:]name:type[=default]' where the optional parts are in square brackets. The question mark at the beginning indicates
that the parameter will be overriden by a local requirement if it is present.
--protocols <protocols> A list of protocols to apply to enums with render functions in them in a form of 'name[:associatedName:associatedType]' where the optional parts are in square brackets. Optional part can be repeated any number of times.
--dry-run Write the output to the console instead of file
-h, --help Show help information.
```

The templates are stored in the `Resources/Pages` folder as plain html files.

If you would like to use it in your own project, you would need to add the `ReparseRuntime` as a dependency to your project.

## Syntax

All special attributes and tags will be removed by the compilation and must not appear in the output of the render function. If they do, then it is a bug.
Expand All @@ -34,44 +47,44 @@ There are a few types of control attributes and can be separated in 3 groups:

Most html tags can be equipped with one of the conditional control attributes:

- **r-if="condition"**
- **r-else-if="condition"**
- **r-else**
- **r-if="condition"**
- **r-else-if="condition"**
- **r-else**

If the condition (which must be a valid Swift expression) is satisfied than the html tag and its contents are rendered.

If the condition is satisfied, then a special variable called `previousUnnamedIfTaken` will be set to true. Otherwise, it will be set to false.

Optionally, you can save the result of the condition to a different variable using an additional attribute:

- **r-tag="tag-name"**
- **r-tag="tag-name"**

#### Loops

- **r-for-every="sequence"**
- **r-for-every="sequence"**

#### Slots

- **r-add-to-slot="slot-name"**
- **r-replace-slot="slot-name"**
- **r-add-to-slot="slot-name"**
- **r-replace-slot="slot-name"**

### Control Tags

- **`<r-extend name="template-name" />`** (must be before any tag other than r-require) to wrap the current template into a default slot of the specified template
- **`<r-require label="optional-label" name="var-name" type="var-type" default="optional-default" />`** (must be before any tag other than r-extend) to define a variable that must be passed into the template from the caller
- **`<r-include name="template-name" />`** to include another template or
- **`<r-include name="template-name"> default slot </r-include>`** to include a template with a default slot provided
- **`<r-block> some data </r-block>`** to group some part of template, e.g. wrap some text with it and now you can apply control attributes to it.
- **`<r-set name="attr-name" value="attr-value" />`** to replace an attribute in a preceding tag (skipping other set/unset tags) or
- **`<r-set name="attr-name" value="attr-value" append />`** to append to it instead
- **`<r-unset name="attr-name" />`** to remove an atttribute from a preceding tag (skipping other set/unset tags)
- **`<r-var name="var-name" line="expression" />`** to assign the result of an expression to a variable
- **`<r-value of="name" default="optional-val" />`** to paste the value of the specified variable or the provided default value if the value was `nil`
- **`<r-eval line="expression" />`** evaluate the expression and paste its result
- **`<r-slot name="optional-name" />`** to mark an area to be filled by the incoming outer slot. If no name is provided, it will be known as 'default' or
- **`<r-slot name="optional-name"> default slot </r-slot>`** to declare a slot and provide the default contents if no matching slot found in the incoming outer slots
- **`<r-index />`** (inside the loops only) to paste the index of the current iteration of the innermost loop
- **`<r-value />`** (inside the loops only) to paste the value of the current iteration of the innermost loop
- **`<r-extend name="template-name" />`** (must be before any tag other than r-require) to wrap the current template into a default slot of the specified template
- **`<r-require label="optional-label" name="var-name" type="var-type" default="optional-default" />`** (must be before any tag other than r-extend) to define a variable that must be passed into the template from the caller
- **`<r-include name="template-name" />`** to include another template or
- **`<r-include name="template-name"> default slot </r-include>`** to include a template with a default slot provided
- **`<r-block> some data </r-block>`** to group some part of template, e.g. wrap some text with it and now you can apply control attributes to it.
- **`<r-set name="attr-name" value="attr-value" />`** to replace an attribute in a preceding tag (skipping other set/unset tags) or
- **`<r-set name="attr-name" value="attr-value" append />`** to append to it instead
- **`<r-unset name="attr-name" />`** to remove an atttribute from a preceding tag (skipping other set/unset tags)
- **`<r-var name="var-name" line="expression" />`** to assign the result of an expression to a variable
- **`<r-value of="name" default="optional-val" />`** to paste the value of the specified variable or the provided default value if the value was `nil`
- **`<r-eval line="expression" />`** evaluate the expression and paste its result
- **`<r-slot name="optional-name" />`** to mark an area to be filled by the incoming outer slot. If no name is provided, it will be known as 'default' or
- **`<r-slot name="optional-name"> default slot </r-slot>`** to declare a slot and provide the default contents if no matching slot found in the incoming outer slots
- **`<r-index />`** (inside the loops only) to paste the index of the current iteration of the innermost loop
- **`<r-value />`** (inside the loops only) to paste the value of the current iteration of the innermost loop

## How does it work?

Expand Down

0 comments on commit 1e8be77

Please sign in to comment.