serbea 0.1.4 → 0.2.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: e9c63686513a6572b1d85616db1bb52f366e97e803ec7dc1ef0fa7e11f8af4d4
4
- data.tar.gz: d8b65adfddf004589a625755df7a293bc0b2fe88aaf89fadd00acb18d0aaf152
3
+ metadata.gz: 73c7ff8202d675a22d379f36b82f5848465b03c94a47b077467a12c2c321f5c6
4
+ data.tar.gz: 65ea57087ca64e0c1f4326b53db82326210fb2269b384da0aa583f0982851acb
5
5
  SHA512:
6
- metadata.gz: 0ea5ba66fc3bfa1fcec0a33fb49dccb9241f0b4f4b4640c87c5d35b3b88f5bf9c47c8c3f8d91e5445536a3490ce9261c97a8445e94525f2117fcce8ad6dbc9a2
7
- data.tar.gz: 0dff40a09f11f71b839d2a7445ab1e9032be49c136074c9efd3a6c8ddbfda00a00a9653609e19c14c4bc8e3c5bb743f259cee55e207db74fb20da3dd889945e9
6
+ metadata.gz: 3e129f0707c7b02325e7fc9e3b73e9a71c4e914a1b4bed7a2cc42aa8d3ccd88d881f3154d17eacb49ef449a0fc304ffef627d9b5bd3a43e40de2ee869c9851a0
7
+ data.tar.gz: 6d841de735b6f21c9f8e620e49504af943a1af1322cd067944b03a502b9f1334b5dec0ea67738cd6714e012c8484bcd7030e9360e30a05accb22d43b5308eb76
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in serbea.gemspec
6
+ gemspec
@@ -1,193 +1,16 @@
1
1
  require "tilt"
2
2
  require "tilt/erubi"
3
- require 'erubi/capture_end'
4
- require "ostruct"
3
+ require "erubi/capture_end"
5
4
 
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 self.output_processor=(processor)
33
- @output_processor = processor
34
- end
35
- def self.output_processor
36
- @output_processor ||= lambda do |input|
37
- # no-op
38
- input
39
- end
40
- end
41
-
42
- def initialize(context, value)
43
- @context = context
44
- @value = value
45
- end
46
-
47
- def filter(sym, *aargs)
48
- if @value.respond_to?(sym)
49
- @value = @value.send(sym, *aargs)
50
- elsif @context.respond_to?(sym)
51
- @value = @context.send(sym, @value, *aargs)
52
- else
53
- "Serbea warning: Filter not found: #{sym}".tap do |warning|
54
- raise_on_missing_filters ? raise(warning) : puts(warning)
55
- end
56
- end
57
-
58
- self
59
- end
60
-
61
- def to_s
62
- self.class.output_processor.call @value.to_s
63
- end
64
-
65
- def raise_on_missing_filters; false; end
66
- end
67
-
68
- class ComponentRenderer
69
- include SerbeaHelpers
70
-
71
- def initialize(variables = {})
72
- @variables = variables
73
- end
74
-
75
- def respond_to_missing?(key)
76
- @variables.key?(key)
77
- end
78
-
79
- def method_missing(key)
80
- @variables[key] if respond_to_missing?(key)
81
- end
82
- end
83
- end
84
-
85
- class SerbeaEngine < Erubi::CaptureEndEngine
86
- FRONT_MATTER_REGEXP = %r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m.freeze
87
-
88
- def self.has_yaml_header?(template)
89
- template.lines.first&.match? %r!\A---\s*\r?\n!
90
- end
91
-
92
- def initialize(input, properties={})
93
- properties[:regexp] = /{%(\:?={1,2}|-|\#|%|\:)?(.*?)([-=])?%}([ \t]*\r?\n)?/m
94
- properties[:strip_front_matter] = true unless properties.key?(:strip_front_matter)
95
- super process_serbea_input(input, properties), properties
96
- end
97
-
98
- def add_postamble(postamble)
99
- src << postamble
100
- src << "@_erbout.html_safe" if postamble.respond_to?(:html_safe)
101
-
102
- src.gsub!("__RAW_START_PRINT__", "{{")
103
- src.gsub!("__RAW_END_PRINT__", "}}")
104
- src.gsub!("__RAW_START_EVAL__", "{%")
105
- src.gsub!("__RAW_END_EVAL__", "%}")
106
- end
107
-
108
- def process_serbea_input(template, properties)
109
- buff = ""
110
-
111
- string = template.dup
112
- if properties[:strip_front_matter] && self.class.has_yaml_header?(string)
113
- if string = string.match(FRONT_MATTER_REGEXP)
114
- string = string.post_match
115
- # yaml_data = SafeYAML.load(template.captures[0])
116
- end
117
- end
118
-
119
- until string.empty?
120
- text, code, string = string.partition(/{% raw %}.*?{% endraw %}/m)
121
-
122
- buff << text
123
- if code.length > 0
124
- buff << code.
125
- sub("{% raw %}", "").
126
- sub("{% endraw %}", "").
127
- gsub("{{", "__RAW_START_PRINT__").
128
- gsub("}}", "__RAW_END_PRINT__").
129
- gsub("{%", "__RAW_START_EVAL__").
130
- gsub("%}", "__RAW_END_EVAL__")
131
- end
132
- end
133
-
134
- string = buff
135
- buff = ""
136
- until string.empty?
137
- text, code, string = string.partition(/{{.*?}}/m)
138
-
139
- buff << text
140
- if code.length > 0
141
- processed_filters = false
142
-
143
- code = code.gsub('\|', "__PIPE_C__")
144
-
145
- subs = code.gsub(/\s*\|\s+(.*?)\s([^|}]*)/) do
146
- args = $2
147
- args = nil if args.strip == ""
148
- prefix = processed_filters ? ")" : "))"
149
- processed_filters = true
150
- "#{prefix}.filter(:#{$1.chomp(":")}" + (args ? ", #{args}" : "")
151
- end
152
-
153
- pipeline_suffix = processed_filters ? ") %}" : ")) %}"
154
-
155
- buff << subs.sub("{{", "{%= pipeline(self, (").sub("}}", pipeline_suffix).gsub("__PIPE_C__", '\|')
156
- end
157
- end
158
-
159
- # puts buff
160
- buff
161
- end # method
162
-
163
- private
164
-
165
- # Handle the <%:= and <%:== tags
166
- # Carried over from the Erubi class but with changed indicators
167
- def handle(indicator, code, tailch, rspace, lspace)
168
- case indicator
169
- when ':=', ':=='
170
- rspace = nil if tailch && !tailch.empty?
171
- add_text(lspace) if lspace
172
- escape_capture = !((indicator == ':=') ^ @escape_capture)
173
- src << "begin; (#{@bufstack} ||= []) << #{@bufvar}; #{@bufvar} = #{@bufval}; #{@bufstack}.last << #{@escapefunc if escape_capture}((" << code
174
- add_text(rspace) if rspace
175
- when ':'
176
- rspace = nil if tailch && !tailch.empty?
177
- add_text(lspace) if lspace
178
- result = @yield_returns_buffer ? " #{@bufvar}; " : ""
179
- src << result << code << ")).to_s; ensure; #{@bufvar} = #{@bufstack}.pop; end;"
180
- add_text(rspace) if rspace
181
- else
182
- super
183
- end
184
- end
185
- end # class
5
+ require "serbea/helpers"
6
+ require "serbea/component_renderer"
7
+ require "serbea/pipeline"
8
+ require "serbea/template_engine"
186
9
 
187
10
  module Tilt
188
11
  class SerbeaTemplate < ErubiTemplate
189
12
  def prepare
190
- @options.merge!(outvar: "@_erbout", engine_class: SerbeaEngine)
13
+ @options.merge!(outvar: "@_erbout", engine_class: Serbea::TemplateEngine)
191
14
  super
192
15
  end
193
16
 
@@ -197,7 +20,7 @@ module Tilt
197
20
  end
198
21
  end
199
22
 
200
- Tilt.register Tilt::SerbeaTemplate, "serb" #, "serbea"
23
+ Tilt.register Tilt::SerbeaTemplate, "serb"
201
24
 
202
25
  if defined?(Rails::Railtie)
203
26
  class Railtie < ::Rails::Railtie
@@ -208,3 +31,7 @@ if defined?(Rails::Railtie)
208
31
  end
209
32
  end
210
33
  end
34
+
35
+ if defined?(Bridgetown)
36
+ require "serbea/bridgetown_support"
37
+ end
@@ -0,0 +1,56 @@
1
+ module Bridgetown
2
+ class SerbeaView < RubyTemplateView
3
+ include Serbea::Helpers
4
+
5
+ def partial(partial_name, options = {})
6
+ options.merge!(options[:locals]) if options[:locals]
7
+
8
+ partial_segments = partial_name.split("/")
9
+ partial_segments.last.sub!(%r!^!, "_")
10
+ partial_name = partial_segments.join("/")
11
+
12
+ Tilt::SerbeaTemplate.new(
13
+ site.in_source_dir(site.config[:partials_dir], "#{partial_name}.serb")
14
+ ).render(self, options)
15
+ end
16
+
17
+ def markdownify
18
+ previous_buffer_state = @_erbout
19
+ @_erbout = +""
20
+ result = yield
21
+ @_erbout = previous_buffer_state
22
+
23
+ content = Bridgetown::Utils.reindent_for_markdown(result)
24
+ converter = site.find_converter_instance(Bridgetown::Converters::Markdown)
25
+ md_output = converter.convert(content).strip
26
+ @_erbout << md_output
27
+ end
28
+ end
29
+
30
+ module Converters
31
+ class SerbeaTemplates < Converter
32
+ input :serb
33
+
34
+ # Logic to do the Serbea content conversion.
35
+ #
36
+ # @param content [String] Content of the file (without front matter).
37
+ # @params convertible [Bridgetown::Page, Bridgetown::Document, Bridgetown::Layout]
38
+ # The instantiated object which is processing the file.
39
+ #
40
+ # @return [String] The converted content.
41
+ def convert(content, convertible)
42
+ serb_view = Bridgetown::SerbeaView.new(convertible)
43
+
44
+ serb_renderer = Tilt::SerbeaTemplate.new(convertible.relative_path) { content }
45
+
46
+ if convertible.is_a?(Bridgetown::Layout)
47
+ serb_renderer.render(serb_view) do
48
+ convertible.current_document_output
49
+ end
50
+ else
51
+ serb_renderer.render(serb_view)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,19 @@
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
@@ -0,0 +1,25 @@
1
+ module Serbea
2
+ module Helpers
3
+ def capture(obj=nil)
4
+ previous_buffer_state = @_erbout
5
+ @_erbout = +""
6
+ result = obj ? yield(obj) : yield
7
+ @_erbout = previous_buffer_state
8
+
9
+ result.respond_to?(:html_safe) ? result.html_safe : result
10
+ end
11
+
12
+ def pipeline(context, value)
13
+ Pipeline.new(context, value)
14
+ end
15
+
16
+ def helper(name, &block)
17
+ self.class.send(:define_method, name, &block)
18
+ end
19
+
20
+ def h(input)
21
+ Erubi.h(input)
22
+ end
23
+ alias_method :escape, :h
24
+ end
25
+ end
@@ -0,0 +1,38 @@
1
+ module Serbea
2
+ class Pipeline
3
+ def self.output_processor=(processor)
4
+ @output_processor = processor
5
+ end
6
+ def self.output_processor
7
+ @output_processor ||= lambda do |input|
8
+ # no-op
9
+ input
10
+ end
11
+ end
12
+
13
+ def initialize(context, value)
14
+ @context = context
15
+ @value = value
16
+ end
17
+
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)
23
+ else
24
+ "Serbea warning: Filter not found: #{sym}".tap do |warning|
25
+ raise_on_missing_filters ? raise(warning) : puts(warning)
26
+ end
27
+ end
28
+
29
+ self
30
+ end
31
+
32
+ def to_s
33
+ self.class.output_processor.call @value.to_s
34
+ end
35
+
36
+ def raise_on_missing_filters; false; end
37
+ end
38
+ end
@@ -8,7 +8,7 @@ module Serbea
8
8
  def handles_encoding?; true; end
9
9
 
10
10
  def compile(template, source)
11
- "self.class.include(SerbeaHelpers)\n" + Tilt::SerbeaTemplate.new { source }.precompiled_template([])
11
+ "self.class.include(Serbea::Helpers);" + Tilt::SerbeaTemplate.new { source }.precompiled_template([])
12
12
  end
13
13
 
14
14
  def self.call(template, source = nil)
@@ -0,0 +1,102 @@
1
+ module Serbea
2
+ class TemplateEngine < Erubi::CaptureEndEngine
3
+ FRONT_MATTER_REGEXP = %r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m.freeze
4
+
5
+ def self.has_yaml_header?(template)
6
+ template.lines.first&.match? %r!\A---\s*\r?\n!
7
+ end
8
+
9
+ def initialize(input, properties={})
10
+ properties[:regexp] = /{%(\:?={1,2}|-|\#|%|\:)?(.*?)([-=])?%}([ \t]*\r?\n)?/m
11
+ properties[:strip_front_matter] = true unless properties.key?(:strip_front_matter)
12
+ super process_serbea_input(input, properties), properties
13
+ end
14
+
15
+ def add_postamble(postamble)
16
+ src << postamble
17
+ src << "@_erbout.html_safe" if postamble.respond_to?(:html_safe)
18
+
19
+ src.gsub!("__RAW_START_PRINT__", "{{")
20
+ src.gsub!("__RAW_END_PRINT__", "}}")
21
+ src.gsub!("__RAW_START_EVAL__", "{%")
22
+ src.gsub!("__RAW_END_EVAL__", "%}")
23
+ end
24
+
25
+ def process_serbea_input(template, properties)
26
+ buff = ""
27
+
28
+ string = template.dup
29
+ if properties[:strip_front_matter] && self.class.has_yaml_header?(string)
30
+ if string = string.match(FRONT_MATTER_REGEXP)
31
+ string = string.post_match
32
+ # yaml_data = SafeYAML.load(template.captures[0])
33
+ end
34
+ end
35
+
36
+ until string.empty?
37
+ text, code, string = string.partition(/{% raw %}.*?{% endraw %}/m)
38
+
39
+ buff << text
40
+ if code.length > 0
41
+ buff << code.
42
+ sub("{% raw %}", "").
43
+ sub("{% endraw %}", "").
44
+ gsub("{{", "__RAW_START_PRINT__").
45
+ gsub("}}", "__RAW_END_PRINT__").
46
+ gsub("{%", "__RAW_START_EVAL__").
47
+ gsub("%}", "__RAW_END_EVAL__")
48
+ end
49
+ end
50
+
51
+ string = buff
52
+ buff = ""
53
+ until string.empty?
54
+ text, code, string = string.partition(/{{.*?}}/m)
55
+
56
+ buff << text
57
+ if code.length > 0
58
+ processed_filters = false
59
+
60
+ code = code.gsub('\|', "__PIPE_C__")
61
+
62
+ subs = code.gsub(/\s*\|\s+(.*?)\s([^|}]*)/) do
63
+ args = $2
64
+ args = nil if args.strip == ""
65
+ prefix = processed_filters ? ")" : "))"
66
+ processed_filters = true
67
+ "#{prefix}.filter(:#{$1.chomp(":")}" + (args ? ", #{args}" : "")
68
+ end
69
+
70
+ pipeline_suffix = processed_filters ? ") %}" : ")) %}"
71
+
72
+ buff << subs.sub("{{", "{%= pipeline(self, (").sub("}}", pipeline_suffix).gsub("__PIPE_C__", '\|')
73
+ end
74
+ end
75
+
76
+ buff
77
+ end
78
+
79
+ private
80
+
81
+ # Handle the {%:= and {%:== tags
82
+ # Carried over from the Erubi class but with changed indicators
83
+ def handle(indicator, code, tailch, rspace, lspace)
84
+ case indicator
85
+ when ':=', ':=='
86
+ rspace = nil if tailch && !tailch.empty?
87
+ add_text(lspace) if lspace
88
+ escape_capture = !((indicator == ':=') ^ @escape_capture)
89
+ src << "begin; (#{@bufstack} ||= []) << #{@bufvar}; #{@bufvar} = #{@bufval}; #{@bufstack}.last << #{@escapefunc if escape_capture}((" << code
90
+ add_text(rspace) if rspace
91
+ when ':'
92
+ rspace = nil if tailch && !tailch.empty?
93
+ add_text(lspace) if lspace
94
+ result = @yield_returns_buffer ? " #{@bufvar}; " : ""
95
+ src << result << code << ")).to_s; ensure; #{@bufvar} = #{@bufstack}.pop; end;"
96
+ add_text(rspace) if rspace
97
+ else
98
+ super
99
+ end
100
+ end
101
+ end # class
102
+ end
@@ -1,3 +1,3 @@
1
1
  module Serbea
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.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.1.4
4
+ version: 0.2.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-17 00:00:00.000000000 Z
11
+ date: 2020-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: erubi
@@ -45,10 +45,16 @@ extensions: []
45
45
  extra_rdoc_files: []
46
46
  files:
47
47
  - ".gitignore"
48
+ - Gemfile
48
49
  - README.md
49
50
  - Rakefile
50
51
  - lib/serbea.rb
52
+ - lib/serbea/bridgetown_support.rb
53
+ - lib/serbea/component_renderer.rb
54
+ - lib/serbea/helpers.rb
55
+ - lib/serbea/pipeline.rb
51
56
  - lib/serbea/rails_support.rb
57
+ - lib/serbea/template_engine.rb
52
58
  - lib/version.rb
53
59
  - serbea.gemspec
54
60
  homepage: https://github.com/bridgetownrb/serbea