devfu-simple-tidy 0.1.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.
data/README.rdoc ADDED
@@ -0,0 +1,27 @@
1
+ = SimpleTidy
2
+
3
+ I've had a lot of trouble with the http://rubyforge.org/projects/tidy/ project (which uses libtidy-ruby),
4
+ getting segfaults and whatnot. So ... I decided to make super simple wrapper around the <b>tidy</b>
5
+ command line app (instead of the C library).
6
+
7
+ == Installation
8
+
9
+ $ sudo gem install devfu-simple-tidy --source http://gems.github.com
10
+
11
+ == Usage
12
+
13
+ require 'simple-tidy'
14
+
15
+ >> puts SimpleTidy.clean '<html>foo</html>', :indent_spaces => 2
16
+ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
17
+
18
+ <html>
19
+ <head>
20
+ <title></title>
21
+ </head>
22
+
23
+ <body>
24
+ foo
25
+ </body>
26
+ </html>
27
+ => nil
data/Rakefile ADDED
@@ -0,0 +1,68 @@
1
+ require 'rake'
2
+ require 'rubygems'
3
+ require 'rake/rdoctask'
4
+ require 'spec/rake/spectask'
5
+
6
+ puts "\nGem: simple-tidy\n\n"
7
+
8
+ begin
9
+ require 'jeweler'
10
+ Jeweler::Tasks.new do |s|
11
+ s.name = 'simple-tidy'
12
+ s.summary = ''
13
+ s.email = 'remi@remitaylor.com'
14
+ s.homepage = 'http://github.com/devfu/simple-tidy'
15
+ s.description = ''
16
+ s.authors = %w( remi )
17
+ s.files = FileList['[A-Z]*', '{lib,spec,bin,examples}/**/*']
18
+ # s.add_dependency 'person-gemname'
19
+ # s.executables << 'script'
20
+ # s.rubyforge_project = 'gemname'
21
+ # s.extra_rdoc_files = %w( README.rdoc )
22
+ end
23
+ rescue LoadError
24
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new do |t|
28
+ t.spec_files = FileList['spec/**/*_spec.rb']
29
+ end
30
+
31
+ desc "Run all examples with RCov"
32
+ Spec::Rake::SpecTask.new('rcov') do |t|
33
+ t.spec_files = FileList['spec/**/*_spec.rb']
34
+ t.rcov = true
35
+ end
36
+
37
+ # require 'hanna'
38
+ # require 'darkfish-rdoc'
39
+
40
+ Rake::RDocTask.new do |rdoc|
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = 'simple-tidy'
43
+ rdoc.options << '--line-numbers' << '--inline-source'
44
+ # rdoc.options += ["--template=#{`allison --path`}"] # sudo gem install allison
45
+ # rdoc.options += %w( -f darkfish ) # sudo gem install darkfish-rdoc
46
+ # rdoc.options += %w( -T hanna ) # sudo gem install mislav-hanna
47
+ rdoc.options += %w( -m README.rdoc ) # the initial page displayed
48
+ rdoc.rdoc_files.include('README.rdoc')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
51
+
52
+ desc 'Confirm that gemspec is $SAFE'
53
+ task :safe do
54
+ require 'yaml'
55
+ require 'rubygems/specification'
56
+ data = File.read('simple-tidy.gemspec')
57
+ spec = nil
58
+ if data !~ %r{!ruby/object:Gem::Specification}
59
+ Thread.new { spec = eval("$SAFE = 3\n#{data}") }.join
60
+ else
61
+ spec = YAML.load(data)
62
+ end
63
+ spec.validate
64
+ puts spec
65
+ puts "OK"
66
+ end
67
+
68
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,60 @@
1
+ require 'open3'
2
+
3
+ class SimpleTidy
4
+
5
+ DEFAULT_OPTIONS = { :tidy_mark => false }
6
+
7
+ OPTIONS_WITHOUT_VALUES = [ :indent ]
8
+
9
+ SINGLE_DASH_OPTIONS = [ :indent ]
10
+
11
+ def initialize options = nil
12
+ @tidy_options = DEFAULT_OPTIONS.merge(options || {})
13
+ end
14
+
15
+ def warnings
16
+ @warnings || []
17
+ end
18
+
19
+ def clean html
20
+ @output, @warnings = get_output_and_warnings html
21
+ @output
22
+ end
23
+
24
+ def self.clean html, options = nil
25
+ SimpleTidy.new(options).clean html
26
+ end
27
+
28
+ private
29
+
30
+ def get_output_and_warnings html
31
+ stdin, stdout, stderr = Open3.popen3 "echo '#{ html }' | tidy #{ tidy_options_text }"
32
+ output = stdout.read.strip
33
+ warnings = stderr.read.split("\n").select {|line| line =~ /line \d+ column \d+ - Warning:/ }
34
+ return output, warnings
35
+ end
36
+
37
+ def tidy_options
38
+
39
+ # check options for dependencies (some options require other options to be set)
40
+ if @tidy_options.keys.include? :indent_spaces and ! @tidy_options[:indent] == true
41
+ @tidy_options[:indent] = true
42
+ end
43
+
44
+ @tidy_options
45
+ end
46
+
47
+ def tidy_options_text
48
+ tidy_options.map {|key, value|
49
+ option = dashes_for_option key
50
+ option << key.to_s.gsub('_', '-')
51
+ option << " #{ value.to_s.gsub('_', '-') }" unless OPTIONS_WITHOUT_VALUES.include?(key)
52
+ option
53
+ }.join(' ')
54
+ end
55
+
56
+ def dashes_for_option option
57
+ SINGLE_DASH_OPTIONS.include?(option) ? '-' : '--'
58
+ end
59
+
60
+ end
@@ -0,0 +1,45 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe SimpleTidy do
4
+
5
+ it 'should be able to easily tidy html (without any options)' do
6
+ html = '<html>foo</html>'
7
+
8
+ SimpleTidy.new.clean(html).should ==
9
+ "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\n" +
10
+ "<html>\n<head>\n<title></title>\n</head>\n<body>\nfoo\n</body>\n</html>"
11
+ end
12
+
13
+ it 'should have a shortcut to tidy html without creating a SimpleTidy instance' do
14
+ html = '<html>foo</html>'
15
+
16
+ SimpleTidy.clean(html).should == SimpleTidy.new.clean(html)
17
+ end
18
+
19
+ it 'should be able to pass options to shortcut'
20
+
21
+ it 'should be able to get warnings' do
22
+ html = '<html>foo</html>'
23
+ tidy = SimpleTidy.new
24
+
25
+ tidy.warnings.should be_empty
26
+ tidy.clean html
27
+ tidy.warnings.length.should == 4
28
+ tidy.warnings.should include('line 1 column 1 - Warning: missing <!DOCTYPE> declaration')
29
+ tidy.warnings.should include("line 1 column 7 - Warning: inserting missing 'title' element")
30
+ end
31
+
32
+ # it 'should be able to get errors'
33
+ # it 'should be able to get diagnostics ... aka info? ... aka debug?'
34
+
35
+ it 'should be able to set the indent option' do
36
+ html = '<html>foo</html>'
37
+
38
+ SimpleTidy.new( :indent_spaces => 2 ).clean(html).should ==
39
+ "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\n\n" +
40
+ "<html>\n<head>\n <title></title>\n</head>\n\n<body>\n foo\n</body>\n</html>"
41
+
42
+ SimpleTidy.clean( html, :indent_spaces => 2 ).should == SimpleTidy.new( :indent_spaces => 2 ).clean(html)
43
+ end
44
+
45
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format specdoc
3
+ --loadby mtime
4
+ --reverse
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require File.dirname(__FILE__) + '/../lib/simple-tidy'
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: devfu-simple-tidy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - remi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-17 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: ""
17
+ email: remi@remitaylor.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - README.rdoc
26
+ - Rakefile
27
+ - VERSION
28
+ - lib/simple-tidy.rb
29
+ - spec/simple-tidy_spec.rb
30
+ - spec/spec.opts
31
+ - spec/spec_helper.rb
32
+ has_rdoc: false
33
+ homepage: http://github.com/devfu/simple-tidy
34
+ licenses:
35
+ post_install_message:
36
+ rdoc_options:
37
+ - --charset=UTF-8
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.3.5
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: ""
59
+ test_files:
60
+ - spec/simple-tidy_spec.rb
61
+ - spec/spec_helper.rb