template-ruby 0.2.1 → 0.2.4
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/.github/workflows/rspec.yml +14 -0
- data/CHANGELOG.md +14 -2
- data/Gemfile.lock +4 -4
- data/LICENSE +7 -0
- data/README.md +98 -5
- data/TODO.md +1 -0
- data/bin/template +3 -3
- data/lib/code/node/call.rb +4 -3
- data/lib/code/object/list.rb +4 -4
- data/lib/code/object/range.rb +18 -18
- data/lib/code/object/string.rb +8 -0
- data/lib/code/object.rb +2 -10
- data/lib/code/parser/code.rb +2 -1
- data/lib/code.rb +14 -10
- data/lib/template/parser/template.rb +6 -2
- data/lib/template/version.rb +3 -0
- data/lib/template-ruby.rb +1 -0
- data/lib/template.rb +22 -13
- data/spec/code/parser/call_spec.rb +1 -3
- data/spec/code_spec.rb +1 -0
- data/spec/template_spec.rb +2 -3
- data/template-ruby.gemspec +3 -2
- metadata +8 -3
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 477a0d0d70a09f5118391d5288031811436a520c8faac5794da2ff3dcee996ad
         | 
| 4 | 
            +
              data.tar.gz: e3e24d79c50c1058f619b63d09154b823acc3f0cd7b3c7ac75029c86df9e1bee
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: ca7d9deac26c98d399d190a11192b8448a1c2bec8119a1dbfae6cca905554fd25d9bdfd91ffdbd2ad8ac73f743c07d30d5f97fab81d5d7d508ac7c8a09832217
         | 
| 7 | 
            +
              data.tar.gz: d6da812be0c57c0c943ef44a46a7e0df3dd45f687251305c9cba5ada1d1f997dd57723405b129e5e2458139c9f2ef3baec31d9b66f4a850d01b5b8b1320cca37
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    | @@ -5,9 +5,22 @@ 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 | 
            +
             | 
| 13 | 
            +
            ## 0.2.3 / 2022-08-31
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            - Add default timeout for code and template parsing and evaluation
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            ## 0.2.2 / 2022-08-31
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            - Fix parsing error when the template is empty, e.g. ""
         | 
| 20 | 
            +
             | 
| 8 21 | 
             
            ## 0.2.1 / 2022-08-31
         | 
| 9 22 |  | 
| 10 | 
            -
            - Fix error on empty code like `Hello {`
         | 
| 23 | 
            +
            - Fix parsing error on empty code like `Hello {`
         | 
| 11 24 |  | 
| 12 25 | 
             
            ## 0.2.0 / 2022-08-30
         | 
| 13 26 |  | 
| @@ -16,4 +29,3 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 | |
| 16 29 | 
             
            ## 0.1.0 / 2022-07-28
         | 
| 17 30 |  | 
| 18 31 | 
             
            - Initial version with interpolation
         | 
| 19 | 
            -
             | 
    
        data/Gemfile.lock
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            PATH
         | 
| 2 2 | 
             
              remote: .
         | 
| 3 3 | 
             
              specs:
         | 
| 4 | 
            -
                template-ruby (0. | 
| 4 | 
            +
                template-ruby (0.2.3)
         | 
| 5 5 | 
             
                  activesupport (~> 7)
         | 
| 6 6 | 
             
                  parslet (~> 2)
         | 
| 7 7 | 
             
                  zeitwerk (~> 2.6)
         | 
| @@ -21,7 +21,7 @@ GEM | |
| 21 21 | 
             
                  tilt
         | 
| 22 22 | 
             
                i18n (1.12.0)
         | 
| 23 23 | 
             
                  concurrent-ruby (~> 1.0)
         | 
| 24 | 
            -
                minitest (5.16. | 
| 24 | 
            +
                minitest (5.16.3)
         | 
| 25 25 | 
             
                parslet (2.0.0)
         | 
| 26 26 | 
             
                prettier (3.2.0)
         | 
| 27 27 | 
             
                  syntax_tree (>= 2.7.1)
         | 
| @@ -42,9 +42,9 @@ GEM | |
| 42 42 | 
             
                  diff-lcs (>= 1.2.0, < 2.0)
         | 
| 43 43 | 
             
                  rspec-support (~> 3.11.0)
         | 
| 44 44 | 
             
                rspec-support (3.11.0)
         | 
| 45 | 
            -
                syntax_tree (3. | 
| 45 | 
            +
                syntax_tree (3.5.0)
         | 
| 46 46 | 
             
                  prettier_print
         | 
| 47 | 
            -
                syntax_tree-haml (1.3. | 
| 47 | 
            +
                syntax_tree-haml (1.3.1)
         | 
| 48 48 | 
             
                  haml (>= 5.2)
         | 
| 49 49 | 
             
                  prettier_print
         | 
| 50 50 | 
             
                  syntax_tree (>= 2.0.1)
         | 
    
        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 | 
            -
            #  | 
| 1 | 
            +
            # Template
         | 
| 2 2 |  | 
| 3 | 
            -
             | 
| 3 | 
            +
            [](https://github.com/dorianmariefr/template-ruby/actions/workflows/rspec.yml)
         | 
| 4 4 |  | 
| 5 | 
            -
             | 
| 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 | 
            -
             | 
| 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:  | 
| 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
         | 
    
        data/lib/code/node/call.rb
    CHANGED
    
    | @@ -8,9 +8,10 @@ class Code | |
| 8 8 | 
             
                    @arguments.map! { |argument| ::Code::Node::CallArgument.new(argument) }
         | 
| 9 9 |  | 
| 10 10 | 
             
                    if call.key?(:right)
         | 
| 11 | 
            -
                      @right = | 
| 12 | 
            -
                         | 
| 13 | 
            -
             | 
| 11 | 
            +
                      @right =
         | 
| 12 | 
            +
                        call
         | 
| 13 | 
            +
                          .fetch(:right)
         | 
| 14 | 
            +
                          .map { |right| ::Code::Node::ChainedCall.new(right) }
         | 
| 14 15 | 
             
                    end
         | 
| 15 16 |  | 
| 16 17 | 
             
                    if call.key?(:block)
         | 
    
        data/lib/code/object/list.rb
    CHANGED
    
    | @@ -59,7 +59,7 @@ class Code | |
| 59 59 | 
             
                        else
         | 
| 60 60 | 
             
                          acc + [element]
         | 
| 61 61 | 
             
                        end
         | 
| 62 | 
            -
                      end
         | 
| 62 | 
            +
                      end,
         | 
| 63 63 | 
             
                    )
         | 
| 64 64 | 
             
                  end
         | 
| 65 65 |  | 
| @@ -138,7 +138,7 @@ class Code | |
| 138 138 | 
             
                      argument.call(
         | 
| 139 139 | 
             
                        arguments: [
         | 
| 140 140 | 
             
                          ::Code::Object::Argument.new(acc),
         | 
| 141 | 
            -
                          ::Code::Object::Argument.new(element)
         | 
| 141 | 
            +
                          ::Code::Object::Argument.new(element),
         | 
| 142 142 | 
             
                        ],
         | 
| 143 143 | 
             
                        context: context,
         | 
| 144 144 | 
             
                        io: io,
         | 
| @@ -169,7 +169,7 @@ class Code | |
| 169 169 | 
             
                          context: context,
         | 
| 170 170 | 
             
                          io: io,
         | 
| 171 171 | 
             
                        ).truthy?
         | 
| 172 | 
            -
                      end
         | 
| 172 | 
            +
                      end,
         | 
| 173 173 | 
             
                    )
         | 
| 174 174 | 
             
                  end
         | 
| 175 175 |  | 
| @@ -183,7 +183,7 @@ class Code | |
| 183 183 | 
             
                          context: context,
         | 
| 184 184 | 
             
                          io: io,
         | 
| 185 185 | 
             
                        )
         | 
| 186 | 
            -
                      end
         | 
| 186 | 
            +
                      end,
         | 
| 187 187 | 
             
                    )
         | 
| 188 188 | 
             
                  end
         | 
| 189 189 |  | 
    
        data/lib/code/object/range.rb
    CHANGED
    
    | @@ -54,13 +54,11 @@ class Code | |
| 54 54 | 
             
                    argument = arguments.first.value
         | 
| 55 55 | 
             
                    ::Code::Object::Boolean.new(
         | 
| 56 56 | 
             
                      raw.any? do |element|
         | 
| 57 | 
            -
                        argument
         | 
| 58 | 
            -
                          . | 
| 59 | 
            -
             | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 62 | 
            -
                          )
         | 
| 63 | 
            -
                          .truthy?
         | 
| 57 | 
            +
                        argument.call(
         | 
| 58 | 
            +
                          arguments: [::Code::Object::Argument.new(element)],
         | 
| 59 | 
            +
                          context: context,
         | 
| 60 | 
            +
                          io: io,
         | 
| 61 | 
            +
                        ).truthy?
         | 
| 64 62 | 
             
                      end,
         | 
| 65 63 | 
             
                    )
         | 
| 66 64 | 
             
                  end
         | 
| @@ -70,13 +68,11 @@ class Code | |
| 70 68 | 
             
                    argument = arguments.first.value
         | 
| 71 69 | 
             
                    ::Code::Object::Boolean.new(
         | 
| 72 70 | 
             
                      raw.all? do |element|
         | 
| 73 | 
            -
                        argument
         | 
| 74 | 
            -
                          . | 
| 75 | 
            -
             | 
| 76 | 
            -
             | 
| 77 | 
            -
             | 
| 78 | 
            -
                          )
         | 
| 79 | 
            -
                          .truthy?
         | 
| 71 | 
            +
                        argument.call(
         | 
| 72 | 
            +
                          arguments: [::Code::Object::Argument.new(element)],
         | 
| 73 | 
            +
                          context: context,
         | 
| 74 | 
            +
                          io: io,
         | 
| 75 | 
            +
                        ).truthy?
         | 
| 80 76 | 
             
                      end,
         | 
| 81 77 | 
             
                    )
         | 
| 82 78 | 
             
                  end
         | 
| @@ -104,7 +100,7 @@ class Code | |
| 104 100 | 
             
                          context: context,
         | 
| 105 101 | 
             
                          io: io,
         | 
| 106 102 | 
             
                        ).truthy?
         | 
| 107 | 
            -
                      end
         | 
| 103 | 
            +
                      end,
         | 
| 108 104 | 
             
                    )
         | 
| 109 105 | 
             
                  end
         | 
| 110 106 |  | 
| @@ -118,7 +114,7 @@ class Code | |
| 118 114 | 
             
                          context: context,
         | 
| 119 115 | 
             
                          io: io,
         | 
| 120 116 | 
             
                        )
         | 
| 121 | 
            -
                      end
         | 
| 117 | 
            +
                      end,
         | 
| 122 118 | 
             
                    )
         | 
| 123 119 | 
             
                  end
         | 
| 124 120 |  | 
| @@ -131,9 +127,13 @@ class Code | |
| 131 127 | 
             
                    list << element
         | 
| 132 128 |  | 
| 133 129 | 
             
                    if @exlucde_end
         | 
| 134 | 
            -
                       | 
| 130 | 
            +
                      while (element = element + argument) < @right
         | 
| 131 | 
            +
                        list << element
         | 
| 132 | 
            +
                      end
         | 
| 135 133 | 
             
                    else
         | 
| 136 | 
            -
                       | 
| 134 | 
            +
                      while (element = element + argument) <= @right
         | 
| 135 | 
            +
                        list << element
         | 
| 136 | 
            +
                      end
         | 
| 137 137 | 
             
                    end
         | 
| 138 138 |  | 
| 139 139 | 
             
                    list
         | 
    
        data/lib/code/object/string.rb
    CHANGED
    
    | @@ -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/object.rb
    CHANGED
    
    | @@ -45,11 +45,7 @@ class Code | |
| 45 45 |  | 
| 46 46 | 
             
                def <=>(other)
         | 
| 47 47 | 
             
                  if respond_to?(:raw)
         | 
| 48 | 
            -
                     | 
| 49 | 
            -
                      raw <=> other.raw
         | 
| 50 | 
            -
                    else
         | 
| 51 | 
            -
                      raw <=> other
         | 
| 52 | 
            -
                    end
         | 
| 48 | 
            +
                    other.respond_to?(:raw) ? raw <=> other.raw : raw <=> other
         | 
| 53 49 | 
             
                  else
         | 
| 54 50 | 
             
                    other <=> self
         | 
| 55 51 | 
             
                  end
         | 
| @@ -57,11 +53,7 @@ class Code | |
| 57 53 |  | 
| 58 54 | 
             
                def ==(other)
         | 
| 59 55 | 
             
                  if respond_to?(:raw)
         | 
| 60 | 
            -
                     | 
| 61 | 
            -
                      raw == other.raw
         | 
| 62 | 
            -
                    else
         | 
| 63 | 
            -
                      raw == other
         | 
| 64 | 
            -
                    end
         | 
| 56 | 
            +
                    other.respond_to?(:raw) ? raw == other.raw : raw == other
         | 
| 65 57 | 
             
                  else
         | 
| 66 58 | 
             
                    other == self
         | 
| 67 59 | 
             
                  end
         | 
    
        data/lib/code/parser/code.rb
    CHANGED
    
    | @@ -9,7 +9,8 @@ class Code | |
| 9 9 | 
             
                  rule(:whitespace?) { whitespace.maybe }
         | 
| 10 10 |  | 
| 11 11 | 
             
                  rule(:code) do
         | 
| 12 | 
            -
                    (whitespace?.ignore >> statement >> whitespace?.ignore).repeat(1) | | 
| 12 | 
            +
                    (whitespace?.ignore >> statement >> whitespace?.ignore).repeat(1) |
         | 
| 13 | 
            +
                      whitespace?.ignore
         | 
| 13 14 | 
             
                  end
         | 
| 14 15 |  | 
| 15 16 | 
             
                  root(:code)
         | 
    
        data/lib/code.rb
    CHANGED
    
    | @@ -1,22 +1,26 @@ | |
| 1 1 | 
             
            class Code
         | 
| 2 | 
            -
              def initialize(input, io: $stdout)
         | 
| 2 | 
            +
              def initialize(input, io: $stdout, timeout: 10)
         | 
| 3 3 | 
             
                @input = input
         | 
| 4 | 
            -
                @parsed = | 
| 4 | 
            +
                @parsed =
         | 
| 5 | 
            +
                  Timeout.timeout(timeout) { ::Code::Parser::Code.new.parse(@input) }
         | 
| 5 6 | 
             
                @io = io
         | 
| 7 | 
            +
                @timeout = timeout
         | 
| 6 8 | 
             
              end
         | 
| 7 9 |  | 
| 8 | 
            -
              def self.evaluate(input, context = "", io: $stdout)
         | 
| 9 | 
            -
                new(input, io: io).evaluate(context)
         | 
| 10 | 
            +
              def self.evaluate(input, context = "", io: $stdout, timeout: 10)
         | 
| 11 | 
            +
                new(input, io: io, timeout: timeout).evaluate(context)
         | 
| 10 12 | 
             
              end
         | 
| 11 13 |  | 
| 12 14 | 
             
              def evaluate(context = "")
         | 
| 13 | 
            -
                 | 
| 14 | 
            -
                  context | 
| 15 | 
            -
             | 
| 16 | 
            -
                   | 
| 17 | 
            -
             | 
| 15 | 
            +
                Timeout.timeout(@timeout) do
         | 
| 16 | 
            +
                  if context.present?
         | 
| 17 | 
            +
                    context = ::Code.evaluate(context, timeout: @timeout)
         | 
| 18 | 
            +
                  else
         | 
| 19 | 
            +
                    context = ::Code::Object::Dictionnary.new
         | 
| 20 | 
            +
                  end
         | 
| 18 21 |  | 
| 19 | 
            -
             | 
| 22 | 
            +
                  ::Code::Node::Code.new(parsed).evaluate(context: context, io: @io)
         | 
| 23 | 
            +
                end
         | 
| 20 24 | 
             
              end
         | 
| 21 25 |  | 
| 22 26 | 
             
              private
         | 
| @@ -15,10 +15,14 @@ class Template | |
| 15 15 | 
             
                  end
         | 
| 16 16 |  | 
| 17 17 | 
             
                  rule(:code_part) do
         | 
| 18 | 
            -
                    left_curly_bracket.ignore >> code >> | 
| 18 | 
            +
                    left_curly_bracket.ignore >> code >>
         | 
| 19 | 
            +
                      (right_curly_bracket.ignore | any.absent?)
         | 
| 19 20 | 
             
                  end
         | 
| 20 21 |  | 
| 21 | 
            -
                  rule(:template)  | 
| 22 | 
            +
                  rule(:template) do
         | 
| 23 | 
            +
                    (text_part.as(:text) | code_part.as(:code)).repeat(1) |
         | 
| 24 | 
            +
                      str("").as(:text).repeat(1, 1)
         | 
| 25 | 
            +
                  end
         | 
| 22 26 |  | 
| 23 27 | 
             
                  root(:template)
         | 
| 24 28 | 
             
                end
         | 
    
        data/lib/template-ruby.rb
    CHANGED
    
    
    
        data/lib/template.rb
    CHANGED
    
    | @@ -1,25 +1,34 @@ | |
| 1 1 | 
             
            class Template
         | 
| 2 | 
            -
               | 
| 3 | 
            -
             | 
| 4 | 
            -
              def initialize(input, io: StringIO.new)
         | 
| 2 | 
            +
              def initialize(input, io: StringIO.new, timeout: 10)
         | 
| 5 3 | 
             
                @input = input
         | 
| 6 | 
            -
                @parsed = | 
| 4 | 
            +
                @parsed =
         | 
| 5 | 
            +
                  Timeout.timeout(timeout) do
         | 
| 6 | 
            +
                    ::Template::Parser::Template.new.parse(@input)
         | 
| 7 | 
            +
                  end
         | 
| 7 8 | 
             
                @io = io
         | 
| 9 | 
            +
                @timeout = timeout
         | 
| 8 10 | 
             
              end
         | 
| 9 11 |  | 
| 10 | 
            -
              def self.render(input, context = "", io: StringIO.new)
         | 
| 11 | 
            -
                new(input, io: io).render(context)
         | 
| 12 | 
            +
              def self.render(input, context = "", io: StringIO.new, timeout: 10)
         | 
| 13 | 
            +
                new(input, io: io, timeout: timeout).render(context)
         | 
| 12 14 | 
             
              end
         | 
| 13 15 |  | 
| 14 16 | 
             
              def render(context = "")
         | 
| 15 | 
            -
                 | 
| 16 | 
            -
                  context | 
| 17 | 
            -
             | 
| 18 | 
            -
                   | 
| 19 | 
            -
             | 
| 17 | 
            +
                Timeout.timeout(@timeout) do
         | 
| 18 | 
            +
                  if context.present?
         | 
| 19 | 
            +
                    context = ::Code.evaluate(context, timeout: @timeout)
         | 
| 20 | 
            +
                  else
         | 
| 21 | 
            +
                    context = ::Code::Object::Dictionnary.new
         | 
| 22 | 
            +
                  end
         | 
| 20 23 |  | 
| 21 | 
            -
             | 
| 24 | 
            +
                  ::Template::Node::Template.new(@parsed).evaluate(
         | 
| 25 | 
            +
                    context: context,
         | 
| 26 | 
            +
                    io: @io,
         | 
| 27 | 
            +
                  )
         | 
| 22 28 |  | 
| 23 | 
            -
             | 
| 29 | 
            +
                  @io.is_a?(StringIO) ? @io.string : nil
         | 
| 30 | 
            +
                end
         | 
| 24 31 | 
             
              end
         | 
| 25 32 | 
             
            end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
            require_relative "template/version"
         | 
    
        data/spec/code_spec.rb
    CHANGED
    
    
    
        data/spec/template_spec.rb
    CHANGED
    
    | @@ -12,9 +12,8 @@ RSpec.describe Template do | |
| 12 12 | 
             
                  '{ user: { first_name: "Dorian" } }',
         | 
| 13 13 | 
             
                  "Hello Dorian",
         | 
| 14 14 | 
             
                ],
         | 
| 15 | 
            -
                [ | 
| 16 | 
            -
             | 
| 17 | 
            -
                  TEMPLATE
         | 
| 15 | 
            +
                ["Hello {", "", "Hello "],
         | 
| 16 | 
            +
                ["", "", ""],
         | 
| 18 17 | 
             
              ].each do |(input, input_context, expected)|
         | 
| 19 18 | 
             
                context "#{input.inspect} #{input_context.inspect}" do
         | 
| 20 19 | 
             
                  let(:input) { input }
         | 
    
        data/template-ruby.gemspec
    CHANGED
    
    | @@ -1,8 +1,8 @@ | |
| 1 | 
            -
            require_relative "lib/template | 
| 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:: | 
| 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. | 
| 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- | 
| 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
         |