Class TidyFFI::Interface
In: lib/tidy_ffi/interface.rb
lib/tidy_ffi/interface.rb
Parent: Object

Low level interface to libtidy.

Methods

Constants

LibTidy = TidyFFI::LibTidy
LibTidy = TidyFFI::LibTidy

Public Class methods

Returns a hash that represents default options for tidy. Key for hash is the option name, value is also a hash… Possible values are:

  • :type - either :string, :integer, :boolean or :enum
  • :readonly?
  • :default - default value of an option
  • :values - possible values for :enum
  • :name

[Source]

     # File lib/tidy_ffi/interface.rb, line 171
171:   def self.default_options
172:     @default_options ||= load_default_options
173:   end

Returns a hash that represents default options for tidy. Key for hash is the option name, value is also a hash… Possible values are:

  • :type - either :string, :integer, :boolean or :enum
  • :readonly?
  • :default - default value of an option
  • :values - possible values for :enum
  • :name

[Source]

     # File lib/tidy_ffi/interface.rb, line 171
171:   def self.default_options
172:     @default_options ||= load_default_options
173:   end

Returns true if value is valid for option and false otherwise.

[Source]

     # File lib/tidy_ffi/interface.rb, line 176
176:   def self.option_valid?(option, value)
177:     return false unless spec = default_options[option]
178:     
179:     case spec[:type]
180:     when :boolean
181:       true == value || false == value || value == 0 || value == 1 || %w(on off true false 0 1 yes no).include?(value.downcase)
182:     when :integer
183:       Integer === value || !!(value =~ /^\d+$/)
184:     when :enum
185:       if Integer === value
186:         !!spec[:values][value]
187:       else
188:         spec[:values].include?(value)
189:       end
190:     when :string
191:       String === value || Symbol === value
192:     end
193:   end

Returns true if value is valid for option and false otherwise.

[Source]

     # File lib/tidy_ffi/interface.rb, line 176
176:   def self.option_valid?(option, value)
177:     return false unless spec = default_options[option]
178:     
179:     case spec[:type]
180:     when :boolean
181:       true == value || false == value || value == 0 || value == 1 || %w(on off true false 0 1 yes no).include?(value.downcase)
182:     when :integer
183:       Integer === value || !!(value =~ /^\d+$/)
184:     when :enum
185:       if Integer === value
186:         !!spec[:values][value]
187:       else
188:         spec[:values].include?(value)
189:       end
190:     when :string
191:       String === value || Symbol === value
192:     end
193:   end

Returns a TidyFFI::Interface with initialized interface.

[Source]

    # File lib/tidy_ffi/interface.rb, line 8
 8:   def self.with_doc
 9:     doc = LibTidy.tidyCreate
10:     nd = new(doc)
11:     nd.with_redirected_error_buffer { yield nd }
12:   ensure
13:     LibTidy.tidyRelease(doc)
14:   end

Returns a TidyFFI::Interface with initialized interface.

[Source]

    # File lib/tidy_ffi/interface.rb, line 8
 8:   def self.with_doc
 9:     doc = LibTidy.tidyCreate
10:     nd = new(doc)
11:     nd.with_redirected_error_buffer { yield nd }
12:   ensure
13:     LibTidy.tidyRelease(doc)
14:   end

Public Instance methods

Apply options

[Source]

    # File lib/tidy_ffi/interface.rb, line 21
21:   def apply_options(options)
22:     options.each do |key, value|
23:       k = key.to_s.gsub('_', '-')
24: 
25:       option = LibTidy.tidyGetOptionByName(@doc, k)
26:       raise ArgumentError, "don't know about option #{key}" if option.null?
27:       id = LibTidy.tidyOptGetId(option)
28:       raise ArgumentError, "can't setup option #{key} to #{value}" if LibTidy.tidyOptSetValue(@doc, id, value.to_s) == 0
29:     end
30:   end

Apply options

[Source]

    # File lib/tidy_ffi/interface.rb, line 21
21:   def apply_options(options)
22:     options.each do |key, value|
23:       k = key.to_s.gsub('_', '-')
24: 
25:       option = LibTidy.tidyGetOptionByName(@doc, k)
26:       raise ArgumentError, "don't know about option #{key}" if option.null?
27:       id = LibTidy.tidyOptGetId(option)
28:       raise ArgumentError, "can't setup option #{key} to #{value}" if LibTidy.tidyOptSetValue(@doc, id, value.to_s) == 0
29:     end
30:   end

Cleans string

[Source]

    # File lib/tidy_ffi/interface.rb, line 38
38:   def clean
39:     @output = nil
40:     LibTidy.tidyCleanAndRepair(@doc)
41:   end

Cleans string

[Source]

    # File lib/tidy_ffi/interface.rb, line 38
38:   def clean
39:     @output = nil
40:     LibTidy.tidyCleanAndRepair(@doc)
41:   end
clean_and_repair()

Alias for clean

clean_and_repair()

Alias for clean

[Source]

    # File lib/tidy_ffi/interface.rb, line 55
55:   def errors
56:     @error_buffer[:bp]
57:   end

[Source]

    # File lib/tidy_ffi/interface.rb, line 55
55:   def errors
56:     @error_buffer[:bp]
57:   end

Returns output from tidy library

[Source]

    # File lib/tidy_ffi/interface.rb, line 46
46:   def output
47:     @output ||= begin
48:       with_buffer_pointer do |buf|
49:         LibTidy.tidySaveBuffer(@doc, buf)
50:         buf[:bp]
51:       end
52:     end
53:   end

Returns output from tidy library

[Source]

    # File lib/tidy_ffi/interface.rb, line 46
46:   def output
47:     @output ||= begin
48:       with_buffer_pointer do |buf|
49:         LibTidy.tidySaveBuffer(@doc, buf)
50:         buf[:bp]
51:       end
52:     end
53:   end
repair()

Alias for clean

repair()

Alias for clean

Sets string to tidy

[Source]

    # File lib/tidy_ffi/interface.rb, line 33
33:   def string=(str)
34:     LibTidy.tidyParseString(@doc, str)
35:   end

Sets string to tidy

[Source]

    # File lib/tidy_ffi/interface.rb, line 33
33:   def string=(str)
34:     LibTidy.tidyParseString(@doc, str)
35:   end

Yields block with new buffer

[Source]

    # File lib/tidy_ffi/interface.rb, line 71
71:   def with_buffer_pointer
72:     buf = tidy_buf_object.new
73:     LibTidy.tidyBufInit(buf)
74:     yield buf
75:   ensure
76:     LibTidy.tidyBufFree(buf)
77:   end

Yields block with new buffer

[Source]

    # File lib/tidy_ffi/interface.rb, line 71
71:   def with_buffer_pointer
72:     buf = tidy_buf_object.new
73:     LibTidy.tidyBufInit(buf)
74:     yield buf
75:   ensure
76:     LibTidy.tidyBufFree(buf)
77:   end

Redirects error buffer

[Source]

    # File lib/tidy_ffi/interface.rb, line 60
60:   def with_redirected_error_buffer
61:     with_buffer_pointer do |buf|
62:       @error_buffer = buf
63:       LibTidy.tidySetErrorBuffer(@doc, buf)
64:       yield
65:     end
66:   ensure
67:     @error_buffer = nil
68:   end

Redirects error buffer

[Source]

    # File lib/tidy_ffi/interface.rb, line 60
60:   def with_redirected_error_buffer
61:     with_buffer_pointer do |buf|
62:       @error_buffer = buf
63:       LibTidy.tidySetErrorBuffer(@doc, buf)
64:       yield
65:     end
66:   ensure
67:     @error_buffer = nil
68:   end

[Validate]