theo-rails 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 72760a93ae4af92ea6165a28783256803999a0ae3aa39dde174b5bd747a2b3cf
4
+ data.tar.gz: 05b64b345ea54a808b2f80c7a8ec64ec5dc0554102fa913e222cdea6801edc3d
5
+ SHA512:
6
+ metadata.gz: 5c702258890cdc47fb26baa89e95b7e2d7290cf077dcc7b60be46bc97313954ee5417dca603e16388849c7fa5c404cc20557364c46fc748df64a7ca8265105d1
7
+ data.tar.gz: eab46ae893713a4e8c7f9c9381e1ab40561ee8794a5db21cbe9a09e82632ba38b9f8465fe5c71c4705c2014c1169fda234f64f023776ab58613eb62dc704f484
@@ -0,0 +1,2 @@
1
+ <%# locals: (form:, name:, **attributes) -%>
2
+ <%= form.email_field name, **attributes %>
@@ -0,0 +1,3 @@
1
+ <%= form_with url: local_assigns[:url] do |form| %>
2
+ <% yield form %>
3
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <%# locals: (form:, name:, **attributes) -%>
2
+ <% if (block = yield).present? %>
3
+ <%= form.label name, **attributes do %><%= block %><% end %>
4
+ <% else %>
5
+ <%= form.label name, **attributes %>
6
+ <% end %>
@@ -0,0 +1,3 @@
1
+ <%# locals: (form:, name:, **attributes) -%>
2
+ <%= form.number_field name, **attributes %>
3
+
@@ -0,0 +1,3 @@
1
+ <%# locals: (form:, name:, options:, **attributes) -%>
2
+ <%= form.select name, options, **attributes %>
3
+
@@ -0,0 +1,3 @@
1
+ <%# locals: (form:, value:, **attributes) -%>
2
+ <%= form.submit value, **attributes %>
3
+
@@ -0,0 +1,3 @@
1
+ <%# locals: (form:, name:, **attributes) -%>
2
+ <%= form.text_area name, **attributes %>
3
+
@@ -0,0 +1,2 @@
1
+ <%# locals: (form:, name:, **attributes) -%>
2
+ <%= form.text_field name, **attributes %>
@@ -0,0 +1,6 @@
1
+ module Theo
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,11 @@
1
+ module Theo
2
+ module Rails
3
+ class Railtie < ::Rails::Railtie
4
+ initializer 'initialize theo template handler' do
5
+ ActiveSupport.on_load(:action_view) do
6
+ ::ActionView::Template.register_template_handler :theo, :'erb.theo', Theo.new
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,76 @@
1
+ module Theo
2
+ module Rails
3
+ TX = '\s*([a-z0-9-]+-partial)\s*(.*?)(?<!%)'.freeze
4
+ RX = %r{(?:<#{TX}>(.*?)</\1>)|(?:<#{TX}/>)}im
5
+ LX = /\s*([^=]+?)\s*(%)?=\s*"([^"]*)"/
6
+ RXA = /^<%=([^%]*)%>$/
7
+
8
+ class Theo
9
+ 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
15
+
16
+ attributes = attributes
17
+ .scan(LX)
18
+ .map { |name, literal, value| [name.to_sym, attribute(value, literal:)] }
19
+ .to_h
20
+
21
+ if attributes[:path]
22
+ path = attributes.delete(:path).delete_prefix("'").delete_suffix("'")
23
+ partial = "#{path}/#{partial}"
24
+ end
25
+
26
+ collection = ''
27
+ if attributes[:collection]
28
+ collection = attributes.delete(:collection).delete_prefix("'").delete_suffix("'")
29
+
30
+ as = ''
31
+ if attributes[:as]
32
+ as = attributes.delete(:as).delete_prefix("'").delete_suffix("'")
33
+ as = ", as: '#{as}'"
34
+ end
35
+ collection = ", collection: #{collection}#{as}"
36
+ end
37
+
38
+ arg = nil
39
+ if attributes[:arg]
40
+ arg = attributes.delete(:arg)
41
+ raise 'arg %= syntax is required' if arg.start_with?("'")
42
+
43
+ arg = "|#{arg}|"
44
+ end
45
+
46
+ if content
47
+ output = "<%= render '#{partial}', {#{attributes.map {|k,v| "#{k}: #{v}"}.join(', ')}} do #{ arg || '' } %>#{process(content)}<% end %>"
48
+ else
49
+ output = "<%= render partial: '#{partial}'#{collection}, locals: {#{attributes.map {|k,v| "#{k}: #{v}"}.join(', ')}} %>"
50
+ end
51
+
52
+ output
53
+ end
54
+ end
55
+
56
+ def attribute(source, literal: false)
57
+ #TODO: support attributes like "a<%= b %>c
58
+
59
+ return source if literal
60
+
61
+ match = RXA.match(source)
62
+ return match[1] if match
63
+
64
+ "'" + source + "'"
65
+ end
66
+
67
+ def call(template, source = nil)
68
+ theo = process(source)
69
+
70
+ ::Rails.logger.info "Theo is generating ERB: \n#{theo}"
71
+
72
+ ActionView::Template::Handlers::ERB.call(template, theo)
73
+ end
74
+ end
75
+ end
76
+ end
data/lib/theo-rails.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'theo-rails/theo'
2
+ require 'theo-rails/engine' if defined?(Rails::Engine)
3
+ require 'theo-rails/railtie' if defined?(Rails::Railtie)
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: theo-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jarek Lipski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-06-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: HTML-like template language for Rails with natural partial syntax
14
+ email: jarek@jareklipski.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - app/views/application/_email_field.html.erb
20
+ - app/views/application/_form_with.html.erb
21
+ - app/views/application/_label.html.erb
22
+ - app/views/application/_number_field.html.erb
23
+ - app/views/application/_select.html.erb
24
+ - app/views/application/_submit.html.erb
25
+ - app/views/application/_text_area.html.erb
26
+ - app/views/application/_text_field.html.erb
27
+ - lib/theo-rails.rb
28
+ - lib/theo-rails/engine.rb
29
+ - lib/theo-rails/railtie.rb
30
+ - lib/theo-rails/theo.rb
31
+ homepage: https://github.com/loomchild/theo-rails
32
+ licenses:
33
+ - MIT
34
+ metadata:
35
+ homepage_uri: https://github.com/loomchild/theo-rails
36
+ source_code_uri: https://github.com/loomchild/theo-rails
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '3.2'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubygems_version: 3.4.10
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: Theo is HTML-like template language
56
+ test_files: []