html2slim 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +17 -5
- data/bin/erb2slim +7 -0
- data/bin/html2slim +1 -1
- data/lib/html2slim.rb +6 -2
- data/lib/html2slim/command.rb +18 -4
- data/lib/html2slim/converter.rb +25 -3
- data/lib/html2slim/hpricot_monkeypatches.rb +18 -26
- data/lib/html2slim/version.rb +1 -1
- metadata +21 -8
data/README.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/slim-template/html2slim.png?branch=master)](https://travis-ci.org/slim-template/html2slim)
|
2
|
+
|
3
|
+
[![Code climate](https://codeclimate.com/github/slim-template/html2slim.png)](https://codeclimate.com/github/slim-template/html2slim)
|
4
|
+
|
1
5
|
## HTML2Slim
|
2
6
|
|
3
|
-
Script for converting HTML
|
7
|
+
Script for converting HTML and ERB files to slim (slim-lang.org).
|
4
8
|
|
5
9
|
It's not perfect, but certainly it helps a lot!
|
6
10
|
|
@@ -8,7 +12,7 @@ It's based on Hpricot. Yeah, I'm old school.
|
|
8
12
|
|
9
13
|
## Usage
|
10
14
|
|
11
|
-
You may convert files using the included
|
15
|
+
You may convert files using the included executables `html2slim` and `erb2slim`.
|
12
16
|
|
13
17
|
# html2slim -h
|
14
18
|
|
@@ -18,7 +22,15 @@ You may convert files using the included executable `html2slim`.
|
|
18
22
|
-h, --help Show this message
|
19
23
|
-v, --version Print version
|
20
24
|
|
21
|
-
|
25
|
+
# erb2slim -h
|
26
|
+
|
27
|
+
Usage: erb2slim INPUT_FILENAME_OR_DIRECTORY [OUTPUT_FILENAME_OR_DIRECTORY] [options]
|
28
|
+
--trace Show a full traceback on error
|
29
|
+
-d, --delete Delete ERB files
|
30
|
+
-h, --help Show this message
|
31
|
+
-v, --version Print version
|
32
|
+
|
33
|
+
Alternatively, to convert files or strings on the fly in your application, you may do so by calling `HTML2Slim.convert!(file, format)` where format is either :html or :erb.
|
22
34
|
|
23
35
|
## License
|
24
36
|
|
@@ -34,6 +46,6 @@ https://github.com/slim-template/html2slim
|
|
34
46
|
|
35
47
|
## ROADMAP
|
36
48
|
|
37
|
-
1.
|
38
|
-
2.
|
49
|
+
1. ERB support is pre-alpha, and works only for 90% of the cases. Lotsa weird regexes. Gotta improve that.
|
50
|
+
2. Use temple.
|
39
51
|
3. Merge with other *2slim gems. Would be handy.
|
data/bin/erb2slim
ADDED
data/bin/html2slim
CHANGED
data/lib/html2slim.rb
CHANGED
@@ -2,7 +2,11 @@ require_relative 'html2slim/version'
|
|
2
2
|
require_relative 'html2slim/converter'
|
3
3
|
|
4
4
|
module HTML2Slim
|
5
|
-
def self.convert!(input)
|
6
|
-
|
5
|
+
def self.convert!(input, format=:html)
|
6
|
+
if format.to_s == "html"
|
7
|
+
HTMLConverter.new(input)
|
8
|
+
else
|
9
|
+
ERBConverter.new(input)
|
10
|
+
end
|
7
11
|
end
|
8
12
|
end
|
data/lib/html2slim/command.rb
CHANGED
@@ -3,6 +3,7 @@ require 'html2slim'
|
|
3
3
|
|
4
4
|
module HTML2Slim
|
5
5
|
class Command
|
6
|
+
|
6
7
|
def initialize(args)
|
7
8
|
@args = args
|
8
9
|
@options = {}
|
@@ -23,8 +24,16 @@ module HTML2Slim
|
|
23
24
|
|
24
25
|
protected
|
25
26
|
|
27
|
+
def format
|
28
|
+
@format ||= (self.class.to_s =~ /ERB/ ? :erb : :html)
|
29
|
+
end
|
30
|
+
|
31
|
+
def command_name
|
32
|
+
@command_name ||= format == :html ? "html2slim" : "erb2slim"
|
33
|
+
end
|
34
|
+
|
26
35
|
def set_opts(opts)
|
27
|
-
opts.banner = "Usage:
|
36
|
+
opts.banner = "Usage: #{command_name} INPUT_FILENAME_OR_DIRECTORY [OUTPUT_FILENAME_OR_DIRECTORY] [options]"
|
28
37
|
|
29
38
|
opts.on('--trace', :NONE, 'Show a full traceback on error') do
|
30
39
|
@options[:trace] = true
|
@@ -36,11 +45,11 @@ module HTML2Slim
|
|
36
45
|
end
|
37
46
|
|
38
47
|
opts.on_tail('-v', '--version', 'Print version') do
|
39
|
-
puts "
|
48
|
+
puts "#{command_name} #{HTML2Slim::VERSION}"
|
40
49
|
exit
|
41
50
|
end
|
42
51
|
|
43
|
-
opts.on('-d', '--delete',
|
52
|
+
opts.on('-d', '--delete', "Delete #{format.upcase} files") do
|
44
53
|
@options[:delete] = true
|
45
54
|
end
|
46
55
|
end
|
@@ -80,10 +89,15 @@ module HTML2Slim
|
|
80
89
|
end
|
81
90
|
|
82
91
|
@options[:output] = slim_file && slim_file != '-' ? File.open(slim_file, 'w') : $stdout
|
83
|
-
|
92
|
+
# raise "|||#{self.class.inspect}|||"
|
93
|
+
@options[:output].puts HTML2Slim.convert!(in_file, format)
|
84
94
|
@options[:output].close
|
85
95
|
|
86
96
|
File.delete(file) if @options[:delete]
|
87
97
|
end
|
88
98
|
end
|
99
|
+
class HTMLCommand < Command
|
100
|
+
end
|
101
|
+
class ERBCommand < Command
|
102
|
+
end
|
89
103
|
end
|
data/lib/html2slim/converter.rb
CHANGED
@@ -1,12 +1,34 @@
|
|
1
1
|
require_relative 'hpricot_monkeypatches'
|
2
|
-
|
2
|
+
|
3
3
|
module HTML2Slim
|
4
4
|
class Converter
|
5
|
+
def to_s
|
6
|
+
@slim
|
7
|
+
end
|
8
|
+
end
|
9
|
+
class HTMLConverter < Converter
|
5
10
|
def initialize(html)
|
6
11
|
@slim = Hpricot(html).to_slim
|
7
12
|
end
|
8
|
-
|
9
|
-
|
13
|
+
end
|
14
|
+
class ERBConverter < Converter
|
15
|
+
def initialize(file)
|
16
|
+
# open.read makes it works for files & IO
|
17
|
+
erb = File.exists?(file) ? open(file).read : file
|
18
|
+
|
19
|
+
erb.gsub!(/<%(.+?)\s*\{\s*(\|.+?\|)?\s*%>/){ %(<%#{$1} do #{$2}%>) }
|
20
|
+
|
21
|
+
# while, case, if, for and blocks...
|
22
|
+
erb.gsub!(/<%(\s*while .+?|\s*case .+?|\s*if .+?|\s*for .+?|.+?do\s*(\|.+?\|)?\s*)%>/){ %(<ruby code="#{$1.gsub(/"/, '"')}">) }
|
23
|
+
# else
|
24
|
+
erb.gsub!(/<%\s*else\s*%>/, %(</ruby><ruby code="else">))
|
25
|
+
# elsif
|
26
|
+
erb.gsub!(/<%\s*(elsif .+?)\s*%>/){ %(</ruby><ruby code="#{$1}">) }
|
27
|
+
# when
|
28
|
+
erb.gsub!(/<%\s*(when .+?)\s*%>/){ %(</ruby><ruby code="#{$1}">) }
|
29
|
+
erb.gsub!(/<%\s*(end|})\s*%>/, %(</ruby>))
|
30
|
+
erb.gsub!(/<%(.+?)\s*%>/){ %(<ruby code="#{$1.gsub(/"/, '"')}"></ruby>) }
|
31
|
+
@slim ||= Hpricot(erb).to_slim
|
10
32
|
end
|
11
33
|
end
|
12
34
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'hpricot'
|
2
2
|
|
3
|
+
Hpricot::XHTMLTransitional.tagset[:ruby] = [:code]
|
4
|
+
# raise Hpricot::XHTMLTransitional.tagset.inspect
|
3
5
|
class Hpricot::BogusETag
|
4
6
|
def to_slim(lvl=0)
|
5
7
|
nil
|
@@ -8,22 +10,14 @@ end
|
|
8
10
|
|
9
11
|
class Hpricot::Text
|
10
12
|
def to_slim(lvl=0)
|
11
|
-
if to_s.strip.empty?
|
12
|
-
|
13
|
-
else
|
14
|
-
(' ' * lvl) + %(| #{to_s.gsub(/\s+/, ' ')})
|
15
|
-
end
|
13
|
+
return nil if to_s.strip.empty?
|
14
|
+
(' ' * lvl) + %(| #{to_s.gsub(/\s+/, ' ')})
|
16
15
|
end
|
17
16
|
end
|
18
17
|
|
19
18
|
class Hpricot::Comment
|
20
19
|
def to_slim(lvl=0)
|
21
|
-
|
22
|
-
if self.content =~ /include (file|virtual)="(.+)"/
|
23
|
-
(' ' * lvl) + "= render '#{$~[2]}'"
|
24
|
-
else
|
25
|
-
nil
|
26
|
-
end
|
20
|
+
nil
|
27
21
|
end
|
28
22
|
end
|
29
23
|
|
@@ -36,22 +30,20 @@ end
|
|
36
30
|
class Hpricot::Elem
|
37
31
|
def slim(lvl=0)
|
38
32
|
r = (' ' * lvl)
|
39
|
-
if self.name ==
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
r += "##{self['id']}"
|
46
|
-
self.remove_attribute('id')
|
47
|
-
end
|
48
|
-
if(self.has_attribute?('class'))
|
49
|
-
r += ".#{self['class'].split(/\s+/).join('.')}"
|
50
|
-
self.remove_attribute('class')
|
51
|
-
end
|
52
|
-
unless attributes_as_html.to_s.strip.empty?
|
53
|
-
r += "[#{attributes_as_html.to_s.strip}]"
|
33
|
+
if self.name == "ruby"
|
34
|
+
if self.attributes["code"].strip[0] == "="
|
35
|
+
return r += self.attributes["code"].strip
|
36
|
+
else
|
37
|
+
return r += "- " + self.attributes["code"].strip
|
38
|
+
end
|
54
39
|
end
|
40
|
+
|
41
|
+
r += self.name unless self.name == 'div' and (self.has_attribute?('id') || self.has_attribute?('class'))
|
42
|
+
r += "##{self['id']}" if self.has_attribute?('id')
|
43
|
+
self.remove_attribute('id')
|
44
|
+
r += ".#{self['class'].split(/\s+/).join('.')}" if self.has_attribute?('class')
|
45
|
+
self.remove_attribute('class')
|
46
|
+
r += "[#{attributes_as_html.to_s.strip}]" unless attributes_as_html.to_s.strip.empty?
|
55
47
|
r
|
56
48
|
end
|
57
49
|
def to_slim(lvl=0)
|
data/lib/html2slim/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: html2slim
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-17 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: hpricot
|
16
|
-
requirement: &
|
16
|
+
requirement: &2153298760 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2153298760
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: minitest
|
27
|
-
requirement: &
|
27
|
+
requirement: &2153298340 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,21 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2153298340
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &2153297920 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2153297920
|
36
47
|
- !ruby/object:Gem::Dependency
|
37
48
|
name: slim
|
38
|
-
requirement: &
|
49
|
+
requirement: &2153297400 !ruby/object:Gem::Requirement
|
39
50
|
none: false
|
40
51
|
requirements:
|
41
52
|
- - ! '>='
|
@@ -43,18 +54,20 @@ dependencies:
|
|
43
54
|
version: 1.0.0
|
44
55
|
type: :development
|
45
56
|
prerelease: false
|
46
|
-
version_requirements: *
|
57
|
+
version_requirements: *2153297400
|
47
58
|
description: Convert HTML to Slim templates. Because HTML sux and Slim rules. That's
|
48
59
|
why.
|
49
60
|
email:
|
50
61
|
- maiz@lulk.in
|
51
62
|
executables:
|
63
|
+
- erb2slim
|
52
64
|
- html2slim
|
53
65
|
extensions: []
|
54
66
|
extra_rdoc_files:
|
55
67
|
- README.md
|
56
68
|
files:
|
57
69
|
- README.md
|
70
|
+
- bin/erb2slim
|
58
71
|
- bin/html2slim
|
59
72
|
- lib/html2slim.rb
|
60
73
|
- lib/html2slim/command.rb
|