#!/usr/bin/ruby # frozen_string_literal: true arg = ARGV[0] val = if arg.nil? $stdin.read else File.read(arg) end d = (val.gsub(/(?<=\s)".*?"/) do |i| "“#{i[1..-2]}”" end).gsub(/\s*”\s*([,.?!])/, '\\1”').gsub(/([ \t])[ \t]+/, '\\1').gsub(/\n\n\n*/, "\n\n").strip if arg.nil? print d else File.write arg, d end