serbea 0.3.5 → 0.4.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/serbea/bridgetown_support.rb +23 -8
- data/lib/serbea/helpers.rb +3 -6
- data/lib/serbea/pipeline.rb +52 -6
- data/lib/serbea/rouge_lexer.rb +53 -0
- data/lib/serbea/template_engine.rb +27 -2
- 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: edb5cf139c2f14b2f524192f87b45fcfcc931efa0eb27b25e19d6f86ec98f3b2
|
4
|
+
data.tar.gz: 40b5e1177175daad6b356371d58a608e838d214a90b08415f63f4930dd28f4e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 074cb4b928fe2e79ab5786169f88be23dc01e17f5545d43b6bacdbe619491dc9a656a16fb019ea86e3f7c3feee1ce45f7f3f916063aaed2b61c6ecf21af00066
|
7
|
+
data.tar.gz: 14f6024cec0af16f22de2549f6c0bf25ede4a75b458ab64b9970dc71088923643cdc4be0414b952dd2c12cc814d2b34aa17b6cc9cda97cfe71d72b8b15efa97a
|
@@ -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,22 @@ module Bridgetown
|
|
51
50
|
serb_renderer.render(serb_view)
|
52
51
|
end
|
53
52
|
end
|
53
|
+
|
54
|
+
def matches(ext, convertible = nil)
|
55
|
+
if convertible
|
56
|
+
if convertible.data[:template_engine] == "serbea" ||
|
57
|
+
(convertible.data[:template_engine].nil? &&
|
58
|
+
@config[:template_engine] == "serbea")
|
59
|
+
return true
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
super(ext)
|
64
|
+
end
|
65
|
+
|
66
|
+
def output_ext(ext)
|
67
|
+
ext == ".serb" ? ".html" : ext
|
68
|
+
end
|
54
69
|
end
|
55
70
|
end
|
56
71
|
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
|
+
result.respond_to?(:html_safe) ? result.html_safe : result
|
26
27
|
end
|
27
28
|
alias_method :escape, :h
|
28
29
|
|
@@ -34,10 +35,6 @@ 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
38
|
def assign_to(input, varname, preserve: false)
|
42
39
|
self.instance_variable_set("@#{varname}", input)
|
43
40
|
preserve ? input : nil
|
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,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
|
-
|
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
|
-
|
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
|
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.4
|
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-19 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
|