Module: Tidy

Defined in:
lib/tidy.rb

Overview

Ruby interface to HTML Tidy Library Project (tidy.sf.net).

Usage

require 'tidy'
Tidy.path = '/usr/lib/tidylib.so'
html = '<html><title>title</title>Body</html>'
xml = Tidy.open(:show_warnings=>true) do |tidy|
  tidy.options.output_xml = true
  puts tidy.options.show_warnings
  xml = tidy.clean(html)
  puts tidy.errors
  puts tidy.diagnostics
  xml
end
puts xml
Author

Kevin Howe

License

Distributes under the same terms as Ruby

Class Method Summary collapse

Class Method Details

.deduce_load_pathObject

Naive way of finding the load path. There has to be a better way .



85
86
87
88
89
# File 'lib/tidy.rb', line 85

def deduce_load_path
  unless @path
    Tidy.path=['/usr/lib','/usr/local/lib','/opt/lib','/opt/local/lib'].collect{|p| ['libtidy.so','libtidy.dylib'].collect{|l| "#{p}/#{l}"}}.flatten.detect{|p|File.exist?(p)}
  end
end

.fresh_tidy_versionObject



55
56
57
# File 'lib/tidy.rb', line 55

def fresh_tidy_version
  !!@fresh_tidy_version
end

.fresh_tidy_version=(bool) ⇒ Object



51
52
53
# File 'lib/tidy.rb', line 51

def fresh_tidy_version=(bool)
  @fresh_tidy_version = bool
end

.new(options = nil) ⇒ Object

Return a Tidyobj instance.



35
36
37
38
# File 'lib/tidy.rb', line 35

def new(options=nil)
  deduce_load_path
  Tidyobj.new(options)
end

.open(options = nil) ⇒ Object

With no block, open is a synonym for Tidy.new. If a block is present, it is passed aTidy as a parameter. aTidyObj.release is ensured at end of the block.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tidy.rb', line 63

def open(options=nil)
  raise "Tidy.path was not specified." unless @path
  tidy = Tidy.new(options)
  if block_given?
    begin
      yield tidy
    ensure
      tidy.release
    end
  else
    tidy
  end
end

.pathObject

Path to Tidylib.



42
# File 'lib/tidy.rb', line 42

def path() @path end

.path=(path) ⇒ Object

Set the path to Tidylib (automatically loads the library).



46
47
48
49
# File 'lib/tidy.rb', line 46

def path=(path)
  Tidylib.load(path)
  @path = path
end

.to_b(value) ⇒ Object

Convert to boolean. 0, false and nil return false, anything else true.



80
81
82
# File 'lib/tidy.rb', line 80

def to_b(value)
  [0,false,nil].include?(value) ? false : true
end