class Rote::Filters::Tidy

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.

Attributes

format[RW]
tidycmd[RW]
tidyopts[RW]

Public Class Methods

new(format = :xhtml, tidycmd = nil, tidyopts = '-q') click to toggle source

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

# 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

Public Instance Methods

filter(text, page) click to toggle source
# 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