super-slim 3.6.1 → 3.6.3
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 +25 -1
- data/README.md +11 -5
- data/lib/super_slim/compiler.rb +44 -7
- data/lib/super_slim/parser.rb +9 -3
- data/lib/super_slim/version.rb +1 -1
- data/lib/super_slim.rb +29 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bdda53d82fc2a91f77cf19258e1ee428e89b765df89a9e91e93af3517a6c2f0f
|
|
4
|
+
data.tar.gz: 6998c577d2553c9ab39efc167c5bdbe576eecc891a959ec31d90b3ffc822c2bd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2adce8b7a0578e7488852e04e267fd8ff964021f59719602be4e018583b1faae79a264c8f165148352ff1879c303957d582917c1d030eaa5d086d4404e505da4
|
|
7
|
+
data.tar.gz: 6d40b5b0e0d8e68655c74237d82cf89f9d9afb0fd2053bf89cfb987a53d670b403de410055a711b640fa4f431529afc3e753fadbbe3fe09a36dc1a7fa5ca4bf4
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,28 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [3.6.3] — 2026-11-21
|
|
6
|
+
|
|
7
|
+
One fix: a line break inside an interpolation of a quoted attribute no longer shifts the lines that follow.
|
|
8
|
+
|
|
9
|
+
**Fixed**
|
|
10
|
+
- A quoted attribute whose `#{…}` spans lines, such as `p title="#{t(:title,` then `count: 2)}"` on the next line,
|
|
11
|
+
no longer moves every later line one line down per break in backtraces, `__LINE__` and compile errors. The
|
|
12
|
+
attributes of a `ruby:` or `markdown:` block keep their lines too.
|
|
13
|
+
|
|
14
|
+
## [3.6.2] — 2026-11-20
|
|
15
|
+
|
|
16
|
+
Template errors found while compiling now point at their template line, in Rails too.
|
|
17
|
+
|
|
18
|
+
**Fixed**
|
|
19
|
+
- `Explicit end statements are forbidden`, `Multiple … attributes specified`, `Invalid xml directive in html mode`
|
|
20
|
+
and `Invalid doctype` are raised as `SuperSlim::SyntaxError` with the file, the line and a caret, like parse
|
|
21
|
+
errors, so Rails' error page shows the template line instead of `compiler.rb`. They are still a `SuperSlim::Error`.
|
|
22
|
+
- Code on the `ruby:` line itself, or an empty `ruby:` block, no longer moves every later line two lines down in
|
|
23
|
+
backtraces and `__LINE__`.
|
|
24
|
+
- Without `file:`, parse errors and `Invalid UTF-8` start their backtrace at `(__TEMPLATE__):<line>`, like the
|
|
25
|
+
others.
|
|
26
|
+
|
|
5
27
|
## [3.6.1] — 2026-11-11
|
|
6
28
|
|
|
7
29
|
One fix: `InstanceVariables` no longer reports an `@` that no name follows.
|
|
@@ -308,7 +330,9 @@ renders what Slim 5 renders with `format: :html, sort_attrs: false`, byte for by
|
|
|
308
330
|
- 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,
|
|
309
331
|
newer than the one Ruby 3.3 ships.
|
|
310
332
|
|
|
311
|
-
[Unreleased]: https://github.com/levups/super-slim/compare/v3.6.
|
|
333
|
+
[Unreleased]: https://github.com/levups/super-slim/compare/v3.6.3...HEAD
|
|
334
|
+
[3.6.3]: https://github.com/levups/super-slim/compare/v3.6.2...v3.6.3
|
|
335
|
+
[3.6.2]: https://github.com/levups/super-slim/compare/v3.6.1...v3.6.2
|
|
312
336
|
[3.6.1]: https://github.com/levups/super-slim/compare/v3.6.0...v3.6.1
|
|
313
337
|
[3.6.0]: https://github.com/levups/super-slim/compare/v3.5.0...v3.6.0
|
|
314
338
|
[3.5.0]: https://github.com/levups/super-slim/compare/v3.4.1...v3.5.0
|
data/README.md
CHANGED
|
@@ -100,6 +100,12 @@ defaults are different (`format: :xhtml, sort_attrs: true`), so if you never cha
|
|
|
100
100
|
Templates for XML formats (`.xml`, `.rss`, `.atom`) render as XML automatically, with `<link />` and
|
|
101
101
|
`doctype xml`.
|
|
102
102
|
|
|
103
|
+
Errors keep Slim's messages, but those found while compiling also name their file and line, which Slim leaves out:
|
|
104
|
+
`Explicit end statements are forbidden`, `Multiple id attributes specified`, an `xml` directive in HTML and an
|
|
105
|
+
unknown doctype. SuperSlim raises them as `SuperSlim::SyntaxError`, like a parse error, so Rails' error page shows
|
|
106
|
+
the template line. Every line of a `ruby:` block also stays on its template line, where Slim moves the lines after
|
|
107
|
+
it, so an error or a `__LINE__` there reports the line you see in the template.
|
|
108
|
+
|
|
103
109
|
## Formatter
|
|
104
110
|
|
|
105
111
|
`super-slim format` rewrites templates in one layout: two spaces per level, a space after `-` and `=`, no `div`
|
|
@@ -455,11 +461,11 @@ bin/release minor --dry-run # shows the notes and the changes, writes nothing
|
|
|
455
461
|
bin/release minor # or patch, major, or an exact version such as 1.2.0
|
|
456
462
|
```
|
|
457
463
|
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
464
|
+
`main` takes no direct push, so `bin/release` dates the notes and bumps the version on a `release/vX.Y.Z` branch,
|
|
465
|
+
opens a pull request that merges itself once CI is green, and waits for it (`bin/release --wait X.Y.Z` if you
|
|
466
|
+
stopped it). The merge starts the Release workflow, which waits for a maintainer's approval, tags the merge commit
|
|
467
|
+
`vX.Y.Z`, publishes the gem to RubyGems through trusted publishing and creates the GitHub release with the notes
|
|
468
|
+
and the `.gem`.
|
|
463
469
|
|
|
464
470
|
## License
|
|
465
471
|
|
data/lib/super_slim/compiler.rb
CHANGED
|
@@ -106,13 +106,20 @@ module SuperSlim
|
|
|
106
106
|
parts
|
|
107
107
|
end
|
|
108
108
|
|
|
109
|
+
# The line breaks inside the text's interpolations, which its compiled code keeps.
|
|
110
|
+
def self.interpolation_newlines(text)
|
|
111
|
+
return 0 unless text.include?("\n") && text.include?('#{')
|
|
112
|
+
|
|
113
|
+
interpolation_parts(text).sum { |kind, code| (kind == :dynamic) ? code.count("\n") : 0 }
|
|
114
|
+
end
|
|
115
|
+
|
|
109
116
|
private
|
|
110
117
|
|
|
111
118
|
def compile_children(nodes)
|
|
112
119
|
block_open = false
|
|
113
120
|
nodes.each do |node|
|
|
114
121
|
if node.is_a?(Control)
|
|
115
|
-
|
|
122
|
+
compile_error!("Explicit end statements are forbidden") if node.code.match?(EXPLICIT_END)
|
|
116
123
|
|
|
117
124
|
emit_code("end") if block_open && !node.code.match?(BLOCK_CONTINUATION)
|
|
118
125
|
block_open = with_do(node.code, node.children).match?(BLOCK_OPENING)
|
|
@@ -223,7 +230,11 @@ module SuperSlim
|
|
|
223
230
|
seen_names = {}
|
|
224
231
|
attributes.each do |attribute|
|
|
225
232
|
next if attribute.name == "class"
|
|
226
|
-
|
|
233
|
+
if seen_names.key?(attribute.name)
|
|
234
|
+
# Reported on the last line of the attributes, like the tag's compiled code, which follows their code.
|
|
235
|
+
lines_ahead = attributes.sum { |other| code_newlines(other) }
|
|
236
|
+
compile_error!("Multiple #{attribute.name} attributes specified", lines_ahead:)
|
|
237
|
+
end
|
|
227
238
|
|
|
228
239
|
seen_names[attribute.name] = true
|
|
229
240
|
end
|
|
@@ -381,6 +392,11 @@ module SuperSlim
|
|
|
381
392
|
end
|
|
382
393
|
|
|
383
394
|
def compile_embedded(embedded)
|
|
395
|
+
case embedded.engine
|
|
396
|
+
when "ruby", "markdown"
|
|
397
|
+
# Their attributes are dropped, and with them the lines their interpolations go on to.
|
|
398
|
+
@code << ("\n" * embedded.attributes.sum { code_newlines(_1) })
|
|
399
|
+
end
|
|
384
400
|
case embedded.engine
|
|
385
401
|
when "ruby" then compile_ruby(embedded.children)
|
|
386
402
|
when "javascript" then compile_tag(Tag.new("script", embedded.attributes, [TextBlock.new(embedded.children)]))
|
|
@@ -391,11 +407,15 @@ module SuperSlim
|
|
|
391
407
|
|
|
392
408
|
# Keeps every line of the block on its template line, blank lines included. The code must end with a
|
|
393
409
|
# newline, as its last line may be a comment: when no blank line follows, that newline is the one of the
|
|
394
|
-
# newline node that comes next.
|
|
410
|
+
# newline node that comes next. Code on the `ruby:` line itself has no newline before it, an empty block none
|
|
411
|
+
# at all.
|
|
395
412
|
def compile_ruby(children)
|
|
396
413
|
code = children.grep(Text).map(&:text).join
|
|
397
|
-
|
|
398
|
-
|
|
414
|
+
leading_newline_count = children.index { _1 != NEWLINE } || children.size
|
|
415
|
+
trailing_newline_count = children.count(NEWLINE) - leading_newline_count - code.count("\n")
|
|
416
|
+
@code << ("\n" * leading_newline_count)
|
|
417
|
+
return if code.empty?
|
|
418
|
+
|
|
399
419
|
emit_code(code)
|
|
400
420
|
@code << ("\n" * [trailing_newline_count, 1].max)
|
|
401
421
|
@newline_emitted = trailing_newline_count.zero?
|
|
@@ -423,14 +443,31 @@ module SuperSlim
|
|
|
423
443
|
def doctype(type)
|
|
424
444
|
type = type.downcase
|
|
425
445
|
if (xml = type.match(/\Axml(\s+(.+))?\z/))
|
|
426
|
-
|
|
446
|
+
compile_error!("Invalid xml directive in html mode") unless @xml
|
|
427
447
|
|
|
428
448
|
%(<?xml version="1.0" encoding="#{xml[2] || "utf-8"}" ?>)
|
|
429
449
|
else
|
|
430
|
-
@doctypes[type] ||
|
|
450
|
+
@doctypes[type] || compile_error!("Invalid doctype #{type}")
|
|
431
451
|
end
|
|
432
452
|
end
|
|
433
453
|
|
|
454
|
+
# The lines an attribute's Ruby code goes on to: a code attribute's, and those of a quoted attribute's
|
|
455
|
+
# interpolations, which the parser leaves to the code.
|
|
456
|
+
def code_newlines(attribute)
|
|
457
|
+
case attribute
|
|
458
|
+
when CodeAttribute then attribute.code.count("\n")
|
|
459
|
+
when QuotedAttribute then Compiler.interpolation_newlines(attribute.text)
|
|
460
|
+
else 0
|
|
461
|
+
end
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
# The compiled code keeps each template line on its own line, so the newlines written so far give the line of
|
|
465
|
+
# the node being compiled: no node carries its line, which would cost every template line. Code not written
|
|
466
|
+
# yet may still hold `lines_ahead` of them.
|
|
467
|
+
def compile_error!(message, lines_ahead: 0)
|
|
468
|
+
raise CompileError.new(message, @code.count("\n") + lines_ahead + 1)
|
|
469
|
+
end
|
|
470
|
+
|
|
434
471
|
def capture_into(variable)
|
|
435
472
|
if @rails
|
|
436
473
|
emit_code("#{variable} = capture do")
|
data/lib/super_slim/parser.rb
CHANGED
|
@@ -148,7 +148,9 @@ module SuperSlim
|
|
|
148
148
|
def invalid_encoding!(source)
|
|
149
149
|
lines = source.b.split("\n")
|
|
150
150
|
lineno = lines.index { |line| !line.dup.force_encoding(Encoding::UTF_8).valid_encoding? } || 0
|
|
151
|
-
|
|
151
|
+
error = SyntaxError.new("Invalid UTF-8", @file, lines[lineno].to_s.force_encoding(Encoding::UTF_8).scrub, lineno + 1, 0)
|
|
152
|
+
error.set_backtrace(["#{error.file}:#{lineno + 1}", *caller])
|
|
153
|
+
raise error
|
|
152
154
|
end
|
|
153
155
|
|
|
154
156
|
def line_starts(source, byte_order_mark_size)
|
|
@@ -627,6 +629,7 @@ module SuperSlim
|
|
|
627
629
|
first = source_offset if @outline
|
|
628
630
|
value = +""
|
|
629
631
|
depth = 0
|
|
632
|
+
breaks = 0
|
|
630
633
|
quote_byte = quote.ord
|
|
631
634
|
|
|
632
635
|
until depth == 0 && @scanner.peek_byte == quote_byte
|
|
@@ -634,7 +637,7 @@ module SuperSlim
|
|
|
634
637
|
# Unlike a stripped continuation line, a line read whole keeps its trailing whitespace in the value.
|
|
635
638
|
@outline[@lineno].text = true if @outline && @scanner.eos? && @scanner.string.equal?(@line)
|
|
636
639
|
value << (@scanner.eos? ? "\n" : " ")
|
|
637
|
-
|
|
640
|
+
breaks += 1
|
|
638
641
|
expect_next_line
|
|
639
642
|
elsif (chunk = @scanner.scan(QUOTED_CHUNK_BY_QUOTE[quote]))
|
|
640
643
|
value << chunk
|
|
@@ -646,6 +649,9 @@ module SuperSlim
|
|
|
646
649
|
end
|
|
647
650
|
end
|
|
648
651
|
|
|
652
|
+
# A break inside an interpolation is already a line of the compiled code, which keeps it: only the others get
|
|
653
|
+
# a newline node.
|
|
654
|
+
(breaks - Compiler.interpolation_newlines(value)).times { @stacks.last << NEWLINE } if breaks > 0
|
|
649
655
|
record_ruby(:text, first, source_offset) if @outline
|
|
650
656
|
@scanner.pos += 1
|
|
651
657
|
value
|
|
@@ -659,7 +665,7 @@ module SuperSlim
|
|
|
659
665
|
|
|
660
666
|
def syntax_error!(message)
|
|
661
667
|
error = SyntaxError.new(message, @file, @line, @lineno, (@line && @scanner) ? column : 0)
|
|
662
|
-
error.set_backtrace(["#{
|
|
668
|
+
error.set_backtrace(["#{error.file}:#{@lineno}", *caller])
|
|
663
669
|
raise error
|
|
664
670
|
end
|
|
665
671
|
end
|
data/lib/super_slim/version.rb
CHANGED
data/lib/super_slim.rb
CHANGED
|
@@ -42,6 +42,17 @@ module SuperSlim
|
|
|
42
42
|
end
|
|
43
43
|
end
|
|
44
44
|
|
|
45
|
+
# A template error found while compiling, at its template line: `compile` adds the file and the line's text, and
|
|
46
|
+
# raises it as a SyntaxError.
|
|
47
|
+
class Compiler::CompileError < Error
|
|
48
|
+
attr_reader :lineno
|
|
49
|
+
|
|
50
|
+
def initialize(message, lineno)
|
|
51
|
+
@lineno = lineno
|
|
52
|
+
super(message)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
45
56
|
class << self
|
|
46
57
|
# Shortcuts also read Tailwind class names, such as `.rounded-b-[3px]` or `.lg:hover:underline`. Off by
|
|
47
58
|
# default so that switching from Slim changes no template: Slim renders `.size-2.5` as two classes.
|
|
@@ -49,6 +60,24 @@ module SuperSlim
|
|
|
49
60
|
|
|
50
61
|
def compile(source, file: nil, tailwind_classes: self.tailwind_classes, **compiler_options)
|
|
51
62
|
Compiler.new(**compiler_options).call(Parser.new(file:, tailwind_classes:).call(source))
|
|
63
|
+
rescue Compiler::CompileError => exception
|
|
64
|
+
# Without its cause: Rails would show the compiler's backtrace below the template line.
|
|
65
|
+
raise compile_syntax_error(exception, source, file), cause: nil
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
# Located like the parser's errors, its backtrace starting at the template line, which Rails reports.
|
|
71
|
+
def compile_syntax_error(exception, source, file)
|
|
72
|
+
lineno = exception.lineno
|
|
73
|
+
# The source parsed, so it is valid UTF-8 whatever its encoding says. A count past the end guards against
|
|
74
|
+
# compiled code taking more lines than its template, which no construct is known to do: the error still
|
|
75
|
+
# raises, without that line.
|
|
76
|
+
line = String.new(Parser.split_lines(source.b)[lineno - 1].to_s, encoding: Encoding::UTF_8)
|
|
77
|
+
line = line.delete_prefix("") if lineno == 1
|
|
78
|
+
error = SyntaxError.new(exception.message, file, line, lineno, line[/\A[ \t]*/].size)
|
|
79
|
+
error.set_backtrace(["#{error.file}:#{lineno}", *exception.backtrace])
|
|
80
|
+
error
|
|
52
81
|
end
|
|
53
82
|
end
|
|
54
83
|
end
|