ate 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +106 -0
  4. data/ate.gemspec +20 -0
  5. data/lib/ate.rb +61 -0
  6. data/test/test_ate.rb +162 -0
  7. metadata +63 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 595c68ba147fc5854c6c36eefd387077bde7cede
4
+ data.tar.gz: a6e6ca6a1d8dd8e6e5f2c8c999341d562b86c308
5
+ SHA512:
6
+ metadata.gz: dfc3f28a5e42a4e3528aab9308d5b7d2bdb76aa1ae4a503bb198fa001ea0dc9bba02c5f9f6f448753621ec560f931f2ebc0e6c3f79f355ce3bd62ef5572b57ec
7
+ data.tar.gz: 3d44504bf9dd171ff30dc9e3423c997609993be992d915db13851ae4f4e907060b1e73b032da8c0bc8e344edf7f5297f0f7043c9918fd6039bd036b1c2eacd8c
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Julio Lopez
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.
@@ -0,0 +1,106 @@
1
+ Ate
2
+ ====
3
+
4
+ Atractive Template Engine for minimalist people
5
+
6
+ ## Installation
7
+
8
+ ```
9
+ $ gem install ate
10
+ ```
11
+
12
+ Usage
13
+ -----
14
+
15
+ ```ruby
16
+ template = Ate.parse("Hello World")
17
+ template.render #=> "Hello World"
18
+ ```
19
+
20
+ ## Ruby code
21
+
22
+ Lines that start with `%` are evaluated as Ruby code.
23
+
24
+ ```
25
+ % if true
26
+ Hi
27
+ % else
28
+ No, I won't display me
29
+ % end
30
+ ```
31
+
32
+ As this is ruby code, you can comment as you has always done
33
+
34
+ ```
35
+ % # I'm a comment.
36
+ ```
37
+
38
+ And you can still doing any ruby thing: blocks, loops, etc.
39
+
40
+ ```
41
+ % 3.times do |i|
42
+ {{i}}
43
+ % end
44
+ ```
45
+
46
+ ## Assignment
47
+
48
+ To print a variable just use `{{` and `}}`
49
+
50
+ ## Send Variables
51
+
52
+ Send a variables as a hash in the parse method to the template so it can get them:
53
+
54
+ ```ruby
55
+ template = Ate.parse("Hello, this is {{user}}", user: "dog")
56
+ template.render #=> "Hello, this is dog"
57
+ ```
58
+
59
+ Also, you can send other kinds of variables:
60
+
61
+ ```ruby
62
+ template = <<-EOT
63
+ % items.each do |item|
64
+ {{ item }}
65
+ % end
66
+ EOT
67
+ parsed = Ate.parse(template, items: ["a", "b", "c"])
68
+ parsed.render #=> "a\nb\n\c"
69
+ ```
70
+
71
+ You can even take advantage of do whatever operation inside the `{{ }}`
72
+
73
+ ```ruby
74
+ template = Ate.parse("The new price is: {{ price + 10 }}", price: 30)
75
+ template.render #=> "The new price is: 40"
76
+ ```
77
+
78
+ ## Contexts
79
+
80
+ For send a particular context to your template, use the context key
81
+
82
+ ```ruby
83
+ user = User.new "Julio"
84
+ puts user.name #=> "Julio"
85
+ template = Ate.parse("Hi, I'm {{ context.name }}", context: user)
86
+ template.render #=> "Hi, I'm Julio"
87
+ ```
88
+
89
+ ## Using files
90
+
91
+ Declare you file with .ate extension in the parse method
92
+
93
+ ```ruby
94
+ template = Ate.parse("example.ate")
95
+ ```
96
+
97
+ Feel free to use any markup language like HTML
98
+ ```ruby
99
+ template = <<-EOT
100
+ <h1>{{ main_title }}</h1>
101
+ % posts.each do |post|
102
+ <article>...</article>
103
+ % end
104
+ EOT
105
+ parsed = Ate.parse(template, main_title: "h1 title!", posts: array_of_posts)
106
+ ```
@@ -0,0 +1,20 @@
1
+ require "./lib/ate"
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "ate"
5
+ s.version = Ate::VERSION
6
+ s.summary = "Atractive Template Engine for minimalist people."
7
+ s.description = "Ate is a minimalist and fast template engine."
8
+ s.authors = ["Julio Lopez"]
9
+ s.email = ["ljuliom@gmail.com"]
10
+ s.homepage = "http://github.com/TheBlasfem/ate"
11
+ s.files = Dir[
12
+ "LICENSE",
13
+ "README.md",
14
+ "lib/**/*.rb",
15
+ "*.gemspec",
16
+ "test/**/*.rb"
17
+ ]
18
+ s.license = "MIT"
19
+ s.add_development_dependency "cutest", "1.1.3"
20
+ end
@@ -0,0 +1,61 @@
1
+ # Copyright (c) 2015 Julio Lopez
2
+
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+ class Ate
21
+ VERSION = "1.0.0"
22
+
23
+ class << self
24
+ def parse(template, vars = {})
25
+ context = vars.fetch(:context, self)
26
+ lines = template.end_with?(".ate") ? File.read(template) : template
27
+ lines = lines.split("\n")
28
+ built = build_proc(lines, vars)
29
+ @parsed = context.instance_eval(built)
30
+ self
31
+ end
32
+
33
+ def build_proc(lines, vars)
34
+ @output = "Proc.new do \n output = \"\" \n "
35
+ declaring_local_variables(vars)
36
+ parsing_lines(lines)
37
+ @output << "output \n end"
38
+ end
39
+
40
+ def declaring_local_variables(vars)
41
+ vars.each do |x, y|
42
+ value = y.is_a?(String) ? "\"#{y}\"" : y
43
+ @output << "#{x} = #{value}\n"
44
+ end
45
+ end
46
+
47
+ def parsing_lines(lines)
48
+ lines.each do |line|
49
+ if line =~ /^\s*(%)(.*?)$/
50
+ @output << "#{line.gsub(/^\s*%(.*?)$/, '\1') } \n"
51
+ else
52
+ @output << "output << %Q|#{line.gsub(/\{\{([^\r\n]*)\}\}/, '#{\1}')}\n| \n "
53
+ end
54
+ end
55
+ end
56
+
57
+ def render
58
+ @parsed.call
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,162 @@
1
+ require File.expand_path("../lib/ate", File.dirname(__FILE__))
2
+
3
+ scope do
4
+ test "returning same number of empty lines" do
5
+ parsed = Ate.parse("\n \n\n \n")
6
+ assert_equal "\n \n\n \n", parsed.render
7
+ end
8
+ test "returning same number of empty lines with code" do
9
+ parsed = Ate.parse("\n% false\n% true\n\n%true")
10
+ assert_equal "\n\n", parsed.render
11
+ end
12
+
13
+ test "empty lines with conditional" do
14
+ parsed = Ate.parse("\n% if true\n\n\n% else\n\n% end\n")
15
+ assert_equal "\n\n\n", parsed.render
16
+ end
17
+
18
+ test "sending nil variable" do
19
+ parsed = Ate.parse("{{ name }}", name: nil)
20
+ assert_equal "\n", parsed.render
21
+ end
22
+
23
+ test "printing string" do
24
+ parsed = Ate.parse('{{ "Hello World!" }}')
25
+ assert_equal "Hello World!\n", parsed.render
26
+ end
27
+
28
+ test "running ruby code in display" do
29
+ parsed = Ate.parse("{{ 2*2 }}")
30
+ assert_equal "4\n", parsed.render
31
+ end
32
+
33
+ test "running an enumerator inside of {{}}" do
34
+ example = Ate.parse("{{ [1, 2, 3].map { |i| i * i }.join(',') }}")
35
+ assert_equal "1,4,9\n", example.render
36
+ end
37
+
38
+ test "comment" do
39
+ template = (<<-EOT).gsub(/ /, "")
40
+ Awesome
41
+ % # i'm a comment
42
+ ATE
43
+ EOT
44
+
45
+ parsed = Ate.parse(template)
46
+ assert_equal "Awesome\nATE\n", parsed.render
47
+ end
48
+
49
+ test "respecting first empty spaces of lines" do
50
+ template = " Hi, Juan\n Bye, I don't have time for this."
51
+ parsed = Ate.parse(template)
52
+ assert_equal " Hi, Juan\n Bye, I don't have time for this.\n", parsed.render
53
+ end
54
+
55
+ test "conditional operation" do
56
+ template = (<<-EOT).gsub(/ {4}/, "")
57
+ % if true
58
+ I'll display to you
59
+ % else
60
+ I won't display, sorry
61
+ % end
62
+ EOT
63
+
64
+ parsed = Ate.parse(template)
65
+ assert_equal " I'll display to you\n", parsed.render
66
+ end
67
+
68
+ test "running a block" do
69
+ template = (<<-EOT).gsub(/ {4}/, "")
70
+ % 3.times do
71
+ Beetlejuice
72
+ % end
73
+ EOT
74
+
75
+ parsed = Ate.parse(template)
76
+ assert_equal " Beetlejuice\n Beetlejuice\n Beetlejuice\n", parsed.render
77
+ end
78
+
79
+ test "int variables" do
80
+ template = (<<-EOT).gsub(/ {4}/, "")
81
+ % number.times {
82
+ Pika {{type}}
83
+ % }
84
+ EOT
85
+
86
+ parsed = Ate.parse(template, number: 3, type: 1000)
87
+ number = 3
88
+ assert_equal "Pika 1000\nPika 1000\nPika 1000\n", parsed.render
89
+ end
90
+
91
+ test "string variables" do
92
+ parsed = Ate.parse("Hello {{name}}", name: "Julio")
93
+ assert_equal "Hello Julio\n", parsed.render
94
+ end
95
+
96
+ test "mixing int and str variables" do
97
+ template = (<<-EOT).gsub(/ {4}/, "")
98
+ % n.times {
99
+ {{ pokemon_name }}
100
+ % }
101
+ EOT
102
+
103
+ parsed = Ate.parse(template, n: 3, pokemon_name: "Pikachu")
104
+ assert_equal "Pikachu\nPikachu\nPikachu\n", parsed.render
105
+ end
106
+
107
+ test "recorring an array" do
108
+ parsed = Ate.parse("% items.each do |item|\n{{item}}\n% end", items: ["a", "b", "c"])
109
+ assert_equal "a\nb\nc\n", parsed.render
110
+ end
111
+
112
+ test "case with multiple lines" do
113
+ parsed = Ate.parse("Better\nCall\nSaul\n!")
114
+ assert_equal "Better\nCall\nSaul\n!\n", parsed.render
115
+ end
116
+
117
+ test "with quotes" do
118
+ parsed = Ate.parse("'this' 'awesome' 'quote'")
119
+ assert_equal "'this' 'awesome' 'quote'\n", parsed.render
120
+ end
121
+
122
+ test "with double quotes" do
123
+ parsed = Ate.parse("Hi, i'm testing \"double\"\"quotes\"")
124
+ assert_equal "Hi, i'm testing \"double\"\"quotes\"\n", parsed.render
125
+ end
126
+
127
+ test "in a particular context" do
128
+ class User
129
+ attr_accessor :name
130
+ end
131
+ user = User.new
132
+ user.name = "Julio"
133
+ parsed = Ate.parse("{{ context.name }}", context: user)
134
+ assert_equal "Julio\n", parsed.render
135
+ end
136
+
137
+ test "with html tags" do
138
+ template = (<<-EOT).gsub(/ {4}/, "")
139
+ <html>
140
+ <head>
141
+ <title>{{ title }}</title>
142
+ </head>
143
+ </html>
144
+ EOT
145
+
146
+ parsed = Ate.parse(template, title: "Cool Site!")
147
+ assert_equal "<html>\n <head>\n<title>Cool Site!</title>\n </head>\n</html>\n", parsed.render
148
+ end
149
+
150
+ test "soporting XML" do
151
+ template = (<<-EOT).gsub(/ {4}/, "")
152
+ <?xml "hello xml" ?>
153
+ EOT
154
+
155
+ parsed = Ate.parse(template)
156
+ assert_equal "<?xml \"hello xml\" ?>\n", parsed.render
157
+ end
158
+
159
+ test "loading a file" do
160
+ assert_equal " Beetlejuice\n Beetlejuice\n Beetlejuice\n", Ate.parse("test/example.ate").render
161
+ end
162
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ate
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Julio Lopez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cutest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.1.3
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.1.3
27
+ description: Ate is a minimalist and fast template engine.
28
+ email:
29
+ - ljuliom@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - ate.gemspec
37
+ - lib/ate.rb
38
+ - test/test_ate.rb
39
+ homepage: http://github.com/TheBlasfem/ate
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.2.2
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Atractive Template Engine for minimalist people.
63
+ test_files: []