serbea 0.9.0 → 0.10.4

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: 212a077a67f1553dc07fc8dd1b989647e19803953fba03976ec9ef95e08a2321
4
+ data.tar.gz: feda65d983102a217bd8426a8404712f882f23d2c6c3fa4be1981518fb20aae4
5
5
  SHA512:
6
- metadata.gz: 6bcdd89d15300e24e051f68de0d87899bec18c9f304162e3b667afc7c8a420ab5978e5eb91ab40f8dfd4201375a7fab769d9419a19562196e3ba93b85692e262
7
- data.tar.gz: 8ba0cddf305d712c3e970adb06979e27f426aae797b2d99702f844e6e5c9248d34a96e35238134314da783e5fbd3de93b56cf7d5701ecc909690719393aa05fc
6
+ metadata.gz: 94edc63cbeb21ce38db5ab1270c2b06f5479d5fd529ea12ccca68d69d6abe7fece8dc83cbfeeeba8e724b4fa7c165f831b23d6059bccd9aef022bc6d6545f16d
7
+ data.tar.gz: b10964bcc171ff4903ae5b98dea1db73b0f21f95a1bbb02027681a84e0834c7838ad3901e00a740586844fc4bf5be78d5d00fc5710e0c310f2bacf44026956db
@@ -2,8 +2,10 @@ require "serbea/rouge_lexer"
2
2
  require "bridgetown-core"
3
3
 
4
4
  module Bridgetown
5
- class SerbeaView < RubyTemplateView
5
+ class SerbeaView < ERBView
6
+ alias_method :_erb_capture, :capture
6
7
  include Serbea::Helpers
8
+ alias_method :capture, :_erb_capture
7
9
 
8
10
  def partial(partial_name, options = {}, &block)
9
11
  options.merge!(options[:locals]) if options[:locals]
@@ -17,14 +19,6 @@ module Bridgetown
17
19
  site.in_source_dir(site.config[:partials_dir], "#{partial_name}.serb")
18
20
  ).render(self, options)
19
21
  end
20
-
21
- def markdownify(input = nil, &block)
22
- content = Bridgetown::Utils.reindent_for_markdown(
23
- block.nil? ? input.to_s : capture(&block)
24
- )
25
- converter = site.find_converter_instance(Bridgetown::Converters::Markdown)
26
- converter.convert(content).strip
27
- end
28
22
  end
29
23
 
30
24
  module Converters
@@ -6,52 +6,31 @@ module Serbea
6
6
  Serbea::Pipeline.deny_value_method %i(escape h prepend append assign_to)
7
7
  end
8
8
 
9
- def capture(obj = nil, &block)
9
+ def capture(*args)
10
10
  previous_buffer_state = @_erbout
11
11
  @_erbout = Serbea::OutputBuffer.new
12
-
13
- # For compatibility with ActionView, not used by Bridgetown normally
14
- previous_ob_state = @output_buffer
15
- @output_buffer = Serbea::OutputBuffer.new
16
-
17
-
18
- result = instance_exec(obj, &block)
19
- if @output_buffer != ""
20
- # use Rails' ActionView buffer if present
21
- result = @output_buffer
22
- end
12
+ result = yield(*args)
23
13
  @_erbout = previous_buffer_state
24
- @output_buffer = previous_ob_state
25
14
 
26
15
  result&.html_safe
27
16
  end
28
-
17
+
29
18
  def pipeline(context, value)
30
19
  Pipeline.new(context, value)
31
20
  end
32
-
21
+
33
22
  def helper(name, &helper_block)
34
23
  self.class.define_method(name) do |*args, &block|
35
24
  previous_buffer_state = @_erbout
36
25
  @_erbout = Serbea::OutputBuffer.new
37
-
38
- # For compatibility with ActionView, not used by Bridgetown normally
39
- previous_ob_state = @output_buffer
40
- @output_buffer = Serbea::OutputBuffer.new
41
-
42
26
  result = helper_block.call(*args, &block)
43
- if @output_buffer != ""
44
- # use Rails' ActionView buffer if present
45
- result = @output_buffer
46
- end
47
27
  @_erbout = previous_buffer_state
48
- @output_buffer = previous_ob_state
49
-
28
+
50
29
  result.is_a?(String) ? result.html_safe : result
51
30
  end
52
31
  end
53
32
  alias_method :macro, :helper
54
-
33
+
55
34
  def h(input)
56
35
  ERB::Util.h(input.to_s)
57
36
  end
@@ -37,4 +37,23 @@ 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
+
46
+ Serbea::TemplateEngine.directive :_, ->(code, buffer) do
47
+ tag_name, space, params = code.lstrip.partition(%r(\s)m)
48
+
49
+ if tag_name.end_with?(":")
50
+ tag_name.chomp!(":")
51
+ tag_name = ":#{tag_name}" unless tag_name.start_with?(":")
52
+ end
53
+
54
+ buffer << "{%= tag.tag_string "
55
+ buffer << tag_name << ", " << params
56
+ buffer << " %}"
57
+ end
58
+
40
59
  ActionView::Template.register_template_handler(:serb, Serbea::Rails::TemplateHandler)
@@ -14,40 +14,58 @@ 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
+
21
+ def self.directives
22
+ @directives ||= {
23
+ "@" => ->(code, buffer) do
24
+ pieces = code.split(" ")
25
+ if pieces[0].start_with?(/[A-Z]/) # Ruby class name
26
+ pieces[0].prepend " "
27
+ pieces[0] << ".new("
28
+ else # string or something else
29
+ pieces[0].prepend "("
30
+ end
31
+
32
+ includes_block = false
33
+ pieces.reverse.each do |piece|
34
+ if piece == "do" && (pieces.last == "do" || pieces.last.end_with?("|"))
35
+ piece.prepend(") ")
36
+ includes_block = true
37
+ break
38
+ end
39
+ end
40
+
41
+ if includes_block
42
+ buffer << "{%= render#{pieces.join(" ")} %}"
43
+ else
44
+ pieces.last << ")"
45
+ buffer << "{%= render#{pieces.join(" ")} %}"
46
+ end
47
+ end
48
+ }
22
49
  end
23
50
 
24
51
  def self.front_matter_preamble=(varname)
25
52
  @front_matter_preamble = varname
26
53
  end
54
+
27
55
  def self.front_matter_preamble
28
56
  @front_matter_preamble ||= "frontmatter = YAML.load"
29
57
  end
30
-
58
+
31
59
  def self.has_yaml_header?(template)
32
60
  template.lines.first&.match? %r!\A---\s*\r?\n!
33
61
  end
34
-
62
+
35
63
  def initialize(input, properties={})
36
64
  properties[:regexp] = /{%(={1,2}|-|\#|%)?(.*?)([-=])?%}([ \t]*\r?\n)?/m
37
65
  properties[:strip_front_matter] = true unless properties.key?(:strip_front_matter)
38
66
  super process_serbea_input(input, properties), properties
39
67
  end
40
-
41
- def add_postamble(postamble)
42
- src << postamble
43
- src << "#{@bufvar}.html_safe"
44
-
45
- src.gsub!("__RAW_START_PRINT__", "{{")
46
- src.gsub!("__RAW_END_PRINT__", "}}")
47
- src.gsub!("__RAW_START_EVAL__", "{%")
48
- src.gsub!("__RAW_END_EVAL__", "%}")
49
- end
50
-
68
+
51
69
  def process_serbea_input(template, properties)
52
70
  buff = ""
53
71
 
@@ -149,39 +167,23 @@ module Serbea
149
167
  string = buff
150
168
  buff = ""
151
169
  until string.empty?
152
- text, code, string = string.partition(/{%@(.*?)%}/m)
153
-
170
+ text, code, string = string.partition(/{%@([a-z_]+)?(.*?)%}/m)
171
+
154
172
  buff << text
155
173
  if code.length > 0
156
- code = $1
174
+ directive = $1
175
+ code = $2
157
176
  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
177
+ directive = $1 ? self.class.directives[$1] : self.class.directives["@"]
167
178
 
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
179
+ if directive
180
+ additional_length = "#{buff}#{code}".lines.size
181
+ directive.(code, buff)
182
+ (additional_length - buff.lines.size).times do
183
+ buff << "{% %}\n" # preserve original directive line length
174
184
  end
175
- end
176
-
177
- if includes_block
178
- buff << "{%= #{self.class.render_directive}#{pieces.join(" ")} %}"
179
185
  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
186
+ raise "Handler for Serbea template directive `#{$1}' not found"
185
187
  end
186
188
  else
187
189
  buff << "{% end %}"
@@ -191,7 +193,7 @@ module Serbea
191
193
 
192
194
  buff
193
195
  end
194
-
196
+
195
197
  private
196
198
 
197
199
  def add_code(code)
@@ -215,5 +217,15 @@ module Serbea
215
217
  def add_expression_result_escaped(code)
216
218
  add_expression_result(code)
217
219
  end
218
- end # class
220
+
221
+ def add_postamble(postamble)
222
+ src << postamble
223
+ src << "#{@bufvar}.html_safe"
224
+
225
+ src.gsub!("__RAW_START_PRINT__", "{{")
226
+ src.gsub!("__RAW_END_PRINT__", "}}")
227
+ src.gsub!("__RAW_START_EVAL__", "{%")
228
+ src.gsub!("__RAW_END_EVAL__", "%}")
229
+ end
230
+ end
219
231
  end
@@ -1,3 +1,3 @@
1
1
  module Serbea
2
- VERSION = "0.9.0"
2
+ VERSION = "0.10.4"
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.4
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-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport