slim 0.8.3 → 0.8.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +5 -0
- data/lib/slim/compiler.rb +11 -3
- data/lib/slim/engine.rb +25 -5
- data/lib/slim/filter.rb +4 -0
- data/lib/slim/parser.rb +16 -19
- data/lib/slim/sections.rb +6 -9
- data/lib/slim/version.rb +1 -1
- data/lib/slim/wrapper.rb +6 -12
- metadata +7 -7
data/README.md
CHANGED
@@ -233,6 +233,10 @@ Here's a quick example to demonstrate what a Slim template looks like:
|
|
233
233
|
h1 id="headline"
|
234
234
|
== page_headline
|
235
235
|
|
236
|
+
Alternatively, if you prefer to use single equal sign, you may do so by setting the `auto_escape` option to false.
|
237
|
+
|
238
|
+
Slim::Engine.default_options[:auto_escape] = false
|
239
|
+
|
236
240
|
### Treat multiple lines of code as text that should bypass parsing
|
237
241
|
|
238
242
|
Use a pipe (`|`) or single quote (`` ' ``) to start the escape.
|
@@ -332,5 +336,6 @@ This project is released under the MIT license.
|
|
332
336
|
## Slim related projects
|
333
337
|
|
334
338
|
* [Textmate bundle](https://github.com/fredwu/ruby-slim-textmate-bundle)
|
339
|
+
* [Haml2Slim converter](https://github.com/fredwu/haml2slim)
|
335
340
|
* [Rails 3 Generators](https://github.com/leogalmeida/slim-rails)
|
336
341
|
* [Slim for Clojure](https://github.com/chaslemley/slim.clj)
|
data/lib/slim/compiler.rb
CHANGED
@@ -2,7 +2,7 @@ module Slim
|
|
2
2
|
# Compiles Slim expressions into Temple::HTML expressions.
|
3
3
|
# @api private
|
4
4
|
class Compiler < Filter
|
5
|
-
set_default_options :
|
5
|
+
set_default_options :auto_escape => true
|
6
6
|
|
7
7
|
# Handle control expression `[:slim, :control, code, content]`
|
8
8
|
#
|
@@ -15,6 +15,14 @@ module Slim
|
|
15
15
|
compile!(content)]
|
16
16
|
end
|
17
17
|
|
18
|
+
# Handle comment expression `[:slim, :comment, content]`
|
19
|
+
#
|
20
|
+
# @param [Array] content Temple expression
|
21
|
+
# @return [Array] Compiled temple expression
|
22
|
+
def on_slim_comment(content)
|
23
|
+
[:html, :comment, compile!(content)]
|
24
|
+
end
|
25
|
+
|
18
26
|
# Handle output expression `[:slim, :output, escape, code, content]`
|
19
27
|
#
|
20
28
|
# @param [Boolean] escape Escape html
|
@@ -23,7 +31,7 @@ module Slim
|
|
23
31
|
# @return [Array] Compiled temple expression
|
24
32
|
def on_slim_output(escape, code, content)
|
25
33
|
if empty_exp?(content)
|
26
|
-
[:multi, escape ? [:escape, :dynamic, code] : [:dynamic, code], content]
|
34
|
+
[:multi, escape && options[:auto_escape] ? [:escape, :dynamic, code] : [:dynamic, code], content]
|
27
35
|
else
|
28
36
|
on_slim_output_block(escape, code, content)
|
29
37
|
end
|
@@ -66,7 +74,7 @@ module Slim
|
|
66
74
|
# @param [String] type Directive type
|
67
75
|
# @return [Array] Compiled temple expression
|
68
76
|
def on_slim_directive(type)
|
69
|
-
if type =~ /^doctype/
|
77
|
+
if type =~ /^doctype/i
|
70
78
|
[:html, :doctype, $'.strip]
|
71
79
|
end
|
72
80
|
end
|
data/lib/slim/engine.rb
CHANGED
@@ -7,26 +7,46 @@ module Slim
|
|
7
7
|
# # Indent html for pretty debugging
|
8
8
|
# Slim::Engine.set_default_options :pretty => true
|
9
9
|
#
|
10
|
+
# This overwrites some temple default options.
|
10
11
|
set_default_options :pretty => false,
|
11
12
|
:attr_wrapper => '"',
|
12
13
|
:format => :html5,
|
13
14
|
:id_delimiter => nil,
|
14
|
-
:generator => Temple::Generators::ArrayBuffer
|
15
|
-
:disable_capture => false,
|
16
|
-
:debug => false
|
15
|
+
:generator => Temple::Generators::ArrayBuffer
|
17
16
|
|
17
|
+
# Document all supported options with purpose, type etc.
|
18
|
+
#
|
19
|
+
# Type | Name | Default value | Purpose
|
20
|
+
# --------------------------------------------------------------------------------------------------------------------------------------------
|
21
|
+
# String | :file | nil | Name of parsed file, set automatically by Slim::Template
|
22
|
+
# Integer | :tabsize | 4 | Number of whitespaces per tab (used by the parser)
|
23
|
+
# String list | :enable_engines | All enabled | List of enabled embedded engines (whitelist)
|
24
|
+
# String list | :disable_engines | None disabled | List of disabled embedded engines (blacklist)
|
25
|
+
# Boolean | :sections | false | Enable sections mode (logic-less)
|
26
|
+
# String | :dictionary | "self" | Name of dictionary variable in sections mode
|
27
|
+
# Symbol | :dictionary_access | :wrapped | Access mode of dictionary variable (:wrapped, :symbol, :string)
|
28
|
+
# Boolean | :disable_capture | false (true in Rails) | Disable capturing in blocks (blocks write to the default buffer then)
|
29
|
+
# Boolean | :auto_escape | true | Enable automatic escaping of strings
|
30
|
+
# Boolean | :use_html_safe | false (true in Rails) | Use String#html_safe? from ActiveSupport (Works together with :auto_escape)
|
31
|
+
# Boolean | :debug | false | Enable debug outputs (Temple internals)
|
32
|
+
# Symbol | :format | :html5 | HTML output format
|
33
|
+
# String | :attr_wrapper | '"' | Character to wrap attributes in html (can be ' or ")
|
34
|
+
# String | :id_delimiter | '_' | Joining character used if multiple html ids are supplied (e.g. #id1#id2)
|
35
|
+
# Boolean | :pretty | false | Pretty html indenting (This is slower!)
|
36
|
+
# Class | :generator | ArrayBuffer/RailsOutputBuffer | Temple code generator (defaults generates array buffer)
|
18
37
|
use Slim::Parser, :file, :tabsize
|
19
38
|
use Slim::EmbeddedEngine, :enable_engines, :disable_engines
|
20
39
|
use Slim::Interpolation
|
21
40
|
use Slim::Sections, :sections, :dictionary, :dictionary_access
|
22
41
|
use Slim::EndInserter
|
23
|
-
use Slim::Compiler, :disable_capture
|
42
|
+
use Slim::Compiler, :disable_capture, :auto_escape
|
24
43
|
filter :EscapeHTML, :use_html_safe
|
44
|
+
filter :Debugger, :debug, :debug_prefix => 'After Slim'
|
25
45
|
use Temple::HTML::Pretty, :format, :attr_wrapper, :id_delimiter, :pretty
|
26
46
|
filter :MultiFlattener
|
27
47
|
filter :StaticMerger
|
28
48
|
filter :DynamicInliner
|
29
|
-
filter :Debugger, :debug, :debug_prefix
|
49
|
+
filter :Debugger, :debug, :debug_prefix => 'Optimized code'
|
30
50
|
chain << proc {|options| options[:generator].new }
|
31
51
|
end
|
32
52
|
end
|
data/lib/slim/filter.rb
CHANGED
data/lib/slim/parser.rb
CHANGED
@@ -69,7 +69,7 @@ module Slim
|
|
69
69
|
# Hello
|
70
70
|
# World!
|
71
71
|
#
|
72
|
-
block_indent,
|
72
|
+
block_indent, text_indent, in_comment = nil, nil, false
|
73
73
|
|
74
74
|
str.each_line do |line|
|
75
75
|
lineno += 1
|
@@ -107,7 +107,7 @@ module Slim
|
|
107
107
|
# This line happens to be indented deeper (or equal) than the block start character (|, ', /).
|
108
108
|
# This means that it's a part of the block.
|
109
109
|
|
110
|
-
if !
|
110
|
+
if !in_comment
|
111
111
|
# The indentation of first line of the text block determines the text base indentation.
|
112
112
|
newline = text_indent ? "\n" : ''
|
113
113
|
text_indent ||= indent
|
@@ -124,15 +124,10 @@ module Slim
|
|
124
124
|
next
|
125
125
|
end
|
126
126
|
|
127
|
-
# Closes the html comment
|
128
|
-
if in_html_comment
|
129
|
-
stacks.last << [:static, '-->']
|
130
|
-
end
|
131
|
-
|
132
127
|
# It's guaranteed that we're now *not* in a block, because
|
133
128
|
# the indent was less than the block start indent.
|
134
129
|
block_indent = text_indent = nil
|
135
|
-
|
130
|
+
in_comment = false
|
136
131
|
end
|
137
132
|
|
138
133
|
# If there's more stacks than indents, it means that the previous
|
@@ -170,24 +165,26 @@ module Slim
|
|
170
165
|
case line[0]
|
171
166
|
when ?|, ?', ?/
|
172
167
|
# Found a block.
|
168
|
+
ch = line.slice!(0)
|
173
169
|
|
174
170
|
# We're now expecting the next line to be indented, so we'll need
|
175
171
|
# to push a block to the stack.
|
176
172
|
block = [:multi]
|
177
|
-
stacks.last <<
|
173
|
+
stacks.last << if ch == ?'
|
174
|
+
# Additional whitespace in front
|
175
|
+
[:multi, block, [:slim, :text, ' ']]
|
176
|
+
elsif ch == ?/ && line[0] == ?!
|
177
|
+
# HTML comment
|
178
|
+
line.slice!(0)
|
179
|
+
[:slim, :comment, block]
|
180
|
+
else
|
181
|
+
in_comment = ch == ?/
|
182
|
+
block
|
183
|
+
end
|
178
184
|
stacks << block
|
179
185
|
block_indent = indent
|
180
186
|
|
181
|
-
|
182
|
-
in_html_comment = line[0] == ?/ && line[1] == ?! && line.slice!(0)
|
183
|
-
line.slice!(0)
|
184
|
-
|
185
|
-
# We're entering a block of html comments, so let's add an opening tag
|
186
|
-
if in_html_comment
|
187
|
-
block << [:static, '<!--']
|
188
|
-
end
|
189
|
-
|
190
|
-
if !in_slim_comment && !line.strip.empty?
|
187
|
+
if !in_comment && !line.strip.empty?
|
191
188
|
block << [:slim, :text, line.sub(/^( )/, '')]
|
192
189
|
text_indent = block_indent + ($1 ? 2 : 1)
|
193
190
|
end
|
data/lib/slim/sections.rb
CHANGED
@@ -17,6 +17,8 @@ module Slim
|
|
17
17
|
def compile(exp)
|
18
18
|
if options[:sections]
|
19
19
|
# Store the dictionary in the _slimdict variable
|
20
|
+
dictionary = options[:dictionary]
|
21
|
+
dictionary = "Slim::Wrapper.new(#{dictionary})" if options[:dictionary_access] == :wrapped
|
20
22
|
[:multi,
|
21
23
|
[:block, "_slimdict = #{dictionary}"],
|
22
24
|
super]
|
@@ -52,6 +54,7 @@ module Slim
|
|
52
54
|
[:block, "if #{tmp1} == true"],
|
53
55
|
content,
|
54
56
|
[:block, 'else'],
|
57
|
+
# Wrap map in array because maps implement each
|
55
58
|
[:block, "#{tmp1} = [#{tmp1}] if #{tmp1}.respond_to?(:has_key?) || !#{tmp1}.respond_to?(:map)"],
|
56
59
|
[:block, "#{tmp2} = _slimdict"],
|
57
60
|
[:block, "#{tmp1}.each do |_slimdict|"],
|
@@ -67,7 +70,10 @@ module Slim
|
|
67
70
|
[:slim, :output, escape, access(name), content]
|
68
71
|
end
|
69
72
|
|
73
|
+
private
|
74
|
+
|
70
75
|
def access(name)
|
76
|
+
return name if name == 'yield'
|
71
77
|
case options[:dictionary_access]
|
72
78
|
when :string
|
73
79
|
"_slimdict[#{name.to_s.inspect}]"
|
@@ -75,14 +81,5 @@ module Slim
|
|
75
81
|
"_slimdict[#{name.to_sym.inspect}]"
|
76
82
|
end
|
77
83
|
end
|
78
|
-
|
79
|
-
def dictionary
|
80
|
-
if options[:dictionary_access] == :wrapped
|
81
|
-
"Slim::Wrapper.new(#{options[:dictionary]})"
|
82
|
-
else
|
83
|
-
options[:dictionary]
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
84
|
end
|
88
85
|
end
|
data/lib/slim/version.rb
CHANGED
data/lib/slim/wrapper.rb
CHANGED
@@ -1,9 +1,5 @@
|
|
1
1
|
module Slim
|
2
2
|
# For logic-less mode, objects can be encased in the Wrapper class.
|
3
|
-
#
|
4
|
-
# For Rails, this allows us to use the environment provided for rendering
|
5
|
-
# a view including the instance variables, view helper and application helper
|
6
|
-
# methods.
|
7
3
|
class Wrapper
|
8
4
|
attr_reader :value, :parent
|
9
5
|
|
@@ -24,15 +20,13 @@ module Slim
|
|
24
20
|
# go through the same steps on the parent object. This is useful when
|
25
21
|
# you are iterating over objects.
|
26
22
|
def [](name)
|
27
|
-
if value.respond_to?(name)
|
28
|
-
|
29
|
-
|
30
|
-
wrap
|
31
|
-
elsif value.instance_variable_defined?("@#{name}")
|
32
|
-
wrap value.instance_variable_get("@#{name}")
|
33
|
-
elsif parent
|
34
|
-
parent[name]
|
23
|
+
return wrap(value.send(name)) if value.respond_to?(name)
|
24
|
+
if value.respond_to?(:has_key?)
|
25
|
+
return wrap(value[name.to_sym]) if value.has_key?(name.to_sym)
|
26
|
+
return wrap(value[name.to_s]) if value.has_key?(name.to_s)
|
35
27
|
end
|
28
|
+
return wrap(value.instance_variable_get("@#{name}")) if value.instance_variable_defined?("@#{name}")
|
29
|
+
parent[name] if parent
|
36
30
|
end
|
37
31
|
|
38
32
|
# Empty objects must appear empty for inverted sections
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 8
|
8
|
-
-
|
9
|
-
version: 0.8.
|
8
|
+
- 4
|
9
|
+
version: 0.8.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Andrew Stone
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date:
|
19
|
+
date: 2011-01-26 00:00:00 -05:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -30,8 +30,8 @@ dependencies:
|
|
30
30
|
segments:
|
31
31
|
- 0
|
32
32
|
- 1
|
33
|
-
-
|
34
|
-
version: 0.1.
|
33
|
+
- 7
|
34
|
+
version: 0.1.7
|
35
35
|
type: :runtime
|
36
36
|
version_requirements: *id001
|
37
37
|
- !ruby/object:Gem::Dependency
|
@@ -44,8 +44,8 @@ dependencies:
|
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
segments:
|
46
46
|
- 1
|
47
|
-
-
|
48
|
-
version: "1.
|
47
|
+
- 2
|
48
|
+
version: "1.2"
|
49
49
|
type: :runtime
|
50
50
|
version_requirements: *id002
|
51
51
|
- !ruby/object:Gem::Dependency
|