super-slim 3.1.0 → 3.1.1
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 +4 -4
- data/CHANGELOG.md +21 -1
- data/README.md +280 -138
- data/lib/super_slim/cli.rb +40 -8
- data/lib/super_slim/compiler.rb +29 -10
- data/lib/super_slim/formatter/line_wrapper.rb +10 -4
- data/lib/super_slim/formatter.rb +63 -20
- data/lib/super_slim/linter/rules.rb +8 -11
- data/lib/super_slim/linter.rb +53 -6
- data/lib/super_slim/parallel.rb +31 -11
- data/lib/super_slim/parser/ruby_brackets.rb +115 -0
- data/lib/super_slim/parser.rb +60 -35
- data/lib/super_slim/rubocop.rb +27 -5
- data/lib/super_slim/runtime.rb +12 -4
- data/lib/super_slim/version.rb +1 -1
- data/lib/super_slim.rb +2 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8a3894df5f66ffe80daaf131669b5457659e414579256020b670719ec7bddf8b
|
|
4
|
+
data.tar.gz: 696bb115aa3776422a79dcde2f31984f35690ccf08af50cf43c9655c8cc2fc08
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ee4af8fa3325f7dcbea6a271776987caef9817a97a2e579ad38f01b4fc054a2c4e98ef04b03a10f8ca8efefd8415f8fc37431381aebe6facf8474c03b4eca2e5
|
|
7
|
+
data.tar.gz: 33e7e5274420e92acfe0c824ee5f710dd544c466f3e6ae2be22b386949d6d3917d607d8a4787ab2c6a639308002f1165c02ca4b3ead9be2e3eaf69328c311986
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,25 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [3.1.1] — 2026-11-06
|
|
6
|
+
|
|
7
|
+
Two ways for data to inject markup closed, hostile templates read in linear time, and Prism kept out of rendering.
|
|
8
|
+
|
|
9
|
+
**Fixed**
|
|
10
|
+
- Loading SuperSlim no longer loads Prism, which only `super-slim format` needs: since 3.1.0, every process that
|
|
11
|
+
loaded the gem, each Puma worker included, paid Prism's load time and about 7 MB.
|
|
12
|
+
- A tab, line feed, form feed or carriage return in a `*splat` or `data`/`aria` key raises
|
|
13
|
+
`InvalidAttributeNameError` instead of ending the attribute name, and `*{tag: …}` raises the new
|
|
14
|
+
`InvalidTagNameError` when its value is not a tag name: both let data inject markup, as in Slim 5.2.2. No valid
|
|
15
|
+
page changes.
|
|
16
|
+
- Hostile templates no longer hang or crash the compiler, `lint`, `format` or the RuboCop plugin: nested `#{`, long
|
|
17
|
+
continued code and deep nesting are read in linear time, and tags nested more than 500 levels deep are a syntax
|
|
18
|
+
error.
|
|
19
|
+
- On a hostile checkout, the CLI skips symlinks, FIFOs and devices, writes atomically and escapes control
|
|
20
|
+
characters in what it prints; a malformed `.super-slim.yml` is a usage error that names the setting.
|
|
21
|
+
- `super-slim format` refuses a wrap of a template that doesn't compile and a move of `__LINE__`, and its check
|
|
22
|
+
works on Prism 0.19 (Ruby 3.3) and Prism 2.
|
|
23
|
+
|
|
5
24
|
## [3.1.0] — 2026-11-06
|
|
6
25
|
|
|
7
26
|
A Bootstrap 5 preset for the linter, Ruby code that continues after an open bracket, and a faster formatter.
|
|
@@ -128,7 +147,8 @@ renders what Slim 5 renders with `format: :html, sort_attrs: false`, byte for by
|
|
|
128
147
|
- Ruby 3.3 or later, every Ruby still maintained, Rails 8.0 or 8.1 for the railtie, and strscan 3.1.1 or later,
|
|
129
148
|
newer than the one Ruby 3.3 ships.
|
|
130
149
|
|
|
131
|
-
[Unreleased]: https://github.com/levups/super-slim/compare/v3.1.
|
|
150
|
+
[Unreleased]: https://github.com/levups/super-slim/compare/v3.1.1...HEAD
|
|
151
|
+
[3.1.1]: https://github.com/levups/super-slim/compare/v3.1.0...v3.1.1
|
|
132
152
|
[3.1.0]: https://github.com/levups/super-slim/compare/v3.0.0...v3.1.0
|
|
133
153
|
[3.0.0]: https://github.com/levups/super-slim/compare/v2.2.0...v3.0.0
|
|
134
154
|
[2.2.0]: https://github.com/levups/super-slim/compare/v2.1.0...v2.2.0
|
data/README.md
CHANGED
|
@@ -1,134 +1,178 @@
|
|
|
1
1
|
# SuperSlim
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
SuperSlim renders [Slim](https://github.com/slim-template/slim) templates in Ruby and Rails apps. Slim is a template
|
|
4
|
+
language that writes HTML with indentation instead of closing tags, and SuperSlim is a new engine for it, written
|
|
5
|
+
from scratch: your existing `.slim` files produce the same HTML, but they compile about nine times faster and
|
|
6
|
+
render about twice as fast.
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
It also comes with a formatter, a linter and a RuboCop plugin. All three read templates with the engine's own
|
|
9
|
+
parser, so they never disagree with it about what a template means.
|
|
10
|
+
|
|
11
|
+
Website and guide: https://levups.github.io/super-slim/
|
|
12
|
+
|
|
13
|
+
## Why SuperSlim
|
|
14
|
+
|
|
15
|
+
A template engine does two jobs. It compiles each template into Ruby code, which Rails does the first time a
|
|
16
|
+
template renders and again after you edit it in development. Then it runs that code on every request. SuperSlim
|
|
17
|
+
makes both cheaper.
|
|
18
|
+
|
|
19
|
+
`bench/compare.rb` renders the same blog page (24 post cards) with five engines and checks that they all produce
|
|
20
|
+
the same HTML. On the last run (November 2026, Ruby 4.0 with YJIT), SuperSlim compiled the page 9.3 times faster
|
|
21
|
+
than Slim and rendered it 2.3 times faster, with a quarter of the allocations:
|
|
22
|
+
|
|
23
|
+
| Engine | Compiles per second | Renders per second | Objects allocated per render |
|
|
24
|
+
|---|---:|---:|---:|
|
|
25
|
+
| SuperSlim 1.0.0 | 12,423 | 60,725 | 111 |
|
|
26
|
+
| Slim 5.2.2 | 1,341 | 26,786 | 480 |
|
|
27
|
+
| Haml 7.5.2 | 1,559 | 37,704 | 471 |
|
|
28
|
+
| Herb 0.11.1 | 5,600 | 17,496 | 830 |
|
|
29
|
+
| Erubi 1.13.1 (ERB) | 51,144 | 62,746 | 115 |
|
|
30
|
+
|
|
31
|
+
On whole apps, the 1,399 templates of a production Rails app compile in 108 ms instead of 522 ms, and 6,053
|
|
32
|
+
templates from 30 open-source projects in 529 ms instead of 2,472 ms. The linter takes about a third of the CPU
|
|
33
|
+
time slim-lint takes on the same templates. The [benchmarks page](https://levups.github.io/super-slim/benchmarks.html)
|
|
34
|
+
has the charts and the steps to reproduce them.
|
|
8
35
|
|
|
9
36
|
## Installation
|
|
10
37
|
|
|
38
|
+
SuperSlim needs Ruby 3.3 or later. In a Rails app, add it to your Gemfile, and remove `slim-rails` if you have it:
|
|
39
|
+
SuperSlim replaces it.
|
|
40
|
+
|
|
11
41
|
```ruby
|
|
12
|
-
|
|
13
|
-
gem "super-slim" # instead of slim-rails
|
|
42
|
+
gem "super-slim"
|
|
14
43
|
```
|
|
15
44
|
|
|
16
|
-
|
|
45
|
+
Rails then renders every `.slim` view with SuperSlim. There is nothing to configure. Rails itself is optional, and
|
|
46
|
+
SuperSlim is tested on Rails 8.0 and later.
|
|
17
47
|
|
|
18
|
-
|
|
48
|
+
Without Rails, `SuperSlim.compile` turns a template into Ruby code, which you evaluate where the template's
|
|
49
|
+
variables and methods are defined:
|
|
19
50
|
|
|
20
|
-
|
|
51
|
+
```ruby
|
|
52
|
+
require "super_slim"
|
|
21
53
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
54
|
+
ruby_code = SuperSlim.compile(File.read("page.slim"), file: "page.slim") # file: is shown in errors and backtraces
|
|
55
|
+
html = eval(ruby_code, binding)
|
|
56
|
+
```
|
|
25
57
|
|
|
26
|
-
|
|
27
|
-
any delimiter, boolean attributes, `*splat`, `data`/`aria` hashes, `class` merging, `<`/`>` whitespace
|
|
28
|
-
markers, `|` and `'` text, inline HTML, `/` and `/!` comments, `-` control code, `=`/`==` output, `#{}` and
|
|
29
|
-
`#{{}}` interpolation, Slim's doctypes (`html`, `transitional` for email clients, their XHTML versions in XML
|
|
30
|
-
templates), and the `ruby:`, `javascript:`, `css:` and `markdown:` (through
|
|
31
|
-
[Markly](https://github.com/ioquatix/markly)) embedded engines.
|
|
58
|
+
## A first template
|
|
32
59
|
|
|
33
|
-
|
|
60
|
+
```slim
|
|
61
|
+
ul.posts
|
|
62
|
+
- posts.each do |post|
|
|
63
|
+
li
|
|
64
|
+
a href=post.url = post.title
|
|
65
|
+
- if post.draft?
|
|
66
|
+
span.badge Draft
|
|
67
|
+
```
|
|
34
68
|
|
|
35
|
-
|
|
36
|
-
that ends the line, until that bracket closes, so calls, arrays and hashes can open on one line and close on their
|
|
37
|
-
own:
|
|
69
|
+
With two posts, the second one a draft, this renders:
|
|
38
70
|
|
|
39
|
-
```
|
|
40
|
-
=
|
|
41
|
-
product:,
|
|
42
|
-
variant: :compact,
|
|
43
|
-
)
|
|
44
|
-
- columns = %w[
|
|
45
|
-
name
|
|
46
|
-
price
|
|
47
|
-
]
|
|
71
|
+
```html
|
|
72
|
+
<ul class="posts"><li><a href="/posts/1">Hello & welcome</a></li><li><a href="/posts/2">Next steps</a><span class="badge">Draft</span></li></ul>
|
|
48
73
|
```
|
|
49
74
|
|
|
50
|
-
|
|
51
|
-
|
|
75
|
+
Lines starting with `-` run Ruby, `=` outputs the result of Ruby code (escaped for HTML), and everything else is a
|
|
76
|
+
tag, text or an attribute. The [Slim documentation](https://github.com/slim-template/slim#readme) describes the whole
|
|
77
|
+
language. SuperSlim reads all of it, except the few parts listed in [What SuperSlim leaves out](#what-superslim-leaves-out).
|
|
78
|
+
|
|
79
|
+
## Switching from Slim
|
|
80
|
+
|
|
81
|
+
SuperSlim renders what Slim renders with the options `format: :html, sort_attrs: false`, byte for byte. Slim's
|
|
82
|
+
defaults are different (`format: :xhtml, sort_attrs: true`), so if you never changed them, switch in two steps:
|
|
52
83
|
|
|
53
|
-
|
|
84
|
+
1. Set those two options on Slim and run your test suite. Fix anything that depended on XHTML output or on
|
|
85
|
+
sorted attributes.
|
|
86
|
+
2. Replace `slim-rails` with `super-slim` in your Gemfile and remove any `Slim::Engine.set_options` or
|
|
87
|
+
`with_options` call.
|
|
54
88
|
|
|
55
|
-
|
|
56
|
-
`
|
|
89
|
+
Templates for XML formats (`.xml`, `.rss`, `.atom`) render as XML automatically, with `<link />` and
|
|
90
|
+
`doctype xml`.
|
|
91
|
+
|
|
92
|
+
## Formatter
|
|
93
|
+
|
|
94
|
+
`super-slim format` rewrites templates in one layout: two spaces per level, a space after `-` and `=`, no `div`
|
|
95
|
+
where a `.class` or `#id` is enough, no trailing whitespace, single blank lines and a final newline. This template:
|
|
57
96
|
|
|
58
97
|
```slim
|
|
59
|
-
.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
98
|
+
div.card
|
|
99
|
+
-if post.draft?
|
|
100
|
+
span.badge Draft
|
|
101
|
+
=link_to post.title, post
|
|
63
102
|
|
|
64
|
-
```ruby
|
|
65
|
-
SuperSlim.tailwind_classes = true # config/initializers/super_slim.rb, or tailwind_classes: true per compile
|
|
66
|
-
```
|
|
67
103
|
|
|
68
|
-
|
|
69
|
-
# .super-slim.yml, for super-slim lint and super-slim format
|
|
70
|
-
tailwind_classes: true
|
|
104
|
+
img src=post.image_url
|
|
71
105
|
```
|
|
72
106
|
|
|
73
|
-
|
|
74
|
-
`a.btn[href="/"]` is still a link with a class and an `href`. A colon followed by a space still nests a tag
|
|
75
|
-
(`li.item: a`), and a final `/` still closes one. What changes is what Slim mangles: it renders `.size-2.5` as
|
|
76
|
-
`class="size-2 5"`, the option as `class="size-2.5"`. On 7,505 templates from 31 apps, 16 compile differently
|
|
77
|
-
with the option, all of them Tailwind classes Slim breaks. It stays off by default all the same, so that
|
|
78
|
-
turning SuperSlim on changes nothing.
|
|
107
|
+
comes out as:
|
|
79
108
|
|
|
80
|
-
|
|
81
|
-
|
|
109
|
+
```slim
|
|
110
|
+
.card
|
|
111
|
+
- if post.draft?
|
|
112
|
+
span.badge Draft
|
|
113
|
+
= link_to post.title, post
|
|
82
114
|
|
|
83
|
-
|
|
115
|
+
img src=post.image_url
|
|
116
|
+
```
|
|
84
117
|
|
|
85
|
-
|
|
118
|
+
```sh
|
|
119
|
+
super-slim format # every .slim file under the current directory (except .git, node_modules, tmp, vendor)
|
|
120
|
+
super-slim format --check # list the files that would change, and fail if there are any
|
|
121
|
+
super-slim format app/views/posts
|
|
122
|
+
```
|
|
86
123
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
124
|
+
`fmt` is an alias of `format`.
|
|
125
|
+
|
|
126
|
+
The formatter can't change what a page renders. Before writing a file, it compiles the old and the new version and
|
|
127
|
+
compares the Ruby code they produce, using [Prism](https://github.com/ruby/prism). Line breaks, spaces, a trailing
|
|
128
|
+
comma or the parentheses of a call may differ. If anything else does, it leaves the file alone and reports it. Text
|
|
129
|
+
blocks move together with their whitespace for the same reason.
|
|
91
130
|
|
|
92
|
-
|
|
131
|
+
To also wrap long lines, set a maximum length in `.super-slim.yml`:
|
|
93
132
|
|
|
94
|
-
|
|
133
|
+
```yaml
|
|
134
|
+
format:
|
|
135
|
+
max_line_length: 120
|
|
136
|
+
attribute_delimiters: "()" # or "[]" or "{}"
|
|
137
|
+
```
|
|
95
138
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
`slim-rails` for `super-slim`, and drop any `Slim::Engine.set_options`/`with_options` call.
|
|
139
|
+
The formatter then puts a long tag's attributes one per line inside those delimiters, and breaks the arguments
|
|
140
|
+
of long Ruby calls after their commas. Lines you already split over several lines stay as they are.
|
|
99
141
|
|
|
100
|
-
##
|
|
142
|
+
## Linter
|
|
101
143
|
|
|
102
|
-
|
|
144
|
+
```sh
|
|
145
|
+
super-slim lint # every .slim file under the current directory
|
|
146
|
+
super-slim lint app/views/posts
|
|
147
|
+
```
|
|
103
148
|
|
|
104
|
-
|
|
105
|
-
`= output` and `p = output` spacing, no redundant `div`, no trailing whitespace, single blank lines, one final
|
|
106
|
-
newline. Text blocks move as a whole, so the whitespace they render stays put — and before writing anything,
|
|
107
|
-
the formatter compiles both versions and refuses unless Prism reads the same Ruby in both, positions aside: line
|
|
108
|
-
breaks, spaces, a trailing comma or the parentheses of a call may differ, nothing else. Formatting cannot change a
|
|
109
|
-
page.
|
|
149
|
+
On the formatted template above, it still finds the image without a text alternative:
|
|
110
150
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
151
|
+
```
|
|
152
|
+
_card.html.slim:6: [ImageAlt] Describe the image with `alt`, or mark it decorative with `alt=""`
|
|
153
|
+
```
|
|
114
154
|
|
|
115
|
-
|
|
155
|
+
The linter reads its settings from `.super-slim.yml`, in the directory you run it from. Its rules come from other
|
|
156
|
+
linters, so most will look familiar:
|
|
116
157
|
|
|
117
|
-
| Rule |
|
|
158
|
+
| Rule | Comes from | Default |
|
|
118
159
|
|---|---|---|
|
|
119
|
-
| Indentation, Tab, TrailingWhitespace, EmptyLines, TrailingBlankLines, ControlStatementSpacing, RedundantDiv | slim-lint
|
|
160
|
+
| Indentation, Tab, TrailingWhitespace, EmptyLines, TrailingBlankLines, ControlStatementSpacing, RedundantDiv | slim-lint (`super-slim format` fixes them) | on |
|
|
120
161
|
| LineLength (`max: 120`), CommentControlStatement, EmptyControlStatement, ConsecutiveControlStatements (`max: 2`), TagCase | slim-lint | on |
|
|
121
162
|
| InstanceVariables (in partials) | haml-lint, Rails conventions | on |
|
|
122
163
|
| StrictLocals (partials declare `/# locals:`) | Rails 7.1 strict locals | off |
|
|
123
164
|
| DuplicateId, ImageAlt, IframeTitle, AnchorHref, NestedLinks, PositiveTabindex, BooleanAttributeValue, AriaRole, AriaAttribute | Herb, eslint-plugin-jsx-a11y | on |
|
|
124
165
|
| ButtonType, InlineStyles | eslint-plugin-react, haml-lint | off |
|
|
125
|
-
| DeprecatedClasses (`classes: {old: new}`, `old-*: new-*` for a prefix), DeprecatedAttributes (`attributes: {old: new}`), both with `preset: bootstrap5` | erb_lint | on, empty |
|
|
166
|
+
| DeprecatedClasses (`classes: {old: new}`, `old-*: new-*` for a prefix), DeprecatedAttributes (`attributes: {old: new}`), both with `preset: bootstrap5` | erb_lint | on, but empty |
|
|
126
167
|
| UnescapedOutput (`==`, `html_safe`, `raw`, `#{{}}`) | Brakeman, erb_lint | off |
|
|
127
168
|
| EmbeddedEngines (`forbidden_engines: [javascript, css]`) | slim-lint | off |
|
|
128
|
-
| HardcodedText (text
|
|
169
|
+
| HardcodedText (text that should be translated) | erb_lint | off |
|
|
129
170
|
| UnnecessaryInterpolation, UnnecessaryStringOutput | haml-lint | on |
|
|
130
171
|
| Zwsp (invisible characters) | slim-lint | on |
|
|
131
172
|
|
|
173
|
+
The website shows [each rule with an example](https://levups.github.io/super-slim/tools.html). A configuration
|
|
174
|
+
file looks like this:
|
|
175
|
+
|
|
132
176
|
```yaml
|
|
133
177
|
# .super-slim.yml
|
|
134
178
|
linters:
|
|
@@ -137,9 +181,9 @@ linters:
|
|
|
137
181
|
ButtonType:
|
|
138
182
|
enabled: true
|
|
139
183
|
DeprecatedClasses:
|
|
140
|
-
preset: bootstrap5 # Bootstrap 4 classes Bootstrap 5.3 renamed or dropped
|
|
184
|
+
preset: bootstrap5 # Bootstrap 4 classes that Bootstrap 5.3 renamed or dropped
|
|
141
185
|
classes:
|
|
142
|
-
media: ~ #
|
|
186
|
+
media: ~ # removes this entry from the preset
|
|
143
187
|
legacy-*: modern-*
|
|
144
188
|
DeprecatedAttributes:
|
|
145
189
|
preset: bootstrap5 # data-toggle becomes data-bs-toggle, and so on
|
|
@@ -151,21 +195,23 @@ linters:
|
|
|
151
195
|
include: ["app/views/shop/**/*.slim"]
|
|
152
196
|
```
|
|
153
197
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
generic ones like `data-content` or `data-delay` go in `attributes:` if your app needs them, and `~` drops an entry of
|
|
157
|
-
the preset your own JavaScript reads.
|
|
198
|
+
Every rule accepts `include:` and `exclude:`, lists of glob patterns matched against paths relative to
|
|
199
|
+
`.super-slim.yml` (`**/` matches any number of directories).
|
|
158
200
|
|
|
159
|
-
|
|
160
|
-
|
|
201
|
+
The `bootstrap5` preset lists the classes and data attributes that Bootstrap 5.3 renamed, including the ones with
|
|
202
|
+
breakpoints such as `text-md-right`. Its data attributes are only those of Bootstrap's own components
|
|
203
|
+
(`data-toggle`, `data-target` and the like). If your app also uses generic ones such as `data-content`, add them
|
|
204
|
+
under `attributes:`. If your own JavaScript still reads an attribute the preset renames, remove that entry with `~`.
|
|
161
205
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
it slimi and Temple
|
|
206
|
+
## RuboCop plugin
|
|
207
|
+
|
|
208
|
+
The linter checks the Slim side of a template. For the Ruby inside it, SuperSlim has a RuboCop plugin, which
|
|
209
|
+
replaces rubocop-slim (and with it slimi and Temple). It passes every `-` and `=` line, Ruby attribute, splat,
|
|
210
|
+
`#{}` interpolation and `ruby:` block to RuboCop. Offenses point at the right line and column of the `.slim` file,
|
|
211
|
+
and `rubocop -a` writes its corrections back into it.
|
|
166
212
|
|
|
167
213
|
```yaml
|
|
168
|
-
# .rubocop-slim.yml, run with rubocop -c .rubocop-slim.yml
|
|
214
|
+
# .rubocop-slim.yml, run with: rubocop -c .rubocop-slim.yml
|
|
169
215
|
inherit_from: .rubocop.yml
|
|
170
216
|
plugins:
|
|
171
217
|
- super-slim
|
|
@@ -174,74 +220,170 @@ AllCops:
|
|
|
174
220
|
- "**/*.slim"
|
|
175
221
|
```
|
|
176
222
|
|
|
177
|
-
|
|
178
|
-
`Lint/UselessAssignment` on a variable a later line
|
|
179
|
-
does.
|
|
180
|
-
(`span> title="x" = name`), so a few offenses
|
|
223
|
+
Some cops make no sense inside a template, such as indentation and alignment across lines, the frozen string
|
|
224
|
+
comment, or `Lint/UselessAssignment` on a variable that a later line uses. The plugin turns them off for `.slim`
|
|
225
|
+
files, as rubocop-slim does. SuperSlim also reads code that slimi skips, for example the output of a tag with
|
|
226
|
+
attributes and whitespace markers (`span> title="x" = name`), so you may see a few new offenses after switching.
|
|
181
227
|
|
|
182
|
-
##
|
|
228
|
+
## Additions to Slim
|
|
229
|
+
|
|
230
|
+
Two additions read templates that Slim refuses or misreads. Neither changes a template that already works with
|
|
231
|
+
Slim.
|
|
183
232
|
|
|
184
|
-
|
|
185
|
-
through Slim and SuperSlim, and both outputs must match.
|
|
186
|
-
- A seeded fuzzer generates thousands of random templates from the grammar and compares both engines; CI adds
|
|
187
|
-
10,000 from a new seed on every run.
|
|
188
|
-
- Golden snapshots (`test/golden`) hold the rendered HTML, compiled Ruby, formatting and lint offenses of
|
|
189
|
-
templates covering the grammar, its errors and Tailwind class names: `UPDATE_GOLDEN=1` rewrites them.
|
|
190
|
-
- `bench/compiled_digests.rb` proves a refactoring leaves the compiled Ruby of a whole corpus unchanged.
|
|
233
|
+
### Ruby code over several lines
|
|
191
234
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
against Slim, and `rake corpus:diff BASE=rev` reports what changes between a revision and the working tree:
|
|
195
|
-
compiled Ruby, formatting, lint offenses. Downloaded templates are never rendered, as rendering runs their
|
|
196
|
-
Ruby; directories you trust, such as your own app, render too when passed in `SUPER_SLIM_CORPUS`.
|
|
235
|
+
Slim lets a line of Ruby continue on the next line after a comma or a backslash. SuperSlim also continues it after
|
|
236
|
+
an opening `(`, `[` or `{` at the end of a line, until the bracket is closed:
|
|
197
237
|
|
|
198
|
-
|
|
238
|
+
```slim
|
|
239
|
+
= render ProductCardComponent.new(
|
|
240
|
+
product:,
|
|
241
|
+
variant: :compact,
|
|
242
|
+
)
|
|
243
|
+
- columns = %w[
|
|
244
|
+
name
|
|
245
|
+
price
|
|
246
|
+
]
|
|
247
|
+
```
|
|
199
248
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
the website reads. Run it before each release, on a quiet machine:
|
|
249
|
+
A bracket opened in the middle of a line, as in `- items.each { |item|`, works as before: the lines below it are
|
|
250
|
+
its children.
|
|
203
251
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
252
|
+
### Tailwind class names
|
|
253
|
+
|
|
254
|
+
Slim stops a `.class` shortcut at characters such as `[`, `:` or `/`, which Tailwind uses all the time. It renders
|
|
255
|
+
`.size-2.5` as `class="size-2 5"`, and refuses `.lg:[&:nth-child(3)]:hover:underline`. With `tailwind_classes` on,
|
|
256
|
+
the shortcut takes any Tailwind class name:
|
|
257
|
+
|
|
258
|
+
```slim
|
|
259
|
+
.p-1.rounded-b-[3px].lg:[&:nth-child(3)]:hover:underline
|
|
260
|
+
.font-['Open_Sans'].bg-(--brand).mx-0.5.w-1/2.!font-bold.@lg:flex
|
|
261
|
+
ul.*:p-2
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
Turn it on for the engine, in `config/initializers/super_slim.rb` for example (or per call with
|
|
265
|
+
`SuperSlim.compile(source, tailwind_classes: true)`):
|
|
266
|
+
|
|
267
|
+
```ruby
|
|
268
|
+
SuperSlim.tailwind_classes = true
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
And for the formatter and the linter, in `.super-slim.yml`:
|
|
272
|
+
|
|
273
|
+
```yaml
|
|
274
|
+
tailwind_classes: true
|
|
208
275
|
```
|
|
209
276
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
277
|
+
Everything else reads as before. Tailwind never puts a bracket right after a letter or a digit, so in
|
|
278
|
+
`a.btn[href="/"]` the brackets are still an attribute list. `li.item: a` still nests a tag, and a final `/` still
|
|
279
|
+
closes one. On 7,505 templates from 31 apps, 16 compiled differently with the option on, and each time it was a
|
|
280
|
+
Tailwind class that Slim broke. The option is off by default anyway, so that switching to SuperSlim changes
|
|
281
|
+
nothing. It costs nothing measurable: compile time on 1,408 templates moved by 0.5%, within noise, and the
|
|
282
|
+
compiled code is the same, so render time is too.
|
|
283
|
+
|
|
284
|
+
## Escaping
|
|
285
|
+
|
|
286
|
+
Like Slim, SuperSlim escapes for HTML everything that `=`, `#{}` and attribute values output. `==` and `#{{}}`
|
|
287
|
+
output raw HTML, so only use them on content you trust. In Rails, strings marked `html_safe` aren't escaped again.
|
|
288
|
+
|
|
289
|
+
Some names are only known when the page renders: the keys of a `*splat` hash, of a `data:` or `aria:` hash, and
|
|
290
|
+
the tag of `*{tag: …}`. SuperSlim checks those names and raises an error instead of rendering one that would break
|
|
291
|
+
the HTML. Slim does not check them. With Slim, `data: {"x\tonclick" => code}` renders an `onclick` attribute, and
|
|
292
|
+
`*{tag: "img src=x onerror=…"}` renders an image with an `onerror` handler. SuperSlim raises
|
|
293
|
+
`SuperSlim::InvalidAttributeNameError` and `SuperSlim::InvalidTagNameError` instead.
|
|
294
|
+
|
|
295
|
+
HTML escaping protects HTML, and nothing else. A `#{}` of user input inside `javascript:` or `css:`, inside an
|
|
296
|
+
unquoted attribute of inline HTML, or in the URL of a `markdown:` link can still inject code. In scripts, write
|
|
297
|
+
`#{{json_escape(value.to_json)}}` (Rails), always quote attributes, and check URLs before they reach a template.
|
|
298
|
+
|
|
299
|
+
## What SuperSlim leaves out
|
|
300
|
+
|
|
301
|
+
These parts of Slim aren't supported. Each one fails when the template compiles, with an error that names it:
|
|
302
|
+
|
|
303
|
+
- the XHTML format, the `sort_attrs`, `attr_quote`, `pretty` and `js_wrapper` options, and every Temple option;
|
|
304
|
+
- the `coffee:`, `sass:`, `scss:`, `less:`, `erb:`, `builder:`, `textile:`, `rdoc:`, `asciidoc:` and wiki
|
|
305
|
+
engines, and `/[if IE]` conditional comments;
|
|
306
|
+
- the `logic_less`, `translator`, `include` and `smart` plugins, and custom shortcuts (only `#id` and `.class`
|
|
307
|
+
exist).
|
|
308
|
+
|
|
309
|
+
The embedded engines that do work are `ruby:`, `javascript:`, `css:` and `markdown:`. `markdown:` uses
|
|
310
|
+
[Markly](https://github.com/ioquatix/markly), so add `gem "markly"` to your Gemfile if you use it. The rest can
|
|
311
|
+
come back if people ask for them, as long as they don't slow down rendering.
|
|
312
|
+
|
|
313
|
+
## Learn more
|
|
314
|
+
|
|
315
|
+
The website has a [guide](https://levups.github.io/super-slim/guide.html) with every option, a page on the
|
|
316
|
+
[formatter and linter](https://levups.github.io/super-slim/tools.html) with an example for each rule, the
|
|
317
|
+
[benchmarks](https://levups.github.io/super-slim/benchmarks.html) and the
|
|
318
|
+
[test setup](https://levups.github.io/super-slim/testing.html). [CHANGELOG.md](CHANGELOG.md) lists the changes in
|
|
319
|
+
each version.
|
|
320
|
+
|
|
321
|
+
## How it is tested
|
|
322
|
+
|
|
323
|
+
Slim is the reference. Its own test suite, including the examples of its specification (`TESTS.md`), runs against
|
|
324
|
+
SuperSlim: every template is rendered by both engines, and the HTML has to match.
|
|
325
|
+
|
|
326
|
+
On top of that:
|
|
327
|
+
|
|
328
|
+
- A fuzzer builds thousands of random templates from the grammar and compares the output of both engines. CI runs
|
|
329
|
+
10,000 new ones on every build.
|
|
330
|
+
- Snapshots in `test/golden` record the HTML, the compiled Ruby, the formatted template and the lint offenses for
|
|
331
|
+
templates covering the whole grammar, its errors and Tailwind class names. `UPDATE_GOLDEN=1` rewrites them after
|
|
332
|
+
a deliberate change.
|
|
333
|
+
- `bench/compiled_digests.rb` checks that a refactoring leaves the compiled Ruby of a whole set of templates
|
|
334
|
+
unchanged.
|
|
335
|
+
|
|
336
|
+
Real templates come from 30 open-source apps, pinned to a commit in `corpus/manifest.yml`. `rake corpus:fetch`
|
|
337
|
+
downloads only their `.slim` files, into `corpus/cache` (ignored by Git). `rake corpus:check` compiles, formats
|
|
338
|
+
and lints each of them and compares the result with Slim. `rake corpus:diff BASE=rev` shows what a change does to
|
|
339
|
+
them: compiled Ruby, formatting and lint offenses. These templates are never rendered, because rendering would run
|
|
340
|
+
their Ruby code. Your own templates can be rendered too: pass their directories in `SUPER_SLIM_CORPUS`.
|
|
213
341
|
|
|
214
342
|
## Development
|
|
215
343
|
|
|
216
344
|
```sh
|
|
217
345
|
bundle install
|
|
218
|
-
bundle exec rake test
|
|
346
|
+
bundle exec rake test # SuperSlim's tests, then Slim's own suites
|
|
219
347
|
BUNDLE_GEMFILE=gemfiles/rails_8_0.gemfile bundle exec rake test # the same on Rails 8.0
|
|
220
348
|
bundle exec rubocop
|
|
221
|
-
FUZZ_SEED=42 FUZZ_CASES=20000 bundle exec ruby -Ilib test/fuzz_test.rb
|
|
222
|
-
bundle exec ruby -Ilib bench/corpus.rb path/to/app
|
|
223
|
-
bundle exec ruby --yjit -Ilib bench/render.rb
|
|
349
|
+
FUZZ_SEED=42 FUZZ_CASES=20000 bundle exec ruby -Ilib -Itest test/fuzz_test.rb
|
|
350
|
+
bundle exec ruby -Ilib bench/corpus.rb path/to/app # compile time and allocations against Slim
|
|
351
|
+
bundle exec ruby --yjit -Ilib bench/render.rb # render speed and allocations against Slim
|
|
352
|
+
bundle exec rake docs # rebuild the linter rules page of the website
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
The website's numbers come from `docs/benchmarks.json`, which `rake bench` rewrites by running `bench/compare.rb`.
|
|
356
|
+
If `corpus/cache` is there, it also times the open-source templates, and compares `super-slim lint` and `format`
|
|
357
|
+
with slim-lint when slim-lint is installed. Run it on a quiet machine:
|
|
358
|
+
|
|
359
|
+
```sh
|
|
360
|
+
bundle exec rake bench # with YJIT, 5 seconds per measurement
|
|
361
|
+
SUPER_SLIM_CORPUS=path/to/app/views bundle exec rake bench # also times compiling a whole app against Slim
|
|
362
|
+
BENCH_LOCK=path/to/.bench.lock bundle exec rake bench # waits while another benchmark holds the lock
|
|
224
363
|
```
|
|
225
364
|
|
|
365
|
+
[CONTRIBUTING.md](CONTRIBUTING.md) explains how to propose a change. Please report security issues privately, as
|
|
366
|
+
[SECURITY.md](SECURITY.md) describes.
|
|
367
|
+
|
|
226
368
|
### Releasing
|
|
227
369
|
|
|
228
|
-
Pull requests
|
|
229
|
-
pull requests merged since the last tag, without committing them. Refresh the website
|
|
230
|
-
(`SUPER_SLIM_CORPUS=path/to/app/views bundle exec rake bench`) and merge them
|
|
231
|
-
up-to-date `main`
|
|
370
|
+
Pull requests don't touch `CHANGELOG.md`. A release starts by writing its notes under `## [Unreleased]`, from the
|
|
371
|
+
pull requests merged since the last tag, without committing them. Refresh the numbers on the website first, on a
|
|
372
|
+
quiet machine (`SUPER_SLIM_CORPUS=path/to/app/views bundle exec rake bench`), and merge them in their own pull
|
|
373
|
+
request. Then, from an up-to-date `main` with green CI:
|
|
232
374
|
|
|
233
375
|
```sh
|
|
234
|
-
bin/release minor --dry-run # the notes and the changes, nothing
|
|
376
|
+
bin/release minor --dry-run # shows the notes and the changes, writes nothing
|
|
235
377
|
bin/release minor # or patch, major, or an exact version such as 1.2.0
|
|
236
378
|
```
|
|
237
379
|
|
|
238
|
-
`main`
|
|
239
|
-
`release/vX.Y.Z
|
|
240
|
-
signed
|
|
241
|
-
the Release workflow, which publishes the gem to RubyGems through trusted publishing and
|
|
242
|
-
with the
|
|
380
|
+
Nobody pushes to `main` directly, so `bin/release` dates the Unreleased section and bumps the version on a
|
|
381
|
+
`release/vX.Y.Z` branch, opens a pull request that merges itself once CI passes, and waits for it. It then pushes
|
|
382
|
+
a signed `vX.Y.Z` tag on the merge commit (if you stopped it while it waited, `bin/release --tag X.Y.Z` does that
|
|
383
|
+
step). The tag starts the Release workflow, which publishes the gem to RubyGems through trusted publishing and
|
|
384
|
+
creates the GitHub release with the notes and the `.gem` file.
|
|
243
385
|
|
|
244
386
|
## License
|
|
245
387
|
|
|
246
|
-
MIT, by [Level UP Solutions](https://github.com/levups). The parser follows the
|
|
247
|
-
|
|
388
|
+
MIT, by [Level UP Solutions](https://github.com/levups). The parser follows the grammar and the parsing algorithm
|
|
389
|
+
of [Slim](https://github.com/slim-template/slim), by the Slim Team (MIT).
|