Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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

This comment has been minimized.

Copy link
Owner Author

commented Sep 19, 2016

Modified to add '' for os x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.