blueprint-html2slim 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +15 -0
- data/LICENSE +21 -0
- data/README.md +136 -0
- data/bin/html2slim +216 -0
- data/bin/index.html +545 -0
- data/lib/blueprint/html2slim/converter.rb +338 -0
- data/lib/blueprint/html2slim/version.rb +5 -0
- data/lib/blueprint/html2slim.rb +7 -0
- metadata +166 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 460bb27c7423062c1fe30a58b2f432f370361e1134a933ca550644e5f8d05b99
|
4
|
+
data.tar.gz: d0cace18262a72410fe46ac69ae055523aec83f51d2da1c6fb8cd7cda137544c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '08bd9ceb42074244e5323c01120dff8bf91c1daaedee6574b9e09a994b286996ac3bad0e7f4a0e104066890e87943348c4dcfe11ea954fd4ee492149366059c5'
|
7
|
+
data.tar.gz: 59b01b4f8f0d86ca4cab804e246149d37dbc57ccb2345687b181b79dd0e878ee7f3e4c72a4c41921b8e2f73b343f005d817247b9cd15920dd0401e732dc4fad9
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
## [1.0.0] - 2024-01-01
|
6
|
+
|
7
|
+
### Added
|
8
|
+
- Initial release of blueprint-html2slim
|
9
|
+
- HTML to Slim conversion
|
10
|
+
- ERB template support
|
11
|
+
- Smart file naming conventions
|
12
|
+
- Backup option for source files
|
13
|
+
- Recursive directory processing
|
14
|
+
- Dry-run mode
|
15
|
+
- Custom output path support
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2024 Blueprint HTML2Slim Contributors
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
# Blueprint HTML2Slim
|
2
|
+
|
3
|
+
A Ruby gem providing a command-line tool to convert HTML and ERB files to Slim format.
|
4
|
+
|
5
|
+
## Requirements
|
6
|
+
|
7
|
+
- Ruby 2.7 - 3.4 (compatible with latest Ruby versions)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'blueprint-html2slim'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
bundle install
|
21
|
+
```
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
```bash
|
26
|
+
gem install blueprint-html2slim
|
27
|
+
```
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
### Programmatic Usage (Ruby)
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
require 'blueprint/html2slim'
|
35
|
+
|
36
|
+
# Create a converter instance
|
37
|
+
converter = Blueprint::Html2Slim::Converter.new
|
38
|
+
|
39
|
+
# Convert HTML string
|
40
|
+
html = '<div class="container"><h1>Hello</h1></div>'
|
41
|
+
slim = converter.convert(html)
|
42
|
+
puts slim
|
43
|
+
# Output: .container
|
44
|
+
# h1 Hello
|
45
|
+
|
46
|
+
# Convert ERB string
|
47
|
+
erb = '<%= form_for @user do |f| %>
|
48
|
+
<div class="field">
|
49
|
+
<%= f.text_field :name %>
|
50
|
+
</div>
|
51
|
+
<% end %>'
|
52
|
+
slim = converter.convert(erb)
|
53
|
+
# Output: = form_for @user do |f|
|
54
|
+
# .field
|
55
|
+
# = f.text_field :name
|
56
|
+
|
57
|
+
# With custom indentation (default is 2)
|
58
|
+
converter = Blueprint::Html2Slim::Converter.new(indent_size: 4)
|
59
|
+
```
|
60
|
+
|
61
|
+
### Command Line Usage
|
62
|
+
|
63
|
+
Convert a single file:
|
64
|
+
```bash
|
65
|
+
html2slim index.html
|
66
|
+
# Creates: index.html.slim
|
67
|
+
```
|
68
|
+
|
69
|
+
Convert with custom output:
|
70
|
+
```bash
|
71
|
+
html2slim -o custom.slim index.html
|
72
|
+
```
|
73
|
+
|
74
|
+
Convert multiple files:
|
75
|
+
```bash
|
76
|
+
html2slim file1.html file2.erb file3.html.erb
|
77
|
+
```
|
78
|
+
|
79
|
+
Convert and backup original files:
|
80
|
+
```bash
|
81
|
+
html2slim -b index.html
|
82
|
+
# Creates: index.html.slim
|
83
|
+
# Renames: index.html -> index.html.bak
|
84
|
+
```
|
85
|
+
|
86
|
+
Convert files in a directory recursively:
|
87
|
+
```bash
|
88
|
+
html2slim -r ./views
|
89
|
+
```
|
90
|
+
|
91
|
+
Convert to target directory (preserves structure):
|
92
|
+
```bash
|
93
|
+
html2slim -t dist/ -r src/
|
94
|
+
# Converts src/views/index.html to dist/views/index.html.slim
|
95
|
+
```
|
96
|
+
|
97
|
+
Delete source files after conversion:
|
98
|
+
```bash
|
99
|
+
html2slim -d old_templates/*.erb
|
100
|
+
# Converts and deletes the original .erb files
|
101
|
+
```
|
102
|
+
|
103
|
+
Dry run (preview what would be converted):
|
104
|
+
```bash
|
105
|
+
html2slim -n file.html
|
106
|
+
```
|
107
|
+
|
108
|
+
## Naming Convention
|
109
|
+
|
110
|
+
- `file.html` → `file.html.slim`
|
111
|
+
- `file.html.erb` → `file.html.slim`
|
112
|
+
- `file.erb` → `file.slim`
|
113
|
+
|
114
|
+
## Options
|
115
|
+
|
116
|
+
- `-o, --output FILE` - Output file path (only for single file conversion)
|
117
|
+
- `-r, --recursive` - Process directories recursively
|
118
|
+
- `-n, --dry-run` - Show what would be converted without actually converting
|
119
|
+
- `-d, --delete` - Delete source files after successful conversion
|
120
|
+
- `-t, --target-dir DIR` - Target directory for converted files (preserves directory structure)
|
121
|
+
- `-f, --force` - Overwrite existing files without prompting
|
122
|
+
- `-b, --backup` - Backup source files with .bak extension
|
123
|
+
- `-i, --indent SIZE` - Indentation size in spaces (default: 2)
|
124
|
+
- `-h, --help` - Show help message
|
125
|
+
- `-v, --version` - Show version
|
126
|
+
|
127
|
+
## Features
|
128
|
+
|
129
|
+
- Converts HTML tags to Slim syntax
|
130
|
+
- Handles HTML attributes, IDs, and classes
|
131
|
+
- Preserves ERB tags (<%= %> and <% %>)
|
132
|
+
- Supports nested elements
|
133
|
+
- Handles void elements correctly
|
134
|
+
- Preserves comments
|
135
|
+
- Smart output file naming
|
136
|
+
- Optional source file backup
|
data/bin/html2slim
ADDED
@@ -0,0 +1,216 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'thor'
|
4
|
+
require 'pathname'
|
5
|
+
require_relative '../lib/blueprint/html2slim'
|
6
|
+
|
7
|
+
class Html2SlimCLI < Thor
|
8
|
+
class_option :help, aliases: '-h', type: :boolean, desc: 'Show help'
|
9
|
+
|
10
|
+
desc 'FILE [FILE2 ...]', 'Convert HTML/ERB files to Slim format'
|
11
|
+
option :output, aliases: '-o', desc: 'Output file path (only for single file conversion)'
|
12
|
+
option :recursive, aliases: '-r', type: :boolean, desc: 'Process directories recursively'
|
13
|
+
option :dry_run, aliases: '-n', type: :boolean, desc: 'Show what would be converted without actually converting'
|
14
|
+
option :delete, aliases: '-d', type: :boolean, desc: 'Delete source files after successful conversion'
|
15
|
+
option :target_dir, aliases: '-t', desc: 'Target directory for converted files (preserves structure)'
|
16
|
+
option :force, aliases: '-f', type: :boolean, desc: 'Overwrite existing files without prompting'
|
17
|
+
option :backup, aliases: '-b', type: :boolean, desc: 'Backup source files with .bak extension'
|
18
|
+
option :indent, aliases: '-i', type: :numeric, default: 2, desc: 'Indentation size (spaces)'
|
19
|
+
|
20
|
+
def self.start(given_args = ARGV, config = {})
|
21
|
+
if given_args.empty? || given_args.first == 'help' || given_args.include?('-h') || given_args.include?('--help')
|
22
|
+
puts 'Usage: html2slim [OPTIONS] FILE [FILE2 ...]'
|
23
|
+
puts ''
|
24
|
+
puts 'Options:'
|
25
|
+
puts ' -o, --output FILE Output file path (only for single file conversion)'
|
26
|
+
puts ' -r, --recursive Process directories recursively'
|
27
|
+
puts ' -n, --dry-run Show what would be converted without actually converting'
|
28
|
+
puts ' -d, --delete Delete source files after successful conversion'
|
29
|
+
puts ' -t, --target-dir DIR Target directory for converted files (preserves structure)'
|
30
|
+
puts ' -f, --force Overwrite existing files without prompting'
|
31
|
+
puts ' -b, --backup Backup source files with .bak extension'
|
32
|
+
puts ' -i, --indent SIZE Indentation size in spaces (default: 2)'
|
33
|
+
puts ' -h, --help Show this help message'
|
34
|
+
puts ' -v, --version Show version'
|
35
|
+
puts ''
|
36
|
+
puts 'Examples:'
|
37
|
+
puts ' html2slim index.html'
|
38
|
+
puts ' html2slim -o output.slim input.html'
|
39
|
+
puts ' html2slim -r ./views'
|
40
|
+
puts ' html2slim -b file1.html file2.erb'
|
41
|
+
puts ' html2slim -t dist/ -r src/'
|
42
|
+
puts ' html2slim -d old_views/*.erb # Convert and delete sources'
|
43
|
+
exit 0
|
44
|
+
elsif given_args.first == 'version' || given_args.include?('-v') || given_args.include?('--version')
|
45
|
+
puts 'html2slim 1.0.0'
|
46
|
+
exit 0
|
47
|
+
else
|
48
|
+
args = given_args.dup
|
49
|
+
options_index = args.index { |arg| arg.start_with?('-') }
|
50
|
+
|
51
|
+
if options_index
|
52
|
+
files = args[0...options_index]
|
53
|
+
args[options_index..] + files
|
54
|
+
else
|
55
|
+
['process'] + args
|
56
|
+
end
|
57
|
+
|
58
|
+
super(['process'] + given_args, config)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
desc '', ''
|
63
|
+
def process(*files)
|
64
|
+
if files.empty?
|
65
|
+
puts 'Error: No files specified'
|
66
|
+
puts 'Usage: html2slim [OPTIONS] FILE [FILE2 ...]'
|
67
|
+
exit 1
|
68
|
+
end
|
69
|
+
|
70
|
+
if options[:output] && files.size > 1
|
71
|
+
puts 'Error: -o/--output can only be used with a single input file'
|
72
|
+
exit 1
|
73
|
+
end
|
74
|
+
|
75
|
+
if options[:output] && options[:target_dir]
|
76
|
+
puts 'Error: Cannot use both -o/--output and -t/--target-dir together'
|
77
|
+
exit 1
|
78
|
+
end
|
79
|
+
|
80
|
+
if options[:backup] && options[:delete]
|
81
|
+
puts 'Warning: -b/--backup takes precedence over -d/--delete'
|
82
|
+
end
|
83
|
+
|
84
|
+
converter = Blueprint::Html2Slim::Converter.new(indent_size: options[:indent])
|
85
|
+
processed_count = 0
|
86
|
+
error_count = 0
|
87
|
+
|
88
|
+
files.each do |file_path|
|
89
|
+
if File.directory?(file_path)
|
90
|
+
if options[:recursive]
|
91
|
+
process_directory(file_path, converter)
|
92
|
+
else
|
93
|
+
puts "Skipping directory: #{file_path} (use -r to process recursively)"
|
94
|
+
end
|
95
|
+
elsif File.exist?(file_path)
|
96
|
+
result = process_file(file_path, converter)
|
97
|
+
if result
|
98
|
+
processed_count += 1
|
99
|
+
else
|
100
|
+
error_count += 1
|
101
|
+
end
|
102
|
+
else
|
103
|
+
puts "File not found: #{file_path}"
|
104
|
+
error_count += 1
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
puts "\nConversion complete: #{processed_count} file(s) processed, #{error_count} error(s)"
|
109
|
+
exit(1) if error_count.positive?
|
110
|
+
end
|
111
|
+
|
112
|
+
default_task :process
|
113
|
+
|
114
|
+
private
|
115
|
+
|
116
|
+
def process_directory(dir_path, converter)
|
117
|
+
patterns = ['**/*.html', '**/*.erb', '**/*.html.erb']
|
118
|
+
patterns.each do |pattern|
|
119
|
+
Dir.glob(File.join(dir_path, pattern)).each do |file|
|
120
|
+
process_file(file, converter, dir_path)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def process_file(file_path, converter, base_dir = nil)
|
126
|
+
input_path = Pathname.new(file_path)
|
127
|
+
|
128
|
+
# Determine output path
|
129
|
+
output_path = if options[:output]
|
130
|
+
# For single file with -o option, use the specified output path
|
131
|
+
Pathname.new(options[:output])
|
132
|
+
elsif options[:target_dir]
|
133
|
+
# Calculate relative path from base directory
|
134
|
+
relative_path = if base_dir
|
135
|
+
input_path.relative_path_from(Pathname.new(base_dir))
|
136
|
+
else
|
137
|
+
input_path.basename
|
138
|
+
end
|
139
|
+
|
140
|
+
# Apply smart naming convention to the filename
|
141
|
+
output_name = case relative_path.to_s
|
142
|
+
when /\.html\.erb$/
|
143
|
+
relative_path.to_s.sub(/\.html\.erb$/, '.html.slim')
|
144
|
+
when /\.erb$/
|
145
|
+
relative_path.to_s.sub(/\.erb$/, '.slim')
|
146
|
+
when /\.html$/
|
147
|
+
relative_path.to_s.sub(/\.html$/, '.html.slim')
|
148
|
+
else
|
149
|
+
"#{relative_path}.slim"
|
150
|
+
end
|
151
|
+
|
152
|
+
Pathname.new(options[:target_dir]).join(output_name)
|
153
|
+
else
|
154
|
+
# Smart naming convention in place
|
155
|
+
case file_path
|
156
|
+
when /\.html\.erb$/
|
157
|
+
# .html.erb -> .html.slim
|
158
|
+
Pathname.new(file_path.sub(/\.html\.erb$/, '.html.slim'))
|
159
|
+
when /\.erb$/
|
160
|
+
# .erb -> .slim
|
161
|
+
Pathname.new(file_path.sub(/\.erb$/, '.slim'))
|
162
|
+
when /\.html$/
|
163
|
+
# .html -> .html.slim
|
164
|
+
Pathname.new(file_path.sub(/\.html$/, '.html.slim'))
|
165
|
+
else
|
166
|
+
# Default: add .slim extension
|
167
|
+
Pathname.new("#{file_path}.slim")
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
if options[:dry_run]
|
172
|
+
puts "Would convert: #{input_path} -> #{output_path}"
|
173
|
+
puts "Would backup: #{input_path} -> #{input_path}.bak" if options[:backup]
|
174
|
+
puts "Would delete: #{input_path}" if options[:delete]
|
175
|
+
return true
|
176
|
+
end
|
177
|
+
|
178
|
+
if output_path.exist? && !options[:force]
|
179
|
+
print "File exists: #{output_path}. Overwrite? (y/n): "
|
180
|
+
response = $stdin.gets.chomp.downcase
|
181
|
+
return false unless response == 'y'
|
182
|
+
end
|
183
|
+
|
184
|
+
begin
|
185
|
+
html_content = File.read(input_path)
|
186
|
+
slim_content = converter.convert(html_content)
|
187
|
+
|
188
|
+
# Create backup if requested
|
189
|
+
if options[:backup]
|
190
|
+
backup_path = Pathname.new("#{file_path}.bak")
|
191
|
+
File.rename(input_path, backup_path)
|
192
|
+
puts "Backed up: #{input_path} -> #{backup_path}"
|
193
|
+
end
|
194
|
+
|
195
|
+
output_path.dirname.mkpath
|
196
|
+
# Ensure content ends with newline when writing to file
|
197
|
+
slim_content += "\n" unless slim_content.end_with?("\n")
|
198
|
+
File.write(output_path, slim_content)
|
199
|
+
|
200
|
+
puts "Converted: #{input_path} -> #{output_path}"
|
201
|
+
|
202
|
+
# Delete source file if requested (and not backing up)
|
203
|
+
if options[:delete] && !options[:backup]
|
204
|
+
File.delete(input_path)
|
205
|
+
puts "Deleted: #{input_path}"
|
206
|
+
end
|
207
|
+
|
208
|
+
true
|
209
|
+
rescue StandardError => e
|
210
|
+
puts "Error converting #{input_path}: #{e.message}"
|
211
|
+
false
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
Html2SlimCLI.start(ARGV)
|