slim 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -87,6 +87,31 @@ So here's what I came up with:
87
87
  tr id="user_#{user.id}"
88
88
 
89
89
 
90
+ #### Treat multiple lines of code as text that should bypass parsing.
91
+
92
+ # Use a backtick to start the escape. Each following line that is
93
+ # indented greater than the backtick is copied over.
94
+
95
+ body
96
+ p
97
+ '
98
+ This is a test of the text block.
99
+
100
+ # The parsed result of the above:
101
+
102
+ <body><p>This is a test of the text block.</p></body>
103
+
104
+ # The left margin is set at the indent of the backtick + one space.
105
+ # Any additional spaces will be copied over.
106
+
107
+ body
108
+ p
109
+ '
110
+ This line is on the left margin.
111
+ This line will have one space in front of it.
112
+ This line will have two spaces in front of it.
113
+ And so on...
114
+
90
115
  ### Things to know:
91
116
 
92
117
  * Standard Ruby syntax after '-' and '='
data/lib/slim.rb CHANGED
@@ -8,7 +8,7 @@ require 'slim/engine'
8
8
  module Slim
9
9
  class << self
10
10
  def version
11
- '0.3.1'
11
+ '0.4.0'
12
12
  end
13
13
  end
14
14
  end
data/lib/slim/compiler.rb CHANGED
@@ -12,17 +12,28 @@ module Slim
12
12
  def compile
13
13
  @_buffer = ["_buf = [];"]
14
14
 
15
- last_indent = -1; enders = []
15
+ @in_text = false
16
+
17
+ text_indent = last_indent = -1; enders = []
16
18
 
17
19
  @template.each_line do |line|
18
20
  line.chomp!; line.rstrip!
19
21
 
22
+ if line.length == 0
23
+ @_buffer << "_buf << \"<br/>\";" if @in_text
24
+ next
25
+ end
26
+
27
+ line =~ REGEX
20
28
 
21
- next if line.length == 0
29
+ indent = $1.to_s.length
22
30
 
23
- line =~ REGEX
31
+ if @in_text && indent > text_indent
32
+ spaces = indent - text_indent
33
+ @_buffer << "_buf << \"#{(' '*(spaces - 1)) + line.lstrip}\";"
34
+ next
35
+ end
24
36
 
25
- indent = $1.to_s.length
26
37
  marker = $2
27
38
  attrs = $3
28
39
  string = $4
@@ -35,6 +46,11 @@ module Slim
35
46
  else :markup
36
47
  end
37
48
 
49
+ if line_type != :text
50
+ @in_text = false
51
+ text_indent = -1
52
+ end
53
+
38
54
  if attrs
39
55
  attrs.gsub!('"', '\"')
40
56
  end
@@ -81,7 +97,10 @@ module Slim
81
97
  end
82
98
  end
83
99
  when :text
84
- @_buffer << "_buf << \"#{string}\";"
100
+ @in_text = true
101
+ text_indent = indent
102
+
103
+ @_buffer << "_buf << \"#{string}\";" if string.to_s.length > 0
85
104
  when :control_code
86
105
  unless enders.detect{|e| e[0] == 'end;' && e[1] == indent}
87
106
  enders << ['end;', indent]
data/slim.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{slim}
8
- s.version = "0.3.1"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andrew Stone"]
12
- s.date = %q{2010-09-17}
12
+ s.date = %q{2010-09-21}
13
13
  s.description = %q{Slim is a template language whose goal is reduce the syntax to the essential parts without becoming cryptic.}
14
14
  s.email = %q{andy@stonean.com}
15
15
  s.extra_rdoc_files = [
@@ -287,6 +287,28 @@ TEMPLATE
287
287
  assert_equal expected, output
288
288
  end
289
289
 
290
+ def test_text_that_starts_with_tag_name
291
+ string = <<HTML
292
+ html
293
+ head
294
+ meta name="description" content="This is a Slim Test, that's all"
295
+ title Simple Test Title
296
+ body
297
+ img width="100" height="50" src="/images/test.jpg"
298
+ p
299
+ `
300
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam malesuada, dui at condimentum dapibus, purus sapien fringilla est, vel eleifend massa purus ut lectus. Praesent aliquam, ipsum eu ornare porta, tellus lacus viverra diam, nec scelerisque ipsum nunc et lacus. Nam adipiscing velit eget dolor ultricies in dignissim augue ultricies. Sed sed leo in sapien pretium dictum. Fusce sem quam, tincidunt vel lobortis sed, tincidunt vulputate est. Phasellus et ipsum quam, ac fringilla orci. In facilisis est et ante tempus suscipit. Morbi elementum quam ut urna laoreet vel malesuada justo adipiscing. Suspendisse eu lorem id lacus molestie dapibus fringilla ut orci. Cras pellentesque auctor leo, nec bibendum est suscipit id. Mauris dignissim aliquet libero vitae vulputate. Maecenas viverra sodales suscipit. Aenean eu nisi velit.
301
+
302
+ Phasellus ultricies vulputate lacus, eget pretium orci tincidunt tristique. Duis vitae luctus risus. Aliquam turpis massa, adipiscing in adipiscing ut, congue sed justo. Sed egestas ullamcorper nisl placerat dictum. Sed a leo lectus, sit amet vehicula nisl. Duis adipiscing congue tortor ut vulputate. Phasellus ligula lectus, congue non lobortis sed, dictum sed tellus. Vestibulum viverra vestibulum felis convallis pharetra. Phasellus a dignissim tellus. Proin dapibus malesuada lorem, et porttitor diam bibendum a. Donec et dui mauris, et tempus metus. Etiam pharetra varius dignissim. Maecenas lacinia, ligula ut tincidunt porttitor, sapien nisi pulvinar magna, nec sollicitudin libero odio bibendum nisi. Aenean ipsum eros, convallis id consequat nec, commodo eget diam. Integer malesuada, libero non dignissim varius, velit metus malesuada lectus, a consequat turpis purus ut elit.
303
+ HTML
304
+
305
+ expected = %q|_buf = [];_buf << "<html>";_buf << "<head>";_buf << "<meta name=\"description\" content=\"This is a Slim Test, that's all\"/>";_buf << "<title>";_buf << "Simple Test Title";_buf << "</title>";_buf << "</head>";_buf << "<body>";_buf << "<img width=\"100\" height=\"50\" src=\"/images/test.jpg\"/>";_buf << "<p>";_buf << " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam malesuada, dui at condimentum dapibus, purus sapien fringilla est, vel eleifend massa purus ut lectus. Praesent aliquam, ipsum eu ornare porta, tellus lacus viverra diam, nec scelerisque ipsum nunc et lacus. Nam adipiscing velit eget dolor ultricies in dignissim augue ultricies. Sed sed leo in sapien pretium dictum. Fusce sem quam, tincidunt vel lobortis sed, tincidunt vulputate est. Phasellus et ipsum quam, ac fringilla orci. In facilisis est et ante tempus suscipit. Morbi elementum quam ut urna laoreet vel malesuada justo adipiscing. Suspendisse eu lorem id lacus molestie dapibus fringilla ut orci. Cras pellentesque auctor leo, nec bibendum est suscipit id. Mauris dignissim aliquet libero vitae vulputate. Maecenas viverra sodales suscipit. Aenean eu nisi velit.";_buf << "<br/>";_buf << "Phasellus ultricies vulputate lacus, eget pretium orci tincidunt tristique. Duis vitae luctus risus. Aliquam turpis massa, adipiscing in adipiscing ut, congue sed justo. Sed egestas ullamcorper nisl placerat dictum. Sed a leo lectus, sit amet vehicula nisl. Duis adipiscing congue tortor ut vulputate. Phasellus ligula lectus, congue non lobortis sed, dictum sed tellus. Vestibulum viverra vestibulum felis convallis pharetra. Phasellus a dignissim tellus. Proin dapibus malesuada lorem, et porttitor diam bibendum a. Donec et dui mauris, et tempus metus. Etiam pharetra varius dignissim. Maecenas lacinia, ligula ut tincidunt porttitor, sapien nisi pulvinar magna, nec sollicitudin libero odio bibendum nisi. Aenean ipsum eros, convallis id consequat nec, commodo eget diam. Integer malesuada, libero non dignissim varius, velit metus malesuada lectus, a consequat turpis purus ut elit.";_buf << "</p>";_buf << "</body>";_buf << "</html>";_buf.join;|
306
+
307
+ output = TestEngine.new(string).compiled
308
+
309
+ iterate_it expected, output
310
+ end
311
+
290
312
 
291
313
  # Use this to do a line by line check. Much easier to see where the problem is.
292
314
  def iterate_it(expected, output)
@@ -111,7 +111,45 @@ HTML
111
111
  assert_equal expected, engine.render(@env)
112
112
  end
113
113
 
114
+ def test_render_with_text_block
115
+ string = <<HTML
116
+ html
117
+ head
118
+ title Simple Test Title
119
+ body
120
+ h1 This is my title
121
+ p id="#\{id_helper}"
122
+ `
123
+ Phasellus ultricies vulputate lacus, eget pretium orci tincidunt tristique. Duis vitae luctus risus. Aliquam turpis massa, adipiscing in adipiscing ut, congue sed justo. Sed egestas ullamcorper nisl placerat dictum. Sed a leo lectus, sit amet vehicula nisl. Duis adipiscing congue tortor ut vulputate. Phasellus ligula lectus, congue non lobortis sed, dictum sed tellus. Vestibulum viverra vestibulum felis convallis pharetra. Phasellus a dignissim tellus. Proin dapibus malesuada lorem, et porttitor diam bibendum a. Donec et dui mauris, et tempus metus. Etiam pharetra varius dignissim. Maecenas lacinia, ligula ut tincidunt porttitor, sapien nisi pulvinar magna, nec sollicitudin libero odio bibendum nisi. Aenean ipsum eros, convallis id consequat nec, commodo eget diam. Integer malesuada, libero non dignissim varius, velit metus malesuada lectus, a consequat turpis purus ut elit.
124
+ HTML
125
+
126
+ engine = Slim::Engine.new(string)
127
+
128
+ expected = "<html><head><title>Simple Test Title</title></head><body><h1>This is my title</h1><p id=\"notice\">Phasellus ultricies vulputate lacus, eget pretium orci tincidunt tristique. Duis vitae luctus risus. Aliquam turpis massa, adipiscing in adipiscing ut, congue sed justo. Sed egestas ullamcorper nisl placerat dictum. Sed a leo lectus, sit amet vehicula nisl. Duis adipiscing congue tortor ut vulputate. Phasellus ligula lectus, congue non lobortis sed, dictum sed tellus. Vestibulum viverra vestibulum felis convallis pharetra. Phasellus a dignissim tellus. Proin dapibus malesuada lorem, et porttitor diam bibendum a. Donec et dui mauris, et tempus metus. Etiam pharetra varius dignissim. Maecenas lacinia, ligula ut tincidunt porttitor, sapien nisi pulvinar magna, nec sollicitudin libero odio bibendum nisi. Aenean ipsum eros, convallis id consequat nec, commodo eget diam. Integer malesuada, libero non dignissim varius, velit metus malesuada lectus, a consequat turpis purus ut elit.</p></body></html>"
129
+
130
+ assert_equal expected, engine.render(@env)
131
+ end
132
+
114
133
 
134
+ def test_render_with_text_block_with_subsequent_markup
135
+ string = <<HTML
136
+ html
137
+ head
138
+ title Simple Test Title
139
+ body
140
+ h1 This is my title
141
+ p id="#\{id_helper}"
142
+ `
143
+ Phasellus ultricies vulputate lacus, eget pretium orci tincidunt tristique. Duis vitae luctus risus. Aliquam turpis massa, adipiscing in adipiscing ut, congue sed justo. Sed egestas ullamcorper nisl placerat dictum. Sed a leo lectus, sit amet vehicula nisl. Duis adipiscing congue tortor ut vulputate. Phasellus ligula lectus, congue non lobortis sed, dictum sed tellus. Vestibulum viverra vestibulum felis convallis pharetra. Phasellus a dignissim tellus. Proin dapibus malesuada lorem, et porttitor diam bibendum a. Donec et dui mauris, et tempus metus. Etiam pharetra varius dignissim. Maecenas lacinia, ligula ut tincidunt porttitor, sapien nisi pulvinar magna, nec sollicitudin libero odio bibendum nisi. Aenean ipsum eros, convallis id consequat nec, commodo eget diam. Integer malesuada, libero non dignissim varius, velit metus malesuada lectus, a consequat turpis purus ut elit.
144
+ p Some more markup
145
+ HTML
146
+
147
+ engine = Slim::Engine.new(string)
148
+
149
+ expected = "<html><head><title>Simple Test Title</title></head><body><h1>This is my title</h1><p id=\"notice\"> Phasellus ultricies vulputate lacus, eget pretium orci tincidunt tristique. Duis vitae luctus risus. Aliquam turpis massa, adipiscing in adipiscing ut, congue sed justo. Sed egestas ullamcorper nisl placerat dictum. Sed a leo lectus, sit amet vehicula nisl. Duis adipiscing congue tortor ut vulputate. Phasellus ligula lectus, congue non lobortis sed, dictum sed tellus. Vestibulum viverra vestibulum felis convallis pharetra. Phasellus a dignissim tellus. Proin dapibus malesuada lorem, et porttitor diam bibendum a. Donec et dui mauris, et tempus metus. Etiam pharetra varius dignissim. Maecenas lacinia, ligula ut tincidunt porttitor, sapien nisi pulvinar magna, nec sollicitudin libero odio bibendum nisi. Aenean ipsum eros, convallis id consequat nec, commodo eget diam. Integer malesuada, libero non dignissim varius, velit metus malesuada lectus, a consequat turpis purus ut elit.</p><p>Some more markup</p></body></html>"
150
+
151
+ assert_equal expected, engine.render(@env)
152
+ end
115
153
 
116
154
 
117
155
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
8
- - 1
9
- version: 0.3.1
7
+ - 4
8
+ - 0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Andrew Stone
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-17 00:00:00 -04:00
17
+ date: 2010-09-21 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies: []
20
20