Sha256: 9628e973b35f4d4d01186995fb3b48748daa2974ee9711713c03136e952f53d8

Contents?: true

Size: 1.96 KB

Versions: 3

Compression:

Stored size: 1.96 KB

Contents

#!/usr/bin/env ruby
require "htmlformatter"
require "optparse"
require "fileutils"

def format(name, input, output, options)
  output.puts HtmlFormatter.format(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 formatted 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(
    "-b", "--keep-blank-lines NUMBER", Integer,
    "Set number of consecutive blank lines"
  ) do |num|
    options[:keep_blank_lines] = num
  end
  opts.on(
    "-n", "--engine STRING", String,
    "Use engine STRING (default erb, allowed: eex)."
  ) do |eng|
    options[:engine] = eng
  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|
      format path, input, output, options
    end
    FileUtils.mv temppath, path
  end
else
  format "standard input", $stdin.read, $stdout, options
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
htmlformatter-1.5.3 bin/htmlformatter
htmlformatter-1.5.1 bin/htmlformatter
htmlformatter-1.5.0 bin/htmlformatter