trmnl-liquid 0.1.0 → 0.2.0
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/trmnl/liquid/file_system.rb +21 -0
- data/lib/trmnl/liquid/template_tag.rb +33 -0
- data/lib/trmnl/liquid/version.rb +1 -1
- data/lib/trmnl/liquid.rb +13 -6
- data/trmnl-liquid.gemspec +1 -1
- metadata +5 -6
- data/lib/trmnl/liquid/template/file_system.rb +0 -23
- data/lib/trmnl/liquid/template/template_tag.rb +0 -35
- data/lib/trmnl/liquid/template.rb +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c484c8483e883a76d94ccf35c0984ad405c498b890a7185e46474d706d1a613e
|
4
|
+
data.tar.gz: 72dc97f7b558c0a6d20e02640f781befb3fe68f15039e4e4e990f5a2520765a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6182847e14836fa4b3fe9b76210faf7c96e983a0c20495cf794234e4c0a62a1c345a933d41a38638f10cdd44df6516fefa9ae40678dd1291f462f500b62330f7
|
7
|
+
data.tar.gz: 0a0da9095cc5be9c55bb284051a711ee2877b233bdd434dcf5dc6b4f320a883f8197f364123bcdb63e9671c45afd4c141fbbf4321cc6fcac3a5e6ffbdc9064f4
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module TRMNL
|
2
|
+
module Liquid
|
3
|
+
# This in-memory "file system" is the backing storage for custom templates defined {% template [name] %} tags.
|
4
|
+
class FileSystem < ::Liquid::BlankFileSystem
|
5
|
+
def initialize
|
6
|
+
super
|
7
|
+
@templates = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
# called by Markup::LiquidTemplateTag to save users' custom shared templates via our custom {% template %} tag
|
11
|
+
def register(name, body)
|
12
|
+
@templates[name] = body
|
13
|
+
end
|
14
|
+
|
15
|
+
# called by ::Liquid::Template for {% render 'foo' %} when rendering screen markup
|
16
|
+
def read_template_file(name)
|
17
|
+
@templates[name] || raise(::Liquid::FileSystemError, "Template not found: #{name}")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module TRMNL
|
2
|
+
module Liquid
|
3
|
+
# The {% template [name] %} tag block is used in conjunction with InlineTemplatesFileSystem to allow users to define
|
4
|
+
# custom templates within the context of the current Liquid template. Generally speaking, they will define their own
|
5
|
+
# templates in the "shared" markup content, which is prepended to the individual screen templates before rendering.
|
6
|
+
class TemplateTag < ::Liquid::Block
|
7
|
+
NAME_REGEX = %r{\A[a-zA-Z0-9_/]+\z}
|
8
|
+
|
9
|
+
def initialize(tag_name, markup, options)
|
10
|
+
super
|
11
|
+
@name = markup.strip
|
12
|
+
end
|
13
|
+
|
14
|
+
def parse(tokens)
|
15
|
+
@body = ""
|
16
|
+
while (token = tokens.shift)
|
17
|
+
break if token.strip == "{% endtemplate %}"
|
18
|
+
|
19
|
+
@body << token
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def render(context)
|
24
|
+
unless @name =~ NAME_REGEX
|
25
|
+
return "Liquid error: invalid template name #{@name.inspect} - template names must contain only letters, numbers, underscores, and slashes"
|
26
|
+
end
|
27
|
+
|
28
|
+
context.registers[:file_system].register(@name, @body.strip)
|
29
|
+
''
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/trmnl/liquid/version.rb
CHANGED
data/lib/trmnl/liquid.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'liquid'
|
4
|
+
|
3
5
|
require 'trmnl/liquid/filters'
|
4
|
-
require 'trmnl/liquid/
|
6
|
+
require 'trmnl/liquid/file_system'
|
7
|
+
require 'trmnl/liquid/template_tag'
|
5
8
|
require 'trmnl/liquid/version'
|
6
9
|
|
7
10
|
begin
|
@@ -20,9 +23,13 @@ end
|
|
20
23
|
|
21
24
|
module TRMNL
|
22
25
|
module Liquid
|
23
|
-
|
26
|
+
def self.build_environment(*args)
|
27
|
+
::Liquid::Environment.build(*args) do |env|
|
28
|
+
env.register_filter(TRMNL::Liquid::Filters)
|
29
|
+
env.register_tag('template', TRMNL::Liquid::TemplateTag)
|
30
|
+
env.file_system = TRMNL::Liquid::FileSystem.new
|
31
|
+
yield(env) if block_given?
|
32
|
+
end
|
33
|
+
end
|
24
34
|
end
|
25
|
-
end
|
26
|
-
|
27
|
-
::Liquid::Template.register_filter(TRMNL::Liquid::Filters)
|
28
|
-
::Liquid::Template.register_tag('template', TRMNL::Liquid::Template::TemplateTag)
|
35
|
+
end
|
data/trmnl-liquid.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trmnl-liquid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- TRMNL
|
@@ -29,14 +29,14 @@ dependencies:
|
|
29
29
|
requirements:
|
30
30
|
- - "~>"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '5.
|
32
|
+
version: '5.6'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
35
|
version_requirements: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
37
|
- - "~>"
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version: '5.
|
39
|
+
version: '5.6'
|
40
40
|
- !ruby/object:Gem::Dependency
|
41
41
|
name: redcarpet
|
42
42
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,10 +58,9 @@ extensions: []
|
|
58
58
|
extra_rdoc_files: []
|
59
59
|
files:
|
60
60
|
- lib/trmnl/liquid.rb
|
61
|
+
- lib/trmnl/liquid/file_system.rb
|
61
62
|
- lib/trmnl/liquid/filters.rb
|
62
|
-
- lib/trmnl/liquid/
|
63
|
-
- lib/trmnl/liquid/template/file_system.rb
|
64
|
-
- lib/trmnl/liquid/template/template_tag.rb
|
63
|
+
- lib/trmnl/liquid/template_tag.rb
|
65
64
|
- lib/trmnl/liquid/version.rb
|
66
65
|
- trmnl-liquid.gemspec
|
67
66
|
homepage: https://github.com/usetrmnl/trmnl-liquid
|
@@ -1,23 +0,0 @@
|
|
1
|
-
module TRMNL
|
2
|
-
module Liquid
|
3
|
-
class Template < ::Liquid::Template
|
4
|
-
# This in-memory "file system" is the backing storage for custom templates defined {% template [name] %} tags.
|
5
|
-
class FileSystem < ::Liquid::BlankFileSystem
|
6
|
-
def initialize
|
7
|
-
super
|
8
|
-
@templates = {}
|
9
|
-
end
|
10
|
-
|
11
|
-
# called by Markup::LiquidTemplateTag to save users' custom shared templates via our custom {% template %} tag
|
12
|
-
def register(name, body)
|
13
|
-
@templates[name] = body
|
14
|
-
end
|
15
|
-
|
16
|
-
# called by ::Liquid::Template for {% render 'foo' %} when rendering screen markup
|
17
|
-
def read_template_file(name)
|
18
|
-
@templates[name] || raise(::Liquid::FileSystemError, "Template not found: #{name}")
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
module TRMNL
|
2
|
-
module Liquid
|
3
|
-
class Template < ::Liquid::Template
|
4
|
-
# The {% template [name] %} tag block is used in conjunction with InlineTemplatesFileSystem to allow users to define
|
5
|
-
# custom templates within the context of the current Liquid template. Generally speaking, they will define their own
|
6
|
-
# templates in the "shared" markup content, which is prepended to the individual screen templates before rendering.
|
7
|
-
class TemplateTag < ::Liquid::Block
|
8
|
-
NAME_REGEX = %r{\A[a-zA-Z0-9_/]+\z}
|
9
|
-
|
10
|
-
def initialize(tag_name, markup, options)
|
11
|
-
super
|
12
|
-
@name = markup.strip
|
13
|
-
end
|
14
|
-
|
15
|
-
def parse(tokens)
|
16
|
-
@body = ""
|
17
|
-
while (token = tokens.shift)
|
18
|
-
break if token.strip == "{% endtemplate %}"
|
19
|
-
|
20
|
-
@body << token
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def render(context)
|
25
|
-
unless @name =~ NAME_REGEX
|
26
|
-
return "Liquid error: invalid template name #{@name.inspect} - template names must contain only letters, numbers, underscores, and slashes"
|
27
|
-
end
|
28
|
-
|
29
|
-
context.registers[:file_system].register(@name, @body.strip)
|
30
|
-
''
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'liquid'
|
2
|
-
|
3
|
-
require_relative 'template/file_system'
|
4
|
-
require_relative 'template/template_tag'
|
5
|
-
|
6
|
-
module TRMNL
|
7
|
-
module Liquid
|
8
|
-
# A very thin wrapper around Liquid::Template with TRMNL-specific functionality.
|
9
|
-
class Template < ::Liquid::Template
|
10
|
-
def self.parse(*)
|
11
|
-
template = super
|
12
|
-
|
13
|
-
# set up a temporary in-memory file system for custom user templates, via the magic :file_system register
|
14
|
-
# which will override the default file system
|
15
|
-
template.registers[:file_system] = FileSystem.new
|
16
|
-
|
17
|
-
template
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|