orb_template 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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/Makefile +45 -0
- data/README.md +429 -0
- data/Rakefile +15 -0
- data/lib/orb/ast/abstract_node.rb +27 -0
- data/lib/orb/ast/attribute.rb +51 -0
- data/lib/orb/ast/block_node.rb +26 -0
- data/lib/orb/ast/control_expression_node.rb +27 -0
- data/lib/orb/ast/newline_node.rb +22 -0
- data/lib/orb/ast/printing_expression_node.rb +29 -0
- data/lib/orb/ast/private_comment_node.rb +22 -0
- data/lib/orb/ast/public_comment_node.rb +22 -0
- data/lib/orb/ast/root_node.rb +11 -0
- data/lib/orb/ast/tag_node.rb +208 -0
- data/lib/orb/ast/text_node.rb +22 -0
- data/lib/orb/ast.rb +19 -0
- data/lib/orb/document.rb +19 -0
- data/lib/orb/errors.rb +40 -0
- data/lib/orb/parser.rb +182 -0
- data/lib/orb/patterns.rb +40 -0
- data/lib/orb/rails_derp.rb +138 -0
- data/lib/orb/rails_template.rb +101 -0
- data/lib/orb/railtie.rb +9 -0
- data/lib/orb/render_context.rb +36 -0
- data/lib/orb/template.rb +72 -0
- data/lib/orb/temple/attributes_compiler.rb +114 -0
- data/lib/orb/temple/compiler.rb +204 -0
- data/lib/orb/temple/engine.rb +40 -0
- data/lib/orb/temple/filters.rb +132 -0
- data/lib/orb/temple/generators.rb +108 -0
- data/lib/orb/temple/identity.rb +16 -0
- data/lib/orb/temple/parser.rb +46 -0
- data/lib/orb/temple.rb +16 -0
- data/lib/orb/token.rb +47 -0
- data/lib/orb/tokenizer.rb +757 -0
- data/lib/orb/tokenizer2.rb +591 -0
- data/lib/orb/utils/erb.rb +40 -0
- data/lib/orb/utils/orb.rb +12 -0
- data/lib/orb/version.rb +5 -0
- data/lib/orb.rb +50 -0
- metadata +89 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: e989a3a4834e24471f5e0db08d8de0f8ae2bb9d9518b5292bee28c58b1d8f10c
|
|
4
|
+
data.tar.gz: f1452ff5d326a96d9585229da9e9cccaf844df4dbdb2f69171682e20b28d152a
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c2c6066121f20f18a9628c32a41266ff257062821ea264cd5adf99a35a2eaa2a20690d671920bd10c61e7e5626f8963d99cff23264306ce96eb53c140060ac37
|
|
7
|
+
data.tar.gz: c6c0d9311c1e590a529730b0fe192e68fa6db4f302657e8ddf5661b2bf8074f8524e36896734aaf6929d9397f2127537f8ef2ba66fe46ec56dabc67a78eccee8
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
|
93
|
+
ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 KUY.io Inc.
|
|
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
|
|
13
|
+
all 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
|
|
21
|
+
THE SOFTWARE.
|
data/Makefile
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Makefile for ORB Ruby Gem
|
|
2
|
+
|
|
3
|
+
.PHONY: all test build clean install lint spec help
|
|
4
|
+
|
|
5
|
+
# Default target
|
|
6
|
+
all: test build
|
|
7
|
+
|
|
8
|
+
# Run test suite
|
|
9
|
+
spec:
|
|
10
|
+
bundle exec rake test
|
|
11
|
+
|
|
12
|
+
# Build the gem
|
|
13
|
+
build:
|
|
14
|
+
gem build orb_template.gemspec
|
|
15
|
+
|
|
16
|
+
# Deploy to RubyGems (requires authentication)
|
|
17
|
+
deploy: build
|
|
18
|
+
gem push orb_template-*.gem
|
|
19
|
+
|
|
20
|
+
# Clean build artifacts
|
|
21
|
+
clean:
|
|
22
|
+
rm -f *.gem
|
|
23
|
+
|
|
24
|
+
# Install the gem locally
|
|
25
|
+
install: build
|
|
26
|
+
gem install orb_template-*.gem
|
|
27
|
+
|
|
28
|
+
# Run linting
|
|
29
|
+
lint:
|
|
30
|
+
bundle exec rubocop
|
|
31
|
+
|
|
32
|
+
# Run both specs and linting
|
|
33
|
+
test: spec lint
|
|
34
|
+
|
|
35
|
+
# Show help
|
|
36
|
+
help:
|
|
37
|
+
@echo "Available targets:"
|
|
38
|
+
@echo " all - Run tests and build the gem (default)"
|
|
39
|
+
@echo " test - Run both specs and linting"
|
|
40
|
+
@echo " build - Build the gem package"
|
|
41
|
+
@echo " install - Build and install the gem locally"
|
|
42
|
+
@echo " clean - Remove built gem files"
|
|
43
|
+
@echo " spec - Run the test suite"
|
|
44
|
+
@echo " lint - Run RuboCop linting"
|
|
45
|
+
@echo " help - Show this help message"
|
data/README.md
ADDED
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
# The ORB Template Language for Ruby
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/rb/orb_template)
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
**ORB** is a template language for Ruby with the express goal of providing a first-class DSL for rendering [ViewComponents](https://viewcomponent.org). It is heavily inspired by [React JSX](https://react.dev/learn/writing-markup-with-jsx) and [Surface](https://surace-ui.org).
|
|
8
|
+
|
|
9
|
+
## Show me an Example
|
|
10
|
+
|
|
11
|
+
**The Old Way (ERB)**
|
|
12
|
+
|
|
13
|
+
```erb
|
|
14
|
+
<%= render CardComponent.new(title: "Your friends") do |card| %>
|
|
15
|
+
<%= card.section(title: "Birthdays today") do %>
|
|
16
|
+
<% @friends.each do |friend| %>
|
|
17
|
+
<%= render ListComponent.new do %>
|
|
18
|
+
<%= render List::ItemComponent.new do %>
|
|
19
|
+
<%= render LinkComponent.new(url: member_path(friend)) do %>
|
|
20
|
+
<%= friend.name %>
|
|
21
|
+
<% end %>
|
|
22
|
+
<% end %>
|
|
23
|
+
<% end %>
|
|
24
|
+
<% end %>
|
|
25
|
+
<% end %>
|
|
26
|
+
<% end %>
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**The ORB Way**
|
|
30
|
+
|
|
31
|
+
```html
|
|
32
|
+
<Card title="Your friends">
|
|
33
|
+
<Card:Section title="Birthdays today">
|
|
34
|
+
<List>
|
|
35
|
+
<List.Item :for={friend in @friends}>
|
|
36
|
+
<Link url={friend_path(friend)}>{{friend.name}}</Link>
|
|
37
|
+
</List.Item>
|
|
38
|
+
</List>
|
|
39
|
+
</Card:Section>
|
|
40
|
+
</Card>
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Table of Contents
|
|
44
|
+
|
|
45
|
+
- [Installation](#installation)
|
|
46
|
+
- [Motivation](#motivation)
|
|
47
|
+
- [Configuration](#configuration)
|
|
48
|
+
- [Syntax & Features](#features)
|
|
49
|
+
- [HTML5](#html5)
|
|
50
|
+
- [Dynamic Attributes](#dynamic-attribute-values)
|
|
51
|
+
- [Splatted Attributes](#splatted-attributes)
|
|
52
|
+
- [Components](#view-components)
|
|
53
|
+
- [Control Flow](#control-flow)
|
|
54
|
+
- [Expressions](#expressions)
|
|
55
|
+
- [View Components](#view-components)
|
|
56
|
+
- [Slots](#viewcomponent-slots)
|
|
57
|
+
- [Namespaces](#namespaces)
|
|
58
|
+
- [Comments](#comments)
|
|
59
|
+
- [Editor Support](#editor-support)
|
|
60
|
+
- [Roadmap](#roadmap)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
## Installation
|
|
64
|
+
|
|
65
|
+
In your `Gemfile`, add:
|
|
66
|
+
|
|
67
|
+
```ruby
|
|
68
|
+
gem "orb_template"
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
then run:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
bundle install
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The gem automatically registers the `ORB` template engine as the renderer for `*.orb` template files through a Railtie.
|
|
78
|
+
|
|
79
|
+
## Motivation
|
|
80
|
+
|
|
81
|
+
There already exist a plethora of fast, battle-proven template langauges for the Ruby/Rails ecosystem, so why invent another one?
|
|
82
|
+
|
|
83
|
+
ORB was born out of the frustration that instantiating and rendering view components with existing template engines quickly becomes tedious. This hindered adoption of ViewComponents in our projects, impacted velocity, maintainance and new-developer onboarding. These effects were especially pronounced with highly customizable view components with long argument lists, as well as deeply nested components, and component trees - like a Design System / Component Library.
|
|
84
|
+
|
|
85
|
+
A common solution to making the rendering of view components less verbose is to define component-specific view helpers like so:
|
|
86
|
+
|
|
87
|
+
```ruby
|
|
88
|
+
module ComponentsHelper
|
|
89
|
+
Components = {
|
|
90
|
+
card: "Components::CardComponent",
|
|
91
|
+
text: "Components::TextComponent",
|
|
92
|
+
# ...
|
|
93
|
+
}.freeze
|
|
94
|
+
|
|
95
|
+
Components.each do |name, klass|
|
|
96
|
+
define_method(name) do |*args, **kwargs, &block|
|
|
97
|
+
capture do
|
|
98
|
+
render(klass.constantize.new(*args, **kwargs)) { |com| block.call(com) if block.present? }
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
You can then using these view helpers in your front-end code, wherever a view components needs to be rendered:
|
|
106
|
+
|
|
107
|
+
```erb
|
|
108
|
+
<%= card(title: "Your friends") do %>
|
|
109
|
+
<%= text(variant: "heading") do %>
|
|
110
|
+
Overview
|
|
111
|
+
<% end %>
|
|
112
|
+
...
|
|
113
|
+
<% end %>
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
But that's still not ideal. The code is still verbose, and the heavy use of Ruby blocks makes it hard to visually parse the structure of the rendered HTML. It also introduces a lot of boilerplate code that needs to be maintained in the view helpers module. It's like pushing around food on your plate that you don't like, but just arranging it differently doesn't make it taste any better.
|
|
117
|
+
|
|
118
|
+
### The ORB Language
|
|
119
|
+
|
|
120
|
+
The `ORB` template language takes another path and allows you to write components exactly how you picture them: as elements in a document tree.
|
|
121
|
+
|
|
122
|
+
```html
|
|
123
|
+
<Card title="Your friends">
|
|
124
|
+
<Card:Section title="Birthdays today">
|
|
125
|
+
<List>
|
|
126
|
+
<List.Item>
|
|
127
|
+
<Link url={member_path(2)}>Carl Schwartz (27)</Link>
|
|
128
|
+
</List.Item>
|
|
129
|
+
<List.Item>
|
|
130
|
+
<Link url={member_path(3)}>Floralie Brain (38)</Link>
|
|
131
|
+
</List.Item>
|
|
132
|
+
</List>
|
|
133
|
+
</Card:Section>
|
|
134
|
+
</Card>
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Your code becomes more focussed, grokable and maintainable. Front-end teams that may already be familiar with JSX or VUE can become productive faster, and can make use of their existing editor tooling for HTML like `Emmet` when writing templates.
|
|
138
|
+
|
|
139
|
+
### Core Values
|
|
140
|
+
|
|
141
|
+
We believe that any template language should be enjoyable to the user:
|
|
142
|
+
|
|
143
|
+
- HTML-First: if you know HTML, you know 90% of ORB.
|
|
144
|
+
- Concise: reduce boilerplate as much as possible, to ease the burden of writing common and repetitive markup.
|
|
145
|
+
- Secure: with an option to turn off evaluation of expressions through configuration.
|
|
146
|
+
- Stateless Compilation: Lexing and parsing, and compilation happen once.
|
|
147
|
+
- Dynamic: Rendering with a context of local variables and objects is fast.
|
|
148
|
+
- Polite: guide the user with useful error messages when their templates contain problems.
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
### Conventions
|
|
152
|
+
|
|
153
|
+
- Components rendered by the `ORB` engine live under the configured namespace and omit the `Component` suffix from their class names.
|
|
154
|
+
- Templates have a file extension of `.orb`, for example: `my_template.html.orb` for a template named `:my_template` that outputs in `format: :html`.
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
## Features
|
|
159
|
+
|
|
160
|
+
ORB fully supports the HTML5 markup language standard and extends HTML with additional syntax for expressions, dynamic attributes, blocks, components, slots, and comments.
|
|
161
|
+
|
|
162
|
+
### Configuration
|
|
163
|
+
You can configure the `ORB` engine in your Rails application with an initializer, e.g., `config/initializers/orb.rb`:
|
|
164
|
+
|
|
165
|
+
```ruby
|
|
166
|
+
ORB.namespace = 'MyComponents'
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
This will instruct the `ORB` engine to look for components under the `MyComponents` namespace.
|
|
170
|
+
|
|
171
|
+
### HTML5
|
|
172
|
+
|
|
173
|
+
Regular HTML tags are fully supported by `ORB`. Just write your HTML tags as you are used to and your are good to go.
|
|
174
|
+
|
|
175
|
+
```html
|
|
176
|
+
<div id="page-banner" class="banner" aria-role="alert">
|
|
177
|
+
<span class="message">Hello World!</span>
|
|
178
|
+
</div>
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Expressions
|
|
182
|
+
|
|
183
|
+
`ORB` supports Ruby expressions in the code through double curly-braces (mustachy syntax). The code inside the curly-braces will be evaluated at render time, and the result will be HTML-safe escaped and inserted into the output.:
|
|
184
|
+
|
|
185
|
+
```html
|
|
186
|
+
<div id="page-banner" class="banner" aria-role="alert">
|
|
187
|
+
<span class="message">{{banner.message}}</span>
|
|
188
|
+
</div>
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Should you need to execute non-printing code, for instance to assign local variables, you can use the non-printing expression syntax with percent signs:
|
|
192
|
+
|
|
193
|
+
```html
|
|
194
|
+
{% user = current_user %}
|
|
195
|
+
|
|
196
|
+
<div id="page-banner" class="banner" aria-role="alert">
|
|
197
|
+
<span class="message">Welcome back, {{user.name}}!</span>
|
|
198
|
+
</div>
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### Dynamic Attribute Values
|
|
202
|
+
|
|
203
|
+
The `ORB` language allows you to define dynamic attribute values for HTML tags through single curly-braces. The code will be evaluated at render time, and assigned to the HTML attribute as a HTML-safe escaped string.
|
|
204
|
+
|
|
205
|
+
Example:
|
|
206
|
+
|
|
207
|
+
```html
|
|
208
|
+
<div id={dom_id(banner)} class={banner.classes}>
|
|
209
|
+
<span class="message">{{banner.message}}</span>
|
|
210
|
+
</div>
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Control Flow
|
|
214
|
+
|
|
215
|
+
`ORB` supports flow control constructs through block instructions. The general syntax for a block is:
|
|
216
|
+
|
|
217
|
+
```html
|
|
218
|
+
{#blockname expression} ... {/blockname}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
For example, a `Banner` may be conditionally rendered through an `{#if}` block construct like this:
|
|
222
|
+
|
|
223
|
+
```html
|
|
224
|
+
{#if banner.urgent?}
|
|
225
|
+
<div id="{dom_id(banner)}" class="{banner.classNames}">
|
|
226
|
+
<span class="message">{{banner.message}}</span>
|
|
227
|
+
</div>
|
|
228
|
+
{/if}
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
Since control flow is such a common thing in templates, `ORB` provides special syntactic sugar for the `{#if}` and `{#for}` blocks through the `:if` and `:for` directives on HTML tags. The above example can thus be rewritten as:
|
|
232
|
+
|
|
233
|
+
```html
|
|
234
|
+
<div id="{dom_id(banner)}" class="{banner.classNames}" :if={banner.urgent?}>
|
|
235
|
+
<span class="message">{{banner.message}}</span>
|
|
236
|
+
</div>
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Splatted Attributes
|
|
240
|
+
`ORB` supports attribute splatting for both HTML tags and view components through the `**attributes` syntax. The expression provided for the splat must evaluate to a `Hash`, whose key-value pairs will be added as attributes to the tag. For example:
|
|
241
|
+
|
|
242
|
+
```html
|
|
243
|
+
<div **banner_attributes>
|
|
244
|
+
... content ...
|
|
245
|
+
</div>
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
### View Components
|
|
250
|
+
|
|
251
|
+
In `ORB` templates, you can render your view components as if they were HTML tags. The component class name is mapped to the tag name by omitting the configured namespace and the `Component` suffix.
|
|
252
|
+
|
|
253
|
+
For example, if you have a `Button` view component that may be defined as:
|
|
254
|
+
|
|
255
|
+
```ruby
|
|
256
|
+
class MyComponents::Button < ::ViewComponent::Base
|
|
257
|
+
def initialize(url: "#", **options)
|
|
258
|
+
@url = url
|
|
259
|
+
@options = options
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
erb_template <<-ERB
|
|
263
|
+
<%= link_to(@url, **@options.merge(class: "btn btn-primary")) do %>
|
|
264
|
+
<%= content %>
|
|
265
|
+
<% end %>
|
|
266
|
+
ERB
|
|
267
|
+
end
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
you can render the component in an ORB template `button.html.orb` as:
|
|
271
|
+
|
|
272
|
+
```jsx
|
|
273
|
+
<Button url="/click_me">I am a button</Button>
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### ViewComponent Slots
|
|
277
|
+
|
|
278
|
+
`ORB` also provides a DSL for invoking a component slot and passing content to the slot through the `Component:Slot` syntax. For example, if you have a `Card` component that defines a `Sections` slot via `renders_many :sections, Card::Section`, you can invoke and fill the slot in an `ORB` template like this:
|
|
279
|
+
|
|
280
|
+
```html
|
|
281
|
+
<Card title="Products">
|
|
282
|
+
<Card:Section title="Description">
|
|
283
|
+
<p>Blue t-shirt in size L ...</p>
|
|
284
|
+
</Card:Section>
|
|
285
|
+
</Card>
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
### Namespaces
|
|
289
|
+
|
|
290
|
+
Sometimes, you may want to organize your components in sub-namespaces, or use components from other libraries. `ORB` supports this through dot notation in the tag names. For example, if you have a `MyComponents::Admin::Button` component, you can render it in an `ORB` template like this:
|
|
291
|
+
|
|
292
|
+
```html
|
|
293
|
+
<Admin.Button url="/admin/click_me">Admin Button</Admin.Button>
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
If you have a third-party component `ThirdParty::UI::Modal`, you can render it like this:
|
|
297
|
+
|
|
298
|
+
```html
|
|
299
|
+
<ThirdParty.UI.Modal title="Terms and Conditions">
|
|
300
|
+
<p>...</p>
|
|
301
|
+
</ThirdParty.UI.Modal>
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
To make life easier when using components from a specific namespace frequently, you can configure additional namespaces in the `ORB` configuration:
|
|
305
|
+
|
|
306
|
+
```ruby
|
|
307
|
+
ORB.namespaces = ['MyComponents ThirdParty::UI']
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
Namespaces defined in this way will be searched in order of definition when resolving component tag names in templates.
|
|
311
|
+
|
|
312
|
+
### Comments
|
|
313
|
+
|
|
314
|
+
**Public comments** are sent to the browser, and can be read by users inspecting the page source. ORB considers default HTML comments `<!-- -->` to be public comments.
|
|
315
|
+
|
|
316
|
+
```heex
|
|
317
|
+
<!-- I will be sent to the browser -->
|
|
318
|
+
<p>Hello World!</p>
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
**Private comments**, unlike public comments, won't be sent to the browser. Use private comments to mark up your ORB template with annotations that you do not wish users to see.
|
|
322
|
+
|
|
323
|
+
```heex
|
|
324
|
+
{!-- I won't be sent to the browser --}
|
|
325
|
+
<p>Hello World!</p>
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
## Editor support
|
|
329
|
+
|
|
330
|
+
- `VSCode` through the [ORB VSCode Extension](https://github/kuyio/orb-vscode).
|
|
331
|
+
- `Zed` through the [ORB Zed Extension](https://github/kuyio/orb-zed).
|
|
332
|
+
- `Neovim` through the [ORB Neovim Plugin](https://github/kuyio/orb-neovim).
|
|
333
|
+
|
|
334
|
+
Your favorite editor is not listed? Feel free to contribute an extension/plugin for your editor of choice! The ORB tree-sitter grammar can be found [here](https://github.com/kuyio/tree-sitter-orb).
|
|
335
|
+
|
|
336
|
+
## Roadmap
|
|
337
|
+
|
|
338
|
+
- [x] **Step 1: Make it work**
|
|
339
|
+
- [x] streaming Lexer based on HTML spec state machine
|
|
340
|
+
- [x] parse token stream into AST
|
|
341
|
+
- [x] properly handle void tags and mark void-tags as self-closing
|
|
342
|
+
- [x] support standard HTML tags (`<div>...</div>`)
|
|
343
|
+
- [x] support dynamic attributes (`foo={bar}`)
|
|
344
|
+
- [x] support expressions (`{{ ... }}`)
|
|
345
|
+
- [x] public comments (`<!-- -->`), and private comments (`{!-- --}`)
|
|
346
|
+
- [x] support non-printing expressions (`{% ... %}`)
|
|
347
|
+
- [x] support conditional blocks (`{#if ...}`)
|
|
348
|
+
- [x] support iterative blocks (`{#for ...}` )
|
|
349
|
+
- [x] support component tags (`<Card> ... </Card>`)
|
|
350
|
+
- [x] support slot tags (`<Card:section> ... </Card:section>`)
|
|
351
|
+
- [x] compile AST to Temple core expressions
|
|
352
|
+
- [x] a Temple engine that renders to HTML with useful pre- and post-processing steps to generate well-formed output
|
|
353
|
+
- [x] a Railtie that automatically registers ORB's temple engine as handler for `*.orb` templates
|
|
354
|
+
- [x] basic test suite covering lexer, parser, compiler
|
|
355
|
+
- [x] basic errors for lexer, parser, compiler
|
|
356
|
+
- [x] Extensions for code editor support
|
|
357
|
+
- [ ] **Step 2: Make it nice**
|
|
358
|
+
- [x] improved errors with super helpful error messages and locations throughout the entire stack, possibly custom rendered error pages
|
|
359
|
+
- [x] `**attribute` splats for html tags, components and slots
|
|
360
|
+
- [x] `:if` directive
|
|
361
|
+
- [x] `:for` directive
|
|
362
|
+
- [x] verbatim tags
|
|
363
|
+
- [x] ensure output safety and proper escaping of output
|
|
364
|
+
- [x] track locations (start_line, start_col, end_line, end_col) for Tokens and AST Nodes to support better error output
|
|
365
|
+
- [x] make Lexer, Parser, Compiler robust to malformed input (e.g., unclosed tags)
|
|
366
|
+
- [ ] emit an warning/error when void tags contain children
|
|
367
|
+
- [ ] support for special comments, like `CDATA`
|
|
368
|
+
- [ ] support sub-blocks (`{#else}`, `{#elsif}`, ...)
|
|
369
|
+
- [ ] support `{#cond ...}` blocks
|
|
370
|
+
- [ ] support `{#case ...}` blocks
|
|
371
|
+
- [ ] support `{#unless ...}` blocks
|
|
372
|
+
- [ ] support raw output `{= ...}}` blocks
|
|
373
|
+
- [ ] `:show` directive
|
|
374
|
+
- [ ] include render tests in test suite
|
|
375
|
+
- [ ] make template engine configurable
|
|
376
|
+
- [ ] support embedded languages like `embedded ruby`, `markdown`, `rdoc`, `sass`, `scss`, `javascript`, `css`, ...
|
|
377
|
+
- [ ] compile AST to a StringBuffer so library can be used standalone / outside of Rails
|
|
378
|
+
- [ ] full YARD-compatible documentation of the library
|
|
379
|
+
- [ ] **Step 3: Make it fast**
|
|
380
|
+
- [x] convert Lexer code to `StringScanner`
|
|
381
|
+
- [ ] create benchmark suite to establish baseline
|
|
382
|
+
- [ ] possibly merge lexer states through more intelligent look-ahead
|
|
383
|
+
- [ ] optimize AST Parser
|
|
384
|
+
- [ ] optimize Compiler
|
|
385
|
+
- [ ] **Step 4: Evolve**
|
|
386
|
+
- [ ] support additional directives, for instance, `Turbo` or `Stimulus` specific directives
|
|
387
|
+
- [ ] support additional block constructs
|
|
388
|
+
- [ ] support additional language constructs
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
> This library is in beta stage and demonstrates the technical aspects of a custom DSL for rendering ViewComponent objects in an HTML-like manner. It is meant as a kick-off point for further discussion on the definition and implementation of the template language. It may contain critical bugs that could compromise the security and integrity of your application. Additionally, the API and DSL are likely to change as the library evolves to a stable state. Don't say we didn't warn you!
|
|
392
|
+
|
|
393
|
+
## Development
|
|
394
|
+
|
|
395
|
+
To set up your development environment, follow these steps:
|
|
396
|
+
1. Clone the repository:
|
|
397
|
+
|
|
398
|
+
```bash
|
|
399
|
+
git clone https://github.com/kuyio/orb_template.git
|
|
400
|
+
cd orb_template
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
2. Install dependencies:
|
|
404
|
+
|
|
405
|
+
```bash
|
|
406
|
+
bundle install
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
3. Run the test suite to ensure everything is set up correctly:
|
|
410
|
+
|
|
411
|
+
```bash
|
|
412
|
+
make test
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
4. Start the development server for the test application:
|
|
416
|
+
|
|
417
|
+
```bash
|
|
418
|
+
bin/rails server
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
## Contributing
|
|
423
|
+
|
|
424
|
+
This project is intended to be a safe, welcoming space for collaboration. Contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. We recommend reading the [contributing guide](./docs/CONTRIBUTING.md) as well.
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
## License
|
|
428
|
+
|
|
429
|
+
ORB is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bundler/gem_tasks"
|
|
4
|
+
require "rubocop/rake_task"
|
|
5
|
+
require "rake/testtask"
|
|
6
|
+
|
|
7
|
+
RuboCop::RakeTask.new
|
|
8
|
+
|
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
|
10
|
+
t.libs << "test"
|
|
11
|
+
t.libs << "lib"
|
|
12
|
+
t.test_files = FileList['test/**/*_test.rb']
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
task default: %i[rubocop test]
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ORB
|
|
4
|
+
module AST
|
|
5
|
+
class AbstractNode
|
|
6
|
+
attr_accessor :children, :attributes, :errors
|
|
7
|
+
|
|
8
|
+
def initialize(*_args)
|
|
9
|
+
@children = []
|
|
10
|
+
@errors = []
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def add_child(node)
|
|
14
|
+
@children << node
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def render(_context)
|
|
18
|
+
raise "Not implemented - you must implement render in your subclass!"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def ==(other)
|
|
22
|
+
self.class == other.class &&
|
|
23
|
+
@children == other.children
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|