Page 1 of 1

Title Case

Posted: 2011-09-30 00:59:01
by Bob Stern
Searching the web for an algorithm to convert text to Title Case, I discovered a Perl script written by John Gruber and Aristotle Pagaltzis:
https://github.com/ap/titlecase/blob/master/titlecase

I would like to embed this Perl script into a NWP macro so I can apply it to the current selection in a document. However, I don't know enough about Perl to figure out where the input variable is in this script. (I can see that the output variable is named $_.)

Martin, would it be too much trouble for you to provide the NWP macro steps that should surround this Perl script?

Thanks a million!

Re: Title Case

Posted: 2011-09-30 13:00:50
by martin
EDIT: this macro was removed because it had shortcomings. See below for a fixed version of the macro.

Error msg

Posted: 2011-09-30 15:00:38
by Bob Stern
Thanks, Martin!

However, when I run the macro, NWP produces the following error msg:
Screen shot 2011-09-30 at 3.52.54 PM.png
Screen shot 2011-09-30 at 3.52.54 PM.png (35.26 KiB) Viewed 8631 times
When I click Open Macro in the error msg, the following line of the macro is highlighted:
\b (_*) (?:

Re: Title Case

Posted: 2011-09-30 15:15:23
by martin
Ah yes. That character \xC2 is a non-breaking space, introduced by the forum when you copy-paste code from the web. Perl doesn't like it. You can fix it by replacing all non-breaking spaces in the macro with regular spaces. Or download the macro I'll attach here.
...
EDIT: this macro was removed because it had shortcomings. See below for a fixed version of the macro.

Unicode

Posted: 2011-10-01 05:03:12
by Bob Stern
Perfect! Thanks!

Another question: How could I have figured out that \xC2 means non-breaking space?

I typed c2 or C2 or \xC2 in a NWP document, selected it, and attempted to use the menu command Edit > Convert > From Unicode Code Points, but the command was grayed out. I cannot figure out how to make this command active.

I also tried using the OS X Character Viewer to view the Unicode Code Table. I must be interpreting this incorrectly, because Unicode 00C2 appears to be "capital A with circumflex".
Screen shot 2011-10-01 at 5.56.57 AM.jpg
Screen shot 2011-10-01 at 5.56.57 AM.jpg (83.63 KiB) Viewed 8620 times

Re: Title Case

Posted: 2011-10-03 13:07:35
by martin
Actually, you're correct Bob, U+00C2 is a capital A with circumflex. I didn't actually look up the character/code, I just knew Perl doesn't like non-breaking spaces and assumed the \xC2 mentioned in Perl's error message was the code for a non-breaking space.

Perl's error message should probably be clarified. When it says "unrecognized character", it really means unrecognized byte in the UTF-8 steam/data. A non-breaking space must be encoded in UTF-8 using a two byte sequence: 0xC2 0xA0. Perl's error only reports that leading byte, even though it's only the first half of the proper Unicode character. It may be that Perl doesn't really deal with full Unicode characters outside of string literals/data, even when declaring "use utf8;"

Edit > Convert > From Unicode Code Points

Posted: 2011-10-03 15:10:30
by Bob Stern
Thanks for the clarification, Martin.

FYI, I just used the Feedback reporter to report as a bug that fact that "Edit > Convert > From Unicode Code Points" is always greyed out.

Re: Title Case

Posted: 2011-10-03 16:47:15
by phspaelti
Bob. a string has to have the form U+xxxx for the "Edit > Convert > From Unicode Code Points" to work.

Of course I agree that they could make it more user-friendly.

Thanks, Philip!

Posted: 2011-10-03 23:14:50
by Bob Stern
The U+ prefix worked.

Nisus should add that to the user guide.

Re: Title Case

Posted: 2011-10-04 11:42:07
by martin
Thanks for the feedback report Bob, glad you have it working. I agree that it should be more lenient in what forms it accepts; being as liberal as possible would be best.

Re: Title Case

Posted: 2015-12-06 13:01:41
by skyblue
Hi,

I was excited to find this macro. I found a bug, which I thought I would report. (I'm not much of a scripter, so I can't fix the issue myself.)

The macro failed with a carriage return between lines.
Example- input text:

Code: Select all

journal of nervous and mental disease
psychotherapy and psychosomatics
journal of humanistic psychology
journal of transpersonal psychology


bipolar disorders

journal of affective disorders
Yields this result:

Code: Select all

Journal of Nervous and Mental Disease
Psychotherapy and Psychosomatics
Journal of Humanistic Psychology
Journal of Transpersonal Psychology
i.e. just lopped off everything after the carriage return. Does the same thing when I change it to only 1 carriage return for the first blank space instead of 2.

When I turn on "View > Show Invisibles," it displays the blank lines with a paragraph symbol.

For now I can work around it just fine, but just thought I would mention it in case further development happens with this macro.

thanks
sb

Re: Title Case

Posted: 2015-12-09 13:32:45
by martin
skyblue wrote:The macro failed with a carriage return between lines.
...
i.e. just lopped off everything after the carriage return.
Thanks for letting us know skyblue. Here's an amended macro that doesn't suffer from this problem.