overshare / overshare-kit

A soup-to-nuts sharing library for iOS.
MIT License
1.82k stars 132 forks source link

Smart quotes curled the wrong way #22

Closed gruber closed 10 years ago

gruber commented 10 years ago

Tried out the as-you-type quote smartener in Sunlit. Please forgive me if the following is a bug only in Sunlit but not in OvershareKit.

If you type an "opening" punctuation character -- think: ( { [ -- and then type a single or double quote, OSK will give you a closing quote, but clearly should give you an opening quote.

In the case where you type a quote immediately after a quote:

  1. If you type two of the same quotes (double-quote double-quote, or single- single), you should get an opener then a closer. OSK does this correctly.
  2. If you type two different quotes in succession (double- single-, or single- double), you should get two opening quotes, because the user is likely writing a quote of a quote. OSK gets this wrong -- it always closes the second quote.
jaredsinclair commented 10 years ago

That's an OvershareKit bug. Nice catch. I'll get it fixed.

jaredsinclair commented 10 years ago

Fixed?

kenosha-kid

jaredsinclair commented 10 years ago

I'm going to mark this one as closed for now. Quotes respect opening brackets now (on the dev branch, will push to master soon).

There are still some edge cases not accounted for that I might tackle later, e.g. abbreviated decades. OSK puts an opening single quote in front of the ’90s instead of a closing single quote.

gruber commented 10 years ago

On 22 Jan 2014, at 9:57pm, Jared Sinclair notifications@github.com wrote:

Fixed?

I think so. What a weird font though. The opening single quote for Helvetica should look like this:

One thing you might find useful is the attached text file. I just opened TextEdit, turned on Cocoa’s smart quotes, and typed a bunch of punctuation characters followed by a quote. Some of these make sense to me, others I can’t figure out. But I suspect there’s a reason for each.

—J.G.

gruber commented 10 years ago

On 23 Jan 2014, at 5:07pm, Jared Sinclair notifications@github.com wrote:

There are still some edge cases not accounted for that I might tackle later, e.g. abbreviated decades. OSK puts an opening single quote in front of the ’90s instead of a closing single quote.

That’s a tough one. I’m not sure I’ve ever seen a smart quotes engine get that right.

My SmartyPants algorithm does, but SmartyPants has the advantage of not being an as-you-type engine. It has the whole string to examine, so it can look behind and ahead.

What about dash-dash for an em-dash?

—J.G.

jaredsinclair commented 10 years ago

The opening single quote for Helvetica should look like this:

Whoa. My TextExpander snippets for smart opening double and single quotes have been wrong for a long time.

What about dash-dash for an em-dash?

Of course. And three periods for an ellipsis.

jaredsinclair commented 10 years ago

I pushed the fix for the single opening quote to dev and to master. The ellipses and em-dashes will require a little more forethought.

jaredsinclair commented 10 years ago

Okay, how about this:

screen shot 2014-01-25 at 5 51 55 pm

I pushed a major refactor to the dev branch. Same smart quote behavior as before, with new additions:

All of this happens as you type. It also only processes the text (roughly) around the area you’re editing, so it should work well with really long documents.

jordanekay commented 10 years ago

Looks good! Would also be nice to have a list of words in the device’s current language that start with ’ (like ’em, ’tis, etc.) so that ‘ isn’t used inadvertently.

jaredsinclair commented 10 years ago

That would be a cool improvement.

One of the reasons this is so hard on iOS is that NSString “is UTF-16.” In order to look ahead or behind by a specific number of characters I have to parse each character individually with the block enumeration method of NSString.

If you want to see how it's working, see the OSKSmartPunctuation utility class on the dev branch. All this stuff is emoji-safe and sensitive to writing direction. The decades correction is tucked into the single-quote method near the bottom of the .m file.

God, how I wish Apple would add an "NSComposedCharacterString" class or something to that effect. It would simplify so much.