Smart Quotes

Difference between version 0 and 0 - Previous - Next
You know, those curly things you can use instead of " and ' . I wanted to convert " and ' to the curly things in an html file - there are HTML character entities for them, though some browsers don't like them. It seems tricky because they come in left and right, single and double, and there might be apostrophes which are the same as right single curly quotes.

Anyway, I have written a converter in Tcl (based on one in gawk at https://opensource.com/article/18/8/gawk-script-convert-smart-quotes). It seems to work.

======
proc smartquote {char prevchar} {
    # return smart quotes depending on the previous character
    # otherwise just print the character as-is

    if { [string is space $prevchar] } {
        # prev char is a space
        switch -exact $char {
        ' {
            return {‘}
            }
        {"} {
            return {“}
            }
        default {
            return $char
            }
        }
    } else {
        switch -exact $char {
        ' {
            return {’}
            }
        {"} {
            return {”}
            }
        default {
            return $char
            }
        }
    }
}

proc FixQuotes {data} {
    set htmltag 0
    set pc \n

    set newdata ""
    foreach c [split $data ""] {
        if { $c eq "<" } {
            set htmltag 1
        }

        if { $htmltag } {
            append newdata $c
        } else {
            append newdata [smartquote $c $pc]
        }

        if { $c eq "<" } {
            set htmltag 1
        }

        set pc $c
    }
    return $newdata
}
======