serbea 0.6.1 β†’ 0.7.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: 843083ae2fadbebd662b112d2a5c1710e3724f34857b1c6a2a54245e7abc52b9
4
- data.tar.gz: 104fdeb290cfd5a94cfa26beed5aa31c55771c8cd23f7501e42af6ec27803de9
3
+ metadata.gz: c5b809ec2d2de960b954c9d12d4bdfaaf1f910ff1f1e5aa7ac217db968cd1ac7
4
+ data.tar.gz: e819b84c2c0a33767f4d3736de70e916bfdd41fe354ba89a9f551172b22bdb41
5
5
  SHA512:
6
- metadata.gz: 8cfc623c9246ca644fdd64b5cfe306a66918ae250c066b7500d34e53b9ef2b2b61504cc1707831b40eb44fd3a27ab24dd3556bc9a5cb943ae4725a5269c4670a
7
- data.tar.gz: 9fc958b5e73f32d6094d9ef811934deb7a0994b74159a7ed3b86ac73137064a2dd727015d7211051dc51901c9c9e444eaa1ea4afbee64fe9e1b526bd43d62da5
6
+ metadata.gz: 71b3ab77eed51511959586f271ff814e094c6ef4fb4278dc60d85b92dafec4a328a084e27359c7626b661dea18e4e4f6007bceb40b4780557e1454752ed95d27
7
+ data.tar.gz: ff277d2b7471f668586248e0524429c8380ca30322f5b698bdf44b094a4cd7a202dfdfd3a87a7d1de3b652140eaf5d9e285f1addeb632e79e4be96b83e2e415b
data/Gemfile CHANGED
@@ -4,3 +4,5 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in serbea.gemspec
6
6
  gemspec
7
+
8
+ gem "erubi", github: "jaredcwhite/erubi", branch: "config-literal-prefix"
data/README.md CHANGED
@@ -1,3 +1,109 @@
1
- πŸ‘‘ Serbea: Similar to ERB, Except Awesomer
1
+ # πŸ‘‘ Serbea: Similar to ERB, Except Awesomer
2
2
 
3
- The Ruby template language you always dreamed of is here. Le roi est mort, vive le roi!
3
+ The Ruby template language you've always dreamed of is finally here. _Le roi est mort, vive le roi!_
4
+
5
+ Serbea combines the best ideas from "brace-style" template languages such as Liquid, Nunjucks, Twig, Jinja, Mustache, etc.β€”and applies them to the world of ERB. You can use Serbea in Rails application, Bridgetown static sites, or pretty much any Ruby scenario you could imagine.
6
+
7
+ ## Features
8
+
9
+ * Real Ruby. Like, for real.
10
+ * Filters!!! Pipeline operators!!!
11
+ * Autoescaped variables by default within the pipeline (`{{ }}`) tags. Use the `safe`/`raw` or `escape`/`h` filters to control escaping on output.
12
+ * Render directive `{%@ %}` as shortcut for rendering either string-named partials (`render "tmpl"`) or object instances (`render MyComponent.new`).
13
+ * Supports every convention of ERB and builds upon it with new features (which is why it's "awesomer!").
14
+ * Builtin frontmatter support (even in Rails!) so you can access the variables written into the top YAML within your templates.
15
+ * The filters accessible within Serbea templates are either helpers (where the variable gets passed as the first argument) or instance methods of the variable itself, so you can build extremely expressive pipelines that take advantage of the code you already know and love. (For example, in Rails you could write `{{ "My Link" | link_to: route_path }}`).
16
+ * The `Serbea::Pipeline.exec` method lets you pass a pipeline template in, along with an optional input value or included helpers module, and you'll get the output as a object of any type (not converted to a string like in traditional templates). For example: `Serbea::Pipeline.exec("[1,2,3] |> map: ->(i) { i * 10 }")` will return `[10, 20, 30]`.
17
+
18
+ ## What It Looks Like
19
+
20
+ ```hbs
21
+ # example.serb
22
+
23
+ {% wow = capture do %}
24
+ This is {{ "amazing" + "!" | upcase }}
25
+ {% end.each_char.reduce("") do |newstr, c|
26
+ newstr += " #{c}"
27
+ end.strip %}
28
+
29
+ {{ wow | prepend: "OMG! " }}
30
+ ```
31
+
32
+ ```hbs
33
+ <p>
34
+ {%
35
+ helper :multiply_array do |input, multiply_by = 2|
36
+ input.map do |i|
37
+ i.to_i * multiply_by
38
+ end
39
+ end
40
+ %}
41
+
42
+ Multiply! {{ [1,3,6, "9"] | multiply_array: 10 }}
43
+ </p>
44
+ ```
45
+
46
+ ```hbs
47
+ {%= form classname: "checkout" do |f| %}
48
+ {{ f.input :first_name, required: true | errors: error_messages }}
49
+ {% end %}
50
+ ```
51
+
52
+ ```hbs
53
+ {%= render "box" do %}
54
+ This is **dope!**
55
+ {%= render "card", title: "Nifty!" do %}
56
+ So great.
57
+ {% end %}
58
+ {% end %}
59
+
60
+ # Let's simplify that using the new render directive!
61
+
62
+ {%@ "box" do %}
63
+ This is **dope!**
64
+ {%@ "card", title: "Nifty!" do %}
65
+ So great.
66
+ {% end %}
67
+ {% end %}
68
+ ```
69
+
70
+ ```hbs
71
+ # Works with ViewComponent!
72
+
73
+ {%= render(Theme::DropdownComponent.new(name: "banner", label: "Banners")) do |dropdown| %}
74
+ {% RegistryTheme::BANNERS.each do |banner| %}
75
+ {% dropdown.slot(:item, value: banner) do %}
76
+ <img src="{{ banner | parameterize: separator: "_" | prepend: "/themes/" | append: ".jpg" }}">
77
+ <strong>{{ banner }}</strong>
78
+ {% end %}
79
+ {% end %}
80
+ {% end %}
81
+
82
+ # Even better, use the new render directive!
83
+
84
+ {%@ Theme::DropdownComponent name: "banner", label: "Banners" do |dropdown| %}
85
+ {% RegistryTheme::BANNERS.each do |banner| %}
86
+ {% dropdown.slot(:item, value: banner) do %}
87
+ <img src="{{ banner | parameterize: separator: "_" | prepend: "/themes/" | append: ".jpg" }}">
88
+ <strong>{{ banner }}</strong>
89
+ {% end %}
90
+ {% end %}
91
+ {% end %}
92
+ ```
93
+
94
+ ```hbs
95
+ # The | and |> pipeline operators are equivalent, so you can write like this if you want!
96
+
97
+ {{
98
+ [1,2,3] |>
99
+ map: -> i {
100
+ i * 10
101
+ } |>
102
+ filter: -> i do
103
+ i > 15
104
+ end |>
105
+ assign_to: :array_length
106
+ }}
107
+
108
+ Array length: {{ @array_length.length }}
109
+ ```
data/Rakefile CHANGED
@@ -1,10 +1,12 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rake/testtask"
3
+ require "bundler"
4
+
5
+ Bundler.setup
3
6
 
4
7
  Rake::TestTask.new(:test) do |t|
5
- t.libs << "test"
6
- t.libs << "lib"
7
- t.test_files = FileList["test/**/test_*.rb"]
8
+ t.test_files = FileList["test/test.rb"]
9
+ t.warning = false
8
10
  end
9
11
 
10
12
  task :default => :test
@@ -4,12 +4,17 @@ require "tilt/erubi"
4
4
  require "serbea/helpers"
5
5
  require "serbea/pipeline"
6
6
  require "serbea/template_engine"
7
- require "serbea/component_renderer"
8
7
 
9
8
  module Tilt
10
9
  class SerbeaTemplate < ErubiTemplate
11
10
  def prepare
12
- @options.merge!(outvar: "@_erbout", bufval: "Serbea::Buffer.new", engine_class: Serbea::TemplateEngine)
11
+ @options.merge!(
12
+ outvar: "@_erbout",
13
+ bufval: "Serbea::Buffer.new",
14
+ literal_prefix: "{%",
15
+ literal_postfix: "%}",
16
+ engine_class: Serbea::TemplateEngine
17
+ )
13
18
  super
14
19
  end
15
20
 
@@ -58,8 +58,6 @@ module Bridgetown
58
58
  if convertible.data[:template_engine] == "serbea" ||
59
59
  (convertible.data[:template_engine].nil? &&
60
60
  @config[:template_engine] == "serbea")
61
- # make sure Liquid doesn't find {% %} and decide to process Serbea code!
62
- convertible.data["render_with_liquid"] = false
63
61
  return true
64
62
  end
65
63
  end
@@ -73,3 +71,13 @@ module Bridgetown
73
71
  end
74
72
  end
75
73
  end
74
+
75
+ Bridgetown::Hooks.register :site, :pre_render, reloadable: false do |site|
76
+ # make sure Liquid doesn't find {% %} and decide to process Serbea code!
77
+ site.contents.each do |convertible|
78
+ convertible.data.render_with_liquid = false if convertible.extname == ".serb"
79
+ end
80
+ site.layouts.values.each do |convertible|
81
+ convertible.data.render_with_liquid = false if convertible.ext == ".serb"
82
+ end
83
+ end
@@ -29,9 +29,27 @@ module Serbea
29
29
  Pipeline.new(context, value)
30
30
  end
31
31
 
32
- def helper(name, &block)
33
- self.class.send(:define_method, name, &block)
32
+ def helper(name, &helper_block)
33
+ self.class.define_method(name) do |*args, &block|
34
+ previous_buffer_state = @_erbout
35
+ @_erbout = Serbea::Buffer.new
36
+
37
+ # For compatibility with ActionView, not used by Bridgetown normally
38
+ previous_ob_state = @output_buffer
39
+ @output_buffer = Serbea::Buffer.new
40
+
41
+ result = helper_block.call(*args, &block)
42
+ if @output_buffer != ""
43
+ # use Rails' ActionView buffer if present
44
+ result = @output_buffer
45
+ end
46
+ @_erbout = previous_buffer_state
47
+ @output_buffer = previous_ob_state
48
+
49
+ result.is_a?(String) ? result.html_safe : result
50
+ end
34
51
  end
52
+ alias_method :macro, :helper
35
53
 
36
54
  def h(input)
37
55
  ERB::Util.h(input.to_s)
@@ -2,7 +2,6 @@
2
2
 
3
3
  # inspired by https://github.com/haml/haml/blob/main/lib/haml/plugin.rb
4
4
  module Serbea
5
-
6
5
  # This module makes Serbea work with Rails using the template handler API.
7
6
  class Plugin
8
7
  def handles_encoding?; true; end
@@ -1,3 +1,5 @@
1
+ require "strscan"
2
+
1
3
  module Serbea
2
4
  class Buffer < String
3
5
  def concat_to_s(input)
@@ -64,13 +66,11 @@ module Serbea
64
66
 
65
67
  # Ensure the raw "tag" will strip out all ERB-style processing
66
68
  until string.empty?
67
- text, code, string = string.partition(/{% raw %}.*?{% endraw %}/m)
69
+ text, code, string = string.partition(/{% raw %}(.*?){% endraw %}/m)
68
70
 
69
71
  buff << text
70
72
  if code.length > 0
71
- buff << code.
72
- sub("{% raw %}", "").
73
- sub("{% endraw %}", "").
73
+ buff << $1.
74
74
  gsub("{{", "__RAW_START_PRINT__").
75
75
  gsub("}}", "__RAW_END_PRINT__").
76
76
  gsub("{%", "__RAW_START_EVAL__").
@@ -82,27 +82,61 @@ module Serbea
82
82
  string = buff
83
83
  buff = ""
84
84
  until string.empty?
85
- text, code, string = string.partition(/{{.*?}}/m)
85
+ text, code, string = string.partition(/{{(.*?)}}/m)
86
86
 
87
87
  buff << text
88
88
  if code.length > 0
89
89
  original_line_length = code.lines.size
90
- processed_filters = false
91
-
92
- code = code.gsub('\|', "__PIPE_C__")
93
-
94
- subs = code.gsub(/\s*\|>?\s+(.*?)\s([^|}]*)/) do
95
- args = $2
96
- args = nil if args.strip == ""
97
- prefix = processed_filters ? ")" : "))"
98
- processed_filters = true
99
- "#{prefix}.filter(:#{$1.chomp(":")}" + (args ? ", #{args}" : "")
90
+
91
+ s = StringScanner.new($1)
92
+ escaped_segment = ""
93
+ segments = []
94
+ until s.eos?
95
+ portion = s.scan_until(/\|>?/)
96
+ if portion
97
+ if portion.end_with?('\|')
98
+ # the pipe is escaped, so save that for later
99
+ escaped_segment += portion.sub(/\\\|$/, "|")
100
+ elsif escaped_segment.length > 0
101
+ # we already have escaped content, so finish that up
102
+ segments << escaped_segment + portion.sub(/\|>?$/, "")
103
+ escaped_segment = ""
104
+ else
105
+ # let's find out if this is actionable now
106
+ if s.check(/\|/)
107
+ # nope, the next character is another pipe, so let's escape
108
+ s.pos += 1
109
+ escaped_segment += portion + "|"
110
+ else
111
+ # finally, we have liftoff!
112
+ segments << portion.sub(/\|>?$/, "")
113
+ end
114
+ end
115
+ else
116
+ # we've reached the last bit of the code
117
+ if escaped_segment.length > 0
118
+ # escape and get the rest
119
+ segments << escaped_segment + s.rest
120
+ else
121
+ # or just the rest will do
122
+ segments << s.rest
123
+ end
124
+ s.terminate
125
+ end
126
+ end
127
+
128
+ segments[0] = "pipeline(self, (#{segments[0].strip}))"
129
+ segments[1..-1].each_with_index do |segment, index|
130
+ filter, args = segment.strip.match(/([^ :]*)(.*)/m).captures
131
+ segments[index + 1] = ".filter(:" + filter
132
+ if args == ""
133
+ segments[index + 1] += ")"
134
+ else
135
+ segments[index + 1] += "," + args.sub(/^:/, "") + ")"
136
+ end
100
137
  end
101
-
102
- pipeline_suffix = processed_filters ? ") %}" : ")) %}"
103
-
104
- subs = subs.sub("{{", "{%= pipeline(self, (").sub("}}", pipeline_suffix).gsub("__PIPE_C__", '|')
105
138
 
139
+ subs = "{%= #{segments.join} %}"
106
140
  buff << subs
107
141
 
108
142
  (original_line_length - subs.lines.size).times do
@@ -111,24 +145,15 @@ module Serbea
111
145
  end
112
146
  end
113
147
 
114
- # Process any directives
115
- #
116
- # TODO: allow custom directives! aka
117
- # {%@something whatever %}
118
- # {%@script
119
- # const foo = "really? #{really}!"
120
- # alert(foo.toLowerCase())
121
- # %}
122
- # {%@preact AwesomeChart data={#{ruby_data.to_json}} %}
148
+ # Process any render directives
123
149
  string = buff
124
150
  buff = ""
125
151
  until string.empty?
126
- text, code, string = string.partition(/{%@.*?%}/m)
152
+ text, code, string = string.partition(/{%@(.*?)%}/m)
127
153
 
128
154
  buff << text
129
155
  if code.length > 0
130
- code.sub! /^\{%@/, ""
131
- code.sub! /%}$/, ""
156
+ code = $1
132
157
  unless ["end", ""].include? code.strip
133
158
  original_line_length = code.lines.size
134
159
 
@@ -1,3 +1,3 @@
1
1
  module Serbea
2
- VERSION = "0.6.1"
2
+ VERSION = "0.7.1"
3
3
  end
@@ -16,6 +16,7 @@ Gem::Specification.new do |spec|
16
16
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r!^(test|script|spec|features)/!) }
17
17
  spec.require_paths = ["lib"]
18
18
 
19
+ spec.add_runtime_dependency("rake", "~> 13.0")
19
20
  spec.add_runtime_dependency("erubi", "~> 1.9")
20
21
  spec.add_runtime_dependency("activesupport", "~> 6.0")
21
22
  spec.add_runtime_dependency("tilt", "~> 2.0")
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serbea
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.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-10-29 00:00:00.000000000 Z
11
+ date: 2020-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '13.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '13.0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: erubi
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -64,7 +78,6 @@ files:
64
78
  - Rakefile
65
79
  - lib/serbea.rb
66
80
  - lib/serbea/bridgetown_support.rb
67
- - lib/serbea/component_renderer.rb
68
81
  - lib/serbea/helpers.rb
69
82
  - lib/serbea/pipeline.rb
70
83
  - lib/serbea/rails_support.rb
@@ -1,19 +0,0 @@
1
- module Serbea
2
- class ComponentRenderer
3
- include Helpers
4
-
5
- def initialize(variables = {})
6
- @variables = variables
7
- end
8
-
9
- def respond_to_missing?(key, include_private = false)
10
- @variables.key?(key)
11
- end
12
-
13
- def method_missing(key)
14
- return @variables[key] if respond_to_missing?(key)
15
-
16
- super
17
- end
18
- end
19
- end