Title Case

Get help using and writing Nisus Writer Pro macros.
Post Reply
Bob Stern
Posts: 170
Joined: 2006-03-12 12:32:47

Title Case

Post 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!
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Title Case

Post by martin »

EDIT: this macro was removed because it had shortcomings. See below for a fixed version of the macro.
Bob Stern
Posts: 170
Joined: 2006-03-12 12:32:47

Error msg

Post 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 36613 times
When I click Open Macro in the error msg, the following line of the macro is highlighted:
\b (_*) (?:
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Title Case

Post 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.
Bob Stern
Posts: 170
Joined: 2006-03-12 12:32:47

Unicode

Post 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 36602 times
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Title Case

Post 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;"
Bob Stern
Posts: 170
Joined: 2006-03-12 12:32:47

Edit > Convert > From Unicode Code Points

Post 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.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Title Case

Post 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.
philip
Bob Stern
Posts: 170
Joined: 2006-03-12 12:32:47

Thanks, Philip!

Post by Bob Stern »

The U+ prefix worked.

Nisus should add that to the user guide.
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Title Case

Post 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.
skyblue
Posts: 5
Joined: 2015-03-18 07:28:09

Re: Title Case

Post 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
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Title Case

Post 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.
Attachments
TitleCase.nwm
(21.16 KiB) Downloaded 1275 times
tedg
Posts: 42
Joined: 2012-09-22 01:52:56
Location: Brisbane, Australia
Contact:

Re: Title Case

Post by tedg »

Much later... Thanks for this.
User since 1990
Most current NWp and MacOS
MacBookPro 16-inch 2021
Post Reply