Class: Rote::Filters::Tidy

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

Overview

Post filter that runs HTML Tidy on the laid-out page to correct and clean up HTML in the output. This filter can be used with any of the asXXXX formats supported by Tidy.

Note that this filter requires the ‘tidy’ command, and should be added to the post_filters array, in contrast to most of the other filters which are page filters.

If ‘tidy’ isn’t in your path you’ll need to specify it here or via a TIDYCMD environment variable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format = :xhtml, tidycmd = nil, tidyopts = '-q') ⇒ Tidy

Create a new filter instance, using the specified output format, and optionally a custom ‘tidy’ command and options.



27
28
29
30
31
32
33
# File 'lib/rote/filters/tidy.rb', line 27

def initialize(format = :xhtml, tidycmd = nil, tidyopts = '-q') 
  @tidycmd = tidycmd || ENV['TIDYCMD'] || (RUBY_PLATFORM =~ /mswin/ ? 'tidy.exe' : 'tidy')
    # TODO windows 'tidy.exe' correct?
    
  @tidyopts = tidyopts
  @format = format
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



35
36
37
# File 'lib/rote/filters/tidy.rb', line 35

def format
  @format
end

#tidycmdObject

Returns the value of attribute tidycmd.



35
36
37
# File 'lib/rote/filters/tidy.rb', line 35

def tidycmd
  @tidycmd
end

#tidyoptsObject

Returns the value of attribute tidyopts.



35
36
37
# File 'lib/rote/filters/tidy.rb', line 35

def tidyopts
  @tidyopts
end

Instance Method Details

#filter(text, page) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rote/filters/tidy.rb', line 37

def filter(text, page)
  # TODO need to properly capture and log warnings here
  result = IO.popen("#{@tidycmd} #{self.tidyopts} -f tidy.log -as#{self.format}","r+") do |fp|
     Thread.new { fp.write(text); fp.close_write }
     fp.read
  end
  
  if $?.exitstatus < 2
    result
  else
    warn 'Tidy command failed (exitstatus: $?)'
    text
  end
end