class TidyFFI::Interface
Low level interface to libtidy.
Constants
- LibTidy
Public Class Methods
Source
# File lib/tidy_ffi/interface.rb, line 172 def self.default_options @default_options ||= load_default_options 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 177 def self.option_valid?(option, value) spec = default_options[option] return false unless spec 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.downcase) end when :string String === value || Symbol === value end end
Returns true if value is valid for option
and false otherwise.
Source
# 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
Returns a TidyFFI::Interface
with initialized interface.
Private Class Methods
Source
# File lib/tidy_ffi/interface.rb, line 117 def load_default_options return if @default_options doc = LibTidy.tidyCreate iterator = LibTidy.tidyGetOptionList(doc) @default_options = {} pointer = FFI::MemoryPointer.new(:pointer, 1) 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 @default_options.freeze ensure pointer.free if pointer LibTidy.tidyRelease(doc) end
Loads default options.
Source
# File lib/tidy_ffi/interface.rb, line 96 def pick_list_for(opt) iterator = LibTidy.tidyOptGetPickList(opt) return nil if iterator.null? pick_list = [] pointer = FFI::MemoryPointer.new(:pointer, 1) pointer.put_pointer(0, iterator) until iterator.null? pick_list << LibTidy.tidyOptGetNextPick(opt, pointer) iterator = pointer.get_pointer(0) end pick_list ensure pointer.free if pointer 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
Public Instance Methods
Source
# 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
Apply options
Source
# File lib/tidy_ffi/interface.rb, line 38 def clean @output = nil LibTidy.tidyCleanAndRepair(@doc) end
Cleans string
Also aliased as: clean_and_repair, repair
Source
# 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
Returns output from tidy library
Source
# File lib/tidy_ffi/interface.rb, line 33 def string=(str) LibTidy.tidyParseString(@doc, str) end
Sets string to tidy
Source
# 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
Yields block with new buffer
Source
# 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
Redirects error buffer
Private Instance Methods
Source
# 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