html2slim 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ ## HTML2Slim
2
+
3
+ Script for converting HTML (or sHTML) files to slim (slim-lang.org).
4
+
5
+ It's not perfect, but certainly it helps a lot!
6
+
7
+ It's based on Hpricot. Yeah, I'm old school.
8
+
9
+ ## Usage
10
+
11
+ You may convert files using the included executable `html2slim`.
12
+
13
+ # html2slim -h
14
+
15
+ Usage: html2slim INPUT_FILENAME_OR_DIRECTORY [OUTPUT_FILENAME_OR_DIRECTORY] [options]
16
+ --trace Show a full traceback on error
17
+ -d, --delete Delete HTML files
18
+ -h, --help Show this message
19
+ -v, --version Print version
20
+
21
+ Alternatively, to convert files or strings on the fly in your application, you may do so by calling `HTML2Slim.convert!`.
22
+
23
+ ## License
24
+
25
+ This project is released under the MIT license.
26
+
27
+ ## Author
28
+
29
+ [Maiz Lulkin] (https://github.com/joaomilho)
30
+
31
+ ## OFFICIAL REPO
32
+
33
+ https://github.com/slim-template/html2slim
34
+
35
+ ## ROADMAP
36
+
37
+ 1. Improve minitests
38
+ 2. ERB support, I guess...
39
+ 3. Merge with other *2slim gems. Would be handy.
data/bin/html2slim ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.dirname(__FILE__) + '/../lib'
4
+ require 'html2slim/command'
5
+
6
+ cmd = HTML2Slim::Command.new(ARGV)
7
+ cmd.run
data/lib/html2slim.rb ADDED
@@ -0,0 +1,8 @@
1
+ require_relative 'html2slim/version'
2
+ require_relative 'html2slim/converter'
3
+
4
+ module HTML2Slim
5
+ def self.convert!(input)
6
+ Converter.new(input)
7
+ end
8
+ end
@@ -0,0 +1,89 @@
1
+ require 'optparse'
2
+ require 'html2slim'
3
+
4
+ module HTML2Slim
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.banner = "Usage: html2slim INPUT_FILENAME_OR_DIRECTORY [OUTPUT_FILENAME_OR_DIRECTORY] [options]"
28
+
29
+ opts.on('--trace', :NONE, 'Show a full traceback on error') do
30
+ @options[:trace] = true
31
+ end
32
+
33
+ opts.on_tail('-h', '--help', 'Show this message') do
34
+ puts opts
35
+ exit
36
+ end
37
+
38
+ opts.on_tail('-v', '--version', 'Print version') do
39
+ puts "html2slim #{HTML2Slim::VERSION}"
40
+ exit
41
+ end
42
+
43
+ opts.on('-d', '--delete', 'Delete HTML files') do
44
+ @options[:delete] = true
45
+ end
46
+ end
47
+
48
+ def process!
49
+ args = @args.dup
50
+
51
+ @options[:input] = file = args.shift
52
+ @options[:output] = destination = args.shift
53
+
54
+ @options[:input] = file = "-" unless file
55
+
56
+ if File.directory?(@options[:input])
57
+ Dir["#{@options[:input]}/**/*.html"].each { |file| _process(file, destination) }
58
+ else
59
+ _process(file, destination)
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ def _process(file, destination = nil)
66
+ require 'fileutils'
67
+ slim_file = file.sub(/\.html$/, '.slim')
68
+
69
+ if File.directory?(@options[:input]) && destination
70
+ FileUtils.mkdir_p(File.dirname(slim_file).sub(@options[:input].chomp('/'), destination))
71
+ slim_file.sub!(@options[:input].chomp('/'), destination)
72
+ else
73
+ slim_file = destination || slim_file
74
+ end
75
+
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 HTML2Slim.convert!(in_file)
84
+ @options[:output].close
85
+
86
+ File.delete(file) if @options[:delete]
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,12 @@
1
+ require_relative 'hpricot_monkeypatches'
2
+ # html = File.read(ARGV[0])
3
+ module HTML2Slim
4
+ class Converter
5
+ def initialize(html)
6
+ @slim = Hpricot(html).to_slim
7
+ end
8
+ def to_s
9
+ @slim
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,74 @@
1
+ require 'hpricot'
2
+
3
+ class Hpricot::BogusETag
4
+ def to_slim(lvl=0)
5
+ nil
6
+ end
7
+ end
8
+
9
+ class Hpricot::Text
10
+ def to_slim(lvl=0)
11
+ if to_s.strip.empty?
12
+ nil
13
+ else
14
+ (' ' * lvl) + %(| #{to_s.gsub(/\s+/, ' ')})
15
+ end
16
+ end
17
+ end
18
+
19
+ class Hpricot::Comment
20
+ def to_slim(lvl=0)
21
+ # For SHTML <!--#include virtual="foo.html" -->
22
+ if self.content =~ /include (file|virtual)="(.+)"/
23
+ (' ' * lvl) + "= render '#{$~[2]}'"
24
+ else
25
+ nil
26
+ end
27
+ end
28
+ end
29
+
30
+ class Hpricot::DocType
31
+ def to_slim(lvl=0)
32
+ 'doctype'
33
+ end
34
+ end
35
+
36
+ class Hpricot::Elem
37
+ def slim(lvl=0)
38
+ r = (' ' * lvl)
39
+ if self.name == 'div' and (self.has_attribute?('id') || self.has_attribute?('class'))
40
+ r += ''
41
+ else
42
+ r += self.name
43
+ end
44
+ if(self.has_attribute?('id'))
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}]"
54
+ end
55
+ r
56
+ end
57
+ def to_slim(lvl=0)
58
+ if respond_to?(:children) and children
59
+ return %(#{self.slim(lvl)}\n#{children.map { |x| x.to_slim(lvl+1) }.select{|e| !e.nil? }.join("\n")})
60
+ else
61
+ self.slim(lvl)
62
+ end
63
+ end
64
+ end
65
+
66
+ class Hpricot::Doc
67
+ def to_slim
68
+ if respond_to?(:children) and children
69
+ children.map { |x| x.to_slim }.select{|e| !e.nil? }.join("\n")
70
+ else
71
+ ''
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ module HTML2Slim
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html2slim
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Maiz Lulkin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-16 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hpricot
16
+ requirement: &2152919480 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2152919480
25
+ - !ruby/object:Gem::Dependency
26
+ name: minitest
27
+ requirement: &2152919060 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2152919060
36
+ - !ruby/object:Gem::Dependency
37
+ name: slim
38
+ requirement: &2152918540 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2152918540
47
+ description: Convert HTML to Slim templates. Because HTML sux and Slim rules. That's
48
+ why.
49
+ email:
50
+ - maiz@lulk.in
51
+ executables:
52
+ - html2slim
53
+ extensions: []
54
+ extra_rdoc_files:
55
+ - README.md
56
+ files:
57
+ - README.md
58
+ - bin/html2slim
59
+ - lib/html2slim.rb
60
+ - lib/html2slim/command.rb
61
+ - lib/html2slim/converter.rb
62
+ - lib/html2slim/hpricot_monkeypatches.rb
63
+ - lib/html2slim/version.rb
64
+ homepage: https://github.com/slim-template/html2slim
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options:
68
+ - --charset=UTF-8
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.15
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: HTML to Slim converter.
89
+ test_files: []