serbea 0.3.3 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/serbea/bridgetown_support.rb +21 -8
- data/lib/serbea/helpers.rb +4 -7
- data/lib/serbea/pipeline.rb +52 -6
- data/lib/serbea/rouge_lexer.rb +53 -0
- data/lib/serbea/template_engine.rb +27 -3
- data/lib/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 22177afa63ae7dccc14171dbfaac1d77b237b75b97f4779eeefc4c7fdf44dd5b
|
|
4
|
+
data.tar.gz: fa0591db51fd9d2f78541d48a717369d7d9b5b5e22be38f5c126972fbba8aa57
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1c5c3e615db4ea48225efc20a0f345a05aadd14f2ba9d38fe7406298fcfa371a77d3e8517d08d05dfe34f42cf7025818309b210ab6e7b0d14daeca0ccabe438c
|
|
7
|
+
data.tar.gz: 699e31b8fc7fd9bd32437170c5cf5c48b625f905c1a2b7312b4476ec8b3a89c76cbe1d7056bffdf1000099cbd67abd2862717a58177ee9725eb65090bc983ce1
|
|
@@ -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
|
-
|
|
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
|
data/lib/serbea/helpers.rb
CHANGED
|
@@ -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
|
|
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
|
+
h.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
|
|
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
|
data/lib/serbea/pipeline.rb
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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,8 +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
|
-
|
|
39
|
-
|
|
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
|
|
40
52
|
end
|
|
41
53
|
end
|
|
42
54
|
|
|
@@ -64,6 +76,7 @@ module Serbea
|
|
|
64
76
|
|
|
65
77
|
buff << text
|
|
66
78
|
if code.length > 0
|
|
79
|
+
original_line_length = code.lines.size
|
|
67
80
|
processed_filters = false
|
|
68
81
|
|
|
69
82
|
code = code.gsub('\|', "__PIPE_C__")
|
|
@@ -78,7 +91,13 @@ module Serbea
|
|
|
78
91
|
|
|
79
92
|
pipeline_suffix = processed_filters ? ") %}" : ")) %}"
|
|
80
93
|
|
|
81
|
-
|
|
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
|
|
82
101
|
end
|
|
83
102
|
end
|
|
84
103
|
|
|
@@ -101,6 +120,8 @@ module Serbea
|
|
|
101
120
|
code.sub! /^\{%@/, ""
|
|
102
121
|
code.sub! /%}$/, ""
|
|
103
122
|
unless ["end", ""].include? code.strip
|
|
123
|
+
original_line_length = code.lines.size
|
|
124
|
+
|
|
104
125
|
pieces = code.split(" ")
|
|
105
126
|
if pieces[0].start_with?(/[A-Z]/) # Ruby class name
|
|
106
127
|
pieces[0].prepend " "
|
|
@@ -124,6 +145,9 @@ module Serbea
|
|
|
124
145
|
pieces.last << ")"
|
|
125
146
|
buff << "{%= #{self.class.render_directive}#{pieces.join(" ")} %}"
|
|
126
147
|
end
|
|
148
|
+
(original_line_length - 1).times do
|
|
149
|
+
buff << "\n{% %}" # preserve original directive line length
|
|
150
|
+
end
|
|
127
151
|
else
|
|
128
152
|
buff << "{%: end %}"
|
|
129
153
|
end
|
data/lib/version.rb
CHANGED
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.
|
|
4
|
+
version: 0.4.2
|
|
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-
|
|
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
|