A quotation is "the repetition of one expression as part of another one". In American English, a quotation is surrounded by double-quotation marks ("), as in the previous sentence. However, this rule differs when a quote appears inside another quote. In this case, the inner quote is surrounded by single-quotation marks (').
Given a string as input, output a quotable version of that string by exchanging double- and single-quotation marks that are used for inner quotation.
There's just one catch, though - not all single-quotation marks are used for quotation! For this challenge, single-quotation mark is used for quotation if and only if:
- The mark is preceded by a non-alphanumeric character, or
- The mark is followed by a non-alphanumeric character that is not a space
The marks used for quotations are guaranteed to be balanced; that is, you do not need to handle invalid input. Additionally, double-quotation marks will only be used in situations where single-quotation marks can be used to represent quotation.
A man, a plan, a canal, Panama. "A man, a plan, a canal, Panama." Socrates once said, "An unexamined life is not worth living." "Socrates once said, 'An unexamined life is not worth living.'" @musicman523 said, "Socrates once said, 'An unexamined life is not worth living.'" "@musicman523 said, 'Socrates once said, "An unexamined life is not worth living."'" "Nested 'quotes "can 'cause "recursion," if,' it," runs,' deep!" "'Nested "quotes 'can "cause 'recursion,' if," it,' runs," deep!'" "Contractions' behaviors can't be easy to account for," he warned. "'Contractions' behaviors can't be easy to account for,' he warned." @carusocomputing said, "Nested, 'quotes, it's unfortunate', to be sure." "@carusocomputing said, 'Nested, "quotes, it's unfortunate", to be sure.'"
This is code-golf, so the shortest answer in each language wins!