Class: Webby::Filters::Tidy

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

Overview

The Tidy filter is used to process HTML (or XHTML) through the tidy program and outpu nicely formatted and correct HTML (or XHTML).

Options can be passed to the tidy program via the Webby.site struct. Setting the tidy_options to the string of desired options will do the trick.

From a project’s Rakefile, include the following line (or one that’s more to your liking):

SITE.tidy_options = "-indent -wrap 80 -utf8"

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Tidy

call-seq:

Tidy.new( html )

Create a new filter that will process the given html through the tidy program.



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

def initialize( str )
  @log = ::Logging::Logger[self]
  @str = str

  # create a temporary file for holding any error messages
  # from the tidy program
  @err = Tempfile.new('tidy_err')
  @err.close
end

Instance Method Details

#processObject

call-seq:

process    => formatted html

Process the original HTML text string passed to the filter when it was created and output Tidy formatted HTML or XHTML.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/webby/filters/tidy.rb', line 43

def process
  cmd = "tidy %s -q -f #{@err.path}" % ::Webby.site.tidy_options
  out = IO.popen(cmd, 'r+') do |tidy|
    tidy.write @str
    tidy.close_write
    tidy.read
  end

  if File.size(@err.path) != 0
    @log.warn File.read(@err.path).strip
  end

  return out
end