serbea 0.2.0 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/serbea.rb +1 -1
- data/lib/serbea/helpers.rb +21 -0
- data/lib/serbea/pipeline.rb +14 -7
- data/lib/serbea/template_engine.rb +60 -4
- data/lib/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bbd946514bace35dc47b5a4cf880db55b6b5af2e6060c40dee607bb776072c30
|
4
|
+
data.tar.gz: 3cac2624f2e6c5f4dd70393f5c6bbef467115da9bc9e465d6a8e91e69e3155b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 122fa4b87a75dacfcdc9026aaac10560695ebbf7cc5b6e0e35999bc7694e880e8df6a5ead634b37685cafa8ad6849380029f5beece1ce17665cef0fad5eb9b38
|
7
|
+
data.tar.gz: bea81f00f032ed410f976041399f1fae6209fb9f4808e747c69ebf6a4d48faf64b9f2c79cb5a470fc0f5a997574916b25143da64de7ed23d9383b2ae71119a6c
|
data/lib/serbea.rb
CHANGED
@@ -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
|
data/lib/serbea/helpers.rb
CHANGED
@@ -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 map assign)
|
5
|
+
end
|
6
|
+
|
3
7
|
def capture(obj=nil)
|
4
8
|
previous_buffer_state = @_erbout
|
5
9
|
@_erbout = +""
|
@@ -21,5 +25,22 @@ 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 map(input, block)
|
38
|
+
input.map(&block)
|
39
|
+
end
|
40
|
+
|
41
|
+
def assign(input, varname, preserve: false)
|
42
|
+
self.instance_variable_set("@#{varname}", input)
|
43
|
+
preserve ? input : nil
|
44
|
+
end
|
24
45
|
end
|
25
46
|
end
|
data/lib/serbea/pipeline.rb
CHANGED
@@ -10,19 +10,26 @@ module Serbea
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
+
def self.deny_value_method(name)
|
14
|
+
value_methods_denylist.merge Array(name)
|
15
|
+
end
|
16
|
+
def self.value_methods_denylist
|
17
|
+
@value_methods_denylist ||= Set.new
|
18
|
+
end
|
19
|
+
|
13
20
|
def initialize(context, value)
|
14
21
|
@context = context
|
15
22
|
@value = value
|
16
23
|
end
|
17
24
|
|
18
|
-
def filter(
|
19
|
-
if @value.respond_to?(
|
20
|
-
@value = @value.send(
|
21
|
-
elsif @context.respond_to?(
|
22
|
-
@value = @context.send(
|
25
|
+
def filter(name, *args)
|
26
|
+
if @value.respond_to?(name) && !self.class.value_methods_denylist.include?(name)
|
27
|
+
@value = @value.send(name, *args)
|
28
|
+
elsif @context.respond_to?(name)
|
29
|
+
@value = @context.send(name, @value, *args)
|
23
30
|
else
|
24
|
-
"Serbea warning: Filter not found: #{
|
25
|
-
raise_on_missing_filters ? raise(warning) : puts(warning)
|
31
|
+
"Serbea warning: Filter not found: #{name}".tap do |warning|
|
32
|
+
raise_on_missing_filters ? raise(warning) : STDERR.puts(warning)
|
26
33
|
end
|
27
34
|
end
|
28
35
|
|
@@ -1,6 +1,13 @@
|
|
1
1
|
module Serbea
|
2
2
|
class TemplateEngine < Erubi::CaptureEndEngine
|
3
3
|
FRONT_MATTER_REGEXP = %r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m.freeze
|
4
|
+
|
5
|
+
def self.render_directive=(directive)
|
6
|
+
@render_directive = directive
|
7
|
+
end
|
8
|
+
def self.render_directive
|
9
|
+
@render_directive ||= "render"
|
10
|
+
end
|
4
11
|
|
5
12
|
def self.has_yaml_header?(template)
|
6
13
|
template.lines.first&.match? %r!\A---\s*\r?\n!
|
@@ -28,11 +35,11 @@ module Serbea
|
|
28
35
|
string = template.dup
|
29
36
|
if properties[:strip_front_matter] && self.class.has_yaml_header?(string)
|
30
37
|
if string = string.match(FRONT_MATTER_REGEXP)
|
31
|
-
string = string.post_match
|
32
|
-
# yaml_data = SafeYAML.load(template.captures[0])
|
38
|
+
string = ("{%#%}\n" * string.to_s.lines.count) + string.post_match
|
33
39
|
end
|
34
40
|
end
|
35
41
|
|
42
|
+
# Ensure the raw "tag" will strip out all ERB-style processing
|
36
43
|
until string.empty?
|
37
44
|
text, code, string = string.partition(/{% raw %}.*?{% endraw %}/m)
|
38
45
|
|
@@ -47,7 +54,8 @@ module Serbea
|
|
47
54
|
gsub("%}", "__RAW_END_EVAL__")
|
48
55
|
end
|
49
56
|
end
|
50
|
-
|
57
|
+
|
58
|
+
# Process any pipelines
|
51
59
|
string = buff
|
52
60
|
buff = ""
|
53
61
|
until string.empty?
|
@@ -59,7 +67,7 @@ module Serbea
|
|
59
67
|
|
60
68
|
code = code.gsub('\|', "__PIPE_C__")
|
61
69
|
|
62
|
-
subs = code.gsub(/\s
|
70
|
+
subs = code.gsub(/\s*\|>?\s+(.*?)\s([^|}]*)/) do
|
63
71
|
args = $2
|
64
72
|
args = nil if args.strip == ""
|
65
73
|
prefix = processed_filters ? ")" : "))"
|
@@ -73,6 +81,54 @@ module Serbea
|
|
73
81
|
end
|
74
82
|
end
|
75
83
|
|
84
|
+
# Process any directives
|
85
|
+
#
|
86
|
+
# TODO: allow custom directives! aka
|
87
|
+
# {%@something whatever %}
|
88
|
+
# {%@script
|
89
|
+
# const foo = "really? #{really}!"
|
90
|
+
# alert(foo.toLowerCase())
|
91
|
+
# %}
|
92
|
+
# {%@preact AwesomeChart data={#{ruby_data.to_json}} %}
|
93
|
+
string = buff
|
94
|
+
buff = ""
|
95
|
+
until string.empty?
|
96
|
+
text, code, string = string.partition(/{%@.*?%}/m)
|
97
|
+
|
98
|
+
buff << text
|
99
|
+
if code.length > 0
|
100
|
+
code.sub! /^\{%@/, ""
|
101
|
+
code.sub! /%}$/, ""
|
102
|
+
unless ["end", ""].include? code.strip
|
103
|
+
pieces = code.split(" ")
|
104
|
+
if pieces[0].start_with?(/[A-Z]/) # Ruby class name
|
105
|
+
pieces[0].prepend " "
|
106
|
+
pieces[0] << ".new("
|
107
|
+
else # string or something else
|
108
|
+
pieces[0].prepend "("
|
109
|
+
end
|
110
|
+
|
111
|
+
includes_block = false
|
112
|
+
pieces.reverse.each do |piece|
|
113
|
+
if piece == "do" && (pieces.last == "do" || pieces.last.end_with?("|"))
|
114
|
+
piece.prepend(") ")
|
115
|
+
includes_block = true
|
116
|
+
break
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
if includes_block
|
121
|
+
buff << "{%:= #{self.class.render_directive}#{pieces.join(" ")} %}"
|
122
|
+
else
|
123
|
+
pieces.last << ")"
|
124
|
+
buff << "{%= #{self.class.render_directive}#{pieces.join(" ")} %}"
|
125
|
+
end
|
126
|
+
else
|
127
|
+
buff << "{%: end %}"
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
76
132
|
buff
|
77
133
|
end
|
78
134
|
|
data/lib/version.rb
CHANGED
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.
|
4
|
+
version: 0.3.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-09-
|
11
|
+
date: 2020-09-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: erubi
|