Class: TidyFFI::Tidy

Inherits:
Object
  • Object
show all
Defined in:
lib/tidy_ffi/tidy.rb

Overview

Clean and simple interface to Tidy

Defined Under Namespace

Classes: InvalidOptionName, InvalidOptionValue

Constant Summary collapse

OptionsContainer =
TidyFFI::OptionsContainer

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, options = {}) ⇒ Tidy

Initializing object.

  • str is a string to tidy

  • options are options for tidy



11
12
13
14
15
# File 'lib/tidy_ffi/tidy.rb', line 11

def initialize(str, options = {})
  @string = str
  @options = OptionsContainer.new(self.class.default_options)
  self.options = options
end

Class Attribute Details

.validate_options=(value) ⇒ Object (writeonly)

Sets the attribute validate_options

Parameters:

  • value

    the value to set the attribute validate_options to.



81
82
83
# File 'lib/tidy_ffi/tidy.rb', line 81

def validate_options=(value)
  @validate_options = value
end

Class Method Details

.clean(str, options = {}) ⇒ Object

Returns cleaned string



29
30
31
# File 'lib/tidy_ffi/tidy.rb', line 29

def self.clean(str, options = {})
  new(str, options).clean
end

.default_optionsObject

Default options for tidy. Works just like options method



61
62
63
# File 'lib/tidy_ffi/tidy.rb', line 61

def default_options
  @default_options ||= OptionsContainer.new
end

.default_options=(options) ⇒ Object

Default options for tidy. Works just like options= method



66
67
68
# File 'lib/tidy_ffi/tidy.rb', line 66

def default_options=(options)
  @default_options.merge_with_options(options)
end

.validate_options?Boolean

When true it validates name and option type (default is true).

Returns:

  • (Boolean)


78
79
80
# File 'lib/tidy_ffi/tidy.rb', line 78

def validate_options?
  @validate_options != false
end

.with_options(options) ⇒ Object

Returns a proxy class with options. Example:

TidyFFI::Tidy.with_options(:show_body_only => true).with_options(:wrap_asp => true).new('test)


73
74
75
# File 'lib/tidy_ffi/tidy.rb', line 73

def with_options(options)
  OptionsContainer::Proxy.new(self, @default_options, options)
end

Instance Method Details

#cleanObject

Returns cleaned string



18
19
20
21
22
23
24
25
26
# File 'lib/tidy_ffi/tidy.rb', line 18

def clean
  @clean ||= TidyFFI::Interface.with_doc do |doc|
    doc.apply_options(@options.to_hash!)
    doc.string = @string
    doc.clean
    @errors = doc.errors
    doc.output
  end
end

#errorsObject

Returns errors for string



34
35
36
37
38
39
# File 'lib/tidy_ffi/tidy.rb', line 34

def errors
  @errors ||= begin
    clean
    @errors
  end
end

#optionsObject

Proxy for options. Supports set and get

tidy.options.show_body_only #=> nil
tidy.options.show_body_only = true
tidy.options.show_body_only #=> true


55
56
57
# File 'lib/tidy_ffi/tidy.rb', line 55

def options
  @options
end

#options=(options) ⇒ Object

Assigns options for tidy. It merges options, not deletes old ones.

tidy.options= {:wrap_asp => true}
tidy.options= {:show_body_only => true}

Will send to tidy both options.



46
47
48
# File 'lib/tidy_ffi/tidy.rb', line 46

def options=(options)
  @options.merge_with_options(options)
end