haml2slim 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +26 -0
- data/bin/haml2slim +7 -0
- data/lib/haml2slim.rb +8 -0
- data/lib/haml2slim/command.rb +71 -0
- data/lib/haml2slim/converter.rb +54 -0
- data/lib/haml2slim/version.rb +3 -0
- metadata +110 -0
data/README.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# Haml2Slim
|
2
|
+
|
3
|
+
[Haml](https://github.com/nex3/haml) to [Slim](https://github.com/stonean/slim) converter.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
You may convert files using the included executable `haml2slim`.
|
8
|
+
|
9
|
+
# haml2slim -h
|
10
|
+
|
11
|
+
Usage: haml2slim [options]
|
12
|
+
-s, --stdin Read input from standard input instead of an input file
|
13
|
+
-o, --output FILENAME Output file destination
|
14
|
+
--trace Show a full traceback on error
|
15
|
+
-h, --help Show this message
|
16
|
+
-v, --version Print version
|
17
|
+
|
18
|
+
Alternatively, to convert files or strings on the fly in your application, you may do so by calling `Haml2Slim.convert!`.
|
19
|
+
|
20
|
+
## License
|
21
|
+
|
22
|
+
This project is released under the MIT license.
|
23
|
+
|
24
|
+
## Author
|
25
|
+
|
26
|
+
[Fred Wu](https://github.com/fredwu)
|
data/bin/haml2slim
ADDED
data/lib/haml2slim.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'haml2slim'
|
3
|
+
|
4
|
+
module Haml2Slim
|
5
|
+
class Command
|
6
|
+
def initialize(args)
|
7
|
+
@args = args
|
8
|
+
@options = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def run
|
12
|
+
@opts = OptionParser.new(&method(:set_opts))
|
13
|
+
@opts.parse!(@args)
|
14
|
+
process
|
15
|
+
exit 0
|
16
|
+
rescue Exception => ex
|
17
|
+
raise ex if @options[:trace] || SystemExit === ex
|
18
|
+
$stderr.print "#{ex.class}: " if ex.class != RuntimeError
|
19
|
+
$stderr.puts ex.message
|
20
|
+
$stderr.puts ' Use --trace for backtrace.'
|
21
|
+
exit 1
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
def set_opts(opts)
|
27
|
+
opts.on('-s', '--stdin', :NONE, 'Read input from standard input instead of an input file') do
|
28
|
+
@options[:input] = $stdin
|
29
|
+
end
|
30
|
+
|
31
|
+
opts.on('-o', '--output FILENAME', :NONE, 'Output file destination') do |filename|
|
32
|
+
@options[:output] = filename
|
33
|
+
end
|
34
|
+
|
35
|
+
opts.on('--trace', :NONE, 'Show a full traceback on error') do
|
36
|
+
@options[:trace] = true
|
37
|
+
end
|
38
|
+
|
39
|
+
opts.on_tail('-h', '--help', 'Show this message') do
|
40
|
+
puts opts
|
41
|
+
exit
|
42
|
+
end
|
43
|
+
|
44
|
+
opts.on_tail('-v', '--version', 'Print version') do
|
45
|
+
puts "Slim #{Haml2Slim::VERSION}"
|
46
|
+
exit
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def process
|
51
|
+
args = @args.dup
|
52
|
+
unless @options[:input]
|
53
|
+
file = args.shift
|
54
|
+
if file
|
55
|
+
@options[:file] = file
|
56
|
+
@options[:input] = File.open(file, 'r')
|
57
|
+
else
|
58
|
+
@options[:file] = 'STDIN'
|
59
|
+
@options[:input] = $stdin
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
unless @options[:output]
|
64
|
+
file = args.shift || file.sub(/\.haml$/, '.slim')
|
65
|
+
@options[:output] = file ? File.open(file, 'w') : $stdout
|
66
|
+
end
|
67
|
+
|
68
|
+
@options[:output].puts Haml2Slim.convert!(@options[:input])
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Haml2Slim
|
2
|
+
class Converter
|
3
|
+
def initialize(haml)
|
4
|
+
@slim = ""
|
5
|
+
|
6
|
+
haml.each_line do |line|
|
7
|
+
@slim << parse_line(line)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
@slim
|
13
|
+
end
|
14
|
+
|
15
|
+
def parse_line(line)
|
16
|
+
indent = line[/^[ \t]*/]
|
17
|
+
line.strip!
|
18
|
+
|
19
|
+
converted = case line[0]
|
20
|
+
when ?%, ?., ?# then parse_tag(line)
|
21
|
+
when ?: then "#{line[1..-1]}:"
|
22
|
+
when ?! then line.sub('!!!', '! doctype')
|
23
|
+
when ?-, ?= then line
|
24
|
+
when ?/ then line
|
25
|
+
when nil then ""
|
26
|
+
else "| #{line}"
|
27
|
+
end
|
28
|
+
|
29
|
+
if converted.chomp!(" |")
|
30
|
+
converted.sub!(/^\| /, "")
|
31
|
+
converted << " \\"
|
32
|
+
end
|
33
|
+
|
34
|
+
"#{indent}#{converted}\n"
|
35
|
+
end
|
36
|
+
|
37
|
+
def parse_tag(tag_line)
|
38
|
+
tag_line.sub!(/^%/, "")
|
39
|
+
|
40
|
+
if attrs = tag_line.match(/\{(.+)\}/)
|
41
|
+
tag = tag_line.match(/(\w+)\{/)[1]
|
42
|
+
attrs = tag_line.match(/\{(.+)\}/)[1]
|
43
|
+
.gsub(/:?"?([A-Za-z0-9\-_]+)"? ?=>/, '\1 =>')
|
44
|
+
.gsub(/ ?=> ?/, "=")
|
45
|
+
.gsub('",', '"')
|
46
|
+
.gsub(/=([^"]+)(?: |$)/, '=(\1)')
|
47
|
+
|
48
|
+
"#{tag} #{attrs}"
|
49
|
+
else
|
50
|
+
tag_line
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: haml2slim
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Fred Wu
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-12-16 00:00:00 +11:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: haml
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 3
|
30
|
+
- 0
|
31
|
+
- 0
|
32
|
+
version: 3.0.0
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: nokogiri
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: ruby_parser
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
type: :runtime
|
60
|
+
version_requirements: *id003
|
61
|
+
description: Convert Haml templates to Slim templates.
|
62
|
+
email:
|
63
|
+
- ifredwu@gmail.com
|
64
|
+
executables:
|
65
|
+
- haml2slim
|
66
|
+
extensions: []
|
67
|
+
|
68
|
+
extra_rdoc_files:
|
69
|
+
- README.md
|
70
|
+
files:
|
71
|
+
- README.md
|
72
|
+
- bin/haml2slim
|
73
|
+
- lib/haml2slim.rb
|
74
|
+
- lib/haml2slim/command.rb
|
75
|
+
- lib/haml2slim/converter.rb
|
76
|
+
- lib/haml2slim/version.rb
|
77
|
+
has_rdoc: true
|
78
|
+
homepage: http://github.com/fredwu/haml2slim
|
79
|
+
licenses: []
|
80
|
+
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options:
|
83
|
+
- --charset=UTF-8
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
version: "0"
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
version: "0"
|
102
|
+
requirements: []
|
103
|
+
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 1.3.7
|
106
|
+
signing_key:
|
107
|
+
specification_version: 3
|
108
|
+
summary: Haml to Slim converter.
|
109
|
+
test_files: []
|
110
|
+
|