serbea 0.3.4 → 0.4.3

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: bbd946514bace35dc47b5a4cf880db55b6b5af2e6060c40dee607bb776072c30
4
- data.tar.gz: 3cac2624f2e6c5f4dd70393f5c6bbef467115da9bc9e465d6a8e91e69e3155b4
3
+ metadata.gz: fd530996d44bf1ab692ebef54630f131669dc773b084a0181713c7d929a1eb3b
4
+ data.tar.gz: a65ff4d61f7c400177f83ac2f83986fcb26b30eaf85d386ec9d4497aa4ec9a48
5
5
  SHA512:
6
- metadata.gz: 122fa4b87a75dacfcdc9026aaac10560695ebbf7cc5b6e0e35999bc7694e880e8df6a5ead634b37685cafa8ad6849380029f5beece1ce17665cef0fad5eb9b38
7
- data.tar.gz: bea81f00f032ed410f976041399f1fae6209fb9f4808e747c69ebf6a4d48faf64b9f2c79cb5a470fc0f5a997574916b25143da64de7ed23d9383b2ae71119a6c
6
+ metadata.gz: 780f93e3d44d80e8ddfc0cd9cff2482027ed003a26d35e78f7b78748996162e1a5df1c7097b69f4526528efa1c3c04950c11cf963cf1c88b7dca72cfc9f4daf9
7
+ data.tar.gz: b18cec53f65e1ae0d91333885657a9b2adec3ebcd61920a8a3c12363d7b95d6168898ed899bebbf48cf7f58990138e8d8c0843c7a4d385b9c41f6ad5861d5a1e
@@ -1,9 +1,12 @@
1
+ require "serbea/rouge_lexer"
2
+
1
3
  module Bridgetown
2
4
  class SerbeaView < RubyTemplateView
3
5
  include Serbea::Helpers
4
6
 
5
7
  def partial(partial_name, options = {})
6
8
  options.merge!(options[:locals]) if options[:locals]
9
+ options[:content] = yield if block_given?
7
10
 
8
11
  partial_segments = partial_name.split("/")
9
12
  partial_segments.last.sub!(%r!^!, "_")
@@ -13,14 +16,9 @@ module Bridgetown
13
16
  site.in_source_dir(site.config[:partials_dir], "#{partial_name}.serb")
14
17
  ).render(self, options)
15
18
  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)
19
+
20
+ def markdownify(&block)
21
+ content = Bridgetown::Utils.reindent_for_markdown(capture(&block))
24
22
  converter = site.find_converter_instance(Bridgetown::Converters::Markdown)
25
23
  md_output = converter.convert(content).strip
26
24
  @_erbout << md_output
@@ -29,6 +27,7 @@ module Bridgetown
29
27
 
30
28
  module Converters
31
29
  class SerbeaTemplates < Converter
30
+ priority :highest
32
31
  input :serb
33
32
 
34
33
  # Logic to do the Serbea content conversion.
@@ -51,6 +50,20 @@ module Bridgetown
51
50
  serb_renderer.render(serb_view)
52
51
  end
53
52
  end
53
+
54
+ def matches(ext, convertible)
55
+ if convertible.data[:template_engine] == "serbea" ||
56
+ (convertible.data[:template_engine].nil? &&
57
+ @config[:template_engine] == "serbea")
58
+ return true
59
+ end
60
+
61
+ super(ext)
62
+ end
63
+
64
+ def output_ext(ext)
65
+ ext == ".serb" ? ".html" : ext
66
+ end
54
67
  end
55
68
  end
56
69
  end
@@ -1,7 +1,7 @@
1
1
  module Serbea
2
2
  module Helpers
3
3
  def self.included(mod)
4
- Serbea::Pipeline.deny_value_method %i(escape h prepend append map assign)
4
+ Serbea::Pipeline.deny_value_method %i(escape h prepend append assign_to)
5
5
  end
6
6
 
7
7
  def capture(obj=nil)
@@ -22,7 +22,8 @@ module Serbea
22
22
  end
23
23
 
24
24
  def h(input)
25
- Erubi.h(input)
25
+ result = Erubi.h(input)
26
+ result.respond_to?(:html_safe) ? result.html_safe : result
26
27
  end
27
28
  alias_method :escape, :h
28
29
 
@@ -34,11 +35,7 @@ module Serbea
34
35
  "#{old_string}#{new_string}"
35
36
  end
36
37
 
37
- def map(input, block)
38
- input.map(&block)
39
- end
40
-
41
- def assign(input, varname, preserve: false)
38
+ def assign_to(input, varname, preserve: false)
42
39
  self.instance_variable_set("@#{varname}", input)
43
40
  preserve ? input : nil
44
41
  end
@@ -1,5 +1,27 @@
1
1
  module Serbea
2
2
  class Pipeline
3
+ def self.exec(template, input: (no_input_passed = true; nil), include_helpers: nil)
4
+ anon = Class.new do
5
+ include Serbea::Helpers
6
+
7
+ attr_accessor :input, :output
8
+ end
9
+
10
+ if include_helpers
11
+ anon.include include_helpers
12
+ end
13
+
14
+ pipeline_obj = anon.new
15
+ pipeline_obj.input = input unless no_input_passed
16
+
17
+ full_template = "{{ #{template} | assign_to: :output }}"
18
+
19
+ tmpl = Tilt::SerbeaTemplate.new { full_template }
20
+ tmpl.render(pipeline_obj)
21
+
22
+ pipeline_obj.output
23
+ end
24
+
3
25
  def self.output_processor=(processor)
4
26
  @output_processor = processor
5
27
  end
@@ -10,6 +32,13 @@ module Serbea
10
32
  end
11
33
  end
12
34
 
35
+ def self.raise_on_missing_filters=(config_boolean)
36
+ @raise_on_missing_filters = config_boolean
37
+ end
38
+ def self.raise_on_missing_filters
39
+ @raise_on_missing_filters ||= false
40
+ end
41
+
13
42
  def self.deny_value_method(name)
14
43
  value_methods_denylist.merge Array(name)
15
44
  end
@@ -22,14 +51,33 @@ module Serbea
22
51
  @value = value
23
52
  end
24
53
 
25
- def filter(name, *args)
54
+ # TODO: clean this up somehow and still support Ruby 2.5..3.0!
55
+ def filter(name, *args, **kwargs)
26
56
  if @value.respond_to?(name) && !self.class.value_methods_denylist.include?(name)
27
- @value = @value.send(name, *args)
57
+ if args.last.is_a?(Proc)
58
+ real_args = args.take(args.length - 1)
59
+ block = args.last
60
+ unless kwargs.empty?
61
+ @value = @value.send(name, *real_args, **kwargs, &block)
62
+ else
63
+ @value = @value.send(name, *real_args, &block)
64
+ end
65
+ else
66
+ unless kwargs.empty?
67
+ @value = @value.send(name, *args, **kwargs)
68
+ else
69
+ @value = @value.send(name, *args)
70
+ end
71
+ end
28
72
  elsif @context.respond_to?(name)
29
- @value = @context.send(name, @value, *args)
73
+ unless kwargs.empty?
74
+ @value = @context.send(name, @value, *args, **kwargs)
75
+ else
76
+ @value = @context.send(name, @value, *args)
77
+ end
30
78
  else
31
79
  "Serbea warning: Filter not found: #{name}".tap do |warning|
32
- raise_on_missing_filters ? raise(warning) : STDERR.puts(warning)
80
+ self.class.raise_on_missing_filters ? raise(warning) : STDERR.puts(warning)
33
81
  end
34
82
  end
35
83
 
@@ -39,7 +87,5 @@ module Serbea
39
87
  def to_s
40
88
  self.class.output_processor.call @value.to_s
41
89
  end
42
-
43
- def raise_on_missing_filters; false; end
44
90
  end
45
91
  end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rouge"
4
+
5
+ module Rouge
6
+ module Lexers
7
+ class Serbea < TemplateLexer
8
+ title "Serbea"
9
+ desc "Embedded Ruby Serbea template files"
10
+
11
+ tag 'serb'
12
+
13
+ filenames '*.serb'
14
+
15
+ def initialize(opts={})
16
+ @ruby_lexer = Ruby.new(opts)
17
+
18
+ super(opts)
19
+ end
20
+
21
+ start do
22
+ parent.reset!
23
+ @ruby_lexer.reset!
24
+ end
25
+
26
+ open = /{%%|{%=|{%#|{%-|{%|{{/
27
+ close = /%%}|-%}|%}|}}/
28
+
29
+ state :root do
30
+ rule %r/{%#/, Comment, :comment
31
+
32
+ rule open, Comment::Preproc, :ruby
33
+
34
+ rule %r/.+?(?=#{open})|.+/m do
35
+ delegate parent
36
+ end
37
+ end
38
+
39
+ state :comment do
40
+ rule close, Comment, :pop!
41
+ rule %r/.+?(?=#{close})|.+/m, Comment
42
+ end
43
+
44
+ state :ruby do
45
+ rule close, Comment::Preproc, :pop!
46
+
47
+ rule %r/.+?(?=#{close})|.+/m do
48
+ delegate @ruby_lexer
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -8,6 +8,13 @@ module Serbea
8
8
  def self.render_directive
9
9
  @render_directive ||= "render"
10
10
  end
11
+
12
+ def self.front_matter_preamble=(varname)
13
+ @front_matter_preamble = varname
14
+ end
15
+ def self.front_matter_preamble
16
+ @front_matter_preamble ||= "frontmatter = YAML.load"
17
+ end
11
18
 
12
19
  def self.has_yaml_header?(template)
13
20
  template.lines.first&.match? %r!\A---\s*\r?\n!
@@ -35,7 +42,13 @@ module Serbea
35
42
  string = template.dup
36
43
  if properties[:strip_front_matter] && self.class.has_yaml_header?(string)
37
44
  if string = string.match(FRONT_MATTER_REGEXP)
38
- string = ("{%#%}\n" * string.to_s.lines.count) + string.post_match
45
+ require "yaml" if self.class.front_matter_preamble.include?(" = YAML.load")
46
+
47
+ string = "{% #{self.class.front_matter_preamble} <<~YAMLDATA\n" +
48
+ string[1].sub(/^---\n/,'') +
49
+ "YAMLDATA\n%}" +
50
+ string[2].sub(/^---\n/, '') +
51
+ string.post_match
39
52
  end
40
53
  end
41
54
 
@@ -63,6 +76,7 @@ module Serbea
63
76
 
64
77
  buff << text
65
78
  if code.length > 0
79
+ original_line_length = code.lines.size
66
80
  processed_filters = false
67
81
 
68
82
  code = code.gsub('\|', "__PIPE_C__")
@@ -77,7 +91,13 @@ module Serbea
77
91
 
78
92
  pipeline_suffix = processed_filters ? ") %}" : ")) %}"
79
93
 
80
- buff << subs.sub("{{", "{%= pipeline(self, (").sub("}}", pipeline_suffix).gsub("__PIPE_C__", '\|')
94
+ subs = subs.sub("{{", "{%= pipeline(self, (").sub("}}", pipeline_suffix).gsub("__PIPE_C__", '|')
95
+
96
+ buff << subs
97
+
98
+ (original_line_length - subs.lines.size).times do
99
+ buff << "\n{% %}" # preserve original line length
100
+ end
81
101
  end
82
102
  end
83
103
 
@@ -100,6 +120,8 @@ module Serbea
100
120
  code.sub! /^\{%@/, ""
101
121
  code.sub! /%}$/, ""
102
122
  unless ["end", ""].include? code.strip
123
+ original_line_length = code.lines.size
124
+
103
125
  pieces = code.split(" ")
104
126
  if pieces[0].start_with?(/[A-Z]/) # Ruby class name
105
127
  pieces[0].prepend " "
@@ -123,6 +145,9 @@ module Serbea
123
145
  pieces.last << ")"
124
146
  buff << "{%= #{self.class.render_directive}#{pieces.join(" ")} %}"
125
147
  end
148
+ (original_line_length - 1).times do
149
+ buff << "\n{% %}" # preserve original directive line length
150
+ end
126
151
  else
127
152
  buff << "{%: end %}"
128
153
  end
@@ -1,3 +1,3 @@
1
1
  module Serbea
2
- VERSION = "0.3.4"
2
+ VERSION = "0.4.3"
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.3.4
4
+ version: 0.4.3
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-29 00:00:00.000000000 Z
11
+ date: 2020-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: erubi
@@ -54,6 +54,7 @@ files:
54
54
  - lib/serbea/helpers.rb
55
55
  - lib/serbea/pipeline.rb
56
56
  - lib/serbea/rails_support.rb
57
+ - lib/serbea/rouge_lexer.rb
57
58
  - lib/serbea/template_engine.rb
58
59
  - lib/version.rb
59
60
  - serbea.gemspec