serbea 0.11.3 → 1.0.0

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: c5c25c113b633bbeaaca37974b02ab662c34c803609966124ed390fccfb14440
4
- data.tar.gz: e1126de9ab8506745882c2410d64146ac98e88d7b22fe7fa13bdbdc4afc0a1be
3
+ metadata.gz: 2014debe9382e252988c8d41ebec4102153508a856dca4dd7b72d2e347fb4023
4
+ data.tar.gz: aa729c3b2212ff33e5399544bc798593864248d6aacaeb0764c92fa43dc0cf8e
5
5
  SHA512:
6
- metadata.gz: a7daa83b85d6674c5e0a5ea657ba7fa8baaae33a5651c09983cd62a56ccd8420752283b04a0f06c26bad4350f48a964126abd1a8e3613a62015b48fe293ffe3c
7
- data.tar.gz: acce221b376b93ce9956bf6928877c9f504ada4b31e8df1b4d5b82cdbb5a9dfd15e6e07aefcc55536a2466fbbc7a5acc5b38434813a05099b01ceb49595f9531
6
+ metadata.gz: c7d3037ee50671772fad0ab5e8cbce90a9ae402769d5ffd49163df182a8c011da83d66809602d01d3186b2cc513d30116922fab461f03d3d0746b2d3f17a977e
7
+ data.tar.gz: 63f25bbbc2edd056c6c5cb461c356b52301f4590cc8733ca2be621f430b39d20df30f8ee881344e3c4088e19fb34d5630f4b9ac045db2fb5a3908830ae6bef18
data/docs/src/index.md CHANGED
@@ -48,7 +48,19 @@ layout: home
48
48
  * Built-in frontmatter support. Now you can access the variables written into a top YAML block within your templates. In any Rails view, including layouts, you'll have access to the `@frontmatter` ivar which is a merged `HashWithDotAccess::Hash` with data from any part of the view tree (partials, pages, layout).
49
49
 
50
50
  For example, you could put `<title>{{ @frontmatter.title }}</title>` in your head partial, and then each page could define `title` frontmatter individually. You can even use Ruby string interpolation within the YAML so titles and other metadata can come from `t` language helpers.
51
+ * Define macros/helpers which can be imported and used within Serbea templates.
51
52
 
53
+ ```serb
54
+ <!-- _macros.serb -->
55
+ {% macro :greet do |name:| %}
56
+ {{ name }}, my old friend!
57
+ {% end %}
58
+
59
+ <!-- tmpl.serb -->
60
+ {% import "macros" %}
61
+
62
+ Hello {{ greet(name: "darkness") }}
63
+ ```
52
64
 
53
65
  {% endraw %}
54
66
 
@@ -70,7 +82,7 @@ layout: home
70
82
  {{ wow | prepend: "OMG! " }}
71
83
  ```
72
84
 
73
- Serbea lets us define helpers inside templates directly:
85
+ Use `helper` (alias `macro`) to create ad-hoc filters:
74
86
 
75
87
  ```serb
76
88
  <p>
@@ -50,4 +50,4 @@ module Rouge
50
50
  end
51
51
  end
52
52
  end
53
- end
53
+ end
@@ -9,11 +9,11 @@ module Serbea
9
9
  def capture(*args)
10
10
  previous_buffer_state = @_erbout
11
11
  @_erbout = Serbea::OutputBuffer.new
12
- yield(*args)
13
- result = @_erbout
12
+ result = yield(*args)
13
+ result = @_erbout.presence || result
14
14
  @_erbout = previous_buffer_state
15
15
 
16
- result&.html_safe
16
+ Serbea::OutputBuffer === result ? result.html_safe : result
17
17
  end
18
18
 
19
19
  def pipeline(context, value)
@@ -22,16 +22,19 @@ module Serbea
22
22
 
23
23
  def helper(name, &helper_block)
24
24
  self.class.define_method(name) do |*args, **kwargs, &block|
25
- previous_buffer_state = @_erbout
26
- @_erbout = Serbea::OutputBuffer.new
27
- result = helper_block.call(*args, **kwargs, &block)
28
- @_erbout = previous_buffer_state
29
-
30
- result.is_a?(String) ? result.html_safe : result
25
+ capture { helper_block.call(*args, **kwargs, &block) }
31
26
  end
32
27
  end
33
28
  alias_method :macro, :helper
34
29
 
30
+ def import(*args, **kwargs, &block)
31
+ helper_names = %i(partial render)
32
+ available_helper = helper_names.find { |meth| respond_to?(meth) }
33
+ raise "Serbea Error: no `render' or `partial' helper available in #{self.class}" unless available_helper
34
+ available_helper == :partial ? partial(*args, **kwargs, &block) : render(*args, **kwargs, &block)
35
+ nil
36
+ end
37
+
35
38
  def h(input)
36
39
  ERB::Util.h(input.to_s)
37
40
  end
@@ -1,3 +1,4 @@
1
+ require "set"
1
2
  require "active_support/core_ext/string/output_safety"
2
3
  require "active_support/core_ext/object/blank"
3
4
 
@@ -68,38 +69,21 @@ module Serbea
68
69
  @value = value
69
70
  end
70
71
 
71
- # TODO: clean this up somehow and still support Ruby 2.5..3.0!
72
72
  def filter(name, *args, **kwargs)
73
73
  if @value.respond_to?(name) && !self.class.value_methods_denylist.include?(name)
74
74
  if args.last.is_a?(Proc)
75
75
  real_args = args.take(args.length - 1)
76
76
  block = args.last
77
- unless kwargs.empty?
78
- @value = @value.send(name, *real_args, **kwargs, &block)
79
- else
80
- @value = @value.send(name, *real_args, &block)
81
- end
77
+ @value = @value.send(name, *real_args, **kwargs, &block)
82
78
  else
83
- unless kwargs.empty?
84
- @value = @value.send(name, *args, **kwargs)
85
- else
86
- @value = @value.send(name, *args)
87
- end
79
+ @value = @value.send(name, *args, **kwargs)
88
80
  end
89
81
  elsif @context.respond_to?(name)
90
- unless kwargs.empty?
91
- @value = @context.send(name, @value, *args, **kwargs)
92
- else
93
- @value = @context.send(name, @value, *args)
94
- end
82
+ @value = @context.send(name, @value, *args, **kwargs)
95
83
  elsif @binding.local_variables.include?(name)
96
84
  var = @binding.local_variable_get(name)
97
85
  if var.respond_to?(:call)
98
- unless kwargs.empty?
99
- @value = var.call(@value, *args, **kwargs)
100
- else
101
- @value = var.call(@value, *args)
102
- end
86
+ @value = var.call(@value, *args, **kwargs)
103
87
  else
104
88
  "Serbea warning: Filter #{name} does not respond to call".tap do |warning|
105
89
  self.class.raise_on_missing_filters ? raise(warning) : STDERR.puts(warning)
data/lib/serbea.rb CHANGED
@@ -25,19 +25,3 @@ module Tilt
25
25
  end
26
26
 
27
27
  Tilt.register Tilt::SerbeaTemplate, "serb"
28
-
29
- if defined?(Rails::Railtie)
30
- class Railtie < ::Rails::Railtie
31
- initializer :serbea do |app|
32
- ActiveSupport.on_load(:action_view) do
33
- require "serbea/rails_support"
34
-
35
- ActionController::Base.include Serbea::Rails::FrontmatterControllerActions
36
- end
37
- end
38
- end
39
- end
40
-
41
- if defined?(Bridgetown)
42
- require "serbea/bridgetown_support"
43
- end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Serbea
2
- VERSION = "0.11.3"
2
+ VERSION = "1.0.0"
3
3
  end
data/serbea.gemspec CHANGED
@@ -11,12 +11,12 @@ Gem::Specification.new do |spec|
11
11
  spec.homepage = "https://github.com/bridgetownrb/serbea"
12
12
  spec.license = "MIT"
13
13
 
14
- spec.required_ruby_version = ">= 2.5"
14
+ spec.required_ruby_version = ">= 2.7"
15
15
 
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("activesupport", "~> 6.0")
19
+ spec.add_runtime_dependency("activesupport", ">= 6.0")
20
20
  spec.add_runtime_dependency("erubi", ">= 1.10")
21
21
  spec.add_runtime_dependency("hash_with_dot_access", "~> 1.1")
22
22
  spec.add_runtime_dependency("tilt", "~> 2.0")
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serbea
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bridgetown Team
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-03 00:00:00.000000000 Z
11
+ date: 2021-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '6.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '6.0'
27
27
  - !ruby/object:Gem::Dependency
@@ -80,7 +80,7 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '13.0'
83
- description:
83
+ description:
84
84
  email: maintainers@bridgetownrb.com
85
85
  executables: []
86
86
  extensions: []
@@ -117,12 +117,10 @@ files:
117
117
  - docs/sync.js
118
118
  - docs/webpack.config.js
119
119
  - docs/yarn.lock
120
+ - lib/rouge/lexers/serbea.rb
120
121
  - lib/serbea.rb
121
- - lib/serbea/bridgetown_support.rb
122
122
  - lib/serbea/helpers.rb
123
123
  - lib/serbea/pipeline.rb
124
- - lib/serbea/rails_support.rb
125
- - lib/serbea/rouge_lexer.rb
126
124
  - lib/serbea/template_engine.rb
127
125
  - lib/version.rb
128
126
  - serbea.gemspec
@@ -130,7 +128,7 @@ homepage: https://github.com/bridgetownrb/serbea
130
128
  licenses:
131
129
  - MIT
132
130
  metadata: {}
133
- post_install_message:
131
+ post_install_message:
134
132
  rdoc_options: []
135
133
  require_paths:
136
134
  - lib
@@ -138,15 +136,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
138
136
  requirements:
139
137
  - - ">="
140
138
  - !ruby/object:Gem::Version
141
- version: '2.5'
139
+ version: '2.7'
142
140
  required_rubygems_version: !ruby/object:Gem::Requirement
143
141
  requirements:
144
142
  - - ">="
145
143
  - !ruby/object:Gem::Version
146
144
  version: '0'
147
145
  requirements: []
148
- rubygems_version: 3.2.3
149
- signing_key:
146
+ rubygems_version: 3.1.4
147
+ signing_key:
150
148
  specification_version: 4
151
149
  summary: Similar to ERB, Except Awesomer
152
150
  test_files: []
@@ -1,68 +0,0 @@
1
- require "serbea/rouge_lexer"
2
- require "bridgetown-core"
3
-
4
- module Bridgetown
5
- class SerbeaView < ERBView
6
- include Serbea::Helpers
7
-
8
- def partial(partial_name, options = {}, &block)
9
- options.merge!(options[:locals]) if options[:locals]
10
- options[:content] = capture(&block) if block
11
-
12
- partial_segments = partial_name.split("/")
13
- partial_segments.last.sub!(%r!^!, "_")
14
- partial_name = partial_segments.join("/")
15
-
16
- Tilt::SerbeaTemplate.new(
17
- site.in_source_dir(site.config[:partials_dir], "#{partial_name}.serb")
18
- ).render(self, options)
19
- end
20
- end
21
-
22
- module Converters
23
- class SerbeaTemplates < Converter
24
- priority :highest
25
- input :serb
26
-
27
- # Logic to do the Serbea content conversion.
28
- #
29
- # @param content [String] Content of the file (without front matter).
30
- # @param convertible [Bridgetown::Page, Bridgetown::Document, Bridgetown::Layout]
31
- # The instantiated object which is processing the file.
32
- #
33
- # @return [String] The converted content.
34
- def convert(content, convertible)
35
- return content if convertible.data[:template_engine] != "serbea"
36
-
37
- serb_view = Bridgetown::SerbeaView.new(convertible)
38
-
39
- serb_renderer = Tilt::SerbeaTemplate.new(convertible.relative_path) { content }
40
-
41
- if convertible.is_a?(Bridgetown::Layout)
42
- serb_renderer.render(serb_view) do
43
- convertible.current_document_output
44
- end
45
- else
46
- serb_renderer.render(serb_view)
47
- end
48
- end
49
-
50
- def matches(ext, convertible)
51
- if convertible.data[:template_engine] == "serbea" ||
52
- (convertible.data[:template_engine].nil? &&
53
- @config[:template_engine] == "serbea")
54
- convertible.data[:template_engine] = "serbea"
55
- return true
56
- end
57
-
58
- super(ext).tap do |ext_matches|
59
- convertible.data[:template_engine] = "serbea" if ext_matches
60
- end
61
- end
62
-
63
- def output_ext(ext)
64
- ext == ".serb" ? ".html" : ext
65
- end
66
- end
67
- end
68
- end
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "hash_with_dot_access"
4
-
5
- module Serbea
6
- module Rails
7
- module FrontmatterHelpers
8
- def set_page_frontmatter=(data)
9
- @frontmatter ||= HashWithDotAccess::Hash.new
10
- @frontmatter.update(data)
11
- end
12
- end
13
-
14
- module FrontmatterControllerActions
15
- extend ActiveSupport::Concern
16
-
17
- included do
18
- Serbea::TemplateEngine.front_matter_preamble = "self.set_page_frontmatter = local_frontmatter = YAML.load"
19
-
20
- before_action { @frontmatter ||= HashWithDotAccess::Hash.new }
21
-
22
- helper Serbea::Rails::FrontmatterHelpers
23
- end
24
- end
25
-
26
- class TemplateHandler
27
- def handles_encoding?; true; end
28
-
29
- def compile(template, source)
30
- "self.class.include(Serbea::Helpers);" + Tilt::SerbeaTemplate.new { source }.precompiled_template([])
31
- end
32
-
33
- def self.call(template, source = nil)
34
- source ||= template.source
35
-
36
- new.compile(template, source)
37
- end
38
- end
39
- end
40
- end
41
-
42
- Serbea::TemplateEngine.directive :form, ->(code, buffer) do
43
- model_name, space, params = code.lstrip.partition(%r(\s)m)
44
- model_name.chomp!(",")
45
- model_name = "#{model_name}," unless params.lstrip.start_with?("do", "{")
46
-
47
- buffer << "{%= form_with model: "
48
- buffer << model_name << " #{params}"
49
- buffer << " %}"
50
- end
51
-
52
- Serbea::TemplateEngine.directive :frame, ->(code, buffer) do
53
- buffer << "{%= turbo_frame_tag "
54
- buffer << code
55
- buffer << " %}"
56
- end
57
-
58
- %i(append prepend update replace remove).each do |action|
59
- Serbea::TemplateEngine.directive action, ->(code, buffer) do
60
- buffer << "{%= turbo_stream.#{action} "
61
- buffer << code
62
- buffer << " %}"
63
- end
64
- end
65
-
66
- Serbea::TemplateEngine.directive :_, ->(code, buffer) do
67
- tag_name, space, params = code.lstrip.partition(%r(\s)m)
68
-
69
- if tag_name.end_with?(":")
70
- tag_name.chomp!(":")
71
- tag_name = ":#{tag_name}" unless tag_name.start_with?(":")
72
- end
73
-
74
- buffer << "{%= tag.tag_string "
75
- buffer << tag_name << ", " << params
76
- buffer << " %}"
77
- end
78
-
79
- ActionView::Template.register_template_handler(:serb, Serbea::Rails::TemplateHandler)