serbea 0.3.2 → 0.4.1

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: fdeba2843c805f483e3514fbceaa63d610ac9b436762b8ff7c34ae6f7f6d8c3a
4
- data.tar.gz: 325e190e80f4cc5cd777e824c512361a71435b482672c1da8d17bf4ffc4d639c
3
+ metadata.gz: 2403adca3b4cbb2e82ad7bb758df50c04257d45f1b303a1a9b03a15453d66953
4
+ data.tar.gz: 1ccfc568da131dd13d9cc627b05fb13577854d4edfbf2ea0a3353b69b097a5f2
5
5
  SHA512:
6
- metadata.gz: fbfa7787a1ed422c4e1e238f2f328c37586e65d1bcc11458e6650803d8d6bfc21a6204bbdd6fa5e038b15e2120d43d08ef9a327e2ff25500e7bd569078d11a0e
7
- data.tar.gz: b8789ea01c5eb34aed8ceb0d770b94401d6a7167d0fca38d9892ca79d4be7681b30705fb96061e31a9f52aaf5babaa9af4fa57921cab665c63f414bf286408ae
6
+ metadata.gz: 22633765cd7a2ce0020cdd27708e2bc349cc9ccd168d1d15f7f135926d4a0333098fa05ebd34b7f77811371620f01860d7876935e0be47160e03714f8efc01d6
7
+ data.tar.gz: 9c5a097fe9fdb43738e3d61f3d61d973d15bff640ad3ae85cc9fb670e61c4a31154f67522bf9651f65ec6a1e67bf755d1b50ea6bc2508b42dfe9dd6f24c627d3
@@ -1,7 +1,7 @@
1
1
  module Serbea
2
2
  module Helpers
3
3
  def self.included(mod)
4
- Serbea::Pipeline.deny_value_method %i(prepend append)
4
+ Serbea::Pipeline.deny_value_method %i(escape h prepend append assign_to)
5
5
  end
6
6
 
7
7
  def capture(obj=nil)
@@ -33,5 +33,10 @@ module Serbea
33
33
  def append(old_string, new_string)
34
34
  "#{old_string}#{new_string}"
35
35
  end
36
+
37
+ def assign_to(input, varname, preserve: false)
38
+ self.instance_variable_set("@#{varname}", input)
39
+ preserve ? input : nil
40
+ end
36
41
  end
37
42
  end
@@ -1,5 +1,27 @@
1
1
  module Serbea
2
2
  class Pipeline
3
+ def self.exec(template, input: (no_input_passed = true; nil), include_helpers: nil)
4
+ anon = Class.new do
5
+ include Serbea::Helpers
6
+
7
+ attr_accessor :input, :output
8
+ end
9
+
10
+ if include_helpers
11
+ anon.include include_helpers
12
+ end
13
+
14
+ pipeline_obj = anon.new
15
+ pipeline_obj.input = input unless no_input_passed
16
+
17
+ full_template = "{{ #{template} | assign_to: :output }}"
18
+
19
+ tmpl = Tilt::SerbeaTemplate.new { full_template }
20
+ tmpl.render(pipeline_obj)
21
+
22
+ pipeline_obj.output
23
+ end
24
+
3
25
  def self.output_processor=(processor)
4
26
  @output_processor = processor
5
27
  end
@@ -22,11 +44,30 @@ module Serbea
22
44
  @value = value
23
45
  end
24
46
 
25
- def filter(name, *args)
47
+ # TODO: clean this up somehow and still support Ruby 2.5..3.0!
48
+ def filter(name, *args, **kwargs)
26
49
  if @value.respond_to?(name) && !self.class.value_methods_denylist.include?(name)
27
- @value = @value.send(name, *args)
50
+ if args.last.is_a?(Proc)
51
+ real_args = args.take(args.length - 1)
52
+ block = args.last
53
+ unless kwargs.empty?
54
+ @value = @value.send(name, *real_args, **kwargs, &block)
55
+ else
56
+ @value = @value.send(name, *real_args, &block)
57
+ end
58
+ else
59
+ unless kwargs.empty?
60
+ @value = @value.send(name, *args, **kwargs)
61
+ else
62
+ @value = @value.send(name, *args)
63
+ end
64
+ end
28
65
  elsif @context.respond_to?(name)
29
- @value = @context.send(name, @value, *args)
66
+ unless kwargs.empty?
67
+ @value = @context.send(name, @value, *args, **kwargs)
68
+ else
69
+ @value = @context.send(name, @value, *args)
70
+ end
30
71
  else
31
72
  "Serbea warning: Filter not found: #{name}".tap do |warning|
32
73
  raise_on_missing_filters ? raise(warning) : STDERR.puts(warning)
@@ -8,6 +8,13 @@ module Serbea
8
8
  def self.render_directive
9
9
  @render_directive ||= "render"
10
10
  end
11
+
12
+ def self.front_matter_preamble=(varname)
13
+ @front_matter_preamble = varname
14
+ end
15
+ def self.front_matter_preamble
16
+ @front_matter_preamble ||= "frontmatter = YAML.load"
17
+ end
11
18
 
12
19
  def self.has_yaml_header?(template)
13
20
  template.lines.first&.match? %r!\A---\s*\r?\n!
@@ -35,8 +42,13 @@ module Serbea
35
42
  string = template.dup
36
43
  if properties[:strip_front_matter] && self.class.has_yaml_header?(string)
37
44
  if string = string.match(FRONT_MATTER_REGEXP)
38
- string = string.post_match
39
- # yaml_data = SafeYAML.load(template.captures[0])
45
+ require "yaml" if self.class.front_matter_preamble.include?(" = YAML.load")
46
+
47
+ string = "{% #{self.class.front_matter_preamble} <<~YAMLDATA\n" +
48
+ string[1].sub(/^---\n/,'') +
49
+ "YAMLDATA\n%}" +
50
+ string[2].sub(/^---\n/, '') +
51
+ string.post_match
40
52
  end
41
53
  end
42
54
 
@@ -64,11 +76,12 @@ module Serbea
64
76
 
65
77
  buff << text
66
78
  if code.length > 0
79
+ original_line_length = code.lines.size
67
80
  processed_filters = false
68
81
 
69
82
  code = code.gsub('\|', "__PIPE_C__")
70
83
 
71
- subs = code.gsub(/\s*\|\s+(.*?)\s([^|}]*)/) do
84
+ subs = code.gsub(/\s*\|>?\s+(.*?)\s([^|}]*)/) do
72
85
  args = $2
73
86
  args = nil if args.strip == ""
74
87
  prefix = processed_filters ? ")" : "))"
@@ -78,7 +91,13 @@ module Serbea
78
91
 
79
92
  pipeline_suffix = processed_filters ? ") %}" : ")) %}"
80
93
 
81
- buff << subs.sub("{{", "{%= pipeline(self, (").sub("}}", pipeline_suffix).gsub("__PIPE_C__", '\|')
94
+ subs = subs.sub("{{", "{%= pipeline(self, (").sub("}}", pipeline_suffix).gsub("__PIPE_C__", '\|')
95
+
96
+ buff << subs
97
+
98
+ (original_line_length - subs.lines.size).times do
99
+ buff << "\n{% %}" # preserve original line length
100
+ end
82
101
  end
83
102
  end
84
103
 
@@ -101,6 +120,8 @@ module Serbea
101
120
  code.sub! /^\{%@/, ""
102
121
  code.sub! /%}$/, ""
103
122
  unless ["end", ""].include? code.strip
123
+ original_line_length = code.lines.size
124
+
104
125
  pieces = code.split(" ")
105
126
  if pieces[0].start_with?(/[A-Z]/) # Ruby class name
106
127
  pieces[0].prepend " "
@@ -124,6 +145,9 @@ module Serbea
124
145
  pieces.last << ")"
125
146
  buff << "{%= #{self.class.render_directive}#{pieces.join(" ")} %}"
126
147
  end
148
+ (original_line_length - 1).times do
149
+ buff << "\n{% %}" # preserve original directive line length
150
+ end
127
151
  else
128
152
  buff << "{%: end %}"
129
153
  end
@@ -1,3 +1,3 @@
1
1
  module Serbea
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.1"
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.3.2
4
+ version: 0.4.1
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-09-25 00:00:00.000000000 Z
11
+ date: 2020-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: erubi