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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/theo-rails/theo.rb +32 -31
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3e4463e090916210fce56a8da009ebb09f945ba3013dc46121c66c43f4cfc9a2
4
- data.tar.gz: 545c9cc533c9a89aa3d4d1a53d0598dc7a21aadd0608e26fad742da9b9cb0c7f
3
+ metadata.gz: 6681a53dbf50c16328a4e7fdd336e4796f816f2b378144ac1fa159a77b84f271
4
+ data.tar.gz: 8a8dd2a5918fb52dbf7a7aa193654921cf91615ea031017c595be10c445a8555
5
5
  SHA512:
6
- metadata.gz: 9e9bbb695426ff687b337ef025a0741324b0defa1778b3b3183d8e674b3928577b534f49c2a267cdd7d43b8184a4fbda5f2ff8895ce3b31a63fdaa186761bada
7
- data.tar.gz: 71688b266300bfe8863fb73e2ed260f2d317d24254d9fe0db9069e4e9327c97a856cf18751a688292ed77ab59815c0d7ca98f8db36b70b5ce6521d658421a768
6
+ metadata.gz: 3b09043ae8e7a6fdd0291f50aff9ae4b3c902b9bec12799c3578f5b01fdb4eae40a0a36fe02c145c577c9afca4e0e54418c51b164c7eeff70d40d32c9ac6a689
7
+ data.tar.gz: 17f3b1401babf88a7849db263b3ec502037a6b8c91fa23a181cbe1be9f0d60eaf8f2a097abee6404aeff84066400d1d14ab8710489079ae029d6376c49f80644
@@ -1,50 +1,53 @@
1
1
  module Theo
2
2
  module Rails
3
- TX = '\s*([a-z0-9-]+-partial)\s*([^>]*?)(?<![%/])'.freeze
4
- RX = %r{(?:<#{TX}>(.*?)</\1>)|(?:<#{TX}/>)}im
5
- LX = /\s*([^=%\s]+)\s*(?:(%)?=\s*"([^"]*)")?/
6
- RXA = /^<%=([^%]*)%>$/
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
- source.gsub(RX) do |_|
11
- match = Regexp.last_match
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
- attributes = attributes
17
- .scan(LX)
18
- .map { |name, literal, value| [name.to_sym, attribute(value || '', literal:)] }
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).delete_prefix("'").delete_suffix("'")
35
+ collection = attribute(attributes.delete(:collection))
29
36
 
30
37
  as = ''
31
38
  if attributes[:as]
32
- as = attributes.delete(:as).delete_prefix("'").delete_suffix("'")
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 = nil
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
- arg = "|#{arg}|"
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 #{ arg || '' } %>#{process(content)}<% end %>"
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, literal: false)
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 = RXA.match(source)
62
+ match = DYNAMIC_EXPRESSION.match(source)
62
63
  return match[1] if match
63
64
 
64
- "'" + source + "'"
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.5
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-06-16 00:00:00.000000000 Z
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