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 171
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 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
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

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
clean_and_repair() click to toggle source
Alias for: clean
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
repair() click to toggle source
Alias for: clean
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