htmlmin 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 +4 -4
- data/lib/htmlmin/slot_machines/comment.rb +10 -0
- data/lib/htmlmin/slot_machines/common.rb +12 -0
- data/lib/htmlmin/slot_machines/doctype.rb +27 -7
- data/lib/htmlmin/slot_machines/tag.rb +16 -2
- data/lib/htmlmin/version.rb +1 -1
- data/lib/htmlmin.rb +7 -4
- data/test/input/comment_7.txt +3 -0
- data/test/input/comment_8.txt +7 -0
- data/test/options/comment_7.rb +6 -0
- data/test/options/comment_8.rb +6 -0
- data/test/output/comment_7.txt +1 -0
- data/test/output/comment_8.txt +6 -0
- data/test/test_htmlmin.rb +7 -1
- metadata +14 -2
data/README.md
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
# Introduction
|
2
2
|
|
3
|
-
|
3
|
+
HTMLMin (HTML minimizer) is a HTML minimizer written in Ruby wich acts like a slot machine.
|
4
4
|
|
5
|
-
This document describes installation, usage, available minifications and development roadmap of
|
5
|
+
This document describes installation, usage, available minifications and development roadmap of HTMLMin.
|
6
6
|
|
7
7
|
Please report issues on [Github] (https://github.com/aishek/htmlmin/issues).
|
8
8
|
|
9
9
|
For feedback, suggestions, etc. write to <aishek@gmail.com>.
|
10
10
|
|
11
|
-
**Important**: this project is in beta and may contain bugs. Current version: 0.0.
|
11
|
+
**Important**: this project is in beta and may contain bugs. Current version: 0.0.2
|
12
12
|
|
13
13
|
# Installation
|
14
14
|
|
@@ -61,4 +61,4 @@ Idea & implementation — Alexandr Borisov (<aishek@gmail.com>)
|
|
61
61
|
|
62
62
|
# Licence
|
63
63
|
|
64
|
-
|
64
|
+
HTMLMin is licensed under [MIT](https://github.com/afelix/csso/blob/master/MIT-LICENSE.txt)
|
@@ -14,6 +14,8 @@ module HTMLMin
|
|
14
14
|
when '-'
|
15
15
|
add_to_buffer char
|
16
16
|
change_state_to :comment_start_end
|
17
|
+
when HTMLMin::Minifier::END_OF_FILE
|
18
|
+
flush_buffer
|
17
19
|
else
|
18
20
|
flush_buffer
|
19
21
|
add_to_result char
|
@@ -22,21 +24,29 @@ module HTMLMin
|
|
22
24
|
end
|
23
25
|
|
24
26
|
def comment_start_end(char)
|
27
|
+
return if char == HTMLMin::Minifier::END_OF_FILE
|
28
|
+
|
25
29
|
add_to_buffer char
|
26
30
|
change_state_to :comment
|
27
31
|
end
|
28
32
|
|
29
33
|
def comment(char)
|
34
|
+
return if char == HTMLMin::Minifier::END_OF_FILE
|
35
|
+
|
30
36
|
add_to_buffer char
|
31
37
|
change_state_to :comment_end_1 if char == '-'
|
32
38
|
end
|
33
39
|
|
34
40
|
def comment_end_1(char)
|
41
|
+
return if char == HTMLMin::Minifier::END_OF_FILE
|
42
|
+
|
35
43
|
add_to_buffer char
|
36
44
|
change_state_to (char == '-' ? :comment_end_2 : :comment)
|
37
45
|
end
|
38
46
|
|
39
47
|
def comment_end_2(char)
|
48
|
+
return if char == HTMLMin::Minifier::END_OF_FILE
|
49
|
+
|
40
50
|
if char == '>'
|
41
51
|
clear_buffer
|
42
52
|
change_state_to :start unless restore_state
|
@@ -28,6 +28,8 @@ module HTMLMin
|
|
28
28
|
when "a".."z", "A".."Z"
|
29
29
|
add_to_buffer char
|
30
30
|
change_state_to :tag_begin
|
31
|
+
when HTMLMin::Minifier::END_OF_FILE
|
32
|
+
flush_buffer
|
31
33
|
else
|
32
34
|
flush_buffer
|
33
35
|
add_to_result char
|
@@ -52,6 +54,8 @@ module HTMLMin
|
|
52
54
|
when 'D'
|
53
55
|
add_to_buffer char
|
54
56
|
change_state_to :doctype_begin_1
|
57
|
+
when HTMLMin::Minifier::END_OF_FILE
|
58
|
+
flush_buffer
|
55
59
|
else
|
56
60
|
flush_buffer
|
57
61
|
add_to_result char
|
@@ -68,8 +72,14 @@ module HTMLMin
|
|
68
72
|
add_to_buffer char
|
69
73
|
end
|
70
74
|
when "<"
|
75
|
+
flush_buffer unless @settings[:minify_whitespaces]
|
76
|
+
|
77
|
+
unshift_spaces_from_buffer
|
71
78
|
add_to_buffer char
|
72
79
|
change_state_to :tag_or_tagend_or_comment_or_doctype_begin
|
80
|
+
when HTMLMin::Minifier::END_OF_FILE
|
81
|
+
unshift_spaces_from_buffer
|
82
|
+
flush_buffer
|
73
83
|
else
|
74
84
|
flush_buffer
|
75
85
|
add_to_result char
|
@@ -84,6 +94,8 @@ module HTMLMin
|
|
84
94
|
when "<"
|
85
95
|
add_to_buffer char
|
86
96
|
change_state_to :tag_or_tagend_or_comment_or_doctype_begin
|
97
|
+
when HTMLMin::Minifier::END_OF_FILE
|
98
|
+
flush_buffer
|
87
99
|
else
|
88
100
|
add_to_result char
|
89
101
|
change_state_to :start
|
@@ -13,6 +13,8 @@ module HTMLMin
|
|
13
13
|
change_state_to :doctype_begin_2
|
14
14
|
when "\s", "\n", "\r", "\t", " "
|
15
15
|
change_state_to_text_prespaced char
|
16
|
+
when HTMLMin::Minifier::END_OF_FILE
|
17
|
+
flush_buffer
|
16
18
|
else
|
17
19
|
flush_buffer
|
18
20
|
|
@@ -29,6 +31,8 @@ module HTMLMin
|
|
29
31
|
when "\s", "\n", "\r", "\t", " "
|
30
32
|
flush_buffer
|
31
33
|
change_state_to_text_prespaced char
|
34
|
+
when HTMLMin::Minifier::END_OF_FILE
|
35
|
+
flush_buffer
|
32
36
|
else
|
33
37
|
flush_buffer
|
34
38
|
|
@@ -45,6 +49,8 @@ module HTMLMin
|
|
45
49
|
when "\s", "\n", "\r", "\t", " "
|
46
50
|
flush_buffer
|
47
51
|
change_state_to_text_prespaced char
|
52
|
+
when HTMLMin::Minifier::END_OF_FILE
|
53
|
+
flush_buffer
|
48
54
|
else
|
49
55
|
flush_buffer
|
50
56
|
|
@@ -61,6 +67,8 @@ module HTMLMin
|
|
61
67
|
when "\s", "\n", "\r", "\t", " "
|
62
68
|
flush_buffer
|
63
69
|
change_state_to_text_prespaced char
|
70
|
+
when HTMLMin::Minifier::END_OF_FILE
|
71
|
+
flush_buffer
|
64
72
|
else
|
65
73
|
flush_buffer
|
66
74
|
|
@@ -77,6 +85,8 @@ module HTMLMin
|
|
77
85
|
when "\s", "\n", "\r", "\t", " "
|
78
86
|
flush_buffer
|
79
87
|
change_state_to_text_prespaced char
|
88
|
+
when HTMLMin::Minifier::END_OF_FILE
|
89
|
+
flush_buffer
|
80
90
|
else
|
81
91
|
flush_buffer
|
82
92
|
|
@@ -93,6 +103,8 @@ module HTMLMin
|
|
93
103
|
when "\s", "\n", "\r", "\t", " "
|
94
104
|
flush_buffer
|
95
105
|
change_state_to_text_prespaced char
|
106
|
+
when HTMLMin::Minifier::END_OF_FILE
|
107
|
+
flush_buffer
|
96
108
|
else
|
97
109
|
flush_buffer
|
98
110
|
|
@@ -109,6 +121,8 @@ module HTMLMin
|
|
109
121
|
when "\s", "\n", "\r", "\t"
|
110
122
|
flush_buffer
|
111
123
|
change_state_to_text_prespaced char
|
124
|
+
when HTMLMin::Minifier::END_OF_FILE
|
125
|
+
flush_buffer
|
112
126
|
else
|
113
127
|
flush_buffer
|
114
128
|
|
@@ -118,14 +132,20 @@ module HTMLMin
|
|
118
132
|
end
|
119
133
|
|
120
134
|
def doctype(char)
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
135
|
+
case char
|
136
|
+
when '>'
|
137
|
+
add_to_buffer char
|
138
|
+
|
139
|
+
unshift_spaces_from_buffer
|
140
|
+
flush_buffer
|
125
141
|
|
126
|
-
|
127
|
-
|
128
|
-
|
142
|
+
add_to_result "\n"
|
143
|
+
change_state_to :start
|
144
|
+
when HTMLMin::Minifier::END_OF_FILE
|
145
|
+
flush_buffer
|
146
|
+
else
|
147
|
+
add_to_buffer char
|
148
|
+
end
|
129
149
|
end
|
130
150
|
|
131
151
|
end
|
@@ -11,6 +11,8 @@ module HTMLMin
|
|
11
11
|
when "a".."z", "A".."Z"
|
12
12
|
add_to_buffer char
|
13
13
|
change_state_to :tag_end_body
|
14
|
+
when HTMLMin::Minifier::END_OF_FILE
|
15
|
+
flush_buffer
|
14
16
|
else
|
15
17
|
flush_buffer
|
16
18
|
change_state_to :start
|
@@ -28,6 +30,8 @@ module HTMLMin
|
|
28
30
|
flush_buffer
|
29
31
|
|
30
32
|
change_state_to :tag_ended
|
33
|
+
when HTMLMin::Minifier::END_OF_FILE
|
34
|
+
flush_buffer
|
31
35
|
else
|
32
36
|
flush_buffer
|
33
37
|
change_state_to :start
|
@@ -35,12 +39,17 @@ module HTMLMin
|
|
35
39
|
end
|
36
40
|
|
37
41
|
def tag_begin(char)
|
38
|
-
add_to_buffer char
|
39
42
|
case char
|
40
43
|
when "/"
|
44
|
+
add_to_buffer char
|
41
45
|
change_state_to :tag_end_expected
|
42
46
|
when ">"
|
47
|
+
add_to_buffer char
|
43
48
|
change_state_to :tag_ended
|
49
|
+
when HTMLMin::Minifier::END_OF_FILE
|
50
|
+
flush_buffer
|
51
|
+
else
|
52
|
+
add_to_buffer char
|
44
53
|
end
|
45
54
|
end
|
46
55
|
|
@@ -49,6 +58,8 @@ module HTMLMin
|
|
49
58
|
when '>'
|
50
59
|
add_to_buffer char
|
51
60
|
change_state_to :tag_ended
|
61
|
+
when HTMLMin::Minifier::END_OF_FILE
|
62
|
+
flush_buffer
|
52
63
|
else
|
53
64
|
flush_buffer
|
54
65
|
change_state_to :start
|
@@ -58,7 +69,7 @@ module HTMLMin
|
|
58
69
|
def tag_ended(char)
|
59
70
|
case char
|
60
71
|
when "\s", "\n", "\r", "\t", " "
|
61
|
-
|
72
|
+
add_to_buffer char unless @settings[:minify_whitespaces]
|
62
73
|
when '<'
|
63
74
|
save_state
|
64
75
|
|
@@ -67,6 +78,9 @@ module HTMLMin
|
|
67
78
|
|
68
79
|
add_to_buffer char
|
69
80
|
change_state_to :tag_or_tagend_or_comment_or_doctype_begin
|
81
|
+
when HTMLMin::Minifier::END_OF_FILE
|
82
|
+
unshift_spaces_from_buffer
|
83
|
+
flush_buffer
|
70
84
|
else
|
71
85
|
unshift_spaces_from_buffer
|
72
86
|
flush_buffer
|
data/lib/htmlmin/version.rb
CHANGED
data/lib/htmlmin.rb
CHANGED
@@ -9,6 +9,8 @@ module HTMLMin
|
|
9
9
|
end
|
10
10
|
|
11
11
|
class Minifier
|
12
|
+
END_OF_FILE = ''
|
13
|
+
|
12
14
|
extend HTMLMin::Buffer
|
13
15
|
extend HTMLMin::SlotMachines::Common
|
14
16
|
extend HTMLMin::SlotMachines::Comment
|
@@ -25,6 +27,7 @@ module HTMLMin
|
|
25
27
|
:debug => false
|
26
28
|
}
|
27
29
|
|
30
|
+
|
28
31
|
def minify(html, options = {})
|
29
32
|
init_settings options
|
30
33
|
|
@@ -55,15 +58,15 @@ module HTMLMin
|
|
55
58
|
char = input[pos]
|
56
59
|
|
57
60
|
self.send @state, char
|
58
|
-
debug_log if @settings[:debug]
|
61
|
+
debug_log(char) if @settings[:debug]
|
59
62
|
|
60
63
|
pos += 1
|
61
64
|
end
|
62
|
-
self.send @state,
|
65
|
+
self.send @state, HTMLMin::Minifier::END_OF_FILE
|
63
66
|
end
|
64
67
|
|
65
|
-
def debug_log
|
66
|
-
p "char: #{char}, state: #{@state.to_s}, buffer: #{@result_buffer.join
|
68
|
+
def debug_log(char)
|
69
|
+
p "char: #{char}, new state: #{@state.to_s}, buffer: '#{@result_buffer.join}', result: '#{@result}'"
|
67
70
|
end
|
68
71
|
|
69
72
|
def save_state
|
@@ -0,0 +1 @@
|
|
1
|
+
<!-- this comment should stay here --><!-- this should also -- because this is not comment<!-- and no whitespaces! -->
|
data/test/test_htmlmin.rb
CHANGED
@@ -7,6 +7,7 @@ class HTMLMinTest < Test::Unit::TestCase
|
|
7
7
|
|
8
8
|
INPUT_DIR = "test/input"
|
9
9
|
OUTPUT_DIR = "test/output"
|
10
|
+
OPTIONS_DIR = "test/options"
|
10
11
|
EXTENSION = "txt"
|
11
12
|
|
12
13
|
def create_test_methods_from_data
|
@@ -22,7 +23,12 @@ class HTMLMinTest < Test::Unit::TestCase
|
|
22
23
|
self.send :define_method, method_name do
|
23
24
|
input = File.read "#{INPUT_DIR}/#{basename}.#{EXTENSION}"
|
24
25
|
output = File.read "#{OUTPUT_DIR}/#{basename}.#{EXTENSION}"
|
25
|
-
|
26
|
+
|
27
|
+
options = {}
|
28
|
+
options_filename = "#{OPTIONS_DIR}/#{basename}.rb"
|
29
|
+
options = eval File.read options_filename if File.exists? options_filename
|
30
|
+
|
31
|
+
minified = HTMLMin.minify(input, options)
|
26
32
|
|
27
33
|
assert_equal output, minified, "Minified '#{basename}' not equals to output!"
|
28
34
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: htmlmin
|
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,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-08-
|
12
|
+
date: 2011-08-22 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: HTMLMin is a HTML-code minification tool implemented in Ruby. It works
|
15
15
|
like a slot machine.
|
@@ -39,6 +39,8 @@ files:
|
|
39
39
|
- test/input/comment_4.txt
|
40
40
|
- test/input/comment_5.txt
|
41
41
|
- test/input/comment_6.txt
|
42
|
+
- test/input/comment_7.txt
|
43
|
+
- test/input/comment_8.txt
|
42
44
|
- test/input/doctype_1.txt
|
43
45
|
- test/input/doctype_2.txt
|
44
46
|
- test/input/tag_1.txt
|
@@ -46,6 +48,8 @@ files:
|
|
46
48
|
- test/input/text_1.txt
|
47
49
|
- test/input/text_2.txt
|
48
50
|
- test/input/text_3.txt
|
51
|
+
- test/options/comment_7.rb
|
52
|
+
- test/options/comment_8.rb
|
49
53
|
- test/output/cc_1.txt
|
50
54
|
- test/output/comment_1.txt
|
51
55
|
- test/output/comment_2.txt
|
@@ -53,6 +57,8 @@ files:
|
|
53
57
|
- test/output/comment_4.txt
|
54
58
|
- test/output/comment_5.txt
|
55
59
|
- test/output/comment_6.txt
|
60
|
+
- test/output/comment_7.txt
|
61
|
+
- test/output/comment_8.txt
|
56
62
|
- test/output/doctype_1.txt
|
57
63
|
- test/output/doctype_2.txt
|
58
64
|
- test/output/tag_1.txt
|
@@ -94,6 +100,8 @@ test_files:
|
|
94
100
|
- test/input/comment_4.txt
|
95
101
|
- test/input/comment_5.txt
|
96
102
|
- test/input/comment_6.txt
|
103
|
+
- test/input/comment_7.txt
|
104
|
+
- test/input/comment_8.txt
|
97
105
|
- test/input/doctype_1.txt
|
98
106
|
- test/input/doctype_2.txt
|
99
107
|
- test/input/tag_1.txt
|
@@ -101,6 +109,8 @@ test_files:
|
|
101
109
|
- test/input/text_1.txt
|
102
110
|
- test/input/text_2.txt
|
103
111
|
- test/input/text_3.txt
|
112
|
+
- test/options/comment_7.rb
|
113
|
+
- test/options/comment_8.rb
|
104
114
|
- test/output/cc_1.txt
|
105
115
|
- test/output/comment_1.txt
|
106
116
|
- test/output/comment_2.txt
|
@@ -108,6 +118,8 @@ test_files:
|
|
108
118
|
- test/output/comment_4.txt
|
109
119
|
- test/output/comment_5.txt
|
110
120
|
- test/output/comment_6.txt
|
121
|
+
- test/output/comment_7.txt
|
122
|
+
- test/output/comment_8.txt
|
111
123
|
- test/output/doctype_1.txt
|
112
124
|
- test/output/doctype_2.txt
|
113
125
|
- test/output/tag_1.txt
|