html_min 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/License.txt ADDED
@@ -0,0 +1 @@
1
+
data/README.rdoc ADDED
@@ -0,0 +1,31 @@
1
+
2
+
3
+ = Riassence HTMLMin
4
+
5
+ * http://rsence.org/
6
+
7
+
8
+ == Description:
9
+
10
+ Simple C extension that squeezes HTML files quickly by removing white space.
11
+ This used to be a fixed part of the Riassence Framework, but it's distributed as a separate gem now.
12
+
13
+ == Usage:
14
+
15
+ require 'html_min'
16
+
17
+ # Makes a HTMLMin instance that squeezes the white-space in html data.
18
+ html_min = HTMLMin.new
19
+
20
+ # Generate a single rangom string
21
+ squeezed_html = html_min.minimize( File.read('document.html') )
22
+
23
+ == Install:
24
+
25
+ * sudo gem install html_min
26
+
27
+ == License:
28
+
29
+ Author: Domen Puncer <domen@cba.si>
30
+ License: BSD
31
+
data/extconf.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'mkmf'
2
+ create_makefile('html_min')
3
+ system('make clean')
4
+ system('make all')
5
+
data/html_min.c ADDED
@@ -0,0 +1,93 @@
1
+ /*
2
+ * html_min - compression of whitespace. Keeps #{, ${ templates intact.
3
+ *
4
+ * Author: Domen Puncer <domen@cba.si>
5
+ * License: BSD
6
+ *
7
+ */
8
+
9
+
10
+ #include <stdlib.h>
11
+ #include "ruby.h"
12
+
13
+ static VALUE html_min_initialize(VALUE self)
14
+ {
15
+ return self;
16
+ }
17
+
18
+ static int isws(int c)
19
+ {
20
+ if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
21
+ return 1;
22
+ return 0;
23
+ }
24
+
25
+ static int html_min(char *dest, const char *src, int len)
26
+ {
27
+ char *orig_dest = dest;
28
+ const char *src_end = src + len;
29
+
30
+ int last_ws = 0;
31
+ int in_template = 0;
32
+
33
+ while (src < src_end) {
34
+ int c = *src++;
35
+ int ws = isws(c);
36
+
37
+ if (in_template == 2) {
38
+ *dest++ = c;
39
+ if (c == '}')
40
+ in_template = 0;
41
+
42
+ } else {
43
+ if (!ws)
44
+ *dest++ = c;
45
+ else if (!last_ws)
46
+ *dest++ = c;
47
+ //*dest++ = ' ';
48
+
49
+ if (c == '#' || c == '$')
50
+ in_template = 1;
51
+ if (in_template == 1 && c == '{')
52
+ in_template = 2;
53
+ }
54
+
55
+ last_ws = ws;
56
+ }
57
+
58
+ return dest - orig_dest;
59
+ }
60
+
61
+ static VALUE html_min_minimize(VALUE self, VALUE str)
62
+ {
63
+ VALUE ret_str;
64
+ const char *src;
65
+ char *dest;
66
+ int src_len;
67
+ int len;
68
+
69
+ src = RSTRING_PTR(str);
70
+ src_len = RSTRING_LEN(str);
71
+
72
+ dest = malloc(src_len);
73
+ if (!dest)
74
+ rb_raise(rb_eNoMemError, "malloc failed in %s", __func__);
75
+
76
+ len = html_min(dest, src, src_len);
77
+
78
+ ret_str = rb_str_new(dest, len);
79
+
80
+ free(dest);
81
+
82
+ return ret_str;
83
+ }
84
+
85
+
86
+ static VALUE cl;
87
+
88
+ void Init_html_min()
89
+ {
90
+ cl = rb_define_class("HTMLMin", rb_cObject);
91
+ rb_define_method(cl, "initialize", html_min_initialize, 0);
92
+ rb_define_method(cl, "minimize", html_min_minimize, 1);
93
+ }
data/html_min.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'html_min'
3
+ s.author = 'Domen Puncer'
4
+ s.email = 'domen@cba.si'
5
+ s.version = '0.1.0'
6
+ s.date = '2010-02-26'
7
+ s.homepage = 'http://www.riassence.org/'
8
+ s.summary = 'Riassence HTMLMin'
9
+ s.has_rdoc = true
10
+ s.require_path = 'html_min'
11
+ s.description = <<END
12
+ Simple C extension that squeezes HTML files quickly by removing white space.
13
+ This used to be a fixed part of the Riassence Framework, but it's distributed as a separate gem now.
14
+ END
15
+ s.files = %w(
16
+ License.txt
17
+ README.rdoc
18
+ extconf.rb
19
+ html_min.c
20
+ html_min.gemspec
21
+ )
22
+ s.files.reject! { |fn| fn.include? ".svn" }
23
+ s.files.reject! { |fn| fn.include? ".git" }
24
+ s.test_file = 'test_html_min.rb'
25
+ s.required_ruby_version = '>= 1.8.6'
26
+ s.extensions = [
27
+ 'extconf.rb'
28
+ ]
29
+ end
30
+
data/test_html_min.rb ADDED
@@ -0,0 +1,57 @@
1
+ require "test/unit"
2
+ require "rubygems"
3
+ require "html_min"
4
+
5
+ class TestHTMLMin < Test::Unit::TestCase
6
+
7
+ @@test_input = %{
8
+
9
+ <html >
10
+ <head > <title
11
+ > fÖö foo </title >
12
+ <body bgcolor = "#ffffff"
13
+ foo = "bar"
14
+ >\#{ foo.foo }</body
15
+ >
16
+
17
+ </html>
18
+
19
+ }
20
+
21
+
22
+ # This is not an error and actually does exactly as specified.
23
+ #
24
+ # All white-space is not compressed, because sometimes white-space is a feature so the
25
+ # amount of white-space is reduced from any amount to 1 chars.
26
+ #
27
+ # The expected output with all white-space removal is:
28
+ #
29
+ # @@test_output = %{<html><head><title>fÖö foo</title><body bgcolor="#ffffff" foo="bar">\#{foo.foo}</body></html>}
30
+ #
31
+ # The actual result is:
32
+ @@test_output = %{
33
+ <html >
34
+ <head > <title
35
+ > fÖö foo </title >
36
+ <body bgcolor = "#ffffff" foo = "bar"
37
+ >\#{ foo.foo }</body >
38
+ </html>
39
+ }
40
+
41
+ def test_init
42
+ html_min = HTMLMin.new
43
+ end
44
+
45
+ def test_types
46
+ html_min = HTMLMin.new
47
+ assert_equal( HTMLMin, html_min.class )
48
+ assert_equal( String, html_min.minimize(@@test_input).class )
49
+ end
50
+
51
+ def test_value
52
+ html_min = HTMLMin.new
53
+ assert_equal( @@test_output, html_min.minimize(@@test_input) )
54
+ end
55
+
56
+ end
57
+
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html_min
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Domen Puncer
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-02-26 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: |
22
+ Simple C extension that squeezes HTML files quickly by removing white space.
23
+ This used to be a fixed part of the Riassence Framework, but it's distributed as a separate gem now.
24
+
25
+ email: domen@cba.si
26
+ executables: []
27
+
28
+ extensions:
29
+ - extconf.rb
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - License.txt
34
+ - README.rdoc
35
+ - extconf.rb
36
+ - html_min.c
37
+ - html_min.gemspec
38
+ has_rdoc: true
39
+ homepage: http://www.riassence.org/
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - html_min
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 1
53
+ - 8
54
+ - 6
55
+ version: 1.8.6
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.6
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Riassence HTMLMin
70
+ test_files:
71
+ - test_html_min.rb