Sha256: 7eba73f8617b45f686c81eb182b889842be2ff434a3fab3d2fd0d5216bd59010

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

#!/usr/bin/env ruby
require 'htmlbeautifier'
require 'optparse'
require 'fileutils'

def beautify(name, input, output, options)
  output.puts HtmlBeautifier.beautify(input, options)
rescue => e
  raise "Error parsing #{name}: #{e}"
end

executable = File.basename(__FILE__)

options = {:tab_stops => 2}
parser = OptionParser.new do |opts|
  opts.banner = "Usage: #{executable} [options] [file ...]"
  opts.separator <<END

#{executable} has two modes of operation:

1. If no files are listed, it will read from standard input and write to
   standard output.
2. If files are listed, it will modify each file in place, overwriting it
   with the beautified output.

The following options are available:

END
  opts.on(
    "-t", "--tab-stops NUMBER", Integer,
    "Set number of spaces per indent (default #{options[:tab_stops]})"
  ) do |num|
    options[:tab_stops] = num
  end
  opts.on(
    "-e", "--stop-on-errors",
    "Stop when invalid nesting is encountered in the input"
  ) do |num|
    options[:stop_on_errors] = num
  end
  opts.on(
    "-h", "--help",
    "Display this help message and exit"
  ) do
    puts opts
    exit
  end
end
parser.parse!

if ARGV.any?
  ARGV.each do |path|
    input = File.read(path)
    temppath = path + ".tmp"
    File.open(temppath, "w") do |output|
      beautify path, input, output, options
    end
    FileUtils.mv temppath, path
  end
else
  beautify "standard input", $stdin.read, $stdout, options
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
htmlbeautifier-1.1.0 bin/htmlbeautifier
htmlbeautifier-1.0.2 bin/htmlbeautifier
htmlbeautifier-1.0.1 bin/htmlbeautifier