theo-rails 0.1.3 → 0.3.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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/theo-rails/theo.rb +58 -29
  3. metadata +17 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8776f6ef2de8b4349c561ec52d66dd3d0b34c64ace4b2ca95c14f42182bb9dd3
4
- data.tar.gz: 1451423cc6d8e8600b4f0553dcd74657ee52bae7bb68c2167b4123e6b4f693f2
3
+ metadata.gz: a08de40e9205127777dc501ee41131f7c56b5d49ce3d793171a4f5fee114c087
4
+ data.tar.gz: 7285c684000a300aefd5c71b9b287656153303fb7eea410a4d59af474e21d494
5
5
  SHA512:
6
- metadata.gz: 914a9a654082457ec89cda8d4fb01f09e01bd94e0aa43fff03b936622d6b972cce88e996e92c6a21445244782164a14a1521ff13bcb5a7f7911da4e7338188c4
7
- data.tar.gz: d0158c52e718792bf613eaafd3b1e8c4b6054feaa02e6e39fb451adc5f681498c55674f1fc904bd29422f61219ad4c3630d889c26a47c3c49f7e765a0f3f760f
6
+ metadata.gz: b5e8ae5ee062f9891934034acaf20e6d652162acdb08c587a2330989b6b10a7fd19a674dbc4ad13a4ce7e370300f3b6cc18de1d990bb6d8ab452855ae4563108
7
+ data.tar.gz: aa2cee291cfc7ec96588b068cd52645e0502c53d87bec1f3c69d6e83e74879aede4aa47ef0b25c69219171a669d4ad6bcff430e3fef7de26efb6b9906c00de07
@@ -5,62 +5,91 @@ module Theo
5
5
  ATTRIBUTE = /(?:(?:(?<name>#{ATTRIBUTE_NAME.source})\s*=\s*#{ATTRIBUTE_VALUE.source})|(?<name>#{ATTRIBUTE_NAME.source}))/
6
6
  DYNAMIC_ATTRIBUTE = /(?:(?<name>#{ATTRIBUTE_NAME.source})\s*%=\s*#{ATTRIBUTE_VALUE.source})/
7
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
8
+ LITERAL_ATTRIBUTES = %i[path as yields collection].freeze
9
+ PARTIAL_TAG = /(?<partial>_\w+)/
10
+ PARTIAL = /(?:<#{PARTIAL_TAG.source}#{ATTRIBUTES.source}\s*>(?<content>.*?)<\/\k<partial>>)|(?:<#{PARTIAL_TAG.source}#{ATTRIBUTES.source}\s*\/>)/
11
+ COMPONENT_TAG = /(?<component>[A-Z]\w+)/
12
+ COMPONENT = /(?:<#{COMPONENT_TAG.source}#{ATTRIBUTES.source}\s*>(?<content>.*?)<\/\k<component>>)|(?:<#{COMPONENT_TAG.source}#{ATTRIBUTES.source}\s*\/>)/
13
+ TEMPLATE = /(?:#{PARTIAL.source})|(?:#{COMPONENT.source})/m
10
14
  DYNAMIC_EXPRESSION = /^<%=([^%]*)%>$/
11
15
 
12
16
  class Theo
13
17
  def process(source)
14
18
  # Attributes
15
- source = source.gsub(DYNAMIC_ATTRIBUTE, '\k<name>="<%=\k<value>%>"')
19
+ source = source.gsub(DYNAMIC_ATTRIBUTE, '\k<name>="<%= \k<value> %>"')
16
20
 
17
21
  # Partials
18
- source.gsub(PARTIAL) do |_|
22
+ source.gsub(TEMPLATE) do |_|
19
23
  match = Regexp.last_match
20
- partial = (match[:partial]).delete_suffix('-partial').underscore
24
+
25
+ partial = match[:partial]
26
+ component = match[:component]
27
+
21
28
  attributes = match[:attrs] || ''
22
- content = match[:content]&.strip
29
+ content = match[:content]
23
30
 
24
- attributes =
25
- attributes
26
- .gsub(ATTRIBUTE)
27
- .map { Regexp.last_match }
28
- .map { |attr| [attr[:name].to_sym, attr[:value] || ''] }
29
- .to_h
31
+ attributes = process_attributes(attributes)
30
32
 
31
- partial = "#{attributes.delete(:path)}/#{partial}" if attributes[:path]
33
+ path = attributes.delete(:path)
32
34
 
33
- collection = ''
34
- if attributes[:collection]
35
- collection = attribute(attributes.delete(:collection))
35
+ collection = attributes.delete(:collection)
36
+ as = attributes.delete(:as)
36
37
 
37
- as = ''
38
- if attributes[:as]
39
- as = attributes.delete(:as)
40
- as = ", as: '#{as}'"
41
- end
42
- collection = ", collection: #{collection}#{as}"
43
- end
38
+ yields = attributes.delete(:yields)
39
+ yields = " |#{yields}|" if yields
40
+
41
+ locals = attributes.empty? ? '' : attributes.map { |k, v| "'#{k}': #{v}" }.join(', ')
42
+
43
+ if partial
44
+ partial = partial.delete_prefix('_')
44
45
 
45
- yields = "|#{attributes.delete(:yields)}|" if attributes[:yields]
46
+ partial = "#{path}/#{partial}" if path
46
47
 
47
- attributes.transform_values! { |value| attribute(value) }
48
+ as = as ? ", as: '#{as}'" : ''
49
+ collection = ", collection: #{collection}#{as}" if collection
48
50
 
49
- if content
50
- output = "<%= render '#{partial}', {#{attributes.map {|k,v| "'#{k}': #{v}"}.join(', ')}} do #{yields || ''} %>#{process(content)}<% end %>"
51
+ if content
52
+ locals = ", {#{locals}}" unless locals.empty?
53
+ output = "<%= render '#{partial}'#{locals} do#{yields} %>#{process(content)}<% end %>"
54
+ else
55
+ locals = ", locals: {#{locals}}" unless locals.empty?
56
+ output = "<%= render partial: '#{partial}'#{collection}#{locals} %>"
57
+ end
51
58
  else
52
- output = "<%= render partial: '#{partial}'#{collection}, locals: {#{attributes.map {|k,v| "'#{k}': #{v}"}.join(', ')}} %>"
59
+ component = "#{component}Component"
60
+
61
+ if content
62
+ output = "<%= render #{component}.new(#{locals}) do#{yields} %>#{process(content)}<% end %>"
63
+ elsif collection
64
+ locals = ", #{locals}" unless locals.empty?
65
+ output = "<%= render #{component}.with_collection(#{collection}#{locals}) %>"
66
+ else
67
+ output = "<%= render #{component}.new(#{locals}) %>"
68
+ end
53
69
  end
54
70
 
55
71
  output
56
72
  end
57
73
  end
58
74
 
75
+ def process_attributes(attributes)
76
+ attributes
77
+ .gsub(ATTRIBUTE)
78
+ .map { Regexp.last_match }
79
+ .map do |attr|
80
+ name = attr[:name].to_sym
81
+ value = attr[:value]
82
+ value = attribute(value) unless LITERAL_ATTRIBUTES.include?(name)
83
+ [name, value]
84
+ end
85
+ .to_h
86
+ end
87
+
59
88
  def attribute(source)
60
89
  # TODO: support attributes like "a<%= b %>c
61
90
 
62
91
  match = DYNAMIC_EXPRESSION.match(source)
63
- return match[1] if match
92
+ return match[1].strip if match
64
93
 
65
94
  "'#{source}'"
66
95
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: theo-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.3.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-08-10 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-09-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: HTML-like template language for Rails with natural partial syntax
14
28
  email: jarek@jareklipski.com
15
29
  executables: []