ytrbium 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/ruby.yml +35 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.standard.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +110 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/examples/hello.template.yaml +5 -0
- data/examples/main.template.yaml +5 -0
- data/exe/ytrbium +37 -0
- data/lib/ytrbium.rb +37 -0
- data/lib/ytrbium/core_ext.rb +62 -0
- data/lib/ytrbium/dsl.rb +36 -0
- data/lib/ytrbium/engine.rb +51 -0
- data/lib/ytrbium/file_resolver.rb +34 -0
- data/lib/ytrbium/string.rb +32 -0
- data/lib/ytrbium/version.rb +3 -0
- data/ytrbium.gemspec +31 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: aabc5530ddbcd3cae0b859d2baa6c586cbae9fbf612283000b612a539192a9b8
|
4
|
+
data.tar.gz: 8cbb49822eaaef6a8d8192246af559c7ff57cb5c76deb5dff277710b693ea6cd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 38006ac729bfec894dfc0a8fea232140674a6969266ad0426f93fe623a8b31afafbc0fa696c45d3e236f1c6e17795d483eb2bf5cad5ce6711590766a278c03d2
|
7
|
+
data.tar.gz: 4553f66c8f4507d355a8f16876710426e8212a0020fe46099218e5f8b1aed01646be2754c29c245b2c237fa4c0c0186fd882c1fc0e983f42a9a077fd4fae1b38
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
2
|
+
# They are provided by a third-party and are governed by
|
3
|
+
# separate terms of service, privacy policy, and support
|
4
|
+
# documentation.
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
7
|
+
|
8
|
+
name: Ruby
|
9
|
+
|
10
|
+
on:
|
11
|
+
push:
|
12
|
+
branches: [ main ]
|
13
|
+
pull_request:
|
14
|
+
branches: [ main ]
|
15
|
+
|
16
|
+
jobs:
|
17
|
+
test:
|
18
|
+
|
19
|
+
runs-on: ubuntu-latest
|
20
|
+
strategy:
|
21
|
+
matrix:
|
22
|
+
ruby-version: ['2.6', '2.7', '3.0']
|
23
|
+
|
24
|
+
steps:
|
25
|
+
- uses: actions/checkout@v2
|
26
|
+
- name: Set up Ruby
|
27
|
+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
28
|
+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
29
|
+
# uses: ruby/setup-ruby@v1
|
30
|
+
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
|
31
|
+
with:
|
32
|
+
ruby-version: ${{ matrix.ruby-version }}
|
33
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
34
|
+
- name: Run tests
|
35
|
+
run: bundle exec rake
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.standard.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at nick@nicksieger.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Nick Sieger
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# Ytrbium
|
2
|
+
|
3
|
+
Ytrbium is a simple YAML+ERB templating library in Ruby. With Ytrbium:
|
4
|
+
|
5
|
+
- Generate large, verbose YAML files from reusable templates.
|
6
|
+
- Collect and organize reusable template methods in multiple files and `import` them into each template.
|
7
|
+
- Indent template items correctly and intuitively.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'ytrbium'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle install
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install ytrbium
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Basic usage is demonstrated by a simple example:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require "ytrbium"
|
31
|
+
template = <<~TEMPL
|
32
|
+
<%! def hello(times) -%>
|
33
|
+
<% times.times do |n| -%>
|
34
|
+
- Hi <%= n + 1 %>!
|
35
|
+
<% end -%>
|
36
|
+
<%! end -%>
|
37
|
+
msg:
|
38
|
+
<%= hello(options[:count] || 4) -%>
|
39
|
+
TEMPL
|
40
|
+
|
41
|
+
Ytrbium.expand(template)
|
42
|
+
# =>
|
43
|
+
# msg:
|
44
|
+
# - Hi 1
|
45
|
+
# - Hi 2
|
46
|
+
# - Hi 3
|
47
|
+
# - Hi 4
|
48
|
+
|
49
|
+
options = { count: 2 }
|
50
|
+
puts Ytrbium.expand(template, binding: binding)
|
51
|
+
# =>
|
52
|
+
# msg:
|
53
|
+
# - Hi 1
|
54
|
+
# - Hi 2
|
55
|
+
```
|
56
|
+
|
57
|
+
A Ytrbium template is an [ERB template](https://ruby-doc.org/stdlib-2.7.1/libdoc/erb/rdoc/ERB.html) over a YAML document with some additional YAML-specific functionality:
|
58
|
+
|
59
|
+
1. You can declare re-usable functions in the template
|
60
|
+
2. You can split templates across files and `import` them into a main template
|
61
|
+
3. Every line of each dynamic ERB tag's content is indented correctly in its surroundings, which enables you to apply 1. and 2. with impunity and not worry that your resulting document is structured incorrectly.
|
62
|
+
|
63
|
+
Expanding the previous example:
|
64
|
+
|
65
|
+
examples/hello.template.yaml:
|
66
|
+
```yaml
|
67
|
+
<%! def hello(times) -%>
|
68
|
+
<% times.times do |n| -%>
|
69
|
+
- Hi <%= n + 1 %>!
|
70
|
+
<% end -%>
|
71
|
+
<%! end -%>
|
72
|
+
```
|
73
|
+
|
74
|
+
examples/main.template.yaml:
|
75
|
+
```yaml
|
76
|
+
<% import 'hello.template.yaml' -%>
|
77
|
+
one_message:
|
78
|
+
<%= hello(1) -%>
|
79
|
+
- final_messages:
|
80
|
+
<%= hello(2) -%>
|
81
|
+
```
|
82
|
+
|
83
|
+
Run the `ytrbium` command on the main template:
|
84
|
+
```
|
85
|
+
ytrbium examples/main.template.yaml
|
86
|
+
---
|
87
|
+
one_message:
|
88
|
+
- Hi 1!
|
89
|
+
- final_messages:
|
90
|
+
- Hi 1!
|
91
|
+
- Hi 2!
|
92
|
+
```
|
93
|
+
|
94
|
+
## Development
|
95
|
+
|
96
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
97
|
+
|
98
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
99
|
+
|
100
|
+
## Contributing
|
101
|
+
|
102
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nicksieger/ytrbium. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/nicksieger/ytrbium/blob/master/CODE_OF_CONDUCT.md).
|
103
|
+
|
104
|
+
## License
|
105
|
+
|
106
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
107
|
+
|
108
|
+
## Code of Conduct
|
109
|
+
|
110
|
+
Everyone interacting in the Ytrbium project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/nicksieger/ytrbium/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ytrbium"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/exe/ytrbium
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "ytrbium"
|
4
|
+
require "optparse"
|
5
|
+
|
6
|
+
paths = []
|
7
|
+
OptionParser.new do |opts|
|
8
|
+
opts.banner = "usage: ytrbium [options] template"
|
9
|
+
|
10
|
+
opts.on "-i", "--include DIR", "Add DIR to the list of paths to search for templates" do |i|
|
11
|
+
paths << i
|
12
|
+
end
|
13
|
+
|
14
|
+
opts.on("-h", "--help", "Print this message") do
|
15
|
+
puts opts
|
16
|
+
exit
|
17
|
+
end
|
18
|
+
|
19
|
+
opts.on("-v", "--version", "Print version") do
|
20
|
+
puts "Ytrbium v#{Ytrbium::VERSION}"
|
21
|
+
exit
|
22
|
+
end
|
23
|
+
end.parse!
|
24
|
+
|
25
|
+
paths += Ytrbium.paths
|
26
|
+
Ytrbium.paths = paths
|
27
|
+
|
28
|
+
if ARGV.present?
|
29
|
+
puts(*ARGV.map do |template|
|
30
|
+
Ytrbium.paths = [File.dirname(template)] + paths
|
31
|
+
Ytrbium.expand(IO.read(template))
|
32
|
+
end)
|
33
|
+
elsif (template = STDIN.read).present?
|
34
|
+
puts(Ytrbium.expand(template))
|
35
|
+
else
|
36
|
+
puts "Please provide one or more template files to expand."
|
37
|
+
end
|
data/lib/ytrbium.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require "yaml"
|
2
|
+
require "erubi"
|
3
|
+
require "erubi/capture_end"
|
4
|
+
|
5
|
+
module Ytrbium
|
6
|
+
def self.expand(template, binding: nil)
|
7
|
+
dsl.engine(template).expand(binding)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.dsl
|
11
|
+
resolver = file_resolver
|
12
|
+
Module.new do
|
13
|
+
@file_resolver = resolver
|
14
|
+
include Ytrbium::DSL
|
15
|
+
extend self
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.paths
|
20
|
+
file_resolver.paths
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.paths=(paths)
|
24
|
+
file_resolver.paths = paths
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.file_resolver
|
28
|
+
@file_resolver ||= FileResolver.new
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
require "ytrbium/file_resolver"
|
33
|
+
require "ytrbium/core_ext"
|
34
|
+
require "ytrbium/dsl"
|
35
|
+
require "ytrbium/engine"
|
36
|
+
require "ytrbium/string"
|
37
|
+
require "ytrbium/version"
|
@@ -0,0 +1,62 @@
|
|
1
|
+
class Object
|
2
|
+
def blank?
|
3
|
+
respond_to?(:empty?) ? !!empty? : !self
|
4
|
+
end
|
5
|
+
|
6
|
+
def present?
|
7
|
+
!blank?
|
8
|
+
end
|
9
|
+
|
10
|
+
# Indents all but the first line in the emitted yaml by the specified number of
|
11
|
+
# spaces.
|
12
|
+
def indented_yaml(preindent)
|
13
|
+
return "" unless present?
|
14
|
+
bare_yaml.indent_by(preindent)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Strip `---\n` header and `\n...\n` trailer from yaml, handle different
|
18
|
+
# versions of libyaml, see https://tickets.puppetlabs.com/browse/PUP-9313 and
|
19
|
+
# related links
|
20
|
+
# - libyaml 0.1.7 emits a `\n...\n` end-of-stream trailer, but 0.2.1 does not
|
21
|
+
# - treat single-line scalars separately to trim the trailing newline *only*
|
22
|
+
# when the emitted yaml is a single line.
|
23
|
+
def bare_yaml
|
24
|
+
return "" unless present?
|
25
|
+
yaml = to_yaml
|
26
|
+
|
27
|
+
# Detect single-line libyaml 0.2.1 scalar and remove trailing newline
|
28
|
+
return yaml.sub(/\A--- ([^\n]+)\n\Z/m, '\1') if yaml.single_line_ending_in_newline?
|
29
|
+
|
30
|
+
yaml.sub(/\A---[ \n]/, "") # Handle header for multi-line yaml snippets
|
31
|
+
.sub(/(\n\.\.\.\n)?\Z/m, "") # Handle libyaml 0.1.7 end of stream marker
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class NilClass
|
36
|
+
def blank?
|
37
|
+
true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class FalseClass
|
42
|
+
def blank?
|
43
|
+
true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class TrueClass
|
48
|
+
def blank?
|
49
|
+
false
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class String
|
54
|
+
def indent_by(preindent)
|
55
|
+
indent_str = " " * preindent
|
56
|
+
lines.join(indent_str)
|
57
|
+
end
|
58
|
+
|
59
|
+
def single_line_ending_in_newline?
|
60
|
+
count("\n") == 1 && self[-1] == "\n"
|
61
|
+
end
|
62
|
+
end
|
data/lib/ytrbium/dsl.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module Ytrbium
|
2
|
+
module DSL
|
3
|
+
def file_resolver
|
4
|
+
@file_resolver ||= FileResolver.new
|
5
|
+
end
|
6
|
+
|
7
|
+
def engine(input = nil, options = {})
|
8
|
+
@engine = nil if input
|
9
|
+
@engine ||= Engine.new(input, options)
|
10
|
+
end
|
11
|
+
|
12
|
+
def expand(binding = nil)
|
13
|
+
engine.expand(binding)
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(options = {})
|
17
|
+
YAML.safe_load expand(binding)
|
18
|
+
end
|
19
|
+
|
20
|
+
def expand_path(name)
|
21
|
+
file_resolver.expand_path name
|
22
|
+
end
|
23
|
+
|
24
|
+
def import(name, as: nil, **options)
|
25
|
+
mod = Ytrbium.dsl
|
26
|
+
file_resolver.load(name) do |io, filename|
|
27
|
+
mod.engine(io.read, filename: filename, module: mod)
|
28
|
+
end
|
29
|
+
if as
|
30
|
+
define_method(as) { mod }
|
31
|
+
else
|
32
|
+
mod.call(options || {})
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
class Ytrbium::Engine < Erubi::CaptureEndEngine
|
2
|
+
def initialize(input, options = {})
|
3
|
+
@template_module = options[:module] || Ytrbium.dsl
|
4
|
+
stack_var = @defstack = "@_ybuf_stack"
|
5
|
+
engine = self
|
6
|
+
@template_module.module_eval do
|
7
|
+
instance_variable_set(:@engine, engine)
|
8
|
+
instance_variable_set(stack_var.to_sym, [])
|
9
|
+
end
|
10
|
+
bufvar = "@_ybuf"
|
11
|
+
bufval = "Ytrbium::String.new"
|
12
|
+
super(input, options.merge(bufval: bufval,
|
13
|
+
bufvar: bufvar,
|
14
|
+
preamble: "options ||= {}; #{stack_var} = []; #{bufvar} = #{bufval};",
|
15
|
+
escape: true,
|
16
|
+
escapefunc: "@_ybuf.indent_expr",
|
17
|
+
regexp: /<%(\|?={1,2}|!|-|\#|%|\|)?(.*?)([-=])?%>([ \t]*\r?\n)?/m))
|
18
|
+
end
|
19
|
+
|
20
|
+
def expand(b = nil)
|
21
|
+
mod_src = src
|
22
|
+
args = []
|
23
|
+
args.unshift @filename, 1 if @filename
|
24
|
+
args.unshift b if b
|
25
|
+
@template_module.module_eval do
|
26
|
+
# rubocop:disable Security/Eval
|
27
|
+
eval(mod_src, *args)
|
28
|
+
# rubocop:enable Security/Eval
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def handle(indicator, code, tailch, rspace, lspace)
|
35
|
+
case indicator
|
36
|
+
when "!"
|
37
|
+
rspace = nil if tailch && !tailch.empty?
|
38
|
+
add_text(lspace) if lspace
|
39
|
+
|
40
|
+
if code.strip == "end"
|
41
|
+
src << "YAML.safe_load(#{@bufvar}.to_s); ensure; #{@bufvar} = #{@defstack}.pop; end;"
|
42
|
+
else
|
43
|
+
src << code << "; #{@defstack} << #{@bufvar}; #{@bufvar} = #{@bufval}; "
|
44
|
+
end
|
45
|
+
|
46
|
+
add_text(rspace) if rspace
|
47
|
+
else
|
48
|
+
super
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "pathname"
|
2
|
+
|
3
|
+
class Ytrbium::FileResolver
|
4
|
+
attr_reader :paths
|
5
|
+
|
6
|
+
def initialize(paths = nil)
|
7
|
+
@paths = paths && Array(paths) || [Dir.getwd]
|
8
|
+
init_search
|
9
|
+
end
|
10
|
+
|
11
|
+
def paths=(new_paths)
|
12
|
+
unless new_paths
|
13
|
+
raise ArgumentError,
|
14
|
+
"must provide a string path or array of string paths"
|
15
|
+
end
|
16
|
+
@paths = Array(new_paths)
|
17
|
+
init_search
|
18
|
+
end
|
19
|
+
|
20
|
+
def expand_path(name)
|
21
|
+
path = @search.detect { |path| (path + name).exist? }
|
22
|
+
raise ArgumentError, "No file #{name} found" unless path
|
23
|
+
(path.expand_path + name).to_s
|
24
|
+
end
|
25
|
+
|
26
|
+
def load(name)
|
27
|
+
filename = expand_path name
|
28
|
+
File.open(filename, "r") { |file| yield file, filename }
|
29
|
+
end
|
30
|
+
|
31
|
+
private def init_search
|
32
|
+
@search = [*@paths, *$LOAD_PATH].map { |p| Pathname.new(p) }
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class Ytrbium::String < ::String
|
2
|
+
def initialize
|
3
|
+
super
|
4
|
+
@indent = 0
|
5
|
+
@last_newline = 0
|
6
|
+
end
|
7
|
+
|
8
|
+
def <<(str)
|
9
|
+
@indent = -1 if str.include?("\n")
|
10
|
+
super.tap do
|
11
|
+
if @indent == -1
|
12
|
+
@last_newline = str.rindex("\n")
|
13
|
+
@indent = 0
|
14
|
+
end
|
15
|
+
if @indent == 0 && self[@last_newline..] =~ /\n([- ]+)\Z/m
|
16
|
+
@indent = $1.length
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def indent_expr(obj)
|
22
|
+
obj.bare_yaml.indent_by(@indent)
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_s
|
26
|
+
if start_with?("---")
|
27
|
+
self
|
28
|
+
else
|
29
|
+
"---\n#{self}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/ytrbium.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative 'lib/ytrbium/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "ytrbium"
|
5
|
+
spec.version = Ytrbium::VERSION
|
6
|
+
spec.authors = ["Nick Sieger"]
|
7
|
+
spec.email = ["nick@nicksieger.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Simple YAML+ERB templating in Ruby}
|
10
|
+
spec.description = %q{Make YAML+ERB templating DWIM and reusable.}
|
11
|
+
spec.homepage = "https://github.com/nicksieger/ytrbium"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
17
|
+
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.9"
|
29
|
+
|
30
|
+
spec.add_dependency "erubi", ">= 1.10"
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ytrbium
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nick Sieger
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-06-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: erubi
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
description: Make YAML+ERB templating DWIM and reusable.
|
42
|
+
email:
|
43
|
+
- nick@nicksieger.com
|
44
|
+
executables:
|
45
|
+
- ytrbium
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".github/workflows/ruby.yml"
|
50
|
+
- ".gitignore"
|
51
|
+
- ".rspec"
|
52
|
+
- ".standard.yml"
|
53
|
+
- CODE_OF_CONDUCT.md
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- bin/console
|
59
|
+
- bin/setup
|
60
|
+
- examples/hello.template.yaml
|
61
|
+
- examples/main.template.yaml
|
62
|
+
- exe/ytrbium
|
63
|
+
- lib/ytrbium.rb
|
64
|
+
- lib/ytrbium/core_ext.rb
|
65
|
+
- lib/ytrbium/dsl.rb
|
66
|
+
- lib/ytrbium/engine.rb
|
67
|
+
- lib/ytrbium/file_resolver.rb
|
68
|
+
- lib/ytrbium/string.rb
|
69
|
+
- lib/ytrbium/version.rb
|
70
|
+
- ytrbium.gemspec
|
71
|
+
homepage: https://github.com/nicksieger/ytrbium
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata:
|
75
|
+
homepage_uri: https://github.com/nicksieger/ytrbium
|
76
|
+
source_code_uri: https://github.com/nicksieger/ytrbium
|
77
|
+
changelog_uri: https://github.com/nicksieger/ytrbium/blob/main/CHANGELOG.md
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: 2.3.0
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubygems_version: 3.0.3
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Simple YAML+ERB templating in Ruby
|
97
|
+
test_files: []
|