class TidyFFI::Interface
Low level interface to libtidy.
Constants
- LibTidy
Public Class Methods
default_options()
click to toggle source
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 172 def self.default_options @default_options ||= load_default_options end
option_valid?(option, value)
click to toggle source
Returns true if value is valid for option
and false otherwise.
# 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
with_doc() { |nd| ... }
click to toggle source
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
Private Class Methods
load_default_options()
click to toggle source
Loads default options.
# 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
pick_list_for(opt)
click to toggle source
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 = [] 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
Public Instance Methods
apply_options(options)
click to toggle source
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
clean()
click to toggle source
Cleans string
# File lib/tidy_ffi/interface.rb, line 38 def clean @output = nil LibTidy.tidyCleanAndRepair(@doc) end
Also aliased as: clean_and_repair, repair
errors()
click to toggle source
# File lib/tidy_ffi/interface.rb, line 55 def errors @error_buffer[:bp] end
output()
click to toggle source
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
string=(str)
click to toggle source
Sets string to tidy
# File lib/tidy_ffi/interface.rb, line 33 def string=(str) LibTidy.tidyParseString(@doc, str) end
with_buffer_pointer() { |buf| ... }
click to toggle source
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
with_redirected_error_buffer() { || ... }
click to toggle source
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
Private Instance Methods
tidy_buf_object()
click to toggle 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