super-slim 3.6.1 → 3.6.2
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 +15 -1
- data/README.md +6 -0
- data/lib/super_slim/compiler.rb +22 -7
- data/lib/super_slim/parser.rb +4 -2
- data/lib/super_slim/version.rb +1 -1
- data/lib/super_slim.rb +28 -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: cdd1bc27d23e754183cb4f4e4963a935d56d030a1b73d60bccfc4ee97f8972bc
|
|
4
|
+
data.tar.gz: 2419f8247b33000daa959e96470679eb54ef778e5c82aa6c0555323e68e7ed02
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 545d8dd22428f0a94ccb6e36c0f1108427ca607d6242abf1abe1dc69c9969ba6db04cef1404262017214599499813f0a028b3e2392fe17f48eb9a25e5746ef50
|
|
7
|
+
data.tar.gz: 339168144fd87e1b8a6ef09e293c1f645d31c0da65c30c3fab686fa63dd6fb9debcbf5f63e92396c244031e75ca0c7aba202c9f9ea0cf0d94a6dd69ceca47018
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [3.6.2] — 2026-11-20
|
|
6
|
+
|
|
7
|
+
Template errors found while compiling now point at their template line, in Rails too.
|
|
8
|
+
|
|
9
|
+
**Fixed**
|
|
10
|
+
- `Explicit end statements are forbidden`, `Multiple … attributes specified`, `Invalid xml directive in html mode`
|
|
11
|
+
and `Invalid doctype` are raised as `SuperSlim::SyntaxError` with the file, the line and a caret, like parse
|
|
12
|
+
errors, so Rails' error page shows the template line instead of `compiler.rb`. They are still a `SuperSlim::Error`.
|
|
13
|
+
- Code on the `ruby:` line itself, or an empty `ruby:` block, no longer moves every later line two lines down in
|
|
14
|
+
backtraces and `__LINE__`.
|
|
15
|
+
- Without `file:`, parse errors and `Invalid UTF-8` start their backtrace at `(__TEMPLATE__):<line>`, like the
|
|
16
|
+
others.
|
|
17
|
+
|
|
5
18
|
## [3.6.1] — 2026-11-11
|
|
6
19
|
|
|
7
20
|
One fix: `InstanceVariables` no longer reports an `@` that no name follows.
|
|
@@ -308,7 +321,8 @@ renders what Slim 5 renders with `format: :html, sort_attrs: false`, byte for by
|
|
|
308
321
|
- 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
322
|
newer than the one Ruby 3.3 ships.
|
|
310
323
|
|
|
311
|
-
[Unreleased]: https://github.com/levups/super-slim/compare/v3.6.
|
|
324
|
+
[Unreleased]: https://github.com/levups/super-slim/compare/v3.6.2...HEAD
|
|
325
|
+
[3.6.2]: https://github.com/levups/super-slim/compare/v3.6.1...v3.6.2
|
|
312
326
|
[3.6.1]: https://github.com/levups/super-slim/compare/v3.6.0...v3.6.1
|
|
313
327
|
[3.6.0]: https://github.com/levups/super-slim/compare/v3.5.0...v3.6.0
|
|
314
328
|
[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`
|
data/lib/super_slim/compiler.rb
CHANGED
|
@@ -112,7 +112,7 @@ module SuperSlim
|
|
|
112
112
|
block_open = false
|
|
113
113
|
nodes.each do |node|
|
|
114
114
|
if node.is_a?(Control)
|
|
115
|
-
|
|
115
|
+
compile_error!("Explicit end statements are forbidden") if node.code.match?(EXPLICIT_END)
|
|
116
116
|
|
|
117
117
|
emit_code("end") if block_open && !node.code.match?(BLOCK_CONTINUATION)
|
|
118
118
|
block_open = with_do(node.code, node.children).match?(BLOCK_OPENING)
|
|
@@ -223,7 +223,11 @@ module SuperSlim
|
|
|
223
223
|
seen_names = {}
|
|
224
224
|
attributes.each do |attribute|
|
|
225
225
|
next if attribute.name == "class"
|
|
226
|
-
|
|
226
|
+
if seen_names.key?(attribute.name)
|
|
227
|
+
# Reported on the last line of the attributes, like the tag's compiled code, which follows their code.
|
|
228
|
+
code_newlines = attributes.sum { _1.is_a?(CodeAttribute) ? _1.code.count("\n") : 0 }
|
|
229
|
+
compile_error!("Multiple #{attribute.name} attributes specified", lines_ahead: code_newlines)
|
|
230
|
+
end
|
|
227
231
|
|
|
228
232
|
seen_names[attribute.name] = true
|
|
229
233
|
end
|
|
@@ -391,11 +395,15 @@ module SuperSlim
|
|
|
391
395
|
|
|
392
396
|
# Keeps every line of the block on its template line, blank lines included. The code must end with a
|
|
393
397
|
# 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.
|
|
398
|
+
# newline node that comes next. Code on the `ruby:` line itself has no newline before it, an empty block none
|
|
399
|
+
# at all.
|
|
395
400
|
def compile_ruby(children)
|
|
396
401
|
code = children.grep(Text).map(&:text).join
|
|
397
|
-
|
|
398
|
-
|
|
402
|
+
leading_newline_count = children.index { _1 != NEWLINE } || children.size
|
|
403
|
+
trailing_newline_count = children.count(NEWLINE) - leading_newline_count - code.count("\n")
|
|
404
|
+
@code << ("\n" * leading_newline_count)
|
|
405
|
+
return if code.empty?
|
|
406
|
+
|
|
399
407
|
emit_code(code)
|
|
400
408
|
@code << ("\n" * [trailing_newline_count, 1].max)
|
|
401
409
|
@newline_emitted = trailing_newline_count.zero?
|
|
@@ -423,14 +431,21 @@ module SuperSlim
|
|
|
423
431
|
def doctype(type)
|
|
424
432
|
type = type.downcase
|
|
425
433
|
if (xml = type.match(/\Axml(\s+(.+))?\z/))
|
|
426
|
-
|
|
434
|
+
compile_error!("Invalid xml directive in html mode") unless @xml
|
|
427
435
|
|
|
428
436
|
%(<?xml version="1.0" encoding="#{xml[2] || "utf-8"}" ?>)
|
|
429
437
|
else
|
|
430
|
-
@doctypes[type] ||
|
|
438
|
+
@doctypes[type] || compile_error!("Invalid doctype #{type}")
|
|
431
439
|
end
|
|
432
440
|
end
|
|
433
441
|
|
|
442
|
+
# The compiled code keeps each template line on its own line, so the newlines written so far give the line of
|
|
443
|
+
# the node being compiled: no node carries its line, which would cost every template line. Code not written
|
|
444
|
+
# yet may still hold `lines_ahead` of them.
|
|
445
|
+
def compile_error!(message, lines_ahead: 0)
|
|
446
|
+
raise CompileError.new(message, @code.count("\n") + lines_ahead + 1)
|
|
447
|
+
end
|
|
448
|
+
|
|
434
449
|
def capture_into(variable)
|
|
435
450
|
if @rails
|
|
436
451
|
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)
|
|
@@ -659,7 +661,7 @@ module SuperSlim
|
|
|
659
661
|
|
|
660
662
|
def syntax_error!(message)
|
|
661
663
|
error = SyntaxError.new(message, @file, @line, @lineno, (@line && @scanner) ? column : 0)
|
|
662
|
-
error.set_backtrace(["#{
|
|
664
|
+
error.set_backtrace(["#{error.file}:#{@lineno}", *caller])
|
|
663
665
|
raise error
|
|
664
666
|
end
|
|
665
667
|
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,23 @@ 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 construct whose compiled code takes
|
|
74
|
+
# more lines than its template can move the count past the end: the error still raises, without that line.
|
|
75
|
+
line = String.new(Parser.split_lines(source.b)[lineno - 1].to_s, encoding: Encoding::UTF_8)
|
|
76
|
+
line = line.delete_prefix("") if lineno == 1
|
|
77
|
+
error = SyntaxError.new(exception.message, file, line, lineno, line[/\A[ \t]*/].size)
|
|
78
|
+
error.set_backtrace(["#{error.file}:#{lineno}", *exception.backtrace])
|
|
79
|
+
error
|
|
52
80
|
end
|
|
53
81
|
end
|
|
54
82
|
end
|