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