serbea 0.3.1 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9dfc45d5f8581d988481bf56c492498b5bbd8b08186910c655e6fa0964fe48dc
4
- data.tar.gz: a25507d87ae9eeed0f575cdc5825212ca5d3bfadd1fbe388b7dfc219b53d721d
3
+ metadata.gz: 18caf5a2b809d0cf64dc3d6ed6b05186ec6c1cdbb6faf4c3c8f01ec607f65918
4
+ data.tar.gz: f94c3df3de70cbfbac1cd481ee41706beff8a0cfc78a23ee54b13ec4345d39fe
5
5
  SHA512:
6
- metadata.gz: 3da42d61791e24028f5ac9c68c24c6cf73d98f7568ffa87fb04cca4c8f45b77d9dddc4a68025cbbd7e9f58429ccaa95e79eecdae249b7a272d424cf32648ed36
7
- data.tar.gz: 216bdddc4a48696fabac9321e9f6d25b63e15adda17a22041f23b7963fd7a807a7840ddfb4e289349309361d8f6285801f651822b0ec86538d07980c54c3fb8b
6
+ metadata.gz: 27f9d07b128e5b7537be078b8056add0da4caed1304375a3f2decbd00d14232a60ade8d07817f7c520f2479f78957aff9774ba980391edd4643bb1c4b72454b2
7
+ data.tar.gz: dfe541a83671109526c0b9069ef2054dfb9a57c21474180d572ccba8cfcf13b3b59cab5a58e3e9b70e3952ca00ff46ab239a0fdf5f260a07e7956d646ef059b9
@@ -3,9 +3,9 @@ require "tilt/erubi"
3
3
  require "erubi/capture_end"
4
4
 
5
5
  require "serbea/helpers"
6
- require "serbea/component_renderer"
7
6
  require "serbea/pipeline"
8
7
  require "serbea/template_engine"
8
+ require "serbea/component_renderer"
9
9
 
10
10
  module Tilt
11
11
  class SerbeaTemplate < ErubiTemplate
@@ -1,5 +1,9 @@
1
1
  module Serbea
2
2
  module Helpers
3
+ def self.included(mod)
4
+ Serbea::Pipeline.deny_value_method %i(escape h prepend append assign_to)
5
+ end
6
+
3
7
  def capture(obj=nil)
4
8
  previous_buffer_state = @_erbout
5
9
  @_erbout = +""
@@ -21,5 +25,18 @@ module Serbea
21
25
  Erubi.h(input)
22
26
  end
23
27
  alias_method :escape, :h
28
+
29
+ def prepend(old_string, new_string)
30
+ "#{new_string}#{old_string}"
31
+ end
32
+
33
+ def append(old_string, new_string)
34
+ "#{old_string}#{new_string}"
35
+ end
36
+
37
+ def assign_to(input, varname, preserve: false)
38
+ self.instance_variable_set("@#{varname}", input)
39
+ preserve ? input : nil
40
+ end
24
41
  end
25
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
@@ -10,19 +32,45 @@ module Serbea
10
32
  end
11
33
  end
12
34
 
35
+ def self.deny_value_method(name)
36
+ value_methods_denylist.merge Array(name)
37
+ end
38
+ def self.value_methods_denylist
39
+ @value_methods_denylist ||= Set.new
40
+ end
41
+
13
42
  def initialize(context, value)
14
43
  @context = context
15
44
  @value = value
16
45
  end
17
46
 
18
- def filter(sym, *aargs)
19
- if @value.respond_to?(sym)
20
- @value = @value.send(sym, *aargs)
21
- elsif @context.respond_to?(sym)
22
- @value = @context.send(sym, @value, *aargs)
47
+ # TODO: clean this up somehow and still support Ruby 2.5..3.0!
48
+ def filter(name, *args, **kwargs)
49
+ if @value.respond_to?(name) && !self.class.value_methods_denylist.include?(name)
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
65
+ elsif @context.respond_to?(name)
66
+ unless kwargs.empty?
67
+ @value = @context.send(name, @value, *args, **kwargs)
68
+ else
69
+ @value = @context.send(name, @value, *args)
70
+ end
23
71
  else
24
- "Serbea warning: Filter not found: #{sym}".tap do |warning|
25
- raise_on_missing_filters ? raise(warning) : puts(warning)
72
+ "Serbea warning: Filter not found: #{name}".tap do |warning|
73
+ raise_on_missing_filters ? raise(warning) : STDERR.puts(warning)
26
74
  end
27
75
  end
28
76
 
@@ -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
 
@@ -56,7 +68,7 @@ module Serbea
56
68
  end
57
69
  end
58
70
 
59
- # Process the pipeline outputs
71
+ # Process any pipelines
60
72
  string = buff
61
73
  buff = ""
62
74
  until string.empty?
@@ -68,7 +80,7 @@ module Serbea
68
80
 
69
81
  code = code.gsub('\|', "__PIPE_C__")
70
82
 
71
- subs = code.gsub(/\s*\|\s+(.*?)\s([^|}]*)/) do
83
+ subs = code.gsub(/\s*\|>?\s+(.*?)\s([^|}]*)/) do
72
84
  args = $2
73
85
  args = nil if args.strip == ""
74
86
  prefix = processed_filters ? ")" : "))"
@@ -82,7 +94,15 @@ module Serbea
82
94
  end
83
95
  end
84
96
 
85
- # Process the render directives
97
+ # Process any directives
98
+ #
99
+ # TODO: allow custom directives! aka
100
+ # {%@something whatever %}
101
+ # {%@script
102
+ # const foo = "really? #{really}!"
103
+ # alert(foo.toLowerCase())
104
+ # %}
105
+ # {%@preact AwesomeChart data={#{ruby_data.to_json}} %}
86
106
  string = buff
87
107
  buff = ""
88
108
  until string.empty?
@@ -1,3 +1,3 @@
1
1
  module Serbea
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.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.3.1
4
+ version: 0.4.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-09-22 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