Skip to content

Instantly share code, notes, and snippets.

@pozorvlak
Created May 27, 2016 17:59
Show Gist options
  • Save pozorvlak/af43fed67c698b7e255b2feb91731acb to your computer and use it in GitHub Desktop.
Save pozorvlak/af43fed67c698b7e255b2feb91731acb 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";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment