Page 1 of 1

Formating help needed.

Posted: 2009-06-16 09:27:22
by HowardNH
I am a returning user to Nisus Writer. The last time I used the program was probably 15 years ago. I thought I remembered that there was a way to record key strokes to create a macro. Maybe that was Word or Excel which I also at the same time.

Based on my ability to manipulate the string, I shall be purchasing Nisus Pro certainly within the next couple of days.

I have spent the morning downloading the demo version and reading these support files and the documentation for Pro application and the Macros. I've seen pieces and parts but not enough to get me going on my own with any useful results.

I have tried using PowerFind but my effort has not gotten too far. To do the formatting it seems as though I need Macros. That is just beyond my understanding at this point. Your assistance and patience are gratefully requested.

I have a stream of text created by FileMaker Pro using a syntax I have specified (see below). I can change the syntax and add tags if necessary.

Here is the description of the steps I want to accomplish. I am assuming the find/replace effort moves sequentially down the string.
Please note that I am using different brackets to enclose related information. If necessary these can be specified as unique identifiers.

Change all double quotes to null.

Change all text to Times 10 point.

Go to the start of each sentence AND
select the next anyword and make it bold AND
find the next punctuation character, select the next anyword and make it blue AND
find the next punctuation character, select the next anyword and highlight it in yellow AND
select the next anyword and make it bold AND
find the next punctuation character, select the next anyword and highlight it in yellow AND
find the next punctuation character, select the next anyword and make it blue
<end>
Here is what the string actually looks like. After the semicolon is a hard return The string ends with a period.

"by [ bii ], { por };
por { por }, [ bay ]."

Here is what the final output should look like:

by< bold> [ bii <blue>], { por <highlight yellow> };
por <bold> { por <highlight yellow> }, [ bay <blue> ].

THANK YOU for your insight and assistance.

Howard

Re: Formating help needed.

Posted: 2009-06-16 10:18:19
by Kino
HowardNH wrote:I thought I remembered that there was a way to record key strokes to create a macro.
Unfortunately NW Pro does not have that fantastic feature yet.

If I understand you well, the following macro will do the job. If not, please post some other examples of the data.

Code: Select all

 Replace All '"', ''
Select All
Menu ':Format:Font:Times'
Menu ':Format:Size:10'

$numFound = Find All '^.+?;\n.+?\.(?=\n)', 'E'  # find/select "(paragraph start)blah blah ... ;<LF>blah blah ... .(before LF)"
if ! $numFound
	exit 'Nothing found, exit...'
end

$doc = Document.active
$sels = $doc.textSelections

$numFound = Find All in Selection '^\w+', 'E'
if $numFound  # if something has been found and selected
	Menu ':Format:Bold'
end

$doc.setSelections $sels  # Restore selections
$numFound = Find All in Selection '(?<={\x20).+(?=\x20})', 'E'  # \x20 is a space char and you can use real space chars instead
if $numFound
	Menu ':Format:Highlight:Yellow'
end

$doc.setSelections $sels  # Restore selections
$numFound = Find All in Selection '(?<=\[\x20).+(?=\x20\])', 'E'
if $numFound
	Menu ':Format:Text Color:Blue'
end

# Then, move the insertion point to the document start
Select Page 1
Select Start

Re: Formating help needed.

Posted: 2009-06-16 10:46:47
by martin
HowardNH wrote:I am a returning user to Nisus Writer. The last time I used the program was probably 15 years ago. I thought I remembered that there was a way to record key strokes to create a macro. Maybe that was Word or Excel which I also at the same time.
Glad to have you back. Unfortunately there is no feature in Nisus Writer Pro to record macros, though Classic Nisus Writer had this functionality.
Change all double quotes to null.
If you mean to delete all the double quotes, this is the macro code:

Code: Select all

Replace All '[“”"]', '', 'E'
The 'E' option turns on PowerFind Pro, which we use here to match any kind of double-quote, curly or straight. But if you meant that you want to replace all double quotes with a real NULL character, use this instead:

Code: Select all

Replace All '[“”"]', "\u0000", 'E'
Instead of replacing the quotes with the empty string we use a special kind of escape that stands for U+0000 (unicode code point 0000), the NULL character.
Change all text to Times 10 point.
To do this, we use commands just as they appear in the Nisus Writer Menus, eg:

Code: Select all

Select All
Times
10
It would probably be more clear if we made the last menu command more explicit:

Code: Select all

Select All
Times
:Format:Size:10

Code: Select all

Go to the start of each sentence AND
  select the next anyword and make it bold AND
  find the next punctuation character, select the next anyword and make it blue AND
  find the next punctuation character, select the next anyword and highlight it in yellow AND
  select the next anyword and make it bold AND
  find the next punctuation character, select the next anyword and highlight it in yellow AND
  find the next punctuation character, select the next anyword and make it blue
Finding the start/end of a sentence can be tricky, as there's no definitive character that always means you've reached the end of a sentence. For now to keep it simple I'll just assume your sentences always end with a period followed by some whitespace. We could come up with a more complex rule if needed.

Also you say "next punctuation character", but appear to mean just open brackets/braces. I'll assume you just want to find these braces.

So, that in mind, your code to do the formatting would look something like this:

Code: Select all

# define some expressions we'll be using several times
$anyWord = '\w+'			# "\w" stands for "any word character" and "+" means "one or more"
$endOfSentence = '\.\s'	# period followed by "\s" which stands for "any whitespace"
$punct = '[\\[{]'			# a bit tricky, but means either an open bracket or open brace character
$options = 'E-W'			# "E" for PowerFind Pro and "-W" to turn off "wrap around"

# select the next anyword and make it bold
While Find Next $anyWord, $options
	Bold

	# find the next punctuation character, select the next anyword and make it blue
	Find $punct, $options
	Find $anyWord, $options
	:Format:Text Color:Blue
	
	# find the next punctuation character, select the next anyword and highlight it in yellow
	Find $punct, $options
	Find $anyWord, $options
	:Format:Highlight:Yellow
	
	# select the next anyword and make it bold
	Find $anyWord, $options
	Bold
	
	# find the next punctuation character, select the next anyword and highlight it in yellow
	Find $punct, $options
	Find $anyWord, $options
	:Format:Highlight:Yellow
	
	# find the next punctuation character, select the next anyword and make it blue
	Find $punct, $options
	Find $anyWord, $options
	:Format:Text Color:Blue

	# move to the end of the sentence
	Find $endOfSentence, $options
End

Re: Formating help needed.

Posted: 2009-06-17 12:25:51
by HowardNH
Martin and Kino,

THANK YOUR SO MUCH for your prompt response.

I will be working with this tonight.

I really appreciate your effort and that you presented it in a tutorial manner. At least, can learn more about this incredible feature set.

Thanks again!

I will reply again with my results.

- Howard :D

Re: Formating help needed.

Posted: 2009-06-17 15:19:56
by HowardNH
Hello All.

Thanks again for your help.

After testing the macro I realized that I often have multiple words before and within brackets. With the macro as provided, the first word gets formated but subsequent words do not. This causes other attributes to be formatted incorrectly as the formatting becomes out of sync.

I tried defining open brace/close brace with a find and yellow highlight in between. I did not get my desired on/off effect like say an an html tag.

I have come to realize that a more generalized approach might provide a better solution. Once provided I think I can adapt it to the several circumstances I have.

Can you please show me how to identify a starting character, highlight everything it encounters with yellow highlight until the specified ending character.

For example: [ word1 word2 word3 ]

The number of the words varies from 1 to 4. I tried using a repeat range but was unsuccessful. Everything within the bracket would be yellow. The brackets themselves would not be.

Thank you again for your assistance.

- Howard

Re: Formating help needed.

Posted: 2009-06-18 01:55:20
by HowardNH
Hi,

Forgive me, but I seem to be stuck on what should be a simple replace step.

Based on the examples above, I am trying to change "xxx" to a hard carriage return.

I have tried these several approaches however the xxx's do not get recognized.

Replace All '[xxx]', "\u00b6", 'E'

Replace All '[xxx]', "\n", 'E'

Find and Replace 'xxx', '\n', 'E'

The replace works manually in Find, but not in the macro.

Re: Formating help needed.

Posted: 2009-06-18 02:39:17
by Kino
Can you please show me how to identify a starting character, highlight everything it encounters with yellow highlight until the specified ending character.
As [ and ] are special characters, they are not interpreted literally unless you put \ before them. You can do something like this.

Code: Select all

Find All '(?<=\[ )[^\]]+(?= \])', 'E'
	# (?<=abc) look behind (preceded by "abc")
	# (?=abc) look ahead (followed by "abc")
	# [abc] character set (a or b or c)
	# ^ stands for NOT as the first character in a character set
	# so [^\]] means any non-LF character which is not "]"
	# + one or more times
Find All in Selection '\S+', 'E'
	# \S matches all visible characters
Menu ':Format:Highlight:Yellow'
The first command finds characters between "[ " and " ]". The second command finds all visible characters (i.e. not space or tab) in selections made by the first command. And the third command applies yellow highlight on those characters. You can omit the second command if you don't mind space characters between words being highlighted. In stead of the first command, you can use also:

Code: Select all

Find All '(?<=\[ ).+?(?= \])', 'E'
	# . any non-LF character
	# +? shortest one or more times
I am trying to change "xxx" to a hard carriage return.
If "xxx" is really "xxx", your

Code: Select all

Find and Replace 'xxx', '\n', 'E'
should work. If you want to replace all occurrences of "xxx" with LF, use

Code: Select all

Find and Replace 'xxx', '\n', 'Ea'
or

Code: Select all

Replace All 'xxx', '\n', 'E'

Re: Formating help needed.

Posted: 2009-06-18 06:24:19
by HowardNH
THANKS AGAIN! Kino.

I did try using

Replace All 'xxx', '\n', 'E'

but it did not work. Not sure why.

I will copy your example and work with it this afternoon.

I shall report later.

THANKS AGAIN!

Howard

Re: Formating help needed.

Posted: 2009-06-18 09:37:25
by martin
HowardNH wrote:After testing the macro I realized that I often have multiple words before and within brackets. With the macro as provided, the first word gets formated but subsequent words do not. This causes other attributes to be formatted incorrectly as the formatting becomes out of sync.
...
Can you please show me how to identify a starting character, highlight everything it encounters with yellow highlight until the specified ending character.
This is no problem. So to highlight a single word, we did this:

Code: Select all

$options = 'E-W'

# find the next punctuation character, select the next anyword and highlight it in yellow
Find '[\\[{]', $options
Find '\w+', $options
:Format:Highlight:Yellow
Now instead of searching for one or more word characters "\w", let's search for all characters that are NOT the close bracket/brace. The code is then:

Code: Select all

$options = 'E-W'

# find the next punctuation character, select the next anyword and highlight it in yellow
Find '[\\[{]', $options
Select End # don't include the open bracket/brace we just found
Find '[^\]}]+', $options
:Format:Highlight:Yellow
The second expression is a little tricky. Kino has explained some of this already, but here's what it means: the "[xyz]" construct means match any single character listed inside the brackets, while "[^xyz]" means match any character NOT listed in the brackets. We want to match anything but the closing bracket or closing brace. Because the closing bracket is a special character here (it terminates the list) we have to escape it using a backslash. I hope that makes sense.

Re: Formating help needed.

Posted: 2009-06-18 18:30:38
by HowardNH
Thanks Guys!

I have been able to get the formatting to accomplish what I want. I used the information you provided along with some subtle changes to my data source to make the formatting a bit easier.

It looks like I will be able to use Nisus Writer for my book project.

For some reason, during my tests, the application would hang. Mostly when I would try to select all and then hit the delete key.

I think I might have corrupted the file by repeatedly cutting and pasting data into the file and then deleting it and replacing it.

I have a file I can send if you are interested.

THANKS AGAIN,

Howard

Re: Formating help needed.

Posted: 2009-06-18 22:09:24
by martin
I'm glad you've got everything working Howard. If you could send me the file that would be great. Please use the menu Help > Send Feedback, and be sure to indicate which part should be deleted to trigger the hang (if you do not in fact "select all" beforehand). Thanks!

Re: Formating help needed.

Posted: 2009-06-19 16:15:04
by HowardNH
Done! A file has been sent.