Skip to content

Commit 761ab76

Browse files
committed
Minor updates inspired by my work on:
https://codalyzed.com/videos/lesscode The changes are: * Improving the `PATTERN` regex to handle more edge cases * Documenting the third type of template interpolation * A couple of minor style updates to the library code * Some minor clarifications in the tests
1 parent afb54ac commit 761ab76

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,26 @@ Silly example, you may say, and I would agree. What follows is a short list of
2222
the different use cases you may face:
2323

2424
```
25+
% # This is a comment
2526
% if user == "Bruno"
2627
{{user}} rhymes with Piano
2728
% elsif user == "Brutus"
2829
{{user}} rhymes with Opus
2930
% end
31+
32+
<?
33+
# Multiline code evaluation
34+
lucky = [1, 3, 7, 9, 13, 15]
35+
prime = [2, 3, 5, 7, 11, 13]
36+
?>
37+
38+
{{ lucky & prime }}
3039
```
3140

3241
## Control flow
3342

34-
Lines that start with `%` are evaluated as Ruby code.
43+
Lines that start with `%` are evaluated as Ruby code. Anything between
44+
`<?` and `?>`, including new lines, is also evaluated as Ruby code.
3545

3646
## Assignment
3747

@@ -45,7 +55,6 @@ There's nothing special about comments, it's just a `#` inside your Ruby code:
4555
% # This is a comment.
4656
```
4757

48-
4958
## Block evaluation
5059

5160
As with control instructions, it happens naturally:

lib/mote.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@
2020
class Mote
2121
VERSION = "1.1.3"
2222

23-
PATTERN = /^(\n)|^\s*(%)\s*(.*?)(?:\n|\Z)|(<\?)\s+(.*?)\s+\?>|(\{\{)(.*?)\}\}/m
23+
PATTERN = /^[^\S\n]*(%)[^\S\n]*(.*?)(?:\n|\Z)|(<\?)\s+(.*?)\s+\?>|(\{\{)(.*?)\}\}/m
2424

2525
def self.parse(template, context = self, vars = [])
2626
terms = template.split(PATTERN)
2727

2828
parts = "Proc.new do |params, __o|\n params ||= {}; __o ||= ''\n"
2929

3030
vars.each do |var|
31-
parts << "%s = params[%s]\n" % [var, var.inspect]
31+
parts << "%s = params[%p]\n" % [var, var]
3232
end
3333

34-
while term = terms.shift
34+
while (term = terms.shift)
3535
case term
3636
when "<?" then parts << "#{terms.shift}\n"
3737
when "%" then parts << "#{terms.shift}\n"

test/mote_test.rb

+11-15
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
end
2323

2424
test "assignment" do
25-
example = Mote.parse("{{ \"***\" }}")
25+
example = Mote.parse("{{ '***' }}")
2626
assert_equal "***", example.call
2727
end
2828

@@ -75,7 +75,7 @@
7575

7676
test "multiline" do
7777
example = Mote.parse("The\nMan\nAnd\n{{\"The\"}}\nSea")
78-
assert_equal "The\nMan\nAnd\nThe\nSea", example[:n => 3]
78+
assert_equal "The\nMan\nAnd\nThe\nSea", example.call
7979
end
8080

8181
test "quotes" do
@@ -87,8 +87,8 @@
8787
context = Object.new
8888
def context.user; "Bruno"; end
8989

90-
example = Mote.parse("{{ context.user }}", context, [:context])
91-
assert_equal "Bruno", example.call(context: context)
90+
example = Mote.parse("{{ user }}", context)
91+
assert_equal "Bruno", example.call
9292
end
9393

9494
test "locals" do
@@ -101,23 +101,19 @@ def context.user; "Bruno"; end
101101
assert_equal "", example.call(user: nil)
102102
end
103103

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-
109104
test "multi-line XML-style directives" do
110105
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
106+
<?
107+
# Multiline code evaluation
108+
lucky = [1, 3, 7, 9, 13, 15]
109+
prime = [2, 3, 5, 7, 11, 13]
115110
?>
116-
{{ res }}
111+
112+
{{ lucky & prime }}
117113
EOT
118114

119115
example = Mote.parse(template)
120-
assert_equal "\n1. 1\n2. 4\n3. 9\n\n", example.call
116+
assert_equal "\n\n[3, 7, 13]\n", example.call
121117
end
122118

123119
test "preserve XML directives" do

0 commit comments

Comments
 (0)