mote-angular 1.1.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cad2d20f1e1a3e673b80aaa657efaf8d15761f54
4
+ data.tar.gz: 96b2a1df2a07719c3488a4b342056d90e4ba6ff7
5
+ SHA512:
6
+ metadata.gz: 66b1278f80bd56c89d9d22235a9ccfbc70f5266ce849331006c9d1af2e2aa284312554b4d4dad5f6c9afb2a4cc99cad7237a263b5039d2343d8b476c8f52649d
7
+ data.tar.gz: e380a3490d532a16fd9c4fdc605df1bf2e35dd416b7db52783356996d1171654df12de4d71725d31f8cc42f037d3cc1998a6eef86f71745cce10a69a834b4085
data/AUTHORS ADDED
@@ -0,0 +1,3 @@
1
+ * Bruno Deferrari (tizoc)
2
+ * Cyril David (cyx)
3
+ * Michel Martens (soveran)
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011-2015 Michel Martens
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
11
+ all 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
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,141 @@
1
+ Mote-Angular
2
+ ====
3
+
4
+ Minimum Operational Template for use with <a href="https://github.com/angular/angular">Angular</a>.
5
+
6
+ Description
7
+ -----------
8
+
9
+ <a href="https://github.com/soveran/mote">Mote</a> is a very simple and fast template engine.
10
+
11
+ Usage
12
+ -----
13
+
14
+ Usage is very similar to that of ERB:
15
+
16
+ ```ruby
17
+ template = Mote.parse("This is a template")
18
+ template.call #=> "This is a template"
19
+ ```
20
+
21
+ Silly example, you may say, and I would agree. What follows is a short list of
22
+ the different use cases you may face:
23
+
24
+ ```
25
+ % if user == "Bruno"
26
+ ~{user}~ rhymes with Piano
27
+ % elsif user == "Brutus"
28
+ ~{user}~ rhymes with Opus
29
+ % end
30
+ ```
31
+
32
+ ## Control flow
33
+
34
+ Lines that start with `%` are evaluated as Ruby code.
35
+
36
+ ## Assignment
37
+
38
+ Whatever it is between `~{` and `}~` gets printed in the template.
39
+ Note that this is different from the standard <a href="https://github.com/soveran/mote">Mote</a> gem.
40
+
41
+ ## Comments
42
+
43
+ There's nothing special about comments, it's just a `#` inside your Ruby code:
44
+
45
+ ```
46
+ % # This is a comment.
47
+ ```
48
+
49
+
50
+ ## Block evaluation
51
+
52
+ As with control instructions, it happens naturally:
53
+
54
+ ```
55
+ % 3.times do |i|
56
+ ~{i}~
57
+ % end
58
+ ```
59
+
60
+ ## Parameters
61
+
62
+ The values passed to the template are available as local variables:
63
+
64
+ ```ruby
65
+ example = Mote.parse("Hello ~{name}~", self, [:name])
66
+ assert_equal "Hello world", example.call(name: "world")
67
+ assert_equal "Hello Bruno", example.call(name: "Bruno")
68
+ ```
69
+
70
+ Please note that the keys in the parameters hash must be symbols.
71
+
72
+ # Helpers
73
+
74
+ There's a helper available in the `Mote::Helpers` module, and you are
75
+ free to include it in your code. To do it, just type:
76
+
77
+ ```ruby
78
+ include Mote::Helpers
79
+ ```
80
+
81
+ ### Using the mote helper
82
+
83
+ The `mote` helper receives a file name and a hash and returns the rendered
84
+ version of its content. The compiled template is cached for subsequent calls.
85
+
86
+ ```ruby
87
+ assert_equal "***\n", mote("test/basic.mote", n: 3)
88
+ ```
89
+
90
+ ### Template caching
91
+
92
+ When the `mote` helper is first called with a template name, the
93
+ file is read and parsed, and a proc is created and stored in the
94
+ current thread. The parameters passed are defined as local
95
+ variables in the template. If you want to provide more parameters
96
+ once the template was cached, you won't be able to access the
97
+ values as local variables, but you can always access the `params`
98
+ hash.
99
+
100
+ For example:
101
+
102
+ ```ruby
103
+ # First call
104
+ mote("foo.mote", a: 1, b: 2)
105
+ ```
106
+
107
+ ## Command line tool
108
+
109
+ Mote ships with a command line tool to render mote templates. The
110
+ result is redirected to standard output.
111
+
112
+ ```
113
+ mote FILE [param1 value1 ... paramN valueN]
114
+ ```
115
+
116
+ The extra parameters supplied will be passed to the template.
117
+ Note that all the parameter values will be treated as strings.
118
+
119
+ ### Example usage
120
+
121
+ If your template is called foo.mote, you can render it with the
122
+ following command:
123
+
124
+ mote foo.mote
125
+
126
+ To write the result to a new file, just redirect the output:
127
+
128
+ mote foo.mote > foo.html
129
+
130
+ If the template uses a local variable `bar`, you can pass a
131
+ value from the command line:
132
+
133
+ mote foo.mote bar 42
134
+
135
+ ## Installation
136
+
137
+ You can install it using rubygems.
138
+
139
+ ```
140
+ $ gem install mote
141
+ ```
data/bin/mote-angular ADDED
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ help = <<-EOS
4
+ MOTE(1)
5
+
6
+ NAME
7
+ mote -- Minimum Operational Template
8
+
9
+ SYNOPSIS
10
+ mote FILE [param1 value1 ... paramN valueN]
11
+
12
+ DESCRIPTION
13
+ Use this command line tool to render mote templates. The result
14
+ is redirected to standard output.
15
+
16
+ The extra parameters supplied will be passed to the template.
17
+ Note that all the parameter values will be treated as strings.
18
+
19
+ EXAMPLES
20
+ If your template is called foo.mote, you can render it with the
21
+ following command:
22
+
23
+ mote foo.mote
24
+
25
+ To write the result to a new file, just redirect the output:
26
+
27
+ mote foo.mote > foo.html
28
+
29
+ If the template uses a local variable `bar`, you can pass a
30
+ value from the command line:
31
+
32
+ mote foo.mote bar 42
33
+
34
+ SEE ALSO
35
+ https://github.com/soveran/mote
36
+ EOS
37
+
38
+ if ARGV.size.even?
39
+ puts help
40
+ exit
41
+ end
42
+
43
+ t = ARGV.shift
44
+ p = Hash[*ARGV]
45
+
46
+ require "mote-angular"
47
+
48
+ include Mote::Helpers
49
+
50
+ print mote(t, p)
@@ -0,0 +1,62 @@
1
+ # Copyright (c) 2011-2015 Michel Martens
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
11
+ # all 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
19
+ # THE SOFTWARE.
20
+ class Mote
21
+ VERSION = "1.1.3"
22
+
23
+ PATTERN = /^(\n)|^\s*(%)\s*(.*?)(?:\n|\Z)|(<\?)\s+(.*?)\s+\?>|(~\{)(.*?)\}~/m
24
+
25
+ def self.parse(template, context = self, vars = [])
26
+ terms = template.split(PATTERN)
27
+
28
+ parts = "Proc.new do |params, __o|\n params ||= {}; __o ||= ''\n"
29
+
30
+ vars.each do |var|
31
+ parts << "%s = params[%s]\n" % [var, var.inspect]
32
+ end
33
+
34
+ while term = terms.shift
35
+ case term
36
+ when "<?" then parts << "#{terms.shift}\n"
37
+ when "%" then parts << "#{terms.shift}\n"
38
+ when "~{" then parts << "__o << (#{terms.shift}).to_s\n"
39
+ else parts << "__o << #{term.dump}\n"
40
+ end
41
+ end
42
+
43
+ parts << "__o; end"
44
+
45
+ compile(context, parts)
46
+ end
47
+
48
+ def self.compile(context, parts)
49
+ context.instance_eval(parts)
50
+ end
51
+
52
+ module Helpers
53
+ def mote(file, params = {}, context = self)
54
+ mote_cache[file] ||= Mote.parse(File.read(file), context, params.keys)
55
+ mote_cache[file][params]
56
+ end
57
+
58
+ def mote_cache
59
+ Thread.current[:_mote_cache] ||= {}
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,22 @@
1
+ require "./lib/mote-angular"
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "mote-angular"
5
+ s.version = Mote::VERSION
6
+ s.summary = "Minimum Operational Template for use with Angular."
7
+ s.description = "Mote is a very simple and fast template engine."
8
+ s.authors = ["Michel Martens"]
9
+ s.email = ["michel@soveran.com"]
10
+ s.homepage = "http://github.com/ErikAGriffin/mote-angular/"
11
+ s.files = Dir[
12
+ "LICENSE",
13
+ "AUTHORS",
14
+ "README.md",
15
+ "Rakefile",
16
+ "lib/**/*.rb",
17
+ "*.gemspec",
18
+ "test/**/*.rb"
19
+ ]
20
+ s.executables.push("mote-angular")
21
+ s.add_development_dependency "cutest"
22
+ end
data/test/mote_test.rb ADDED
@@ -0,0 +1,159 @@
1
+ require File.expand_path("../lib/mote", File.dirname(__FILE__))
2
+
3
+ scope do
4
+ test "empty lines" do
5
+ example = Mote.parse("\n\n \n")
6
+ assert_equal "\n\n \n", example.call
7
+ end
8
+
9
+ test "empty lines with mixed code" do
10
+ example = Mote.parse("\n% true\n\n% false\n\n")
11
+ assert_equal "\n\n\n", example.call
12
+ end
13
+
14
+ test "empty lines with control flow" do
15
+ example = Mote.parse("\n% if true\n\n\n% else\n\n% end\n")
16
+ assert_equal "\n\n\n", example.call
17
+ end
18
+
19
+ test "control flow without final newline" do
20
+ example = Mote.parse("\n% if true\n\n\n% else\n\n% end")
21
+ assert_equal "\n\n\n", example.call
22
+ end
23
+
24
+ test "assignment" do
25
+ example = Mote.parse("~{ \"***\" }~")
26
+ assert_equal "***", example.call
27
+ end
28
+
29
+ test "comment" do
30
+ template = (<<-EOT).gsub(/ {4}/, "")
31
+ *
32
+ % # "*"
33
+ *
34
+ EOT
35
+
36
+ example = Mote.parse(template)
37
+ assert_equal "*\n*\n", example.call.squeeze("\n")
38
+ end
39
+
40
+ test "control flow" do
41
+ template = (<<-EOT).gsub(/ {4}/, "")
42
+ % if false
43
+ *
44
+ % else
45
+ ***
46
+ % end
47
+ EOT
48
+
49
+ example = Mote.parse(template)
50
+ assert_equal " ***\n", example.call
51
+ end
52
+
53
+ test "block evaluation" do
54
+ template = (<<-EOT).gsub(/ {4}/, "")
55
+ % 3.times {
56
+ *
57
+ % }
58
+ EOT
59
+
60
+ example = Mote.parse(template)
61
+ assert_equal "*\n*\n*\n", example.call
62
+ end
63
+
64
+ test "parameters" do
65
+ template = (<<-EOT).gsub(/ {4}/, "")
66
+ % params[:n].times {
67
+ *
68
+ % }
69
+ EOT
70
+
71
+ example = Mote.parse(template)
72
+ assert_equal "*\n*\n*\n", example[:n => 3]
73
+ assert_equal "*\n*\n*\n*\n", example[:n => 4]
74
+ end
75
+
76
+ test "multiline" do
77
+ example = Mote.parse("The\nMan\nAnd\n~{\"The\"}~\nSea")
78
+ assert_equal "The\nMan\nAnd\nThe\nSea", example[:n => 3]
79
+ end
80
+
81
+ test "quotes" do
82
+ example = Mote.parse("'foo' 'bar' 'baz'")
83
+ assert_equal "'foo' 'bar' 'baz'", example.call
84
+ end
85
+
86
+ test "context" do
87
+ context = Object.new
88
+ def context.user; "Bruno"; end
89
+
90
+ example = Mote.parse("~{ context.user }~", context, [:context])
91
+ assert_equal "Bruno", example.call(context: context)
92
+ end
93
+
94
+ test "locals" do
95
+ example = Mote.parse("~{ user }~", TOPLEVEL_BINDING, [:user])
96
+ assert_equal "Bruno", example.call(user: "Bruno")
97
+ end
98
+
99
+ test "nil" do
100
+ example = Mote.parse("~{ user }~", TOPLEVEL_BINDING, [:user])
101
+ assert_equal "", example.call(user: nil)
102
+ end
103
+
104
+ test "curly bug" do
105
+ example = Mote.parse("~{ [1, 2, 3].map { |i| i * i }.join(',') }~")
106
+ assert_equal "1,4,9", example.call
107
+ end
108
+
109
+ test "multi-line XML-style directives" do
110
+ template = (<<-EOT).gsub(/^ /, "")
111
+ <? res = ""
112
+ [1, 2, 3].each_with_index do |item, idx|
113
+ res << "%d. %d\n" % [idx + 1, item * item]
114
+ end
115
+ ?>
116
+ ~{ res }~
117
+ EOT
118
+
119
+ example = Mote.parse(template)
120
+ assert_equal "\n1. 1\n2. 4\n3. 9\n\n", example.call
121
+ end
122
+
123
+ test "preserve XML directives" do
124
+ template = (<<-EOT).gsub(/^ /, "")
125
+ <?xml "hello" ?>
126
+ EOT
127
+
128
+ example = Mote.parse(template)
129
+ assert_equal "<?xml \"hello\" ?>\n", example.call
130
+ end
131
+ end
132
+
133
+ include Mote::Helpers
134
+
135
+ class Cutest::Scope
136
+ def foo
137
+ "foo"
138
+ end
139
+ end
140
+
141
+ scope do
142
+ prepare do
143
+ mote_cache.clear
144
+ end
145
+
146
+ test "helpers" do
147
+ assert_equal " *\n *\n *\n", mote("test/basic.mote", :n => 3)
148
+ end
149
+
150
+ test "using functions in the context" do
151
+ assert_equal "foo\n", mote("test/foo.mote")
152
+ end
153
+
154
+ test "passing in a context" do
155
+ assert_raise NameError do
156
+ mote("test/foo.mote", {}, TOPLEVEL_BINDING)
157
+ end
158
+ end
159
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mote-angular
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Michel Martens
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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Mote is a very simple and fast template engine.
28
+ email:
29
+ - michel@soveran.com
30
+ executables:
31
+ - mote-angular
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - AUTHORS
36
+ - LICENSE
37
+ - README.md
38
+ - bin/mote-angular
39
+ - lib/mote-angular.rb
40
+ - mote-angular.gemspec
41
+ - test/mote_test.rb
42
+ homepage: http://github.com/ErikAGriffin/mote-angular/
43
+ licenses: []
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 2.4.5
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Minimum Operational Template for use with Angular.
65
+ test_files: []