292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
# File 'lib/html2haml/html.rb', line 292
def to_haml(tabs, options)
return "" if converted_to_haml
if name == "script" &&
(attr_hash['type'].nil? || attr_hash['type'].to_s == "text/javascript") &&
(attr_hash.keys - ['type']).empty?
return to_haml_filter(:javascript, tabs, options)
elsif name == "style" &&
(attr_hash['type'].nil? || attr_hash['type'].to_s == "text/css") &&
(attr_hash.keys - ['type']).empty?
return to_haml_filter(:css, tabs, options)
end
output = tabulate(tabs)
if options[:erb] && HAML_TAGS.include?(name)
case name
when "haml_loud"
lines = CGI.unescapeHTML(inner_text).split("\n").
map {|s| s.rstrip}.reject {|s| s.strip.empty?}
if attribute("raw")
lines.first.gsub!(/^[ \t]*/, "!= ")
else
lines.first.gsub!(/^[ \t]*/, "= ")
end
if lines.size > 1 indent_str = lines.last[/^[ \t]*/]
indent_re = /^[ \t]{0,#{indent_str.count(" ") + 8 * indent_str.count("\t")}}/
lines.map! {|s| s.gsub!(indent_re, '')}
lines[1..-1].each {|s| s.gsub!(/^/, " ")}
length = lines.map {|s| s.size}.max + 1
lines.map! {|s| "%#{-length}s|" % s}
if next_sibling && next_sibling.is_a?(Nokogiri::XML::Element) && next_sibling.name == "haml_loud" &&
next_sibling.inner_text.split("\n").reject {|s| s.strip.empty?}.size > 1
lines << "-#"
end
end
return lines.map {|s| output + s + "\n"}.join
when "haml_silent"
return CGI.unescapeHTML(inner_text).split("\n").map do |line|
next "" if line.strip.empty?
"#{output}- #{line.strip}\n"
end.join
when "haml_block"
return render_children("", tabs, options)
end
end
if self.next && self.next.text? && self.next.content =~ /\A[^\s]/
if self.previous.nil? || self.previous.text? &&
(self.previous.content =~ /[^\s]\Z/ ||
self.previous.content =~ /\A\s*\Z/ && self.previous.previous.nil?)
nuke_outer_whitespace = true
else
output << "= succeed #{self.next.content.slice!(/\A[^\s]+/).dump} do\n"
tabs += 1
output << tabulate(tabs)
self.next.content = ""
end
end
output << "%#{name}" unless name.to_s == 'div' &&
(static_id?(options) ||
static_classname?(options) &&
attr_hash['class'].to_s.split(' ').any?(&method(:haml_css_attr?)))
if attr_hash
if static_id?(options)
output << "##{attr_hash['id'].to_s}"
remove_attribute('id')
end
if static_classname?(options)
leftover = attr_hash['class'].to_s.split(' ').reject do |c|
next unless haml_css_attr?(c)
output << ".#{c}"
end
remove_attribute('class')
set_attribute('class', leftover.join(' ')) unless leftover.empty?
end
output << haml_attributes(options) if attr_hash.length > 0
end
output << ">" if nuke_outer_whitespace
output << "/" if to_xhtml.end_with?("/>")
if children && children.size == 1
child = children.first
if child.is_a?(::Nokogiri::XML::Text)
if !child.to_s.include?("\n")
text = child.to_haml(tabs + 1, options)
return output + " " + text.lstrip.gsub(/^\\/, '') unless text.chomp.include?("\n") || text.empty?
return output + "\n" + text
elsif ["pre", "textarea"].include?(name) ||
(name == "code" && parent.is_a?(::Nokogiri::XML::Element) && parent.name == "pre")
return output + "\n#{tabulate(tabs + 1)}:preserve\n" +
inner_text.gsub(/^/, tabulate(tabs + 2))
end
elsif child.is_a?(::Nokogiri::XML::Element) && child.name == "haml_loud"
return output + child.to_haml(tabs + 1, options).lstrip
end
end
render_children(output + "\n", tabs, options)
end
|