html_compressor 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require "rubygems"
2
+ require "rake/gempackagetask"
3
+ require "rake/rdoctask"
4
+ require "rake/testtask"
5
+
6
+ task :default => :test
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.libs += ["lib", "test"]
10
+ t.test_files = FileList["test/*_test.rb"]
11
+ t.verbose = true
12
+ end
13
+
14
+ Rake::RDocTask.new do |t|
15
+ t.rdoc_files.include("README.rdoc", "lib/**/*.rb")
16
+ end
17
+
18
+ Rake::GemPackageTask.new(eval(IO.read(File.join(File.dirname(__FILE__), "html_compressor.gemspec")))) do |pkg|
19
+ pkg.need_zip = true
20
+ pkg.need_tar = true
21
+ end
@@ -0,0 +1,128 @@
1
+ require "popen4"
2
+ require "shellwords"
3
+ require "stringio"
4
+
5
+ module HtmlCompressor #:nodoc:
6
+ class Compressor
7
+ VERSION = "0.0.1"
8
+
9
+ class Error < StandardError; end
10
+ class OptionError < Error; end
11
+ class RuntimeError < Error; end
12
+
13
+ attr_reader :options
14
+
15
+ def self.default_options #:nodoc:
16
+ { :charset => "utf-8", :line_break => nil }
17
+ end
18
+
19
+ def self.compressor_type #:nodoc:
20
+ raise Error, "create a HtmlCompressor instead"
21
+ end
22
+
23
+ def initialize(options = {}) #:nodoc:
24
+ @options = self.class.default_options.merge(options)
25
+ @command = [path_to_java, "-jar", path_to_jar_file, *(command_option_for_type + command_options)]
26
+ end
27
+
28
+ def command #:nodoc:
29
+ @command.map { |word| Shellwords.escape(word) }.join(" ")
30
+ end
31
+
32
+ # Compress a stream or string of code with HTML Compressor. (A stream is
33
+ # any object that responds to +read+ and +close+ like an IO.) If a block
34
+ # is given, you can read the compressed code from the block's argument.
35
+ # Otherwise, +compress+ returns a string of compressed code.
36
+ #
37
+ # ==== Example: Compress HTML
38
+ # compressor = HtmlCompressor::HtmlCompressor.new
39
+ # compressor.compress(<<-END_HTML)
40
+ # <html>
41
+ # <head>
42
+ # </head>
43
+ # <body>
44
+ # fdgdfgf
45
+ #
46
+ #
47
+ # </body>
48
+ # </html>
49
+ # END_HTML
50
+ # # => "<html><head></head><body>fdgdfgf</body></html>"
51
+ #
52
+ def compress(stream_or_string)
53
+ streamify(stream_or_string) do |stream|
54
+ output = true
55
+ status = POpen4.popen4(command, "b") do |stdout, stderr, stdin, pid|
56
+ begin
57
+ stdin.binmode
58
+ transfer(stream, stdin)
59
+
60
+ if block_given?
61
+ yield stdout
62
+ else
63
+ output = stdout.read
64
+ end
65
+
66
+ rescue Exception => e
67
+ raise RuntimeError, "compression failed"
68
+ end
69
+ end
70
+
71
+ if status.exitstatus.zero?
72
+ output
73
+ else
74
+ raise RuntimeError, "compression failed"
75
+ end
76
+ end
77
+ end
78
+
79
+ private
80
+ def command_options
81
+ options.inject([]) do |command_options, (name, argument)|
82
+ method = begin
83
+ method(:"command_option_for_#{name}")
84
+ rescue NameError
85
+ raise OptionError, "undefined option #{name.inspect}"
86
+ end
87
+
88
+ command_options.concat(method.call(argument))
89
+ end
90
+ end
91
+
92
+ def path_to_java
93
+ options.delete(:java) || "java"
94
+ end
95
+
96
+ def path_to_jar_file
97
+ options.delete(:jar_file) || File.join(File.dirname(__FILE__), *%w".. htmlcompressor-1.3.1.jar")
98
+ end
99
+
100
+ def streamify(stream_or_string)
101
+ if stream_or_string.respond_to?(:read)
102
+ yield stream_or_string
103
+ else
104
+ yield StringIO.new(stream_or_string.to_s)
105
+ end
106
+ end
107
+
108
+ def transfer(from_stream, to_stream)
109
+ while buffer = from_stream.read(4096)
110
+ to_stream.write(buffer)
111
+ end
112
+ from_stream.close
113
+ to_stream.close
114
+ end
115
+
116
+ def command_option_for_type
117
+ ["--type", self.class.compressor_type.to_s]
118
+ end
119
+
120
+ def command_option_for_charset(charset)
121
+ ["--charset", charset.to_s]
122
+ end
123
+
124
+ def command_option_for_line_break(line_break)
125
+ line_break ? ["--line-break", line_break.to_s] : []
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,36 @@
1
+ require "popen4"
2
+ require "shellwords"
3
+ require "stringio"
4
+
5
+ module HtmlCompressor #:nodoc:
6
+ class HtmlCompressor < Compressor
7
+ def self.compressor_type #:nodoc:
8
+ "html"
9
+ end
10
+
11
+ def self.default_options #:nodoc:
12
+ super.merge(
13
+ :munge => false,
14
+ :optimize => true,
15
+ :preserve_semicolons => false
16
+ )
17
+ end
18
+
19
+ def initialize(options = {})
20
+ super
21
+ end
22
+
23
+ private
24
+ def command_option_for_munge(munge)
25
+ munge ? [] : ["--nomunge"]
26
+ end
27
+
28
+ def command_option_for_optimize(optimize)
29
+ optimize ? [] : ["--disable-optimizations"]
30
+ end
31
+
32
+ def command_option_for_preserve_semicolons(preserve_semicolons)
33
+ preserve_semicolons ? ["--preserve-semi"] : []
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,2 @@
1
+ require "html_compressor/compressor"
2
+ require "html_compressor/html_compressor"
Binary file
Binary file
@@ -0,0 +1,7 @@
1
+ require "test/unit"
2
+ require "html_compressor/compressor"
3
+
4
+ module HtmlCompressor
5
+ class CompressorTest < Test::Unit::TestCase
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html_compressor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ivan Turkovic
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-05-16 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: POpen4
16
+ requirement: &2156656280 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.1.4
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2156656280
25
+ description: Easily compress your files with html_compressor. Use html_compressor
26
+ natively inside ruby code.
27
+ email: me@ivanturkovic.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - Rakefile
33
+ - lib/html_compressor/compressor.rb
34
+ - lib/html_compressor/html_compressor.rb
35
+ - lib/html_compressor.rb
36
+ - lib/htmlcompressor-1.3.1.jar
37
+ - lib/yuicompressor-2.4.4.jar
38
+ - test/html_compressor_test.rb
39
+ homepage: http://github.com/thorsson/html_compressor/
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project: html_compressor
59
+ rubygems_version: 1.8.1
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: HTML wrapper for htmlcompressor
63
+ test_files:
64
+ - test/html_compressor_test.rb