html2slim-ruby3 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 341a612643fd43e43994291229aca4b10e34f3c598ce34e1dda8dacfc2fb5652
4
+ data.tar.gz: 5477ca1c64b65d3e2082747f73a776fe0dff8425a19b7e02b05a6b7bc04076cf
5
+ SHA512:
6
+ metadata.gz: 3cfcd443f10f9d2985dba9ff19bb05422e4e7f4cf34b2a105fa94859646ce7c1a9a3a783d5c75da33559632c55c2aee526fa09401f833de5e43f437ef3402fe1
7
+ data.tar.gz: 25de1efd4886a7ba6791710aafb10a9c03574f1884939700c5cfb937414eee8ce8d2742cc0aa4773862a0fe9b03fa2ce8f2b031b03e2adf692855f51208fed2d
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ ![Version](https://img.shields.io/gem/v/html2slim.svg)
2
+
3
+ [![Build Status](https://travis-ci.org/slim-template/html2slim.png?branch=master)](https://travis-ci.org/slim-template/html2slim)
4
+
5
+ [![Code climate](https://codeclimate.com/github/slim-template/html2slim.png)](https://codeclimate.com/github/slim-template/html2slim)
6
+
7
+ ## HTML2Slim
8
+
9
+ Script for converting HTML and ERB files to [slim](http://slim-lang.com/).
10
+
11
+ ## Usage
12
+
13
+ You may convert files using the included executables `html2slim` and `erb2slim`.
14
+
15
+ # html2slim -h
16
+
17
+ Usage: html2slim INPUT_FILENAME_OR_DIRECTORY [OUTPUT_FILENAME_OR_DIRECTORY] [options]
18
+ --trace Show a full traceback on error
19
+ -d, --delete Delete HTML files
20
+ -h, --help Show this message
21
+ -v, --version Print version
22
+
23
+ # erb2slim -h
24
+
25
+ Usage: erb2slim INPUT_FILENAME_OR_DIRECTORY [OUTPUT_FILENAME_OR_DIRECTORY] [options]
26
+ --trace Show a full traceback on error
27
+ -d, --delete Delete ERB files
28
+ -h, --help Show this message
29
+ -v, --version Print version
30
+
31
+ 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`.
32
+
33
+ ## License
34
+
35
+ This project is released under the MIT license.
36
+
37
+ ## Author
38
+
39
+ [Maiz Lulkin](https://github.com/joaomilho) and [contributors](https://github.com/slim-template/html2slim/graphs/contributors)
40
+
41
+ ## OFFICIAL REPO
42
+
43
+ [https://github.com/slim-template/html2slim](https://github.com/slim-template/html2slim)
44
+
45
+ ## GOOD TO KNOW
46
+
47
+ ERB requires a full Ruby parser, so it doesn't really work for all use cases, but it's certainly helpful.
data/bin/erb2slim 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::ERBCommand.new(ARGV)
7
+ cmd.run
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::HTMLCommand.new(ARGV)
7
+ cmd.run
@@ -0,0 +1,108 @@
1
+ require 'optparse'
2
+ require 'html2slim'
3
+
4
+ module HTML2Slim
5
+ class Command
6
+
7
+ def initialize(args)
8
+ @args = args
9
+ @options = {}
10
+ end
11
+
12
+ def run
13
+ @opts = OptionParser.new(&method(:set_opts))
14
+ @opts.parse!(@args)
15
+ process!
16
+ exit 0
17
+ rescue Exception => ex
18
+ raise ex if @options[:trace] || SystemExit === ex
19
+ $stderr.print "#{ex.class}: " if ex.class != RuntimeError
20
+ $stderr.puts ex.message
21
+ $stderr.puts ' Use --trace for backtrace.'
22
+ exit 1
23
+ end
24
+
25
+ protected
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
+
35
+ def set_opts(opts)
36
+ opts.banner = "Usage: #{command_name} INPUT_FILENAME_OR_DIRECTORY [OUTPUT_FILENAME_OR_DIRECTORY] [options]"
37
+
38
+ opts.on('--trace', :NONE, 'Show a full traceback on error') do
39
+ @options[:trace] = true
40
+ end
41
+
42
+ opts.on_tail('-h', '--help', 'Show this message') do
43
+ puts opts
44
+ exit
45
+ end
46
+
47
+ opts.on_tail('-v', '--version', 'Print version') do
48
+ puts "#{command_name} #{HTML2Slim::VERSION}"
49
+ exit
50
+ end
51
+
52
+ opts.on('-d', '--delete', "Delete #{format.upcase} files") do
53
+ @options[:delete] = true
54
+ end
55
+ end
56
+
57
+ def process!
58
+ args = @args.dup
59
+
60
+ @options[:input] = file = args.shift
61
+ @options[:output] = destination = args.shift
62
+
63
+ @options[:input] = file = "-" unless file
64
+
65
+ if File.directory?(@options[:input])
66
+ Dir["#{@options[:input]}/**/*.#{format}"].each { |file| _process(file, destination) }
67
+ else
68
+ _process(file, destination)
69
+ end
70
+ end
71
+
72
+ private
73
+
74
+ def input_is_dir?
75
+ File.directory? @options[:input]
76
+ end
77
+
78
+ def _process(file, destination = nil)
79
+ require 'fileutils'
80
+ slim_file = file.sub(/\.#{format}/, '.slim')
81
+
82
+ if input_is_dir? && destination
83
+ FileUtils.mkdir_p(File.dirname(slim_file).sub(@options[:input].chomp('/'), destination))
84
+ slim_file.sub!(@options[:input].chomp('/'), destination)
85
+ else
86
+ slim_file = destination || slim_file
87
+ end
88
+
89
+ fail(ArgumentError, "Source and destination files can't be the same.") if @options[:input] != '-' && file == slim_file
90
+
91
+ in_file = if @options[:input] == "-"
92
+ $stdin
93
+ else
94
+ File.open(file, 'r')
95
+ end
96
+
97
+ @options[:output] = slim_file && slim_file != '-' ? File.open(slim_file, 'w') : $stdout
98
+ @options[:output].puts HTML2Slim.convert!(in_file, format)
99
+ @options[:output].close
100
+
101
+ File.delete(file) if @options[:delete]
102
+ end
103
+ end
104
+ class HTMLCommand < Command
105
+ end
106
+ class ERBCommand < Command
107
+ end
108
+ end
@@ -0,0 +1,34 @@
1
+ require_relative 'hpricot_monkeypatches'
2
+
3
+ module HTML2Slim
4
+ class Converter
5
+ def to_s
6
+ @slim
7
+ end
8
+ end
9
+ class HTMLConverter < Converter
10
+ def initialize(html)
11
+ @slim = Hpricot(html).to_slim
12
+ end
13
+ end
14
+ class ERBConverter < Converter
15
+ def initialize(file)
16
+ # open.read makes it works for files & IO
17
+ erb = File.exist?(file) ? open(file).read : file
18
+
19
+ erb.gsub!(/<%(.+?)\s*\{\s*(\|.+?\|)?\s*%>/){ %(<%#{$1} do #{$2}%>) }
20
+
21
+ # case, if, for, unless, until, while, and blocks...
22
+ erb.gsub!(/<%(-\s+)?((\s*(case|if|for|unless|until|while) .+?)|.+?do\s*(\|.+?\|)?\s*)-?%>/){ %(<ruby code="#{$2.gsub(/"/, '&quot;')}">) }
23
+ # else
24
+ erb.gsub!(/<%-?\s*else\s*-?%>/, %(</ruby><ruby code="else">))
25
+ # elsif
26
+ erb.gsub!(/<%-?\s*(elsif .+?)\s*-?%>/){ %(</ruby><ruby code="#{$1.gsub(/"/, '&quot;')}">) }
27
+ # when
28
+ erb.gsub!(/<%-?\s*(when .+?)\s*-?%>/){ %(</ruby><ruby code="#{$1.gsub(/"/, '&quot;')}">) }
29
+ erb.gsub!(/<%\s*(end|}|end\s+-)\s*%>/, %(</ruby>))
30
+ erb.gsub!(/<%-?(.+?)\s*-?%>/m){ %(<ruby code="#{$1.gsub(/"/, '&quot;')}"></ruby>) }
31
+ @slim ||= Hpricot(erb).to_slim
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,130 @@
1
+ require 'hpricot'
2
+
3
+ Hpricot::XHTMLTransitional.tagset[:ruby] = [:code]
4
+
5
+ module SlimText
6
+ def to_slim(lvl=0)
7
+ return nil if to_s.strip.empty?
8
+ (' ' * lvl) + %(| #{to_s.gsub(/\s+/, ' ')})
9
+ end
10
+ end
11
+
12
+ class Hpricot::CData
13
+ include SlimText
14
+ end
15
+
16
+ class Hpricot::BogusETag
17
+ def to_slim(lvl=0)
18
+ nil
19
+ end
20
+ end
21
+
22
+ class Hpricot::Text
23
+ def to_slim(lvl=0)
24
+ str = content.to_s
25
+ return nil if str.strip.empty?
26
+ (' ' * lvl) + %(| #{str.gsub(/\s+/, ' ')})
27
+ end
28
+ end
29
+
30
+ class Hpricot::Comment
31
+ def to_slim(lvl=0)
32
+ nil
33
+ end
34
+ end
35
+
36
+ class Hpricot::DocType
37
+ def to_slim(lvl=0)
38
+ if to_s.include? "xml"
39
+ to_s.include?("iso-8859-1") ? "doctype xml ISO-88591" : "doctype xml"
40
+ elsif to_s.include? "XHTML" or self.to_s.include? "HTML 4.01"
41
+ available_versions = Regexp.union ["Basic", "1.1", "strict", "Frameset", "Mobile", "Transitional"]
42
+ version = to_s.match(available_versions).to_s.downcase
43
+ "doctype #{version}"
44
+ else
45
+ "doctype html"
46
+ end
47
+ end
48
+ end
49
+
50
+ class Hpricot::Elem
51
+ BLANK_RE = /\A[[:space:]]*\z/
52
+
53
+ def slim(lvl=0)
54
+ r = ' ' * lvl
55
+
56
+ return r + slim_ruby_code(r) if ruby?
57
+
58
+ r += name unless skip_tag_name?
59
+ r += slim_id
60
+ r += slim_class
61
+ r += slim_attributes
62
+ r
63
+ end
64
+
65
+ def to_slim(lvl=0)
66
+ if respond_to?(:children) and children
67
+ return %(#{slim(lvl)}\n#{children.map{|c| c.to_slim(lvl+1) }.select{|e| !e.nil? }.join("\n")})
68
+ else
69
+ slim(lvl)
70
+ end
71
+ end
72
+
73
+ private
74
+
75
+ def slim_ruby_code(r)
76
+ (code.strip[0] == "=" ? "" : "- ") + code.strip.gsub(/\n/, "\n#{r}- ")
77
+ end
78
+
79
+ def code
80
+ attributes["code"]
81
+ end
82
+
83
+ def skip_tag_name?
84
+ div? and (has_id? || has_class?)
85
+ end
86
+
87
+ def slim_id
88
+ has_id?? "##{self['id']}" : ""
89
+ end
90
+
91
+ def slim_class
92
+ has_class?? ".#{self['class'].strip.split(/\s+/).join('.')}" : ""
93
+ end
94
+
95
+ def slim_attributes
96
+ remove_attribute('class')
97
+ remove_attribute('id')
98
+ has_attributes?? "[#{attributes_as_html.to_s.strip}]" : ""
99
+ end
100
+
101
+ def has_attributes?
102
+ attributes.to_hash.any?
103
+ end
104
+
105
+ def has_id?
106
+ has_attribute?('id') && !(BLANK_RE === self['id'])
107
+ end
108
+
109
+ def has_class?
110
+ has_attribute?('class') && !(BLANK_RE === self['class'])
111
+ end
112
+
113
+ def ruby?
114
+ name == "ruby"
115
+ end
116
+
117
+ def div?
118
+ name == "div"
119
+ end
120
+ end
121
+
122
+ class Hpricot::Doc
123
+ def to_slim
124
+ if respond_to?(:children) and children
125
+ children.map { |x| x.to_slim }.select{|e| !e.nil? }.join("\n")
126
+ else
127
+ ''
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,3 @@
1
+ module HTML2Slim
2
+ VERSION = '0.2.1'
3
+ end
data/lib/html2slim.rb ADDED
@@ -0,0 +1,12 @@
1
+ require_relative 'html2slim/version'
2
+ require_relative 'html2slim/converter'
3
+
4
+ module HTML2Slim
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
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html2slim-ruby3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Maiz Lulkin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-10-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hpricot
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
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: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
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: slim
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 1.0.0
69
+ description: Convert HTML to Slim templates. Because HTML sux and Slim rules. That's
70
+ why.
71
+ email:
72
+ - maiz@lulk.in
73
+ executables:
74
+ - erb2slim
75
+ - html2slim
76
+ extensions: []
77
+ extra_rdoc_files:
78
+ - README.md
79
+ files:
80
+ - README.md
81
+ - bin/erb2slim
82
+ - bin/html2slim
83
+ - lib/html2slim.rb
84
+ - lib/html2slim/command.rb
85
+ - lib/html2slim/converter.rb
86
+ - lib/html2slim/hpricot_monkeypatches.rb
87
+ - lib/html2slim/version.rb
88
+ homepage: https://github.com/slim-template/html2slim
89
+ licenses: []
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options:
93
+ - "--charset=UTF-8"
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubygems_version: 3.4.21
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: HTML to Slim converter.
111
+ test_files: []