philiprehberger-template 0.1.1
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 +7 -0
- data/CHANGELOG.md +17 -0
- data/LICENSE +21 -0
- data/README.md +89 -0
- data/lib/philiprehberger/template/context.rb +33 -0
- data/lib/philiprehberger/template/parser.rb +56 -0
- data/lib/philiprehberger/template/renderer.rb +63 -0
- data/lib/philiprehberger/template/version.rb +7 -0
- data/lib/philiprehberger/template.rb +26 -0
- metadata +57 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 8c13a7306a91589954080ddec62dad14e40061d66d85dc7ac4e71059857654ae
|
|
4
|
+
data.tar.gz: f894a69123fbac0a48c5268d0973cc3e00c531113e237138534f8d637e10c462
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: f2d4244f64204a7a4c7fa9576b2c1c1ce0a355bf271351599f348fe91afe33bf47bc8d84572df365366b13d58f351f3c78553af69f8b2171a6a8af44931ef7fa
|
|
7
|
+
data.tar.gz: 77f2b443b5f3c262c7c193a186a69ad53ac58f8f8b8c39a759f075f8446ffe15b2a2ffd064ea141d33da274f368724e5bf32d0bb4b8f03e2d9ab00d9539fe956
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this gem will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.1.0] - 2026-03-15
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- Initial release
|
|
14
|
+
- Mustache-style logic-less templates
|
|
15
|
+
- Safe rendering without eval
|
|
16
|
+
- Section and inverted section support
|
|
17
|
+
- Partial template inclusion
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 philiprehberger
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# philiprehberger-template
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/rb/philiprehberger-template)
|
|
4
|
+
[](https://github.com/philiprehberger/rb-template/actions/workflows/ci.yml)
|
|
5
|
+
|
|
6
|
+
Logic-less Mustache-style template engine with safe rendering for Ruby.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
Add to your Gemfile:
|
|
11
|
+
|
|
12
|
+
```ruby
|
|
13
|
+
gem 'philiprehberger-template'
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Or install directly:
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
gem install philiprehberger-template
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```ruby
|
|
25
|
+
require 'philiprehberger/template'
|
|
26
|
+
|
|
27
|
+
# Simple variable interpolation
|
|
28
|
+
tpl = Philiprehberger::Template.new('Hello, {{name}}!')
|
|
29
|
+
tpl.render(name: 'World')
|
|
30
|
+
# => "Hello, World!"
|
|
31
|
+
|
|
32
|
+
# Load from file
|
|
33
|
+
tpl = Philiprehberger::Template.from_file('greeting.mustache')
|
|
34
|
+
tpl.render(name: 'World')
|
|
35
|
+
|
|
36
|
+
# Sections (truthy/falsy)
|
|
37
|
+
tpl = Philiprehberger::Template.new('{{#show}}visible{{/show}}')
|
|
38
|
+
tpl.render(show: true) # => "visible"
|
|
39
|
+
tpl.render(show: false) # => ""
|
|
40
|
+
|
|
41
|
+
# Array iteration
|
|
42
|
+
tpl = Philiprehberger::Template.new('{{#items}}* {{name}}\n{{/items}}')
|
|
43
|
+
tpl.render(items: [{ name: 'Alice' }, { name: 'Bob' }])
|
|
44
|
+
# => "* Alice\n* Bob\n"
|
|
45
|
+
|
|
46
|
+
# Inverted sections
|
|
47
|
+
tpl = Philiprehberger::Template.new('{{^items}}No items found.{{/items}}')
|
|
48
|
+
tpl.render(items: [])
|
|
49
|
+
# => "No items found."
|
|
50
|
+
|
|
51
|
+
# Nested scopes (child inherits parent variables)
|
|
52
|
+
tpl = Philiprehberger::Template.new('{{#user}}{{greeting}}, {{name}}{{/user}}')
|
|
53
|
+
tpl.render(greeting: 'Hi', user: { name: 'Alice' })
|
|
54
|
+
# => "Hi, Alice"
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Supported Syntax
|
|
58
|
+
|
|
59
|
+
| Tag | Description |
|
|
60
|
+
|-----|-------------|
|
|
61
|
+
| `{{var}}` | Variable interpolation (missing variables render as empty string) |
|
|
62
|
+
| `{{#section}}...{{/section}}` | Section block (renders if truthy; iterates if array) |
|
|
63
|
+
| `{{^section}}...{{/section}}` | Inverted section (renders if falsy or empty) |
|
|
64
|
+
|
|
65
|
+
## API
|
|
66
|
+
|
|
67
|
+
### `Philiprehberger::Template.new(source)`
|
|
68
|
+
|
|
69
|
+
Compiles a template string into a renderable template.
|
|
70
|
+
|
|
71
|
+
### `Philiprehberger::Template.from_file(path)`
|
|
72
|
+
|
|
73
|
+
Reads a file and compiles its contents as a template.
|
|
74
|
+
|
|
75
|
+
### `#render(variables = {})`
|
|
76
|
+
|
|
77
|
+
Renders the template with the given variable hash. Accepts both symbol and string keys. Missing variables produce an empty string.
|
|
78
|
+
|
|
79
|
+
## Development
|
|
80
|
+
|
|
81
|
+
```
|
|
82
|
+
bundle install
|
|
83
|
+
bundle exec rake spec
|
|
84
|
+
bundle exec rake rubocop
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## License
|
|
88
|
+
|
|
89
|
+
MIT License. See [LICENSE](LICENSE) for details.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Philiprehberger
|
|
4
|
+
class Template
|
|
5
|
+
class Context
|
|
6
|
+
def initialize(variables = {})
|
|
7
|
+
@stack = [normalize(variables)]
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def lookup(name)
|
|
11
|
+
key = name.to_sym
|
|
12
|
+
@stack.reverse_each do |scope|
|
|
13
|
+
return scope[key] if scope.key?(key)
|
|
14
|
+
end
|
|
15
|
+
nil
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def push(scope)
|
|
19
|
+
@stack.push(normalize(scope))
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def pop
|
|
23
|
+
@stack.pop if @stack.size > 1
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def normalize(hash)
|
|
29
|
+
hash.transform_keys(&:to_sym)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Philiprehberger
|
|
4
|
+
class Template
|
|
5
|
+
class Parser
|
|
6
|
+
TAG_PATTERN = %r{\{\{([#^/]?)(\s*\w+\s*)\}\}}
|
|
7
|
+
|
|
8
|
+
def initialize(source)
|
|
9
|
+
@source = source
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def parse
|
|
13
|
+
tokens = tokenize
|
|
14
|
+
build_tree(tokens, nil)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def tokenize
|
|
20
|
+
tokens = []
|
|
21
|
+
scanner = @source.dup
|
|
22
|
+
while (match = scanner.match(TAG_PATTERN))
|
|
23
|
+
tokens << [:text, match.pre_match] unless match.pre_match.empty?
|
|
24
|
+
type = tag_type(match[1])
|
|
25
|
+
tokens << [type, match[2].strip]
|
|
26
|
+
scanner = match.post_match
|
|
27
|
+
end
|
|
28
|
+
tokens << [:text, scanner] unless scanner.empty?
|
|
29
|
+
tokens
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def tag_type(prefix)
|
|
33
|
+
{ '#' => :section_open, '^' => :inverted_open, '/' => :section_close }.fetch(prefix, :variable)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def build_tree(tokens, closing_tag) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
|
|
37
|
+
nodes = []
|
|
38
|
+
while (token = tokens.shift)
|
|
39
|
+
case token[0]
|
|
40
|
+
when :text then nodes << { type: :text, value: token[1] }
|
|
41
|
+
when :variable then nodes << { type: :variable, name: token[1] }
|
|
42
|
+
when :section_open
|
|
43
|
+
nodes << { type: :section, name: token[1], children: build_tree(tokens, token[1]) }
|
|
44
|
+
when :inverted_open
|
|
45
|
+
nodes << { type: :inverted, name: token[1], children: build_tree(tokens, token[1]) }
|
|
46
|
+
when :section_close
|
|
47
|
+
raise "Mismatched tag: {{/#{token[1]}}}" if closing_tag && token[1] != closing_tag
|
|
48
|
+
|
|
49
|
+
return nodes
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
nodes
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Philiprehberger
|
|
4
|
+
class Template
|
|
5
|
+
class Renderer
|
|
6
|
+
def initialize(tree, context)
|
|
7
|
+
@tree = tree
|
|
8
|
+
@context = context
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def render
|
|
12
|
+
render_nodes(@tree)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def render_nodes(nodes)
|
|
18
|
+
nodes.map { |node| render_node(node) }.join
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def render_node(node)
|
|
22
|
+
case node[:type]
|
|
23
|
+
when :text then node[:value]
|
|
24
|
+
when :variable then @context.lookup(node[:name]).to_s
|
|
25
|
+
when :section then render_section(node)
|
|
26
|
+
when :inverted then render_inverted(node)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def render_section(node)
|
|
31
|
+
value = @context.lookup(node[:name])
|
|
32
|
+
return '' unless truthy?(value)
|
|
33
|
+
return render_array(node, value) if value.is_a?(Array)
|
|
34
|
+
|
|
35
|
+
@context.push(value.is_a?(Hash) ? value : {})
|
|
36
|
+
result = render_nodes(node[:children])
|
|
37
|
+
@context.pop
|
|
38
|
+
result
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def render_inverted(node)
|
|
42
|
+
value = @context.lookup(node[:name])
|
|
43
|
+
truthy?(value) ? '' : render_nodes(node[:children])
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def render_array(node, items)
|
|
47
|
+
items.map do |item|
|
|
48
|
+
@context.push(item.is_a?(Hash) ? item : {})
|
|
49
|
+
result = render_nodes(node[:children])
|
|
50
|
+
@context.pop
|
|
51
|
+
result
|
|
52
|
+
end.join
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def truthy?(value)
|
|
56
|
+
return false if value.nil? || value == false
|
|
57
|
+
return false if value.is_a?(Array) && value.empty?
|
|
58
|
+
|
|
59
|
+
true
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'template/version'
|
|
4
|
+
require_relative 'template/parser'
|
|
5
|
+
require_relative 'template/renderer'
|
|
6
|
+
require_relative 'template/context'
|
|
7
|
+
|
|
8
|
+
module Philiprehberger
|
|
9
|
+
class Template
|
|
10
|
+
attr_reader :source, :tree
|
|
11
|
+
|
|
12
|
+
def self.from_file(path)
|
|
13
|
+
new(File.read(path))
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def initialize(source)
|
|
17
|
+
@source = source
|
|
18
|
+
@tree = Parser.new(source).parse
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def render(variables = {})
|
|
22
|
+
ctx = Context.new(variables)
|
|
23
|
+
Renderer.new(@tree, ctx).render
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: philiprehberger-template
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Philip Rehberger
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-03-15 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: A lightweight Mustache-style template engine supporting variable interpolation,
|
|
14
|
+
sections, inverted sections, and nested scopes with safe rendering that never raises
|
|
15
|
+
on missing variables.
|
|
16
|
+
email:
|
|
17
|
+
- me@philiprehberger.com
|
|
18
|
+
executables: []
|
|
19
|
+
extensions: []
|
|
20
|
+
extra_rdoc_files: []
|
|
21
|
+
files:
|
|
22
|
+
- CHANGELOG.md
|
|
23
|
+
- LICENSE
|
|
24
|
+
- README.md
|
|
25
|
+
- lib/philiprehberger/template.rb
|
|
26
|
+
- lib/philiprehberger/template/context.rb
|
|
27
|
+
- lib/philiprehberger/template/parser.rb
|
|
28
|
+
- lib/philiprehberger/template/renderer.rb
|
|
29
|
+
- lib/philiprehberger/template/version.rb
|
|
30
|
+
homepage: https://github.com/philiprehberger/rb-template
|
|
31
|
+
licenses:
|
|
32
|
+
- MIT
|
|
33
|
+
metadata:
|
|
34
|
+
homepage_uri: https://github.com/philiprehberger/rb-template
|
|
35
|
+
source_code_uri: https://github.com/philiprehberger/rb-template
|
|
36
|
+
changelog_uri: https://github.com/philiprehberger/rb-template/blob/main/CHANGELOG.md
|
|
37
|
+
rubygems_mfa_required: 'true'
|
|
38
|
+
post_install_message:
|
|
39
|
+
rdoc_options: []
|
|
40
|
+
require_paths:
|
|
41
|
+
- lib
|
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: 3.1.0
|
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
48
|
+
requirements:
|
|
49
|
+
- - ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: '0'
|
|
52
|
+
requirements: []
|
|
53
|
+
rubygems_version: 3.5.22
|
|
54
|
+
signing_key:
|
|
55
|
+
specification_version: 4
|
|
56
|
+
summary: Logic-less Mustache-style template engine with safe rendering
|
|
57
|
+
test_files: []
|