template-ruby 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d5e2a863316056d011b034cc0c819984d710899e8b51657dd15636e45db058b0
4
- data.tar.gz: 47974cad2e705fb70706b68a496523f414b3027c6f57ec6d22344c588cdce143
3
+ metadata.gz: 477a0d0d70a09f5118391d5288031811436a520c8faac5794da2ff3dcee996ad
4
+ data.tar.gz: e3e24d79c50c1058f619b63d09154b823acc3f0cd7b3c7ac75029c86df9e1bee
5
5
  SHA512:
6
- metadata.gz: 1ba52300910fb32458e115c200842c3a2ef18a19e8fddee92c38a961d60637d3c3c73906304184ef0ec5771ad85cc5dd19d8dbe0803a872a2c5c0dd69cc82a16
7
- data.tar.gz: d526bbd95df4375aee06d4c9cc8bb8c6f2927b725514527dd9d3e29229223235bf0c8870d917eb61ae30cbce52e2c27a5f18ba31a2b1fcc94b0dfc574bf77bd1
6
+ metadata.gz: ca7d9deac26c98d399d190a11192b8448a1c2bec8119a1dbfae6cca905554fd25d9bdfd91ffdbd2ad8ac73f743c07d30d5f97fab81d5d7d508ac7c8a09832217
7
+ data.tar.gz: d6da812be0c57c0c943ef44a46a7e0df3dd45f687251305c9cba5ada1d1f997dd57723405b129e5e2458139c9f2ef3baec31d9b66f4a850d01b5b8b1320cca37
@@ -0,0 +1,14 @@
1
+ name: RSpec
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ name: RSpec
8
+ runs-on: ubuntu-latest
9
+
10
+ steps:
11
+ - uses: actions/checkout@v1
12
+ - uses: actions/setup-ruby@v1
13
+ - run: bundle install
14
+ - run: bundle exec rspec
data/CHANGELOG.md CHANGED
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## 0.2.4 / 2022-08-02
9
+
10
+ - Add method `String#*`, e.g. `{"Dorian " \* 2}" -> "Dorian Dorian "
11
+ - Add executable to gem, e.g. `template --help`
12
+
8
13
  ## 0.2.3 / 2022-08-31
9
14
 
10
15
  - Add default timeout for code and template parsing and evaluation
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- template-ruby (0.2.2)
4
+ template-ruby (0.2.3)
5
5
  activesupport (~> 7)
6
6
  parslet (~> 2)
7
7
  zeitwerk (~> 2.6)
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2022 Dorian Marié <dorian@dorianmarie.fr>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,10 +1,103 @@
1
- # `template-ruby`
1
+ # Template
2
2
 
3
- A templating programming language
3
+ [![RSpec](https://github.com/dorianmariefr/template-ruby/actions/workflows/rspec.yml/badge.svg)](https://github.com/dorianmariefr/template-ruby/actions/workflows/rspec.yml)
4
4
 
5
- Like `Hello {name}` with `{name: "Dorian"}` gives `Hello Dorian`.
5
+ See [templatelang.com](https://templatelang.com) for the full documentation and
6
+ live code editing.
7
+
8
+ ## The programming language
9
+
10
+ Hi, I'm [Dorian Marié](https://dorianmarie.fr), I created Template to let users of my websites provide templates to customize their experience.
11
+
12
+ Template is meant to be:
13
+
14
+ - **Simple**: `Hello` and `Hello {name}`
15
+ - **Safe**: Can be provided user input
16
+ - **Powerful**: Functions, object-oriented, built-in methods
17
+
18
+ Template is currently written in Ruby and embeddable as a Ruby gem.
19
+
20
+ ## Install
21
+
22
+ ### As a command line tool:
23
+
24
+ ```bash
25
+ $ gem install template-ruby
26
+ $ template --input "Hello {name}" --context '{ name: "Dorian" }'
27
+ Hello Dorian
28
+ $ template --input "1 + 2 = {1 + 2}"
29
+ 1 + 2 = 3
30
+ ```
31
+
32
+ ### As a Ruby gem:
33
+
34
+ In a `Gemfile`:
6
35
 
7
36
  ```ruby
8
- > Template.render("Hello {name}", '{name: "Dorian"}')
9
- => "Hello Dorian"
37
+ gem "template-ruby"
10
38
  ```
39
+
40
+ Then `$ bundle install`
41
+
42
+ Then you can use Template like:
43
+
44
+ ```ruby
45
+ Template.render("Hello {name}", '{ name: "Dorian" }')
46
+ # => "Hello Dorian"
47
+ Template.render("1 + 2 = {1 + 2}")
48
+ # => "1 + 2 = 3"
49
+ Template.render(input, context, io: StringIO.new, timeout: 10)
50
+ ```
51
+
52
+ The context is a sub-language called Code, you can use it like:
53
+
54
+ ```ruby
55
+ Code.evaluate("1 + 2") # => 3
56
+ ```
57
+
58
+ ## Future work
59
+
60
+ - Extend standard library
61
+ - Global methods from Ruby, e.g. `{markdown "**bold**"}`
62
+ - Object methods from Ruby, e.g. `{"**bold**".markdown}`
63
+ - Classes, e.g. `{class User end}`
64
+ - Write JavaScript version
65
+ - Write Crystal version
66
+
67
+ ## Contributing
68
+
69
+ Feel free to open [issues](https://github.com/dorianmariefr/template-ruby/issues),
70
+ and [pull requests](https://github.com/dorianmariefr/template-ruby/pulls).
71
+
72
+ To develop locally:
73
+
74
+ ```text
75
+ $ git clone https://github.com/dorianmariefr/template-ruby
76
+ $ cd template-ruby
77
+ $ bundle
78
+ $ rspec
79
+ $ bin/template -i docs/...
80
+ ```
81
+
82
+ ## Credits
83
+
84
+ Thanks to [thoughtbot](https://thoughtbot.com) who let me work on this programming
85
+ language as a Friday project.
86
+
87
+ Thanks to [Kaspar Schiess](https://github.com/kschiess) who made
88
+ [Parslet](https://kschiess.github.io/parslet/), the gem that helped me write the parser.
89
+
90
+ Inspiration from [Ruby](https://www.ruby-lang.org/en/) and
91
+ [Liquid](https://shopify.github.io/liquid/).
92
+
93
+ ## License
94
+
95
+ MIT
96
+
97
+ Copyright 2022 Dorian Marié <dorian@dorianmarie.fr>
98
+
99
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
100
+
101
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
102
+
103
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/TODO.md ADDED
@@ -0,0 +1 @@
1
+ - Code gem
data/bin/template CHANGED
@@ -6,9 +6,9 @@ require_relative "../lib/template-ruby"
6
6
  options = {}
7
7
 
8
8
  OptionParser.new do |opts|
9
- opts.banner = "Usage: #{$PROGRAM_NAME} [options]"
9
+ opts.banner = "Usage: template [options]"
10
10
 
11
- opts.on("-i INPUT", "--input=INPUT", "Input in the template language") do |input|
11
+ opts.on("-i INPUT", "--input=INPUT", "Input in the template language (String or File)") do |input|
12
12
  if File.exists?(input)
13
13
  input = File.read(input)
14
14
  end
@@ -16,7 +16,7 @@ OptionParser.new do |opts|
16
16
  options[:input] = input
17
17
  end
18
18
 
19
- opts.on("-c CONTEXT", "--context=CONTEXT", "Context in the code language") do |context|
19
+ opts.on("-c CONTEXT", "--context=CONTEXT", "Context in the code language (String or File)") do |context|
20
20
  if File.exists?(context)
21
21
  context = File.read(context)
22
22
  end
@@ -15,6 +15,8 @@ class Code
15
15
  to_function(arguments)
16
16
  elsif operator == "+"
17
17
  plus(arguments)
18
+ elsif operator == "*"
19
+ multiplication(arguments)
18
20
  elsif operator == "reverse"
19
21
  reverse(arguments)
20
22
  else
@@ -51,6 +53,12 @@ class Code
51
53
  ::Code::Object::String.new(raw + other.raw)
52
54
  end
53
55
 
56
+ def multiplication(arguments)
57
+ sig(arguments, ::Code::Object::Number)
58
+ other = arguments.first.value
59
+ ::Code::Object::String.new(raw * other.raw)
60
+ end
61
+
54
62
  def reverse(arguments)
55
63
  sig(arguments)
56
64
  ::Code::Object::String.new(raw.reverse)
data/lib/code.rb CHANGED
@@ -1,9 +1,8 @@
1
1
  class Code
2
2
  def initialize(input, io: $stdout, timeout: 10)
3
3
  @input = input
4
- @parsed = Timeout::timeout(timeout) do
5
- ::Code::Parser::Code.new.parse(@input)
6
- end
4
+ @parsed =
5
+ Timeout.timeout(timeout) { ::Code::Parser::Code.new.parse(@input) }
7
6
  @io = io
8
7
  @timeout = timeout
9
8
  end
@@ -13,7 +12,7 @@ class Code
13
12
  end
14
13
 
15
14
  def evaluate(context = "")
16
- Timeout::timeout(@timeout) do
15
+ Timeout.timeout(@timeout) do
17
16
  if context.present?
18
17
  context = ::Code.evaluate(context, timeout: @timeout)
19
18
  else
@@ -0,0 +1,3 @@
1
+ require_relative "../template"
2
+
3
+ Template::Version = Gem::Version.new("0.2.4")
data/lib/template.rb CHANGED
@@ -1,11 +1,10 @@
1
1
  class Template
2
- VERSION = Gem::Version.new("0.2.3")
3
-
4
2
  def initialize(input, io: StringIO.new, timeout: 10)
5
3
  @input = input
6
- @parsed = Timeout::timeout(timeout) do
7
- ::Template::Parser::Template.new.parse(@input)
8
- end
4
+ @parsed =
5
+ Timeout.timeout(timeout) do
6
+ ::Template::Parser::Template.new.parse(@input)
7
+ end
9
8
  @io = io
10
9
  @timeout = timeout
11
10
  end
@@ -15,16 +14,21 @@ class Template
15
14
  end
16
15
 
17
16
  def render(context = "")
18
- Timeout::timeout(@timeout) do
17
+ Timeout.timeout(@timeout) do
19
18
  if context.present?
20
19
  context = ::Code.evaluate(context, timeout: @timeout)
21
20
  else
22
21
  context = ::Code::Object::Dictionnary.new
23
22
  end
24
23
 
25
- ::Template::Node::Template.new(@parsed).evaluate(context: context, io: @io)
24
+ ::Template::Node::Template.new(@parsed).evaluate(
25
+ context: context,
26
+ io: @io,
27
+ )
26
28
 
27
29
  @io.is_a?(StringIO) ? @io.string : nil
28
30
  end
29
31
  end
30
32
  end
33
+
34
+ require_relative "template/version"
data/spec/code_spec.rb CHANGED
@@ -95,6 +95,7 @@ RSpec.describe Code do
95
95
  ["until true end", ""],
96
96
  ["until true\nend", ""],
97
97
  %w[("Good".."Bad").first Good],
98
+ ['"Dorian " * 2', "Dorian Dorian "],
98
99
  ].each do |(input, expected)|
99
100
  context input.inspect do
100
101
  let(:input) { input }
@@ -1,8 +1,8 @@
1
- require_relative "lib/template-ruby"
1
+ require_relative "lib/template/version"
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "template-ruby"
5
- s.version = ::Template::VERSION
5
+ s.version = ::Template::Version
6
6
  s.summary = "A templating programming language"
7
7
  s.description =
8
8
  'Like "Hello {name}" with {name: "Dorian"} gives "Hello Dorian"'
@@ -13,6 +13,7 @@ Gem::Specification.new do |s|
13
13
  s.require_paths = ["lib"]
14
14
  s.homepage = "https://github.com/dorianmariefr/template-ruby"
15
15
  s.license = "MIT"
16
+ s.executables = "template"
16
17
 
17
18
  s.add_dependency "activesupport", "~> 7"
18
19
  s.add_dependency "parslet", "~> 2"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: template-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-31 00:00:00.000000000 Z
11
+ date: 2022-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -82,18 +82,22 @@ dependencies:
82
82
  version: '3'
83
83
  description: 'Like "Hello {name}" with {name: "Dorian"} gives "Hello Dorian"'
84
84
  email: dorian@dorianmarie.fr
85
- executables: []
85
+ executables:
86
+ - template
86
87
  extensions: []
87
88
  extra_rdoc_files: []
88
89
  files:
89
90
  - ".editorconfig"
91
+ - ".github/workflows/rspec.yml"
90
92
  - ".gitignore"
91
93
  - ".prettierrc"
92
94
  - ".rspec"
93
95
  - CHANGELOG.md
94
96
  - Gemfile
95
97
  - Gemfile.lock
98
+ - LICENSE
96
99
  - README.md
100
+ - TODO.md
97
101
  - bin/template
98
102
  - docs/euler/1.template
99
103
  - docs/euler/2.template
@@ -201,6 +205,7 @@ files:
201
205
  - lib/template/node/text_part.rb
202
206
  - lib/template/parser.rb
203
207
  - lib/template/parser/template.rb
208
+ - lib/template/version.rb
204
209
  - spec/call_spec.rb
205
210
  - spec/code/error/type_error_spec.rb
206
211
  - spec/code/parser/boolean_spec.rb