Sha256: 923493ae2cea3cc6511aceab5b200233bc7557c02c965416ffe28a527a489510

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

# -*- encoding: utf-8 -*-
require 'tempfile'

module Webgen::ContentProcessor

  # Uses the external +tidy+ program to format the content as valid (X)HTML.
  class Tidy

    include Webgen::Loggable

    # Process the content of +context+ with the +tidy+ program.
    def call(context)
      error_file = Tempfile.new('webgen-tidy')
      error_file.close

      `tidy -v 2>&1`
      if $?.exitstatus != 0
        raise Webgen::CommandNotFoundError.new('tidy', self.class.name, context.dest_node.alcn)
      end

      cmd = "tidy -q -f #{error_file.path} #{context.website.config['contentprocessor.tidy.options']}"
      result = IO.popen(cmd, 'r+') do |io|
        io.write(context.content)
        io.close_write
        io.read
      end
      if $?.exitstatus != 0
        File.readlines(error_file.path).each do |line|
          log($?.exitstatus == 1 ? :warn : :error) { "Tidy reported problems for <#{context.dest_node.alcn}>: #{line}" }
        end
      end
      context.content = result
      context
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
webgen-0.5.11 lib/webgen/contentprocessor/tidy.rb
webgen-0.5.10 lib/webgen/contentprocessor/tidy.rb
webgen-0.5.9 lib/webgen/contentprocessor/tidy.rb