serbea 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/README.md +3 -0
- data/Rakefile +10 -0
- data/lib/serbea.rb +200 -0
- data/lib/serbea/rails_support.rb +30 -0
- data/lib/version.rb +3 -0
- data/serbea.gemspec +18 -0
- metadata +49 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 41905c3b6c58badc2f9dfb5945292ae5c1c43c292e8e8020aa41e6c183b818a3
|
4
|
+
data.tar.gz: 37446c2d015a7d50f42000f37a36381c546b5dd4f8d5b1c6f367bee28736f57e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 84fa8bee2baf3be03ca8d983ea4918cc6a9485a9ade1394c1a1850225461daf4928f8c2a016ea308f35dcff98ff8d3553f248c220c21a22e6ef0cbb93720a7cc
|
7
|
+
data.tar.gz: 35c32ad6993b06ba9e33c612443d97d08f111ac119b60fd0dbd5d9d09c16130c44ae2cecae2379286bd6a56b0143c4910d54dbca48cc0501377a352f8dcccd4a
|
data/.gitignore
ADDED
data/README.md
ADDED
data/Rakefile
ADDED
data/lib/serbea.rb
ADDED
@@ -0,0 +1,200 @@
|
|
1
|
+
require "tilt"
|
2
|
+
require "tilt/erubi"
|
3
|
+
require 'erubi/capture_end'
|
4
|
+
require "ostruct"
|
5
|
+
|
6
|
+
module SerbeaHelpers
|
7
|
+
def capture(obj=nil)
|
8
|
+
previous_buffer_state = @_erbout
|
9
|
+
@_erbout = +""
|
10
|
+
result = obj ? yield(obj) : yield
|
11
|
+
@_erbout = previous_buffer_state
|
12
|
+
|
13
|
+
result.respond_to?(:html_safe) ? result.html_safe : result
|
14
|
+
end
|
15
|
+
|
16
|
+
def pipeline(context, value)
|
17
|
+
Serbea::Pipeline.new(context, value)
|
18
|
+
end
|
19
|
+
|
20
|
+
def helper(name, &block)
|
21
|
+
self.class.send(:define_method, name, &block)
|
22
|
+
end
|
23
|
+
|
24
|
+
def h(input)
|
25
|
+
Erubi.h(input)
|
26
|
+
end
|
27
|
+
alias_method :escape, :h
|
28
|
+
end
|
29
|
+
|
30
|
+
module Serbea
|
31
|
+
class Pipeline
|
32
|
+
def initialize(context, value)
|
33
|
+
@context = context
|
34
|
+
@value = value
|
35
|
+
end
|
36
|
+
|
37
|
+
def filter(sym, *aargs)
|
38
|
+
if @value.respond_to?(sym)
|
39
|
+
@value = @value.send(sym, *aargs)
|
40
|
+
elsif @context.respond_to?(sym)
|
41
|
+
@value = @context.send(sym, @value, *aargs)
|
42
|
+
else
|
43
|
+
"Serbea warning: Filter not found: #{sym}".tap do |warning|
|
44
|
+
raise_on_missing_filters ? raise(warning) : puts(warning)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
self
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_s
|
52
|
+
@value.to_s
|
53
|
+
end
|
54
|
+
|
55
|
+
def raise_on_missing_filters; false; end
|
56
|
+
end
|
57
|
+
|
58
|
+
class ComponentRenderer
|
59
|
+
include SerbeaHelpers
|
60
|
+
|
61
|
+
def initialize(variables = {})
|
62
|
+
@variables = variables
|
63
|
+
end
|
64
|
+
|
65
|
+
def respond_to_missing?(key)
|
66
|
+
@variables.key?(key)
|
67
|
+
end
|
68
|
+
|
69
|
+
def method_missing(key)
|
70
|
+
@variables[key] if respond_to_missing?(key)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
class SerbeaEngine < Erubi::CaptureEndEngine
|
76
|
+
FRONT_MATTER_REGEXP = %r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m.freeze
|
77
|
+
|
78
|
+
def self.has_yaml_header?(template)
|
79
|
+
template.lines.first&.match? %r!\A---\s*\r?\n!
|
80
|
+
end
|
81
|
+
|
82
|
+
def initialize(input, properties={})
|
83
|
+
properties[:regexp] = /{%(\:?={1,2}|-|\#|%|\:)?(.*?)([-=])?%}([ \t]*\r?\n)?/m
|
84
|
+
properties[:strip_front_matter] = true unless properties.key?(:strip_front_matter)
|
85
|
+
super process_serbea_input(input, properties), properties
|
86
|
+
end
|
87
|
+
|
88
|
+
def add_postamble(postamble)
|
89
|
+
src << postamble
|
90
|
+
src << "@_erbout.html_safe" if postamble.respond_to?(:html_safe)
|
91
|
+
|
92
|
+
src.gsub!("__RAW_START_PRINT__", "{{")
|
93
|
+
src.gsub!("__RAW_END_PRINT__", "}}")
|
94
|
+
src.gsub!("__RAW_START_EVAL__", "{%")
|
95
|
+
src.gsub!("__RAW_END_EVAL__", "%}")
|
96
|
+
end
|
97
|
+
|
98
|
+
def process_serbea_input(template, properties)
|
99
|
+
buff = ""
|
100
|
+
|
101
|
+
string = template.dup
|
102
|
+
if properties[:strip_front_matter] && self.class.has_yaml_header?(string)
|
103
|
+
if string = string.match(FRONT_MATTER_REGEXP)
|
104
|
+
string = string.post_match
|
105
|
+
# yaml_data = SafeYAML.load(template.captures[0])
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
until string.empty?
|
110
|
+
text, code, string = string.partition(/{% raw %}.*?{% endraw %}/m)
|
111
|
+
|
112
|
+
buff << text
|
113
|
+
if code.length > 0
|
114
|
+
buff << code.
|
115
|
+
sub("{% raw %}", "").
|
116
|
+
sub("{% endraw %}", "").
|
117
|
+
gsub("{{", "__RAW_START_PRINT__").
|
118
|
+
gsub("}}", "__RAW_END_PRINT__").
|
119
|
+
gsub("{%", "__RAW_START_EVAL__").
|
120
|
+
gsub("%}", "__RAW_END_EVAL__")
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
string = buff
|
125
|
+
buff = ""
|
126
|
+
until string.empty?
|
127
|
+
text, code, string = string.partition(/{{.*?}}/m)
|
128
|
+
|
129
|
+
buff << text
|
130
|
+
if code.length > 0
|
131
|
+
processed_filters = false
|
132
|
+
|
133
|
+
code = code.gsub('\|', "__PIPE_C__")
|
134
|
+
|
135
|
+
subs = code.gsub(/ *\| +(.*?) ([^|}]*)/) do
|
136
|
+
args = $2
|
137
|
+
args = nil if args.strip == ""
|
138
|
+
prefix = processed_filters ? ")" : "))"
|
139
|
+
processed_filters = true
|
140
|
+
"#{prefix}.filter(:#{$1.chomp(":")}" + (args ? ", #{args}" : "")
|
141
|
+
end
|
142
|
+
|
143
|
+
pipeline_suffix = processed_filters ? ") %}" : ")) %}"
|
144
|
+
|
145
|
+
buff << subs.sub("{{", "{%= pipeline(self, (").sub("}}", pipeline_suffix).gsub("__PIPE_C__", '\|')
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
# puts buff
|
150
|
+
buff
|
151
|
+
end # method
|
152
|
+
|
153
|
+
private
|
154
|
+
|
155
|
+
# Handle the <%:= and <%:== tags
|
156
|
+
# Carried over from the Erubi class but with changed indicators
|
157
|
+
def handle(indicator, code, tailch, rspace, lspace)
|
158
|
+
case indicator
|
159
|
+
when ':=', ':=='
|
160
|
+
rspace = nil if tailch && !tailch.empty?
|
161
|
+
add_text(lspace) if lspace
|
162
|
+
escape_capture = !((indicator == ':=') ^ @escape_capture)
|
163
|
+
src << "begin; (#{@bufstack} ||= []) << #{@bufvar}; #{@bufvar} = #{@bufval}; #{@bufstack}.last << #{@escapefunc if escape_capture}((" << code
|
164
|
+
add_text(rspace) if rspace
|
165
|
+
when ':'
|
166
|
+
rspace = nil if tailch && !tailch.empty?
|
167
|
+
add_text(lspace) if lspace
|
168
|
+
result = @yield_returns_buffer ? " #{@bufvar}; " : ""
|
169
|
+
src << result << code << ")).to_s; ensure; #{@bufvar} = #{@bufstack}.pop; end;"
|
170
|
+
add_text(rspace) if rspace
|
171
|
+
else
|
172
|
+
super
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end # class
|
176
|
+
|
177
|
+
module Tilt
|
178
|
+
class SerbeaTemplate < ErubiTemplate
|
179
|
+
def prepare
|
180
|
+
@options.merge!(outvar: "@_erbout", engine_class: SerbeaEngine)
|
181
|
+
super
|
182
|
+
end
|
183
|
+
|
184
|
+
def encoding
|
185
|
+
@src.encoding
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
Tilt.register Tilt::SerbeaTemplate, "serb" #, "serbea"
|
191
|
+
|
192
|
+
if defined?(Rails::Railtie)
|
193
|
+
class Railtie < ::Rails::Railtie
|
194
|
+
initializer :serbea do |app|
|
195
|
+
ActiveSupport.on_load(:action_view) do
|
196
|
+
require "serbea/rails_support"
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# copied from https://github.com/haml/haml/blob/main/lib/haml/plugin.rb
|
4
|
+
module Serbea
|
5
|
+
|
6
|
+
# This module makes Serbea work with Rails using the template handler API.
|
7
|
+
class Plugin
|
8
|
+
def handles_encoding?; true; end
|
9
|
+
|
10
|
+
def compile(template, source)
|
11
|
+
foo = "self.class.include(SerbeaHelpers)\n" + Tilt::SerbeaTemplate.new { source }.precompiled_template([])
|
12
|
+
p foo
|
13
|
+
foo
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.call(template, source = nil)
|
17
|
+
source ||= template.source
|
18
|
+
|
19
|
+
new.compile(template, source)
|
20
|
+
end
|
21
|
+
|
22
|
+
# def cache_fragment(block, name = {}, options = nil)
|
23
|
+
# @view.fragment_for(block, name, options) do
|
24
|
+
# eval("_hamlout.buffer", block.binding)
|
25
|
+
# end
|
26
|
+
# end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
ActionView::Template.register_template_handler(:serb, Serbea::Plugin)
|
data/lib/version.rb
ADDED
data/serbea.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "serbea"
|
7
|
+
spec.version = Serbea::VERSION
|
8
|
+
spec.author = "Bridgetown Team"
|
9
|
+
spec.email = "maintainers@bridgetownrb.com"
|
10
|
+
spec.summary = "Similar to ERB, Except Awesomer"
|
11
|
+
spec.homepage = "https://github.com/bridgetownrb/serbea"
|
12
|
+
spec.license = "MIT"
|
13
|
+
|
14
|
+
spec.required_ruby_version = ">= 2.5"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r!^(test|script|spec|features)/!) }
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: serbea
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bridgetown Team
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-09-11 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email: maintainers@bridgetownrb.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- ".gitignore"
|
20
|
+
- README.md
|
21
|
+
- Rakefile
|
22
|
+
- lib/serbea.rb
|
23
|
+
- lib/serbea/rails_support.rb
|
24
|
+
- lib/version.rb
|
25
|
+
- serbea.gemspec
|
26
|
+
homepage: https://github.com/bridgetownrb/serbea
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '2.5'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubygems_version: 3.0.6
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: Similar to ERB, Except Awesomer
|
49
|
+
test_files: []
|