gtx 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +155 -0
  3. data/lib/gtx/version.rb +3 -0
  4. data/lib/gtx.rb +52 -0
  5. metadata +49 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dbf48aaa95d8b915bf90f634babcbf37ef5b95527000b84677f273cde39fa8a5
4
+ data.tar.gz: f95cae7c6a9b04b5de554a2600040fe943a628667d0601a2e8afdbf1745b8c4c
5
+ SHA512:
6
+ metadata.gz: a7929b4d3985d2a146dac93693f51a74ea78d8745505980fb717e0fbd4c32ad07b7ac5d60ea202a030ff742631666ea0a3b1cdc2c1607c6f192aeb913fa0ec4b
7
+ data.tar.gz: b9e3b4427d0d4f23fa6130cbf2bf73f531cf7fcc1f3465fa405852b434b7554c8d04fefda00725662e3124f2e685c543b5bed8025fa384864b11b0a63629e604
data/README.md ADDED
@@ -0,0 +1,155 @@
1
+ # GTX - Minimal Template Engine
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/gtx.svg)](https://badge.fury.io/rb/gtx)
4
+ [![Build Status](https://github.com/DannyBen/gtx/workflows/Test/badge.svg)](https://github.com/DannyBen/gtx/actions?query=workflow%3ATest)
5
+
6
+ ---
7
+
8
+ GTX is a minimal template engine that transpiles to ERB before using it to
9
+ generate the output.
10
+
11
+ As opposed to ERB, GTX is a code-first template engine - where Ruby code does
12
+ not need to be enclosed in tags. Instead, the output of the template needs to
13
+ be marked with a "Greater Than" sign (hence the name).
14
+
15
+ ## Install
16
+
17
+ ```bash
18
+ $ gem install gtx
19
+ ```
20
+
21
+ ## Template Syntax
22
+
23
+ ### Summary
24
+
25
+ GTX converts your template code to ERB before rendering its output.
26
+
27
+ <table>
28
+ <tr><th>GTX</th><th>ERB</th></tr>
29
+ <tr><td>
30
+
31
+ ```ruby
32
+ > any text
33
+ > inline code: {{ "hello " * 3 }}
34
+ > or: <%= "world " * 3 %>
35
+
36
+ 3.times do |i|
37
+ > loopy text {{ i + 1 }}
38
+ end
39
+
40
+ = user.welcome_message
41
+ ```
42
+
43
+ </td><td>
44
+
45
+ ```ruby
46
+ any text
47
+ inline code: <%= "hello " * 3 %>
48
+ or: <%= "world " * 3 %>
49
+
50
+ <%- 3.times do |i| -%>
51
+ loopy text <%= i + 1 %>
52
+ <%- end -%>
53
+
54
+ <%= user.welcome_message %>
55
+ ```
56
+
57
+ </td></tr></table>
58
+
59
+ The conversion is specifically kept at 1:1 ratio, so that the correct line number
60
+ can be referenced in case of an error.
61
+
62
+ ### Explanation
63
+
64
+ Lines starting with `>` are treated as output. Any number of spaces before and
65
+ one space after the `>` are ignored:
66
+
67
+ ```ruby
68
+ > this line will output as is
69
+ ```
70
+
71
+ Using `{{ ... }}` in an output line executes inline ruby code, as if it is
72
+ ERB's `<%= ... %>` syntax:
73
+
74
+ ```ruby
75
+ > any ruby code: {{ "hello " * 3 }}
76
+ ```
77
+
78
+ Lines starting with `=` can be used to execute ruby code that returns a string
79
+ that is expected to be a part of the output.
80
+
81
+ ```ruby
82
+ = user.welcome_message(:morning)
83
+ # which is a shortcut to:
84
+ > {{ user.welcome_message(:morning) }}
85
+ ```
86
+
87
+ Any other line, will be treated as Ruby code and will be enclosed using ERB's
88
+ `<%- ... -%>` syntax.
89
+
90
+ See the [example template](examples/full.rb) for additional nuances.
91
+
92
+ ## Usage
93
+
94
+ ### Using a GTX Instance
95
+
96
+ ```ruby
97
+ require 'gtx'
98
+
99
+ # Create an instance
100
+ path = "path/to/template_file"
101
+ template = File.read path
102
+ gtx = GTX.new template, filename: path
103
+
104
+ # Parse it with optional context
105
+ gtx.parse any_object
106
+
107
+ # ... or with local binding
108
+ gtx.parse binding
109
+
110
+ # Get the ERB source
111
+ gtx.erb_source
112
+
113
+ # ... or the ERB object
114
+ gtx.erb
115
+
116
+ ```
117
+
118
+ ### Class Shortcuts
119
+
120
+ ```ruby
121
+ require 'gtx'
122
+
123
+ # One-liner render template from file
124
+ GTX.render_file path
125
+
126
+ # ... with a context or Binding object
127
+ GTX.render_file path, context: any_object
128
+
129
+ # Get an instance, and parse later
130
+ gtx = GTX.load_file path
131
+ gtx.parse optional_context_object
132
+
133
+ # Render from string
134
+ GTX.render string, context: optional_object, filename: optional_filename
135
+ ```
136
+
137
+
138
+ ## But... why?
139
+
140
+ GTX was created to provide a code-first alternative to ERB, specifically for
141
+ the code generation templates used by [Bashly][bashly]. Enclosing Ruby code
142
+ inside ERB tags, and ensuring there are no excess empty lines in the ERB
143
+ template yielded some hard-to-maintain templates.
144
+
145
+
146
+ ## Contributing / Support
147
+
148
+ If you experience any issue, have a question or a suggestion, or if you wish
149
+ to contribute, feel free to [open an issue][issues].
150
+
151
+ ---
152
+
153
+ [issues]: https://github.com/DannyBen/gtx/issues
154
+ [bashly]: https://bashly.dannyb.co/
155
+
@@ -0,0 +1,3 @@
1
+ class GTX
2
+ VERSION = "0.1.0"
3
+ end
data/lib/gtx.rb ADDED
@@ -0,0 +1,52 @@
1
+ require 'erb'
2
+
3
+ class GTX
4
+ class << self
5
+ def render(template, context: nil, filename: nil)
6
+ new(template, filename: filename).parse context
7
+ end
8
+
9
+ def load_file(path, filename: nil)
10
+ new File.read(path), filename: (filename || path)
11
+ end
12
+
13
+ def render_file(path, context: nil, filename: nil)
14
+ load_file(path, filename: filename).parse context
15
+ end
16
+ end
17
+
18
+ attr_reader :template, :filename
19
+
20
+ def initialize(template, filename: nil)
21
+ @template, @filename = template, filename
22
+ end
23
+
24
+ def erb_source
25
+ template.each_line.map do |line|
26
+ case line
27
+ when /^\s*> ?(.*)/ then eval_vars $1
28
+ when /^\s*= ?(.*)/ then "<%= #{eval_vars $1.strip} %>"
29
+ else "<%- #{line.strip} -%>"
30
+ end
31
+ end.join "\n"
32
+ end
33
+
34
+ def erb
35
+ ERB.new(erb_source, trim_mode: '-').tap { |a| a.filename = filename }
36
+ end
37
+
38
+ def parse(context = nil)
39
+ context ||= self
40
+ context = context.instance_eval { binding } unless context.is_a? Binding
41
+ erb.result context
42
+ end
43
+
44
+ protected
45
+
46
+ def eval_vars(string)
47
+ string.gsub(/{{([^{].*?)}}/, '<%=\1%>')
48
+ .gsub(/\\\}\\\}/, '}}')
49
+ .gsub(/\\\{\\\{/, '{{')
50
+ end
51
+
52
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gtx
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Danny Ben Shitrit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-06-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Create templates that transpile to ERB
14
+ email: db@dannyben.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - lib/gtx.rb
21
+ - lib/gtx/version.rb
22
+ homepage: https://github.com/dannyben/gtx
23
+ licenses:
24
+ - MIT
25
+ metadata:
26
+ bug_tracker_uri: https://github.com/DannyBen/gtx/issues
27
+ changelog_uri: https://github.com/DannyBen/gtx/blob/master/CHANGELOG.md
28
+ homepage_uri: https://github.com/dannyben/gtx
29
+ source_code_uri: https://github.com/dannyben/gtx
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: 2.7.0
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubygems_version: 3.3.14
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: GTX Template Engine
49
+ test_files: []