serbea 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 352c314f5f5a9b6df23933e1b379af39617c82c9b583f4c0703480a5f71df498
4
- data.tar.gz: 364a353dba20ce3af4bcc071247477ec6dfa6f95c15bb94c88677520344a3cf1
3
+ metadata.gz: f7961cb287d077b528dd0367681aee6087a5efc9b3838670e7f8c7072b09c9ad
4
+ data.tar.gz: ca1a4701f70e49e28c67dfb407018dfd56eaf42391a0aa4230e1a15dc1b57f86
5
5
  SHA512:
6
- metadata.gz: 6bcdd89d15300e24e051f68de0d87899bec18c9f304162e3b667afc7c8a420ab5978e5eb91ab40f8dfd4201375a7fab769d9419a19562196e3ba93b85692e262
7
- data.tar.gz: 8ba0cddf305d712c3e970adb06979e27f426aae797b2d99702f844e6e5c9248d34a96e35238134314da783e5fbd3de93b56cf7d5701ecc909690719393aa05fc
6
+ metadata.gz: 84036346ec67f5789c380bd0772f1da6644223e1b82f6a5974de93c9c3b82c98b34c5a6f27c8c69dbb598b230a24b33a0ae16a9bee3a284b494eec81cec9f0ff
7
+ data.tar.gz: 78da2c4e5f3828153b9933fd0173475aaacdf3bb42e4a56a4ec69f12b6991488559a7cfb57f8f1a64008173dd280b7c3c47de4daebc4cda537ed3638c7af3cbc
@@ -37,4 +37,15 @@ module Serbea
37
37
  end
38
38
  end
39
39
 
40
+ Serbea::TemplateEngine.directive :form, ->(code, buffer) do
41
+ buffer << "{%= form_with model:"
42
+ buffer << code
43
+ buffer << " %}"
44
+ end
45
+ Serbea::TemplateEngine.directive :_, ->(code, buffer) do
46
+ buffer << "{%= content_tag "
47
+ buffer << code
48
+ buffer << " %}"
49
+ end
50
+
40
51
  ActionView::Template.register_template_handler(:serb, Serbea::Rails::TemplateHandler)
@@ -14,11 +14,42 @@ module Serbea
14
14
  class TemplateEngine < Erubi::Engine
15
15
  FRONT_MATTER_REGEXP = %r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m.freeze
16
16
 
17
- def self.render_directive=(directive)
18
- @render_directive = directive
17
+ def self.directive(new_directive, directive_resolution)
18
+ directives[new_directive.to_s] = directive_resolution
19
19
  end
20
- def self.render_directive
21
- @render_directive ||= "render"
20
+ def self.directives
21
+ @directives ||= {
22
+ "@" => ->(code, buffer) do
23
+ original_line_length = code.lines.size
24
+
25
+ pieces = code.split(" ")
26
+ if pieces[0].start_with?(/[A-Z]/) # Ruby class name
27
+ pieces[0].prepend " "
28
+ pieces[0] << ".new("
29
+ else # string or something else
30
+ pieces[0].prepend "("
31
+ end
32
+
33
+ includes_block = false
34
+ pieces.reverse.each do |piece|
35
+ if piece == "do" && (pieces.last == "do" || pieces.last.end_with?("|"))
36
+ piece.prepend(") ")
37
+ includes_block = true
38
+ break
39
+ end
40
+ end
41
+
42
+ if includes_block
43
+ buffer << "{%= render#{pieces.join(" ")} %}"
44
+ else
45
+ pieces.last << ")"
46
+ buffer << "{%= render#{pieces.join(" ")} %}"
47
+ end
48
+ (original_line_length - 1).times do
49
+ buffer << "\n{% %}" # preserve original directive line length
50
+ end
51
+ end
52
+ }
22
53
  end
23
54
 
24
55
  def self.front_matter_preamble=(varname)
@@ -149,39 +180,19 @@ module Serbea
149
180
  string = buff
150
181
  buff = ""
151
182
  until string.empty?
152
- text, code, string = string.partition(/{%@(.*?)%}/m)
153
-
183
+ text, code, string = string.partition(/{%@([a-z_]+)?(.*?)%}/m)
184
+
154
185
  buff << text
155
186
  if code.length > 0
156
- code = $1
187
+ directive = $1
188
+ code = $2
157
189
  unless ["end", ""].include? code.strip
158
- original_line_length = code.lines.size
159
-
160
- pieces = code.split(" ")
161
- if pieces[0].start_with?(/[A-Z]/) # Ruby class name
162
- pieces[0].prepend " "
163
- pieces[0] << ".new("
164
- else # string or something else
165
- pieces[0].prepend "("
166
- end
190
+ directive = $1 ? self.class.directives[$1] : self.class.directives["@"]
167
191
 
168
- includes_block = false
169
- pieces.reverse.each do |piece|
170
- if piece == "do" && (pieces.last == "do" || pieces.last.end_with?("|"))
171
- piece.prepend(") ")
172
- includes_block = true
173
- break
174
- end
175
- end
176
-
177
- if includes_block
178
- buff << "{%= #{self.class.render_directive}#{pieces.join(" ")} %}"
192
+ if directive
193
+ directive.(code, buff)
179
194
  else
180
- pieces.last << ")"
181
- buff << "{%= #{self.class.render_directive}#{pieces.join(" ")} %}"
182
- end
183
- (original_line_length - 1).times do
184
- buff << "\n{% %}" # preserve original directive line length
195
+ raise "Handler for Serbea template directive `#{$1}' not found"
185
196
  end
186
197
  else
187
198
  buff << "{% end %}"
@@ -1,3 +1,3 @@
1
1
  module Serbea
2
- VERSION = "0.9.0"
2
+ VERSION = "0.10.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serbea
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bridgetown Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-19 00:00:00.000000000 Z
11
+ date: 2020-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport