Skip to content

Instantly share code, notes, and snippets.

@jondkinney
Forked from pozorvlak/single_quote.pl
Last active September 19, 2016 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jondkinney/b47e85cf9099227338f9da8a9e8c5609 to your computer and use it in GitHub Desktop.
Save jondkinney/b47e85cf9099227338f9da8a9e8c5609 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# Script to (generate a script to) fix excess use of double-quotes in HAML.
# To use:
#
# haml-lint app/views/ > haml_errors
# grep "you don't need string interpolation" haml_errors | awk '{print $1}' > single_quote_me
# perl single_quote.pl
# bash fixme.sh
# git commit -a -m "Use single-quotes in straightforward cases"
#
# This should fix all the "easy" cases (only one string per line). Eyeball the
# file problem_lines, delete any cases where string interpolation is used,
# and save it. Then run
#
# awk '{print $1}' problem_lines > single_quote_me
# perl single_quote.pl -f
# bash fixme.sh
# git commit -a -m "Use single-quotes in slightly harder cases"
#
# Now fix the remaining hard cases by hand.
open my $fixups, "<", "single_quote_me";
open my $problem_lines, ">", "problem_lines";
open my $gen_script, ">", "fixme.sh";
my $force = ($ARGV[0] eq "-f");
print $gen_script "#!/bin/sh\n\n";
for my $fixup (<$fixups>) {
chomp $fixup;
my ($file, $line_num) = split(/:/, $fixup);
open my $fh, "<", $file;
my @lines = <$fh>;
my $line = $lines[$line_num - 1];
my @quotes = ($line =~ /"/g);
if ($force || (scalar(@quotes) <= 2)) {
my $sed = 'sed -i '' "' . $line_num . ' s/\"/\'/g"' . " $file\n";
print $gen_script $sed;
} else {
print $problem_lines "$fixup $line";
}
}
@jondkinney
Copy link
Author

Modified to add '' for os x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment