haml2erb 0.3.0.pre → 0.3.0.pre.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,77 @@
1
+ module Haml2Erb
2
+ class AttributesParser
3
+ class DynamicAttributes < StandardError
4
+ end
5
+
6
+ def initialize attributes
7
+ @attributes = attributes
8
+ @pairs = []
9
+ end
10
+ attr_reader :pairs, :attributes
11
+
12
+ CONTENTS = /^, \{(.*)\}$/
13
+ ROCKET = '\=\>'
14
+
15
+ SYMBOL_TEXT = '[\w_]+'
16
+ STRING_TEXT = '[\w_-]+'
17
+
18
+ SYMBOL_KEY = /^(?:\:(#{SYMBOL_TEXT})\s*#{ROCKET}|(#{SYMBOL_TEXT}):)\s*/
19
+ STRING_KEY = /^(?:'(#{STRING_TEXT})'|"(#{STRING_TEXT})")\s*#{ROCKET}\s*/
20
+
21
+ STRING_VALUE = /^:(#{SYMBOL_TEXT})\s*/
22
+ SYMBOL_VALUE = /^(?:"([^"]+)"|'([^']+)')\s*/
23
+
24
+ def parse!
25
+ rest = attributes.strip.scan(CONTENTS).flatten.first
26
+
27
+ begin
28
+ while not rest.empty?
29
+ if rest =~ SYMBOL_KEY
30
+ key = $1 || $2
31
+ rest.gsub! SYMBOL_KEY, ''
32
+ elsif rest =~ STRING_KEY
33
+ key = $1 || $2
34
+ rest.gsub! STRING_KEY, ''
35
+ else
36
+ raise DynamicAttributes
37
+ end
38
+
39
+ if rest =~ STRING_VALUE
40
+ value = $1
41
+ elsif rest =~ SYMBOL_VALUE
42
+ value = $1 || $2
43
+ else
44
+ raise DynamicAttributes
45
+ end
46
+
47
+ pairs << [key, value]
48
+ end
49
+ rescue DynamicAttributes
50
+ @dynamic = true
51
+ return
52
+ end
53
+ end
54
+
55
+ def dynamic?
56
+ @dynamic
57
+ end
58
+
59
+ def to_html
60
+ if attributes.strip.empty?
61
+ return ''
62
+ else
63
+ parse!
64
+ if dynamic?
65
+ hash = attributes.scan(CONTENTS).flatten.first
66
+ hash.strip!
67
+ hash.gsub! /\s*,$/, ''
68
+ " <%= tag_options({#{hash}}, false) %>"
69
+ else
70
+ ' ' << pairs.map do |(key, value)|
71
+ "#{key}='#{value.gsub("'", '&#x27;')}'"
72
+ end.join(' ')
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -1,10 +1,10 @@
1
1
  require 'haml'
2
+ require 'haml2erb/attributes_parser'
2
3
 
3
4
  module Haml2Erb
5
+ class Engine < Haml::Engine
4
6
 
5
- # puts RUBY_VERSION
6
7
 
7
- class Engine < Haml::Engine
8
8
  def push_silent(text, can_suppress = false)
9
9
  flush_merged_text
10
10
  return if can_suppress && options[:suppress_eval]
@@ -140,6 +140,105 @@ module Haml2Erb
140
140
  end
141
141
  end
142
142
 
143
- end
143
+ def compile_tag
144
+ t = @node.value
145
+
146
+ # Get rid of whitespace outside of the tag if we need to
147
+ rstrip_buffer! if t[:nuke_outer_whitespace]
148
+
149
+ dont_indent_next_line =
150
+ (t[:nuke_outer_whitespace] && !block_given?) ||
151
+ (t[:nuke_inner_whitespace] && block_given?)
152
+
153
+ if @options[:suppress_eval]
154
+ object_ref = "nil"
155
+ parse = false
156
+ value = t[:parse] ? nil : t[:value]
157
+ attributes_hashes = {}
158
+ preserve_script = false
159
+ else
160
+ object_ref = t[:object_ref]
161
+ parse = t[:parse]
162
+ value = t[:value]
163
+ attributes_hashes = t[:attributes_hashes]
164
+ preserve_script = t[:preserve_script]
165
+ end
166
+
167
+ # Check if we can render the tag directly to text and not process it in the buffer
168
+ if object_ref == "nil" && attributes_hashes.empty? && !preserve_script
169
+ tag_closed = !block_given? && !t[:self_closing] && !parse
170
+
171
+ open_tag = prerender_tag(t[:name], t[:self_closing], t[:attributes])
172
+ if tag_closed
173
+ open_tag << "#{value}</#{t[:name]}>"
174
+ open_tag << "\n" unless t[:nuke_outer_whitespace]
175
+ elsif !(parse || t[:nuke_inner_whitespace] ||
176
+ (t[:self_closing] && t[:nuke_outer_whitespace]))
177
+ open_tag << "\n"
178
+ end
179
+
180
+ push_merged_text(open_tag,
181
+ tag_closed || t[:self_closing] || t[:nuke_inner_whitespace] ? 0 : 1,
182
+ !t[:nuke_outer_whitespace])
183
+
184
+ @dont_indent_next_line = dont_indent_next_line
185
+ return if tag_closed
186
+ else
187
+ # raise attributes_hashes.inspect unless attributes_hashes.empty?
188
+ if attributes_hashes.empty?
189
+ attributes_hashes = ''
190
+ elsif attributes_hashes.size == 1
191
+ attributes_hashes = ", #{attributes_hashes.first}"
192
+ else
193
+ attributes_hashes = ", (#{attributes_hashes.join(").merge(")})"
194
+ end
195
+
196
+ push_merged_text "<#{t[:name]}", 0, !t[:nuke_outer_whitespace]
197
+
198
+ # WAS:
199
+ # push_generated_script(
200
+ # "_hamlout.attributes(#{inspect_obj(t[:attributes])}, #{object_ref}#{attributes_hashes})")
201
+ # NOW: attempt a simplistic parse of the attributes
202
+ concat_merged_text AttributesParser.new(attributes_hashes).to_html
203
+
204
+ concat_merged_text(
205
+ if t[:self_closing] && xhtml?
206
+ " />" + (t[:nuke_outer_whitespace] ? "" : "\n")
207
+ else
208
+ ">" + ((if t[:self_closing] && html?
209
+ t[:nuke_outer_whitespace]
210
+ else
211
+ !block_given? || t[:preserve_tag] || t[:nuke_inner_whitespace]
212
+ end) ? "" : "\n")
213
+ end)
214
+
215
+ if value && !parse
216
+ concat_merged_text("#{value}</#{t[:name]}>#{t[:nuke_outer_whitespace] ? "" : "\n"}")
217
+ else
218
+ @to_merge << [:text, '', 1] unless t[:nuke_inner_whitespace]
219
+ end
144
220
 
221
+ @dont_indent_next_line = dont_indent_next_line
222
+ end
223
+
224
+ return if t[:self_closing]
225
+
226
+ if value.nil?
227
+ @output_tabs += 1 unless t[:nuke_inner_whitespace]
228
+ yield if block_given?
229
+ @output_tabs -= 1 unless t[:nuke_inner_whitespace]
230
+ rstrip_buffer! if t[:nuke_inner_whitespace]
231
+ push_merged_text("</#{t[:name]}>" + (t[:nuke_outer_whitespace] ? "" : "\n"),
232
+ t[:nuke_inner_whitespace] ? 0 : -1, !t[:nuke_inner_whitespace])
233
+ @dont_indent_next_line = t[:nuke_outer_whitespace]
234
+ return
235
+ end
236
+
237
+ if parse
238
+ push_script(value, t.merge(:in_tag => true))
239
+ concat_merged_text("</#{t[:name]}>" + (t[:nuke_outer_whitespace] ? "" : "\n"))
240
+ end
241
+ end
242
+
243
+ end
145
244
  end
@@ -1,3 +1,3 @@
1
1
  module Haml2Erb
2
- VERSION = '0.3.0.pre'
2
+ VERSION = '0.3.0.pre.2'
3
3
  end
@@ -1,9 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
- haml = %{
3
+ haml = %q{
4
4
  !!!
5
5
  %html(lang="en")
6
- %head
6
+ %head(dynamic="#{attribute}")
7
7
  - ciao
8
8
  - if 3
9
9
  = pippo
@@ -37,9 +37,9 @@ haml = %{
37
37
  %section#hammurabi
38
38
  }
39
39
 
40
- erb = %{<!DOCTYPE html>
40
+ erb = %q{<!DOCTYPE html>
41
41
  <html lang='en'>
42
- <head>
42
+ <head <%= tag_options({"dynamic" => "#{attribute}"}, false) %>>
43
43
  <% ciao %>
44
44
  <% if 3 %>
45
45
  <%= pippo %>
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haml2erb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 961915996
4
+ hash: 1923831965
5
5
  prerelease: 6
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
9
  - 0
10
10
  - pre
11
- version: 0.3.0.pre
11
+ - 2
12
+ version: 0.3.0.pre.2
12
13
  platform: ruby
13
14
  authors:
14
15
  - Elia Schito
@@ -16,7 +17,7 @@ autorequire:
16
17
  bindir: bin
17
18
  cert_chain: []
18
19
 
19
- date: 2011-11-09 00:00:00 Z
20
+ date: 2011-11-15 00:00:00 Z
20
21
  dependencies:
21
22
  - !ruby/object:Gem::Dependency
22
23
  type: :runtime
@@ -81,6 +82,7 @@ files:
81
82
  - bin/haml2erb
82
83
  - haml2erb.gemspec
83
84
  - lib/haml2erb.rb
85
+ - lib/haml2erb/attributes_parser.rb
84
86
  - lib/haml2erb/engine.rb
85
87
  - lib/haml2erb/version.rb
86
88
  - spec/haml2erb_spec.rb
@@ -116,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
118
  requirements: []
117
119
 
118
120
  rubyforge_project: haml2erb
119
- rubygems_version: 1.8.10
121
+ rubygems_version: 1.8.11
120
122
  signing_key:
121
123
  specification_version: 3
122
124
  summary: Convert Haml templates to Erb!