Skip to content

Instantly share code, notes, and snippets.

@steckel
Last active January 7, 2020 06:40
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 steckel/5d93a3152ad268fb7bd4314402fb898e to your computer and use it in GitHub Desktop.
Save steckel/5d93a3152ad268fb7bd4314402fb898e to your computer and use it in GitHub Desktop.

Converting Quotes to Smart Quotes workflow

smart-quotes.awk can be used with awk to convert html file contents' quotes to smart quotes ("" -> “” and '' -> ‘’).

First run the script and create a new file with the output.

awk -f ~/Development/marxists.org/smart-quotes.awk input.htm > output.htm

Then run a difftool and check the output. I like to run colordiff and pipe it to less with a pattern for highlighting the characters we just attempted to edit.

colordiff -u input.htm output.htm | less -R --pattern="“|”|‘|’"

Screen Shot 2020-01-06 at 10 39 05 PM

function smartquote (char, prevchar, open) {
# print smart quotes depending on the previous character
# otherwise just print the character as-is
# prev char is a space
if (char == "'") {
if (open[0] == 1) {
printf("’");
open[0] = 0;
} else if (prevchar == " ") {
printf("‘");
open[0] = 1;
} else {
printf("’");
}
} else if (char == "\"") {
if (open[1] == 1) {
printf("”");
open[1] = 0;
} else {
printf("“");
open[1] = 1;
}
} else {
printf("%c", char);
}
}
BEGIN {
htmltag = 0;
}
{
# for each line, scan one letter at a time:
linelen = length($0);
open[0] = 0;
open[1] = 0;
prev = "\n";
for (i = 1; i <= linelen; i++) {
char = substr($0, i, 1);
if (char == "<") {
htmltag = 1;
}
if (htmltag == 1) {
printf("%c", char);
}
else {
smartquote(char, prev, open);
prev = char;
}
if (char == ">") {
htmltag = 0;
}
}
# add trailing newline at end of each line
printf ("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment