Low level interface to libtidy.
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
# File lib/tidy_ffi/interface.rb, line 171 def self.default_options @default_options ||= load_default_options end
Returns true if value is valid for option
and false otherwise.
# File lib/tidy_ffi/interface.rb, line 176 def self.option_valid?(option, value) return false unless spec = default_options[option] case spec[:type] when :boolean true == value || false == value || value == 0 || value == 1 || %w(on off true false 0 1 yes no).include?(value.downcase) when :integer Integer === value || !!(value =~ %r^\d+$/) when :enum if Integer === value !!spec[:values][value] else spec[:values].include?(value) end when :string String === value || Symbol === value end end
Returns a TidyFFI::Interface with initialized interface.
# File lib/tidy_ffi/interface.rb, line 8 def self.with_doc doc = LibTidy.tidyCreate nd = new(doc) nd.with_redirected_error_buffer { yield nd } ensure LibTidy.tidyRelease(doc) end
Apply options
# File lib/tidy_ffi/interface.rb, line 21 def apply_options(options) options.each do |key, value| k = key.to_s.gsub('_', '-') option = LibTidy.tidyGetOptionByName(@doc, k) raise ArgumentError, "don't know about option #{key}" if option.null? id = LibTidy.tidyOptGetId(option) raise ArgumentError, "can't setup option #{key} to #{value}" if LibTidy.tidyOptSetValue(@doc, id, value.to_s) == 0 end end
Cleans string
# File lib/tidy_ffi/interface.rb, line 38 def clean @output = nil LibTidy.tidyCleanAndRepair(@doc) end
# File lib/tidy_ffi/interface.rb, line 55 def errors @error_buffer[:bp] end
Returns output from tidy library
# File lib/tidy_ffi/interface.rb, line 46 def output @output ||= begin with_buffer_pointer do |buf| LibTidy.tidySaveBuffer(@doc, buf) buf[:bp] end end end
Sets string to tidy
# File lib/tidy_ffi/interface.rb, line 33 def string=(str) LibTidy.tidyParseString(@doc, str) end
Yields block with new buffer
# File lib/tidy_ffi/interface.rb, line 71 def with_buffer_pointer buf = tidy_buf_object.new LibTidy.tidyBufInit(buf) yield buf ensure LibTidy.tidyBufFree(buf) end
Redirects error buffer
# File lib/tidy_ffi/interface.rb, line 60 def with_redirected_error_buffer with_buffer_pointer do |buf| @error_buffer = buf LibTidy.tidySetErrorBuffer(@doc, buf) yield end ensure @error_buffer = nil end