def educate_quotes(str)
punct_class = '[!"#\$\%\'()*+,\-.\/:;<=>?\@\[\\\\\]\^_`{|}~]'
str = str.dup
str.gsub!(/^'(?=#{punct_class}\B)/,
entity(:single_right_quote))
str.gsub!(/^"(?=#{punct_class}\B)/,
entity(:double_right_quote))
str.gsub!(/"'(?=\w)/,
"#{entity(:double_left_quote)}#{entity(:single_left_quote)}")
str.gsub!(/'"(?=\w)/,
"#{entity(:single_left_quote)}#{entity(:double_left_quote)}")
str.gsub!(/'(?=\d\ds)/,
entity(:single_right_quote))
close_class = %![^\ \t\r\n\\[\{\(\-]!
dec_dashes = "#{entity(:en_dash)}|#{entity(:em_dash)}"
str.gsub!(/([[:space:]]| |--|&[mn]dash;|#{dec_dashes}|ȁ[34];)'(?=\w)/,
'\1' + entity(:single_left_quote))
str.gsub!(/(#{close_class})'/,
'\1' + entity(:single_right_quote))
str.gsub!(/'(\s|s\b|$)/,
entity(:single_right_quote) + '\1')
str.gsub!(/'/,
entity(:single_left_quote))
str.gsub!(/([[:space:]]| |--|&[mn]dash;|#{dec_dashes}|ȁ[34];)"(?=\w)/,
'\1' + entity(:double_left_quote))
str.gsub!(/(#{close_class})"/,
'\1' + entity(:double_right_quote))
str.gsub!(/"(\s|s\b|$)/,
entity(:double_right_quote) + '\1')
str.gsub!(/"/,
entity(:double_left_quote))
str
end