"_text_" to italic

Get help using and writing Nisus Writer Pro macros.
Post Reply
MyronGochnauer
Posts: 7
Joined: 2009-08-12 02:59:46

"_text_" to italic

Post by MyronGochnauer » 2019-12-03 07:54:39

I need a macro to convert indications of italics in txt files (e.g. "_any text_") to true italics in an RTF file.

It seems like a simple thing, but none of the same macros I've seen come close to what I need: find text, change format

Myron

User avatar
phspaelti
Posts: 1043
Joined: 2007-02-07 00:58:12
Location: Japan

Re: "_text_" to italic

Post by phspaelti » 2019-12-03 08:30:50

MyronGochnauer wrote:
2019-12-03 07:54:39
… what I need: find text, change format
…and presumably remove the underlines?

Actually for this kind of thing you don't even need a macro. You can use Find/Replace:
Find_Replace_Markup_with_Italics.png
Find_Replace_Markup_with_Italics.png (110.78 KiB) Viewed 49 times
Note that the replace expression has the Italics applied.

You can of course macroize the above, but you'll need to be careful about the interactions with styles in the macro. So I prefer doing it in two steps: (1) Find/Replace (2) apply the Italics. To be fail safe you should test before applying the italics.

Code: Select all

if Find and Replace '_(.+?)_', '\1', 'Ea'
Italic
end
Finally, if you prefer you can do the same using the macro language:

Code: Select all

$doc = Document.active
$bits = $doc.text.findAndReplace '_(.+?)_', '\1', 'Ea'
if $bits.count
    $doc.setSelection $bits
    Menu.activateAtPath 'Italic'
end
philip

MyronGochnauer
Posts: 7
Joined: 2009-08-12 02:59:46

Re: "_text_" to italic

Post by MyronGochnauer » 2019-12-05 10:16:59

Good Grief! The Find/Replace routine works! I had no idea it was possible. :)

I'll have to study the Find/Replace documentation... It's not at all obvious why you wouldn't use "AnyText" as the wildcard. [...and at the moment I have no idea what "1+shortest" is/does. Time for some reading.]

The macro version seems to do exactly what I want. (Although I've learned always to preserve a path back to the starting position)

Thanks for the help!!


For what it is worth, learning macros in NWP seems needlessly difficult because the Macro Reference does not start with a clear specification of the nature of a stored macro (an ASCII file that is interpreted), or the syntax requirements. Can multiple commands be linked on the same line of code, or is one-thing-per-line absolute? Is white space ignored? Is there a way to end the code part of a line short of a Return/LF? How are comments marked out?

User avatar
phspaelti
Posts: 1043
Joined: 2007-02-07 00:58:12
Location: Japan

Re: "_text_" to italic

Post by phspaelti » 2019-12-05 18:22:16

MyronGochnauer wrote:
2019-12-05 10:16:59
It's not at all obvious why you wouldn't use "AnyText" as the wildcard.
Indeed it is not.

Consider the following case:

Code: Select all

_Nisus_ Writer is a great _document_ processor.
If you ask: "What is the text between two underlines?", you might answer "Nisus" and "document". But there is another possible answer: "Nisus_ Writer is a great _document".

An algorithm that prefers the second answer is called "greedy". Powerfind is greedy. Using "shortest" is the way to get Powerfind to stop being greedy.
If you look at the Powerfind Pro version of AnyText you will see that it has the greedy repetition built in, which is why I chose to roll my own "Any Text".
philip

Post Reply