Class: Haml2Slim::Converter
- Inherits:
-
Object
- Object
- Haml2Slim::Converter
- Defined in:
- lib/haml2slim/converter.rb
Instance Method Summary collapse
-
#initialize(haml) ⇒ Converter
constructor
A new instance of Converter.
- #parse_attrs(attrs, key_prefix = '') ⇒ Object
- #parse_line(line) ⇒ Object
- #parse_tag(tag_line) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(haml) ⇒ Converter
Returns a new instance of Converter.
3 4 5 6 7 8 9 |
# File 'lib/haml2slim/converter.rb', line 3 def initialize(haml) @slim = "" haml.each_line do |line| @slim << parse_line(line) end end |
Instance Method Details
#parse_attrs(attrs, key_prefix = '') ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/haml2slim/converter.rb', line 61 def parse_attrs(attrs, key_prefix='') data_temp = {} attrs.gsub!(/:data\s*=>\s*\{([^\}]*)\}/) do key = rand(99999).to_s data_temp[key] = parse_attrs($1, 'data-') ":#{key} => #{key}" end attrs.gsub!(/,?( ?):?"?([^"'{ ]+)"?\s*=>\s*([^,]*)/) do space = $1 key = $2 value = $3 wrapped_value = value.to_s =~ /\s+/ ? "(#{value})" : value "#{space}#{key_prefix}#{key}=#{wrapped_value}" end data_temp.each do |k, v| attrs.gsub!("#{k}=#{k}", v) end attrs end |
#parse_line(line) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/haml2slim/converter.rb', line 15 def parse_line(line) indent = line[/^[ \t]*/] line.strip! # removes the HAML's whitespace removal characters ('>' and '<') line.gsub!(/(>|<)$/, '') converted = case line[0, 2] when '&=' then line.sub(/^&=/, '==') when '!=' then line.sub(/^!=/, '==') when '-#' then line.sub(/^-#/, '/') when '#{' then line else case line[0] when ?%, ?., ?# then parse_tag(line) when ?: then "#{line[1..-1]}:" when ?! then line == "!!!" ? line.sub(/^!!!/, 'doctype html') : line.sub(/^!!!/, 'doctype') when ?-, ?= then line when ?~ then line.sub(/^~/, '=') when ?/ then line.sub(/^\//, '/!') when ?\ then line.sub(/^\\/, '|') when nil then "" else "| #{line}" end end if converted.chomp!(' |') converted.sub!(/^\| /, '') converted << ' \\' end "#{indent}#{converted}\n" end |
#parse_tag(tag_line) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/haml2slim/converter.rb', line 49 def parse_tag(tag_line) tag_line.sub!(/^%/, '') tag_line.sub!(/^(\w+)!=/, '\1==') if tag_line_contains_attr = tag_line.match(/([^\{]+)\{(.+)\}(.*)/) tag, attrs, text = *tag_line_contains_attr[1..3] "#{tag} #{parse_attrs(attrs)} #{text}" else tag_line.sub(/^!=/, '=') end end |
#to_s ⇒ Object
11 12 13 |
# File 'lib/haml2slim/converter.rb', line 11 def to_s @slim end |