"_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 »

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: 1090
Joined: 2007-02-07 00:58:12
Location: Japan

Re: "_text_" to italic

Post by phspaelti »

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 1797 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 »

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: 1090
Joined: 2007-02-07 00:58:12
Location: Japan

Re: "_text_" to italic

Post by phspaelti »

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

martin
Official Nisus Person
Posts: 4696
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: "_text_" to italic

Post by martin »

MyronGochnauer wrote:
2019-12-05 10:16:59
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.]
If you're willing to do a good amount of reading then you might check out information on regular expressions (aka: regex), which Nisus Writer supports in our PowerFind Pro search mode. Learning regex can change your life if you work with text! Though regex is certainly a very complicated topic.
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.
I'm sorry about that. The macro references is indeed more of a reference. It's not that friendly in introducing all the concepts you need to know. You might best be served just looking at a few macros from the Macro menu for example code.

I'll try to answer your questions, but please let us know if you have more.

Any text file can be a macro. It can be a plain text file, or more likely in Nisus Writer, an RTF file with the .nwm extension. Generally speaking you should have one command per line. You must use returns/newlines to terminate commands; there's no terminal character like semicolons in C++. Comments are marked by prefixing the line with #, or you can mark multi-line comments using /* */. Whitespace is mostly ignored as in other programming languages. But there are a few exceptions to that, like invoking menu commands. Consider these lines of macro code:

Code: Select all

Font Face:Bold
Menu ":Format:Font Face:Bold"
Font  Face:Bold
Menu ":Format:Font  Face:Bold"
The first two lines work correctly. The second two lines fail because there's an extra space in the menu title name.

Post Reply