haml2slim 0.4.6 → 0.4.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d3f15b271d9ca5c4d98e54f98582d4b6bebaffbd
4
+ data.tar.gz: 2b1abb84b3cfb8637b7f824975f87fd5dfc89e44
5
+ SHA512:
6
+ metadata.gz: c9b62cae1b4e381e292ccad5df16a02de21698fcd4501f9255b41d4ce8a72651ef0df978abf2c08a390ae939548559428cc4cbadce0fbb6448b10c93b0cf8776
7
+ data.tar.gz: bf2a11d1e54f10eb90df16c86f79465bfd0a89379b89e131e6dd6ebe1bcf4cbbc8bca07c222b6e51719dfd8c594f4b3d79e05b047b82a784a085988505e57d00
data/README.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  [Haml](https://github.com/nex3/haml) to [Slim](https://github.com/stonean/slim) converter.
4
4
 
5
+ ## Limitation
6
+
7
+ Due to the complex logic behind both Haml's and Slim's parsers, there is currently no way of reliably converting between Haml and Slim templates.
8
+
9
+ `Haml2Slim` only serves as a preliminary tool for templates conversion. You should __always__ manually verify the converted templates.
10
+
5
11
  ## Usage
6
12
 
7
13
  You may convert files using the included executable `haml2slim`.
@@ -51,6 +51,8 @@ module Haml2Slim
51
51
  @options[:input] = file = args.shift
52
52
  @options[:output] = destination = args.shift
53
53
 
54
+ @options[:input] = file = "-" unless file
55
+
54
56
  if File.directory?(@options[:input])
55
57
  Dir["#{@options[:input]}/**/*.haml"].each { |file| _process(file, destination) }
56
58
  else
@@ -71,8 +73,14 @@ module Haml2Slim
71
73
  slim_file = destination || slim_file
72
74
  end
73
75
 
74
- @options[:output] = file ? File.open(slim_file, 'w') : $stdout
75
- @options[:output].puts Haml2Slim.convert!(File.open(file, 'r'))
76
+ in_file = if @options[:input] == "-"
77
+ $stdin
78
+ else
79
+ File.open(file, 'r')
80
+ end
81
+
82
+ @options[:output] = slim_file && slim_file != '-' ? File.open(slim_file, 'w') : $stdout
83
+ @options[:output].puts Haml2Slim.convert!(in_file)
76
84
  @options[:output].close
77
85
 
78
86
  File.delete(file) if @options[:delete]
@@ -16,15 +16,19 @@ module Haml2Slim
16
16
  indent = line[/^[ \t]*/]
17
17
  line.strip!
18
18
 
19
+ # removes the HAML's whitespace removal characters ('>' and '<')
20
+ line.gsub!(/(>|<)$/, '')
21
+
19
22
  converted = case line[0, 2]
20
23
  when '&=' then line.sub(/^&=/, '==')
21
- when '!=' then line.sub(/^!=/, '=')
24
+ when '!=' then line.sub(/^!=/, '==')
22
25
  when '-#' then line.sub(/^-#/, '/')
26
+ when '#{' then line
23
27
  else
24
28
  case line[0]
25
29
  when ?%, ?., ?# then parse_tag(line)
26
30
  when ?: then "#{line[1..-1]}:"
27
- when ?! then line.sub(/^!!!/, 'doctype')
31
+ when ?! then line == "!!!" ? line.sub(/^!!!/, 'doctype html') : line.sub(/^!!!/, 'doctype')
28
32
  when ?-, ?= then line
29
33
  when ?~ then line.sub(/^~/, '=')
30
34
  when ?/ then line.sub(/^\//, '/!')
@@ -44,17 +48,34 @@ module Haml2Slim
44
48
 
45
49
  def parse_tag(tag_line)
46
50
  tag_line.sub!(/^%/, '')
51
+ tag_line.sub!(/^(\w+)!=/, '\1==')
47
52
 
48
- if tag_line_contains_attr = tag_line.match(/(.+)\{(.+)\}/)
49
- tag, attrs = *tag_line_contains_attr[1..2]
50
-
51
- attrs.gsub!(/,?( ?):?"?([^"'{ ]+)"? ?=> ?/, '\1\2=')
52
- attrs.gsub!(/=([^"']+)(?: |$)/, '=(\1)')
53
-
54
- "#{tag} #{attrs}"
53
+ if tag_line_contains_attr = tag_line.match(/([^\{]+)\{(.+)\}(.*)/)
54
+ tag, attrs, text = *tag_line_contains_attr[1..3]
55
+ "#{tag} #{parse_attrs(attrs)} #{text}"
55
56
  else
56
- tag_line
57
+ tag_line.sub(/^!=/, '=')
58
+ end
59
+ end
60
+
61
+ def parse_attrs(attrs, key_prefix='')
62
+ data_temp = {}
63
+ attrs.gsub!(/:data\s*=>\s*\{([^\}]*)\}/) do
64
+ key = rand(99999).to_s
65
+ data_temp[key] = parse_attrs($1, 'data-')
66
+ ":#{key} => #{key}"
67
+ end
68
+ attrs.gsub!(/,?( ?):?"?([^"'{ ]+)"?\s*=>\s*([^,]*)/) do
69
+ space = $1
70
+ key = $2
71
+ value = $3
72
+ wrapped_value = value.to_s =~ /\s+/ ? "(#{value})" : value
73
+ "#{space}#{key_prefix}#{key}=#{wrapped_value}"
74
+ end
75
+ data_temp.each do |k, v|
76
+ attrs.gsub!("#{k}=#{k}", v)
57
77
  end
78
+ attrs
58
79
  end
59
80
  end
60
- end
81
+ end
@@ -1,3 +1,3 @@
1
1
  module Haml2Slim
2
- VERSION = '0.4.6'
2
+ VERSION = '0.4.7'
3
3
  end
metadata CHANGED
@@ -1,71 +1,94 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: haml2slim
3
- version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.4.6
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.7
6
5
  platform: ruby
7
- authors:
6
+ authors:
8
7
  - Fred Wu
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
-
13
- date: 2011-06-25 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
11
+ date: 2013-05-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
16
14
  name: haml
17
- prerelease: false
18
- requirement: &id001 !ruby/object:Gem::Requirement
19
- none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "3.0"
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
24
20
  type: :runtime
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
27
- name: nokogiri
28
21
  prerelease: false
29
- requirement: &id002 !ruby/object:Gem::Requirement
30
- none: false
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
34
- version: "0"
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
35
34
  type: :runtime
36
- version_requirements: *id002
37
- - !ruby/object:Gem::Dependency
38
- name: ruby_parser
39
35
  prerelease: false
40
- requirement: &id003 !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- version: "0"
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ruby_parser
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
46
48
  type: :runtime
47
- version_requirements: *id003
48
- - !ruby/object:Gem::Dependency
49
- name: slim
50
49
  prerelease: false
51
- requirement: &id004 !ruby/object:Gem::Requirement
52
- none: false
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: 0.8.3
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
57
62
  type: :development
58
- version_requirements: *id004
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: slim
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 1.0.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 1.0.0
59
83
  description: Convert Haml templates to Slim templates.
60
- email:
84
+ email:
61
85
  - ifredwu@gmail.com
62
- executables:
86
+ executables:
63
87
  - haml2slim
64
88
  extensions: []
65
-
66
- extra_rdoc_files:
89
+ extra_rdoc_files:
67
90
  - README.md
68
- files:
91
+ files:
69
92
  - README.md
70
93
  - bin/haml2slim
71
94
  - lib/haml2slim.rb
@@ -74,30 +97,26 @@ files:
74
97
  - lib/haml2slim/version.rb
75
98
  homepage: http://github.com/fredwu/haml2slim
76
99
  licenses: []
77
-
100
+ metadata: {}
78
101
  post_install_message:
79
- rdoc_options:
102
+ rdoc_options:
80
103
  - --charset=UTF-8
81
- require_paths:
104
+ require_paths:
82
105
  - lib
83
- required_ruby_version: !ruby/object:Gem::Requirement
84
- none: false
85
- requirements:
86
- - - ">="
87
- - !ruby/object:Gem::Version
88
- version: "0"
89
- required_rubygems_version: !ruby/object:Gem::Requirement
90
- none: false
91
- requirements:
92
- - - ">="
93
- - !ruby/object:Gem::Version
94
- version: "0"
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
95
116
  requirements: []
96
-
97
117
  rubyforge_project:
98
- rubygems_version: 1.8.5
118
+ rubygems_version: 2.0.3
99
119
  signing_key:
100
- specification_version: 3
120
+ specification_version: 4
101
121
  summary: Haml to Slim converter.
102
122
  test_files: []
103
-