terrbear-tidy 1.1.3 → 1.1.4

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/CHANGES ADDED
@@ -0,0 +1,75 @@
1
+ V 1.1.3 2009/06/01
2
+
3
+ - Updated tidybuf to work with new tidy distrubtions
4
+ (TidyAllocator was introduced)
5
+
6
+ V 1.1.2 2005/03/14
7
+
8
+ - Tidybuf.to_s bugfix: added nil check
9
+ @struct.bp ? @struct.bp.to_s(@struct.size) : ''
10
+
11
+ V 1.1.1 2005/03/07
12
+
13
+ - Bugfix
14
+
15
+ Tidybuf.to_s assumes that TidyBuffer.bp is null-terminated. This is
16
+ only true if the length of the data is evenly divisable by 256, but
17
+ if Tidy output is e.g. 512 bytes long the buffer is not null-terminated
18
+ and the code reads out of bounds. Result: garbage chars in output.
19
+
20
+ Fix: Code changed to read only TidyBuffer.size bytes from the buffer.
21
+ In TidyBuffer#to_s: @struct.bp.to_s => @struct.bp.to_s(@struct.size)
22
+ '.'*345 = 512 char output (for testing)
23
+
24
+ V 1.1.0 2005/02/21
25
+
26
+ - $TIDYLIB global eliminated
27
+ Path to the library is now specified as follows:
28
+ Tidy.path = '/path/to/tidylib.so'
29
+
30
+ - minor code/docs/gemspec cleanup
31
+
32
+ V 1.0.1
33
+
34
+ .sub calls changed to .gsub
35
+
36
+ V 1.0.0
37
+
38
+ Oct/18/2004 - Declared stable, now packaged as a RubyGem
39
+
40
+ VB.2.5
41
+
42
+ - Auto-detection and ENV variables removed, adds too much complexity
43
+ Module location is now specified by defining $TIDYLIB before require 'tidy'
44
+ $TIDYLIB is the system path to the library (ex: /usr/lib/tidylib.so)
45
+ Raises an error if $TIDYLIB is not defined
46
+
47
+ V.B2.4
48
+
49
+ Now uses ENV['TIDY_LIB'] instead of $TIDY_LIB
50
+
51
+ V B2.3
52
+
53
+ - Added library auto-detection
54
+ a) use $TIDY_LIB if defined
55
+ b) Search $: and ENV['PATH'] paths for (tidy|tidylib|libtidy|htmltidy).(dll|so)
56
+ c) Raise an error if not found, otherwise loads
57
+
58
+ V.B2.2
59
+
60
+ - Tidy.to_b added
61
+
62
+ V.B2.1
63
+
64
+ - load_config method added
65
+
66
+ V.B2
67
+
68
+ - load(path) method added, library must be loaded explicity
69
+ this avoids hard coding the library name
70
+ require 'tidy'
71
+ Tidy.load('path/to/tidylib.so')
72
+
73
+ V.B1
74
+
75
+ First release
data/README ADDED
@@ -0,0 +1,39 @@
1
+ Tidy README
2
+ ============
3
+
4
+ Ruby interface to HTML Tidy Library Project (http://tidy.sf.net)
5
+
6
+ Requirements
7
+ ------------
8
+
9
+ * Recent version of Ruby
10
+ * RubyGems 1.8+ (http://rubygems.rubyforge.org)
11
+ * HTML Tidy Library (compiled) (this version has been tested on 1.46, and it *definitely* won't work with anything prior to .99)
12
+
13
+ Install
14
+ -------
15
+
16
+ - Download library from http://tidy.sf.net (pre-compiled versions available).
17
+
18
+ - Install using the following:
19
+
20
+ $ gem install terrbear-tidy.gem
21
+
22
+ - Open test/usage.rb, change Tidy.path to point to your compiled Tidy library, run.
23
+
24
+ Usage
25
+ -----
26
+
27
+ See API docs
28
+
29
+ License
30
+ -------
31
+
32
+ Distributes under the same terms as Ruby
33
+ http://www.ruby-lang.org/en/LICENSE.txt
34
+
35
+ Maintained by
36
+ Terry Heath <theath at gmail dot com>
37
+
38
+ Written by
39
+ Kevin Howe <kh at newclear.ca>
@@ -0,0 +1,43 @@
1
+ # Buffer structure.
2
+ #
3
+ class Tidybuf
4
+
5
+ extend DL::Importable
6
+
7
+ # Access TidyBuffer instance.
8
+ #
9
+ attr_reader(:struct)
10
+
11
+ # Mimic TidyBuffer.
12
+ #
13
+ TidyBuffer = struct [
14
+ "TidyAllocator* allocator",
15
+ "byte* bp",
16
+ "uint size",
17
+ "uint allocated",
18
+ "uint next"
19
+ ]
20
+
21
+ def initialize
22
+ @struct = TidyBuffer.malloc
23
+ end
24
+
25
+ # Free current contents and zero out.
26
+ #
27
+ def free
28
+ Tidylib.buf_free(@struct)
29
+ end
30
+
31
+ # Convert to array.
32
+ #
33
+ def to_a
34
+ to_s.split("\r\n")
35
+ end
36
+
37
+ # Convert to string.
38
+ #
39
+ def to_s
40
+ @struct.bp ? @struct.bp.to_s(@struct.size) : ''
41
+ end
42
+
43
+ end
@@ -0,0 +1,30 @@
1
+ # Parameterized error message.
2
+ #
3
+ class Tidyerr < String
4
+
5
+ # Error parameter.
6
+ #
7
+ attr_reader :severity, :line, :column, :message
8
+
9
+ # Create new instance.
10
+ #
11
+ def initialize(error)
12
+ super(error.to_s)
13
+ parameterize
14
+ end
15
+
16
+ # Parse error message into parameters (where applicable).
17
+ #
18
+ def parameterize
19
+ if to_str[0,4] == 'line'
20
+ tokens = to_str.split(' ', 7)
21
+ @severity = tokens[5][0,1] # W or E
22
+ @line = tokens[1].to_i
23
+ @column = tokens[3].to_i
24
+ @message = tokens[6]
25
+ end
26
+ end
27
+
28
+ protected :parameterize
29
+
30
+ end
@@ -0,0 +1,111 @@
1
+ # Ruby wrapper for HTML Tidy Library Project (http://tidy.sf.net).
2
+ #
3
+ module Tidylib
4
+
5
+ extend DL::Importable
6
+
7
+ module_function
8
+
9
+ # Load the library.
10
+ #
11
+ def load(path)
12
+ begin
13
+ dlload(path)
14
+ rescue
15
+ raise LoadError, "Unable to load #{path}"
16
+ end
17
+ extern "void *tidyCreate()"
18
+ extern "void tidyBufFree(void*)"
19
+ extern "int tidyCleanAndRepair(void*)"
20
+ extern "int tidyLoadConfig(void*, char*)"
21
+ extern "int tidyOptGetIdForName(char*)"
22
+ extern "char tidyOptGetValue(void*, unsigned int)"
23
+ extern "int tidyOptParseValue(void*, char*, char*)"
24
+ extern "int tidyParseString(void*, char*)"
25
+ extern "void tidyRelease(void*)"
26
+ extern "char* tidyReleaseDate()"
27
+ extern "int tidyRunDiagnostics(void*)"
28
+ extern "int tidySaveBuffer(void*, void*)"
29
+ extern "int tidySetErrorBuffer(void*, void*)"
30
+ end
31
+
32
+ # tidyBufFree
33
+ #
34
+ def buf_free(buf)
35
+ tidyBufFree(buf)
36
+ end
37
+
38
+ # tidyCreate
39
+ #
40
+ def create
41
+ tidyCreate
42
+ end
43
+
44
+ # tidyCleanAndRepair
45
+ #
46
+ def clean_and_repair(doc)
47
+ tidyCleanAndRepair(doc)
48
+ end
49
+
50
+ # tidyLoadConfig
51
+ #
52
+ def load_config(doc, file)
53
+ tidyLoadConfig(doc, file.to_s)
54
+ end
55
+
56
+ # tidyOptParseValue
57
+ #
58
+ def opt_parse_value(doc, name, value)
59
+ tidyOptParseValue(doc, translate_name(name), value.to_s)
60
+ end
61
+
62
+ # tidyOptGetValue (returns true/false instead of 1/0)
63
+ #
64
+ def opt_get_value(doc, name)
65
+ value = tidyOptGetValue(doc, tidyOptGetIdForName(translate_name(name)))
66
+ Tidy.to_b(value)
67
+ end
68
+
69
+ # tidyParseString
70
+ #
71
+ def parse_string(doc, str)
72
+ tidyParseString(doc, str.to_s)
73
+ end
74
+
75
+ # tidyRelease
76
+ #
77
+ def release(doc)
78
+ tidyRelease(doc)
79
+ end
80
+
81
+ # tidyReleaseDate
82
+ #
83
+ def release_date
84
+ tidyReleaseDate
85
+ end
86
+
87
+ # tidyRunDiagnostics
88
+ #
89
+ def run_diagnostics(doc)
90
+ tidyRunDiagnostics(doc)
91
+ end
92
+
93
+ # tidySaveBuffer
94
+ #
95
+ def save_buffer(doc, buf)
96
+ tidySaveBuffer(doc, buf)
97
+ end
98
+
99
+ # tidySetErrorBuffer
100
+ #
101
+ def set_error_buffer(doc, buf)
102
+ tidySetErrorBuffer(doc, buf)
103
+ end
104
+
105
+ # Convert to string, replace underscores with dashes (:output_xml => 'output-xml').
106
+ #
107
+ def translate_name(name)
108
+ name.to_s.gsub('_', '-')
109
+ end
110
+
111
+ end
@@ -0,0 +1,108 @@
1
+ # Ruby interface to Tidylib.
2
+ #
3
+ class Tidyobj
4
+
5
+ # Diagnostics Buffer (Array of String).
6
+ #
7
+ attr_reader(:diagnostics)
8
+
9
+ # Access the tidy instance.
10
+ #
11
+ attr_reader(:doc)
12
+
13
+ # Error Buffer (Array of Tidyerr).
14
+ #
15
+ attr_reader(:errors)
16
+
17
+ # Options interface (Tidyopt).
18
+ #
19
+ attr_reader(:options)
20
+
21
+ # Construct a new instance.
22
+ # Receives a hash of options to be set.
23
+ #
24
+ def initialize(options=nil)
25
+ @diagnostics = Array.new
26
+ @doc = Tidylib.create
27
+ @errors = Array.new
28
+ @errbuf = Tidybuf.new
29
+ @outbuf = Tidybuf.new
30
+ @options = Tidyopt.new(@doc)
31
+ rc = Tidylib.set_error_buffer(@doc, @errbuf.struct)
32
+ verify_severe(rc)
33
+ unless options.nil?
34
+ options.each { |name, value| Tidylib.opt_parse_value(@doc, name, value) }
35
+ end
36
+ end
37
+
38
+ # Clean and Repair.
39
+ #
40
+ def clean(str)
41
+ `echo verify_doc >> /tidylog`
42
+ verify_doc
43
+ rc = -1
44
+
45
+ # Clean and repair the string.
46
+ #
47
+ `echo parse_string >> /tidylog`
48
+ rc = Tidylib.parse_string(@doc, str) # Parse the input
49
+ `echo clean_and_rep >> /tidylog`
50
+ rc = Tidylib.clean_and_repair(@doc) if rc >= 0 # Tidy it up!
51
+ `echo opt_parse >> /tidylog`
52
+ rc = (Tidylib.opt_parse_value(@doc, :force_output, true) == 1 ? rc : -1) if rc > 1 # If error, force output
53
+ `echo save_buffer >> /tidylog`
54
+ rc = Tidylib.save_buffer(@doc, @outbuf.struct) if rc >= 0 # Pretty Print
55
+ verify_severe(rc)
56
+
57
+ # Save and clear output/errors.
58
+ #
59
+ output = @outbuf.to_s
60
+ @errors = @errbuf.to_a.collect { |e| Tidyerr.new(e) }
61
+ @outbuf.free
62
+ @errbuf.free
63
+
64
+ # Save diagnostics.
65
+ #
66
+ rc = Tidylib.run_diagnostics(@doc)
67
+ verify_severe(rc)
68
+ @diagnostics = @errbuf.to_a
69
+ @errbuf.free
70
+
71
+ output
72
+ end
73
+
74
+ # Load a tidy config file.
75
+ #
76
+ def load_config(file)
77
+ verify_doc
78
+ rc = Tidylib.load_config(@doc, file)
79
+ case rc
80
+ when -1 then raise LoadError, "#{file} does not exist"
81
+ when 1 then raise LoadError, "errors parsing #{file}"
82
+ end
83
+ rc
84
+ end
85
+
86
+ # Clear the tidy instance.
87
+ #
88
+ def release
89
+ verify_doc
90
+ Tidylib.release(@doc)
91
+ @doc = nil
92
+ end
93
+
94
+ # Raise an error if the tidy document is invalid.
95
+ #
96
+ def verify_doc
97
+ raise TypeError, 'Invalid Tidy document' unless @doc.class == DL::PtrData
98
+ end
99
+
100
+ # Raise severe error based on tidy status value.
101
+ #
102
+ def verify_severe(rc)
103
+ raise "A severe error (#{rc}) occurred.\n" if rc < 0
104
+ end
105
+
106
+ protected :verify_doc, :verify_severe
107
+
108
+ end
@@ -0,0 +1,31 @@
1
+ # Ruby interface to Tidylib options.
2
+ #
3
+ class Tidyopt
4
+
5
+ # Construct a new instance.
6
+ #
7
+ def initialize(doc)
8
+ @doc = doc
9
+ end
10
+
11
+ # Reader for options (Hash syntax).
12
+ #
13
+ def [](name)
14
+ Tidylib.opt_get_value(@doc, name)
15
+ end
16
+
17
+ # Writer for options (Hash syntax).
18
+ #
19
+ def []=(name, value)
20
+ Tidylib.opt_parse_value(@doc, name, value)
21
+ end
22
+
23
+ # Reader/Writer for options (Object syntax).
24
+ #
25
+ def method_missing(name, value=:none, *args)
26
+ name = name.to_s.gsub('=', '')
27
+ return self[name] if value == :none
28
+ self[name] = value
29
+ end
30
+
31
+ end
data/lib/tidy.rb ADDED
@@ -0,0 +1,75 @@
1
+ # Ruby interface to HTML Tidy Library Project (http://tidy.sf.net).
2
+ #
3
+ # =Usage
4
+ #
5
+ # require 'tidy'
6
+ # Tidy.path = '/usr/lib/tidylib.so'
7
+ # html = '<html><title>title</title>Body</html>'
8
+ # xml = Tidy.open(:show_warnings=>true) do |tidy|
9
+ # tidy.options.output_xml = true
10
+ # puts tidy.options.show_warnings
11
+ # xml = tidy.clean(html)
12
+ # puts tidy.errors
13
+ # puts tidy.diagnostics
14
+ # xml
15
+ # end
16
+ # puts xml
17
+ #
18
+ # Author:: Kevin Howe
19
+ # License:: Distributes under the same terms as Ruby
20
+ #
21
+ module Tidy
22
+
23
+ require 'dl/import'
24
+ require 'dl/struct'
25
+ require 'tidy/tidybuf'
26
+ require 'tidy/tidyerr'
27
+ require 'tidy/tidylib'
28
+ require 'tidy/tidyobj'
29
+ require 'tidy/tidyopt'
30
+
31
+ module_function
32
+
33
+ # Return a Tidyobj instance.
34
+ #
35
+ def new(options=nil)
36
+ Tidyobj.new(options)
37
+ end
38
+
39
+ # Path to Tidylib.
40
+ #
41
+ def path() @path end
42
+
43
+ # Set the path to Tidylib (automatically loads the library).
44
+ #
45
+ def path=(path)
46
+ Tidylib.load(path)
47
+ @path = path
48
+ end
49
+
50
+ # With no block, open is a synonym for Tidy.new.
51
+ # If a block is present, it is passed aTidy as a parameter.
52
+ # aTidyObj.release is ensured at end of the block.
53
+ #
54
+ def open(options=nil)
55
+ raise "Tidy.path was not specified." unless @path
56
+ tidy = Tidy.new(options)
57
+ if block_given?
58
+ begin
59
+ yield tidy
60
+ ensure
61
+ tidy.release
62
+ end
63
+ else
64
+ tidy
65
+ end
66
+ end
67
+
68
+ # Convert to boolean.
69
+ # 0, false and nil return false, anything else true.
70
+ #
71
+ def to_b(value)
72
+ [0,false,nil].include?(value) ? false : true
73
+ end
74
+
75
+ end
data/test/usage.rb ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/local/bin/ruby
2
+ $LOAD_PATH.unshift('../lib')
3
+ require 'tidy'
4
+ #Tidy.path = '/usr/lib/tidylib.so'
5
+ Tidy.path = '/usr/lib64/libtidy.so'
6
+ html = '<html><title>title</title>Body</html>'
7
+ xml = Tidy.open(:show_warnings=>true) do |tidy|
8
+ tidy.options.output_xml = true
9
+ puts tidy.options.show_warnings
10
+ xml = tidy.clean(html)
11
+ puts tidy.errors
12
+ puts tidy.diagnostics
13
+ xml
14
+ end
15
+ puts xml
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terrbear-tidy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Terry Heath
@@ -21,8 +21,16 @@ extensions: []
21
21
 
22
22
  extra_rdoc_files: []
23
23
 
24
- files: []
25
-
24
+ files:
25
+ - README
26
+ - CHANGES
27
+ - test/usage.rb
28
+ - lib/tidy.rb
29
+ - lib/tidy/tidybuf.rb
30
+ - lib/tidy/tidyerr.rb
31
+ - lib/tidy/tidylib.rb
32
+ - lib/tidy/tidyobj.rb
33
+ - lib/tidy/tidyopt.rb
26
34
  has_rdoc: true
27
35
  homepage: http://github.com/terrbear/tidy/tree/master
28
36
  post_install_message: