Sha256: dd30bdde1e5432768417d924e63bc6c7e01c4888711207558c2e406576685b0b

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 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 = { indent: "  " }
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[:indent] = " " * num
  end
  opts.on(
    "-T", "--tab",
    "Indent using tabs"
  ) do
    options[:indent] = "\t"
  end
  opts.on(
    "-i", "--indent-by NUMBER", Integer,
    "Indent the output by NUMBER steps (default 0)."
  ) do |num|
    options[:initial_level] = 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

2 entries across 2 versions & 1 rubygems

Version Path
htmlbeautifier-1.2.1 bin/htmlbeautifier
htmlbeautifier-1.2.0 bin/htmlbeautifier