simple-tidy 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/simple-tidy.rb +66 -0
  2. metadata +55 -0
@@ -0,0 +1,66 @@
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 "tidy #{ tidy_options_text } #{ temp_file(html) }"
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
+ def temp_file html
61
+ filename = "/tmp/simple-tidy-#{ Time.now.strftime('%Y%m%d%H%M%S') }.html"
62
+ File.open(filename, 'w'){|f| f << html }
63
+ filename
64
+ end
65
+
66
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-tidy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - remi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-10 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A simple wrapper around the Tidy command line interface (not libtidy-ruby)
17
+ email: remi@remitaylor.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/simple-tidy.rb
26
+ has_rdoc: true
27
+ homepage: http://github.com/devfu/simple-tidy
28
+ licenses: []
29
+
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.3.5
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: A simple wrapper around Tidy
54
+ test_files: []
55
+