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 =~ /^\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
Loads default options.
# File lib/tidy_ffi/interface.rb, line 116 def load_default_options return if @default_options doc = LibTidy.tidyCreate iterator = LibTidy.tidyGetOptionList(doc) @default_options = {} FFI::MemoryPointer.new(:pointer, 1) do |pointer| pointer.put_pointer(0, iterator) until iterator.null? opt = LibTidy.tidyGetNextOption(doc, pointer) option = {} option[:name] = LibTidy.tidyOptGetName(opt).gsub('-', '_').intern option[:readonly?] = LibTidy.tidyOptIsReadOnly(opt) != 0 option[:type] = LibTidy::TIDY_OPTION_TYPE[LibTidy.tidyOptGetType(opt)] option[:default] = case option[:type] when :string (LibTidy.tidyOptGetDefault(opt) rescue "") when :integer if pick_list = pick_list_for(opt) option[:type] = :enum option[:values] = pick_list pick_list[LibTidy.tidyOptGetDefaultInt(opt)] else LibTidy.tidyOptGetDefaultInt(opt) end when :boolean LibTidy.tidyOptGetDefaultBool(opt) != 0 end @default_options[option[:name]] = option iterator = pointer.get_pointer(0) end end @default_options.freeze ensure LibTidy.tidyRelease(doc) end
Returns enumeration for opt.
Some tidy options might try to trespass as integer, and in order to caught perpertraitors we need to call tidyOptGetPickList
# File lib/tidy_ffi/interface.rb, line 96 def pick_list_for(opt) iterator = LibTidy.tidyOptGetPickList(opt) return nil if iterator.null? pick_list = [] FFI::MemoryPointer.new(:pointer, 1) do |pointer| pointer.put_pointer(0, iterator) until iterator.null? pick_list << LibTidy.tidyOptGetNextPick(opt, pointer) iterator = pointer.get_pointer(0) end end pick_list 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
# File lib/tidy_ffi/interface.rb, line 79 def tidy_buf_object @tidy_buf_object ||= begin release_date = Date.parse(LibTidy.tidyReleaseDate) rescue nil if release_date && (release_date > Date.parse("Dec 29 2006")) TidyFFI::LibTidy::TidyBufWithAllocator else TidyFFI::LibTidy::TidyBuf end end end