theo-rails 0.0.5 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/theo-rails/theo.rb +32 -31
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6681a53dbf50c16328a4e7fdd336e4796f816f2b378144ac1fa159a77b84f271
|
4
|
+
data.tar.gz: 8a8dd2a5918fb52dbf7a7aa193654921cf91615ea031017c595be10c445a8555
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b09043ae8e7a6fdd0291f50aff9ae4b3c902b9bec12799c3578f5b01fdb4eae40a0a36fe02c145c577c9afca4e0e54418c51b164c7eeff70d40d32c9ac6a689
|
7
|
+
data.tar.gz: 17f3b1401babf88a7849db263b3ec502037a6b8c91fa23a181cbe1be9f0d60eaf8f2a097abee6404aeff84066400d1d14ab8710489079ae029d6376c49f80644
|
data/lib/theo-rails/theo.rb
CHANGED
@@ -1,50 +1,53 @@
|
|
1
1
|
module Theo
|
2
2
|
module Rails
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
ATTRIBUTE_NAME = /[\w\-:@]+/
|
4
|
+
ATTRIBUTE_VALUE = /(?:(?:"(?<value>[^"]*)")|(?:'(?<value>[^']*)'))/
|
5
|
+
ATTRIBUTE = /(?:(?:(?<name>#{ATTRIBUTE_NAME.source})\s*=\s*#{ATTRIBUTE_VALUE.source})|(?<name>#{ATTRIBUTE_NAME.source}))/
|
6
|
+
DYNAMIC_ATTRIBUTE = /(?:(?<name>#{ATTRIBUTE_NAME.source})\s*%=\s*#{ATTRIBUTE_VALUE.source})/
|
7
|
+
ATTRIBUTES = /(?<attrs>(?:\s+#{ATTRIBUTE.source})*)/
|
8
|
+
PARTIAL_TAG = /(?<partial>[\w-]+-partial)/
|
9
|
+
PARTIAL = /(?:<#{PARTIAL_TAG.source}#{ATTRIBUTES.source}\s*>(?<content>.*?)<\/\k<partial>>)|(?:<#{PARTIAL_TAG.source}#{ATTRIBUTES.source}\s*\/>)/im
|
10
|
+
DYNAMIC_EXPRESSION = /^<%=([^%]*)%>$/
|
7
11
|
|
8
12
|
class Theo
|
9
13
|
def process(source)
|
10
|
-
|
11
|
-
|
12
|
-
partial = (match[1] || match[4]).delete_suffix('-partial').underscore
|
13
|
-
attributes = match[2] || match[5] || ''
|
14
|
-
content = match[3]&.strip
|
14
|
+
# Attributes
|
15
|
+
source = source.gsub(DYNAMIC_ATTRIBUTE, '\k<name>="<%=\k<value>%>"')
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
# Partials
|
18
|
+
source.gsub(PARTIAL) do |_|
|
19
|
+
match = Regexp.last_match
|
20
|
+
partial = (match[:partial]).delete_suffix('-partial').underscore
|
21
|
+
attributes = match[:attrs] || ''
|
22
|
+
content = match[:content]&.strip
|
23
|
+
|
24
|
+
attributes =
|
25
|
+
attributes
|
26
|
+
.gsub(ATTRIBUTE)
|
27
|
+
.map { Regexp.last_match }
|
28
|
+
.map { |attr| [attr[:name].to_sym, attr[:value] || ''] }
|
19
29
|
.to_h
|
20
30
|
|
21
|
-
if attributes[:path]
|
22
|
-
path = attributes.delete(:path).delete_prefix("'").delete_suffix("'")
|
23
|
-
partial = "#{path}/#{partial}"
|
24
|
-
end
|
31
|
+
partial = "#{attributes.delete(:path)}/#{partial}" if attributes[:path]
|
25
32
|
|
26
33
|
collection = ''
|
27
34
|
if attributes[:collection]
|
28
|
-
collection = attributes.delete(:collection)
|
35
|
+
collection = attribute(attributes.delete(:collection))
|
29
36
|
|
30
37
|
as = ''
|
31
38
|
if attributes[:as]
|
32
|
-
as = attributes.delete(:as)
|
39
|
+
as = attributes.delete(:as)
|
33
40
|
as = ", as: '#{as}'"
|
34
41
|
end
|
35
42
|
collection = ", collection: #{collection}#{as}"
|
36
43
|
end
|
37
44
|
|
38
|
-
arg =
|
39
|
-
if attributes[:arg]
|
40
|
-
arg = attributes.delete(:arg)
|
41
|
-
raise 'arg %= syntax is required' if arg.start_with?("'")
|
45
|
+
arg = "|#{attributes.delete(:arg)}|" if attributes[:arg]
|
42
46
|
|
43
|
-
|
44
|
-
end
|
47
|
+
attributes.transform_values! { |value| attribute(value) }
|
45
48
|
|
46
49
|
if content
|
47
|
-
output = "<%= render '#{partial}', {#{attributes.map {|k,v| "'#{k}': #{v}"}.join(', ')}} do #{
|
50
|
+
output = "<%= render '#{partial}', {#{attributes.map {|k,v| "'#{k}': #{v}"}.join(', ')}} do #{arg || ''} %>#{process(content)}<% end %>"
|
48
51
|
else
|
49
52
|
output = "<%= render partial: '#{partial}'#{collection}, locals: {#{attributes.map {|k,v| "'#{k}': #{v}"}.join(', ')}} %>"
|
50
53
|
end
|
@@ -53,15 +56,13 @@ module Theo
|
|
53
56
|
end
|
54
57
|
end
|
55
58
|
|
56
|
-
def attribute(source
|
57
|
-
#TODO: support attributes like "a<%= b %>c
|
58
|
-
|
59
|
-
return source if literal
|
59
|
+
def attribute(source)
|
60
|
+
# TODO: support attributes like "a<%= b %>c
|
60
61
|
|
61
|
-
match =
|
62
|
+
match = DYNAMIC_EXPRESSION.match(source)
|
62
63
|
return match[1] if match
|
63
64
|
|
64
|
-
"'
|
65
|
+
"'#{source}'"
|
65
66
|
end
|
66
67
|
|
67
68
|
def call(template, source = nil)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: theo-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jarek Lipski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-07-12 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: HTML-like template language for Rails with natural partial syntax
|
14
14
|
email: jarek@jareklipski.com
|